Skip to content

Commit 8a882ce

Browse files
authoredNov 9, 2022
fix(compiler-sfc): handle method shorthand syntax in withDefaults (#6972)
fix #6971
1 parent 5bfe438 commit 8a882ce

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed
 

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -1744,12 +1744,14 @@ export default /*#__PURE__*/_defineComponent({
17441744
bar: { type: Number, required: false },
17451745
baz: { type: Boolean, required: true },
17461746
qux: { type: Function, required: false, default() { return 1 } },
1747-
quux: { type: Function, required: false, default() { } }
1747+
quux: { type: Function, required: false, default() { } },
1748+
quuxx: { type: Promise, required: false, async default() { return await Promise.resolve('hi') } },
1749+
fred: { type: String, required: false, get default() { return 'fred' } }
17481750
},
17491751
setup(__props: any, { expose }) {
17501752
expose();
17511753

1752-
const props = __props as { foo: string, bar?: number, baz: boolean, qux(): number, quux(): void };
1754+
const props = __props as { foo: string, bar?: number, baz: boolean, qux(): number, quux(): void, quuxx: Promise<string>, fred: string };
17531755

17541756

17551757

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -1041,10 +1041,14 @@ const emit = defineEmits(['a', 'b'])
10411041
baz: boolean;
10421042
qux?(): number;
10431043
quux?(): void
1044+
quuxx?: Promise<string>;
1045+
fred?: string
10441046
}>(), {
10451047
foo: 'hi',
10461048
qux() { return 1 },
1047-
['quux']() { }
1049+
['quux']() { },
1050+
async quuxx() { return await Promise.resolve('hi') },
1051+
get fred() { return 'fred' }
10481052
})
10491053
</script>
10501054
`)
@@ -1061,7 +1065,13 @@ const emit = defineEmits(['a', 'b'])
10611065
`quux: { type: Function, required: false, default() { } }`
10621066
)
10631067
expect(content).toMatch(
1064-
`{ foo: string, bar?: number, baz: boolean, qux(): number, quux(): void }`
1068+
`quuxx: { type: Promise, required: false, async default() { return await Promise.resolve('hi') } }`
1069+
)
1070+
expect(content).toMatch(
1071+
`fred: { type: String, required: false, get default() { return 'fred' } }`
1072+
)
1073+
expect(content).toMatch(
1074+
`{ foo: string, bar?: number, baz: boolean, qux(): number, quux(): void, quuxx: Promise<string>, fred: string }`
10651075
)
10661076
expect(content).toMatch(`const props = __props`)
10671077
expect(bindings).toStrictEqual({
@@ -1070,6 +1080,8 @@ const emit = defineEmits(['a', 'b'])
10701080
baz: BindingTypes.PROPS,
10711081
qux: BindingTypes.PROPS,
10721082
quux: BindingTypes.PROPS,
1083+
quuxx: BindingTypes.PROPS,
1084+
fred: BindingTypes.PROPS,
10731085
props: BindingTypes.SETUP_CONST
10741086
})
10751087
})

‎packages/compiler-sfc/src/compileScript.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,9 @@ export function compileScript(
805805
prop.value.end!
806806
)}`
807807
} else {
808-
defaultString = `default() ${scriptSetupSource.slice(
808+
defaultString = `${prop.async ? 'async ' : ''}${
809+
prop.kind !== 'method' ? `${prop.kind} ` : ''
810+
}default() ${scriptSetupSource.slice(
809811
prop.body.start!,
810812
prop.body.end!
811813
)}`

0 commit comments

Comments
 (0)
Please sign in to comment.