diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
index 0974a1d59a3..3ab28dceb2d 100644
--- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
+++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
@@ -206,7 +206,7 @@ return { x }
exports[`SFC compile
{{ x }} {{ yy }} {{ x$y }}
FooBar
+ {{ \`\${VAR}VAR2\${VAR3}\` }}
`)
- 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)
})
})
diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts
index 8a6b9e60f7f..031d20634cf 100644
--- a/packages/compiler-sfc/src/compileScript.ts
+++ b/packages/compiler-sfc/src/compileScript.ts
@@ -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 ''
}