Skip to content

Commit

Permalink
fix(compiler-sfc): fix import usage check for last expression
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Aug 9, 2021
1 parent c9c7030 commit 1e1682f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,15 @@ 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 } from './x'
import { FooBar, FooBaz, FooQux, vMyDir, x, y, z, x$y, Last } from './x'
export default _defineComponent({
setup(__props, { expose }) {
expose()
const fooBar: FooBar = 1
return { fooBar, FooBaz, FooQux, vMyDir, x, z, x$y }
return { fooBar, FooBaz, FooQux, vMyDir, x, z, x$y, Last }
}
})"
Expand Down
5 changes: 3 additions & 2 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,14 @@ 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 } from './x'
import { FooBar, FooBaz, FooQux, vMyDir, x, y, z, x$y, 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>
<Last/>
</template>
`)
assertCode(content)
Expand All @@ -231,7 +232,7 @@ defineExpose({ foo: 123 })
// y: should not be matched by {{ yy }} or 'y' in binding exps
// x$y: #4274 should escape special chars when creating Regex
expect(content).toMatch(
`return { fooBar, FooBaz, FooQux, vMyDir, x, z, x$y }`
`return { fooBar, FooBaz, FooQux, vMyDir, x, z, x$y, Last }`
)
})
})
Expand Down
1 change: 1 addition & 0 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2220,6 +2220,7 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
]
})

code += ';'
templateUsageCheckCache.set(content, code)
return code
}
Expand Down

0 comments on commit 1e1682f

Please sign in to comment.