From: Evan You Date: Mon, 9 Aug 2021 16:17:22 +0000 (-0400) Subject: fix(compiler-sfc): fix import usage detection for names containing $ X-Git-Tag: v3.2.0~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=88a4504e8215392e277f07db41ab9f46fc68b4d3;p=thirdparty%2Fvuejs%2Fcore.git fix(compiler-sfc): fix import usage detection for names containing $ fix #4274 --- diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 5e30973ac1..85aa31337e 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 @@ -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 }` + ) }) }) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 320c00b521..89a33f2ecc 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -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] = {