]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): fix import usage detection for names containing $
authorEvan You <yyx990803@gmail.com>
Mon, 9 Aug 2021 16:17:22 +0000 (12:17 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 9 Aug 2021 16:17:22 +0000 (12:17 -0400)
fix #4274

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index 5e30973ac1e64229e282579565b979e43ad5016f..85aa31337e1e63181aa23d9d4266b3962131cf6b 100644 (file)
@@ -206,7 +206,7 @@ return { x }
 
 exports[`SFC compile <script setup> imports imports not used in <template> should not be exposed 1`] = `
 "import { defineComponent as _defineComponent } from 'vue'
-import { FooBar, FooBaz, FooQux, vMyDir, x, y, z } from './x'
+import { FooBar, FooBaz, FooQux, vMyDir, x, y, z, x$y } from './x'
         
 export default _defineComponent({
   setup(__props, { expose }) {
@@ -214,7 +214,7 @@ export default _defineComponent({
 
         const fooBar: FooBar = 1
         
-return { fooBar, FooBaz, FooQux, vMyDir, x, z }
+return { fooBar, FooBaz, FooQux, vMyDir, x, z, x$y }
 }
 
 })"
index e529bdf1cecc5fba979e55316895d4b2c8edafd7..0731d0f23fc23a924a1a44eef72edb0142eea21b 100644 (file)
@@ -213,11 +213,11 @@ defineExpose({ foo: 123 })
     test('imports not used in <template> should not be exposed', () => {
       const { content } = compile(`
         <script setup lang="ts">
-        import { FooBar, FooBaz, FooQux, vMyDir, x, y, z } from './x'
+        import { FooBar, FooBaz, FooQux, vMyDir, x, y, z, x$y } from './x'
         const fooBar: FooBar = 1
         </script>
         <template>
-          <FooBaz v-my-dir>{{ x }} {{ yy }}</FooBaz>
+          <FooBaz v-my-dir>{{ x }} {{ yy }} {{ x$y }}</FooBaz>
           <foo-qux/>
           <div :id="z + 'y'">FooBar</div>
         </template>
@@ -229,7 +229,10 @@ defineExpose({ foo: 123 })
       // vMyDir: used as directive v-my-dir
       // x: used in interpolation
       // y: should not be matched by {{ yy }} or 'y' in binding exps
-      expect(content).toMatch(`return { fooBar, FooBaz, FooQux, vMyDir, x, z }`)
+      // x$y: #4274 should escape special chars when creating Regex
+      expect(content).toMatch(
+        `return { fooBar, FooBaz, FooQux, vMyDir, x, z, x$y }`
+      )
     })
   })
 
index 320c00b52161b39af97b7eb3125fb3fa52a58fd0..89a33f2ecc887131922ce554dbf043cdce14d582 100644 (file)
@@ -332,9 +332,11 @@ export function compileScript(
 
     let isUsedInTemplate = true
     if (isTS && sfc.template && !sfc.template.src) {
-      isUsedInTemplate = new RegExp(`\\b${local}\\b`).test(
-        resolveTemplateUsageCheckString(sfc)
-      )
+      isUsedInTemplate = new RegExp(
+        // #4274 escape $ since it's a special char in regex
+        // (and is the only regex special char that is valid in identifiers)
+        `[^\\w$_]${local.replace(/\$/g, '\\$')}[^\\w$_]`
+      ).test(resolveTemplateUsageCheckString(sfc))
     }
 
     userImports[local] = {