Skip to content

Commit bdb1a79

Browse files
authored
fix(compiler-sfc): handle empty strings during template usage analysis of setup bindings (#4608)
fix #4599
1 parent fcd5422 commit bdb1a79

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

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

+16
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@ return { props, a, emit }
202202
}"
203203
`;
204204
205+
exports[`SFC compile <script setup> dev mode import usage check attribute expressions 1`] = `
206+
"import { defineComponent as _defineComponent } from 'vue'
207+
import { bar, baz } from './x'
208+
209+
export default /*#__PURE__*/_defineComponent({
210+
setup(__props, { expose }) {
211+
expose()
212+
213+
const cond = true
214+
215+
return { cond, bar, baz }
216+
}
217+
218+
})"
219+
`;
220+
205221
exports[`SFC compile <script setup> dev mode import usage check components 1`] = `
206222
"import { defineComponent as _defineComponent } from 'vue'
207223
import { FooBar, FooBaz, FooQux, foo } from './x'

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

+15
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,21 @@ defineExpose({ foo: 123 })
342342
assertCode(content)
343343
})
344344

345+
// https://github.com/vuejs/vue-next/issues/4599
346+
test('attribute expressions', () => {
347+
const { content } = compile(`
348+
<script setup lang="ts">
349+
import { bar, baz } from './x'
350+
const cond = true
351+
</script>
352+
<template>
353+
<div :class="[cond ? '' : bar(), 'default']" :style="baz"></div>
354+
</template>
355+
`)
356+
expect(content).toMatch(`return { cond, bar, baz }`)
357+
assertCode(content)
358+
})
359+
345360
test('vue interpolations', () => {
346361
const { content } = compile(`
347362
<script setup lang="ts">

packages/compiler-sfc/src/compileScript.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1826,7 +1826,7 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
18261826

18271827
function stripStrings(exp: string) {
18281828
return exp
1829-
.replace(/'[^']+'|"[^"]+"/g, '')
1829+
.replace(/'[^']*'|"[^"]*"/g, '')
18301830
.replace(/`[^`]+`/g, stripTemplateString)
18311831
}
18321832

0 commit comments

Comments
 (0)