fix(compile-sfc): analyze v-bind shorthand usage in template (#10518)

close #10515
This commit is contained in:
edison 2024-04-15 16:32:04 +08:00 committed by GitHub
parent 6b4b8ec5b1
commit e5919d4658
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 4 deletions

View File

@ -66,14 +66,14 @@ return { get vMyDir() { return vMyDir } }
exports[`dynamic arguments 1`] = ` exports[`dynamic arguments 1`] = `
"import { defineComponent as _defineComponent } from 'vue' "import { defineComponent as _defineComponent } from 'vue'
import { FooBar, foo, bar, unused, baz } from './x' import { FooBar, foo, bar, unused, baz, msg } from './x'
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
setup(__props, { expose: __expose }) { setup(__props, { expose: __expose }) {
__expose(); __expose();
return { get FooBar() { return FooBar }, get foo() { return foo }, get bar() { return bar }, get baz() { return baz } } return { get FooBar() { return FooBar }, get foo() { return foo }, get bar() { return bar }, get baz() { return baz }, get msg() { return msg } }
} }
})" })"

View File

@ -45,7 +45,7 @@ test('directive', () => {
test('dynamic arguments', () => { test('dynamic arguments', () => {
const { content } = compile(` const { content } = compile(`
<script setup lang="ts"> <script setup lang="ts">
import { FooBar, foo, bar, unused, baz } from './x' import { FooBar, foo, bar, unused, baz, msg } from './x'
</script> </script>
<template> <template>
<FooBar #[foo.slotName] /> <FooBar #[foo.slotName] />
@ -53,11 +53,12 @@ test('dynamic arguments', () => {
<div :[bar.attrName]="15"></div> <div :[bar.attrName]="15"></div>
<div unused="unused"></div> <div unused="unused"></div>
<div #[\`item:\${baz.key}\`]="{ value }"></div> <div #[\`item:\${baz.key}\`]="{ value }"></div>
<FooBar :msg />
</template> </template>
`) `)
expect(content).toMatch( expect(content).toMatch(
`return { get FooBar() { return FooBar }, get foo() { return foo }, ` + `return { get FooBar() { return FooBar }, get foo() { return foo }, ` +
`get bar() { return bar }, get baz() { return baz } }`, `get bar() { return bar }, get baz() { return baz }, get msg() { return msg } }`,
) )
assertCode(content) assertCode(content)
}) })

View File

@ -60,6 +60,9 @@ function resolveTemplateUsedIdentifiers(sfc: SFCDescriptor): Set<string> {
extractIdentifiers(ids, prop.forParseResult!.source) extractIdentifiers(ids, prop.forParseResult!.source)
} else if (prop.exp) { } else if (prop.exp) {
extractIdentifiers(ids, prop.exp) extractIdentifiers(ids, prop.exp)
} else if (prop.name === 'bind' && !prop.exp) {
// v-bind shorthand name as identifier
ids.add((prop.arg as SimpleExpressionNode).content)
} }
} }
if ( if (