Skip to content

Commit 88a4504

Browse files
committed
fix(compiler-sfc): fix import usage detection for names containing $
fix #4274
1 parent 4781965 commit 88a4504

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,15 @@ return { x }
206206
207207
exports[`SFC compile <script setup> imports imports not used in <template> should not be exposed 1`] = `
208208
"import { defineComponent as _defineComponent } from 'vue'
209-
import { FooBar, FooBaz, FooQux, vMyDir, x, y, z } from './x'
209+
import { FooBar, FooBaz, FooQux, vMyDir, x, y, z, x$y } from './x'
210210
211211
export default _defineComponent({
212212
setup(__props, { expose }) {
213213
expose()
214214
215215
const fooBar: FooBar = 1
216216
217-
return { fooBar, FooBaz, FooQux, vMyDir, x, z }
217+
return { fooBar, FooBaz, FooQux, vMyDir, x, z, x$y }
218218
}
219219
220220
})"

packages/compiler-sfc/__tests__/compileScript.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,11 @@ defineExpose({ foo: 123 })
213213
test('imports not used in <template> should not be exposed', () => {
214214
const { content } = compile(`
215215
<script setup lang="ts">
216-
import { FooBar, FooBaz, FooQux, vMyDir, x, y, z } from './x'
216+
import { FooBar, FooBaz, FooQux, vMyDir, x, y, z, x$y } from './x'
217217
const fooBar: FooBar = 1
218218
</script>
219219
<template>
220-
<FooBaz v-my-dir>{{ x }} {{ yy }}</FooBaz>
220+
<FooBaz v-my-dir>{{ x }} {{ yy }} {{ x$y }}</FooBaz>
221221
<foo-qux/>
222222
<div :id="z + 'y'">FooBar</div>
223223
</template>
@@ -229,7 +229,10 @@ defineExpose({ foo: 123 })
229229
// vMyDir: used as directive v-my-dir
230230
// x: used in interpolation
231231
// y: should not be matched by {{ yy }} or 'y' in binding exps
232-
expect(content).toMatch(`return { fooBar, FooBaz, FooQux, vMyDir, x, z }`)
232+
// x$y: #4274 should escape special chars when creating Regex
233+
expect(content).toMatch(
234+
`return { fooBar, FooBaz, FooQux, vMyDir, x, z, x$y }`
235+
)
233236
})
234237
})
235238

packages/compiler-sfc/src/compileScript.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,11 @@ export function compileScript(
332332

333333
let isUsedInTemplate = true
334334
if (isTS && sfc.template && !sfc.template.src) {
335-
isUsedInTemplate = new RegExp(`\\b${local}\\b`).test(
336-
resolveTemplateUsageCheckString(sfc)
337-
)
335+
isUsedInTemplate = new RegExp(
336+
// #4274 escape $ since it's a special char in regex
337+
// (and is the only regex special char that is valid in identifiers)
338+
`[^\\w$_]${local.replace(/\$/g, '\\$')}[^\\w$_]`
339+
).test(resolveTemplateUsageCheckString(sfc))
338340
}
339341

340342
userImports[local] = {

0 commit comments

Comments
 (0)