]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): fix import usage check in template strings in expressions
authorEvan You <yyx990803@gmail.com>
Mon, 16 Aug 2021 22:03:03 +0000 (18:03 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 16 Aug 2021 22:03:09 +0000 (18:03 -0400)
fix #4340

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

index 0974a1d59a338fd56338d2af3b92b8336ad0941d..3ab28dceb2d8443cb2eda08acd974f1ce729f211 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, x$y, Last } from './x'
+import { FooBar, FooBaz, FooQux, vMyDir, x, y, z, x$y, VAR, VAR2, VAR3, Last } 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, x$y, Last }
+return { fooBar, FooBaz, FooQux, vMyDir, x, z, x$y, VAR, VAR3, Last }
 }
 
 })"
index e5be15b076a1b47c3d208f2e156bb1f37a05a3eb..ce8816910dfefb9a3689e5a13c1ad02d2c14acc6 100644 (file)
@@ -213,17 +213,17 @@ 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, x$y, Last } from './x'
+        import { FooBar, FooBaz, FooQux, vMyDir, x, y, z, x$y, VAR, VAR2, VAR3, Last } from './x'
         const fooBar: FooBar = 1
         </script>
         <template>
           <FooBaz v-my-dir>{{ x }} {{ yy }} {{ x$y }}</FooBaz>
           <foo-qux/>
           <div :id="z + 'y'">FooBar</div>
+          {{ \`\${VAR}VAR2\${VAR3}\` }}
           <Last/>
         </template>
         `)
-      assertCode(content)
       // FooBar: should not be matched by plain text
       // FooBaz: used as PascalCase component
       // FooQux: used as kebab-case component
@@ -231,9 +231,11 @@ defineExpose({ foo: 123 })
       // x: used in interpolation
       // y: should not be matched by {{ yy }} or 'y' in binding exps
       // x$y: #4274 should escape special chars when creating Regex
+      // VAR & VAR3: #4340 interpolations in tempalte strings
       expect(content).toMatch(
-        `return { fooBar, FooBaz, FooQux, vMyDir, x, z, x$y, Last }`
+        `return { fooBar, FooBaz, FooQux, vMyDir, x, z, x$y, VAR, VAR3, Last }`
       )
+      assertCode(content)
     })
   })
 
index 8a6b9e60f7ffca6d4af89dfdf9d5ca611ad0c9dd..031d20634cf2f8acc495da51d4d94323cd4fcf1a 100644 (file)
@@ -2242,5 +2242,15 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
 }
 
 function stripStrings(exp: string) {
-  return exp.replace(/'[^']+'|"[^"]+"|`[^`]+`/g, '')
+  return exp
+    .replace(/'[^']+'|"[^"]+"/g, '')
+    .replace(/`[^`]+`/g, stripTemplateString)
+}
+
+function stripTemplateString(str: string): string {
+  const interpMatch = str.match(/\${[^}]+}/g)
+  if (interpMatch) {
+    return interpMatch.map(m => m.slice(2, -1)).join(',')
+  }
+  return ''
 }