This commit is contained in:
yangxiuxiu 2025-05-05 20:38:34 +00:00 committed by GitHub
commit 4dcbc6b29b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 118 additions and 0 deletions

View File

@ -1143,6 +1143,36 @@ describe('SFC analyze <script> bindings', () => {
})
})
it('recognizes data/setup return', () => {
const { bindings } = compile(`
<script>
const bar = 2
const foo = 2
const msg = 2
const hello = 2
export default {
setup() {
return {
foo: 1,
hello: numm
}
},
data() {
return {
foo: null,
msg: null
}
}
}
</script>
`)
expect(bindings).toStrictEqual({
foo: BindingTypes.SETUP_MAYBE_REF,
msg: BindingTypes.DATA,
hello: BindingTypes.SETUP_MAYBE_REF,
})
})
it('recognizes methods', () => {
const { bindings } = compile(`
<script>

View File

@ -87,6 +87,8 @@ function analyzeBindingsFromOptions(node: ObjectExpression): BindingMetadata {
bodyItem.argument.type === 'ObjectExpression'
) {
for (const key of getObjectExpressionKeys(bodyItem.argument)) {
// use variables in setup first, consistent with runtime
if (bindings[key] === BindingTypes.SETUP_MAYBE_REF) continue
bindings[key] =
property.key.name === 'setup'
? BindingTypes.SETUP_MAYBE_REF

View File

@ -0,0 +1,86 @@
import { defineComponent, h, renderToString } from '@vue/runtime-test'
describe('api: options', () => {
test('mix api options: setup and data with created', () => {
const mixinA = defineComponent({
setup() {
return {
a: 'from setup',
}
},
data() {
return {
a: 'from data',
}
},
created(this: any) {
this.a = 'from created'
},
render() {
return `${this.a}`
},
})
expect(renderToString(h(mixinA))).toBe(`from created`)
})
test('mix api options: data and setup with created', () => {
const mixinA = defineComponent({
data() {
return {
a: 'from data',
}
},
setup() {
return {
a: 'from setup',
}
},
created(this: any) {
this.a = 'from created'
},
render() {
return `${this.a}`
},
})
expect(renderToString(h(mixinA))).toBe(`from created`)
})
test('mix api options: data and setup', () => {
const mixinA = defineComponent({
data() {
return {
a: 'from data',
}
},
setup() {
return {
a: 'from setup',
}
},
created(this: any) {},
render() {
return `${this.a}`
},
})
expect(renderToString(h(mixinA))).toBe(`from setup`)
})
test('mix api options: setup and data', () => {
const mixinA = defineComponent({
setup() {
return {
a: 'from setup',
}
},
data() {
return {
a: 'from data',
}
},
render() {
return `${this.a}`
},
})
expect(renderToString(h(mixinA))).toBe(`from setup`)
})
})