fix(compiler-sfc): fix object default values for reactive props destructure

This commit is contained in:
Evan You 2022-05-10 08:39:27 +08:00
parent 0683a022ec
commit 7dfe146096
3 changed files with 9 additions and 9 deletions

View File

@ -65,7 +65,7 @@ exports[`sfc props transform default values w/ runtime declaration 1`] = `
export default { export default {
props: _mergeDefaults(['foo', 'bar'], { props: _mergeDefaults(['foo', 'bar'], {
foo: 1, foo: 1,
bar: () => {} bar: () => ({})
}), }),
setup(__props) { setup(__props) {
@ -83,7 +83,7 @@ exports[`sfc props transform default values w/ type declaration 1`] = `
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
props: { props: {
foo: { type: Number, required: false, default: 1 }, foo: { type: Number, required: false, default: 1 },
bar: { type: Object, required: false, default: () => {} } bar: { type: Object, required: false, default: () => ({}) }
}, },
setup(__props: any) { setup(__props: any) {
@ -101,11 +101,11 @@ exports[`sfc props transform default values w/ type declaration, prod mode 1`] =
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
props: { props: {
foo: { default: 1 }, foo: { default: 1 },
bar: { default: () => {} }, bar: { default: () => ({}) },
baz: null, baz: null,
boola: { type: Boolean }, boola: { type: Boolean },
boolb: { type: [Boolean, Number] }, boolb: { type: [Boolean, Number] },
func: { type: Function, default: () => () => {} } func: { type: Function, default: () => (() => {}) }
}, },
setup(__props: any) { setup(__props: any) {

View File

@ -59,7 +59,7 @@ describe('sfc props transform', () => {
// function // function
expect(content).toMatch(`props: _mergeDefaults(['foo', 'bar'], { expect(content).toMatch(`props: _mergeDefaults(['foo', 'bar'], {
foo: 1, foo: 1,
bar: () => {} bar: () => ({})
})`) })`)
assertCode(content) assertCode(content)
}) })
@ -74,7 +74,7 @@ describe('sfc props transform', () => {
// function // function
expect(content).toMatch(`props: { expect(content).toMatch(`props: {
foo: { type: Number, required: false, default: 1 }, foo: { type: Number, required: false, default: 1 },
bar: { type: Object, required: false, default: () => {} } bar: { type: Object, required: false, default: () => ({}) }
}`) }`)
assertCode(content) assertCode(content)
}) })
@ -92,11 +92,11 @@ describe('sfc props transform', () => {
// function // function
expect(content).toMatch(`props: { expect(content).toMatch(`props: {
foo: { default: 1 }, foo: { default: 1 },
bar: { default: () => {} }, bar: { default: () => ({}) },
baz: null, baz: null,
boola: { type: Boolean }, boola: { type: Boolean },
boolb: { type: [Boolean, Number] }, boolb: { type: [Boolean, Number] },
func: { type: Function, default: () => () => {} } func: { type: Function, default: () => (() => {}) }
}`) }`)
assertCode(content) assertCode(content)
}) })

View File

@ -735,7 +735,7 @@ export function compileScript(
destructured.default.end! destructured.default.end!
) )
const isLiteral = destructured.default.type.endsWith('Literal') const isLiteral = destructured.default.type.endsWith('Literal')
return isLiteral ? value : `() => ${value}` return isLiteral ? value : `() => (${value})`
} }
} }