Skip to content

Commit b0a1f05

Browse files
authored
fix(compiler-sfc): no params were generated when using withDefaults (#12823)
fix #12822
1 parent 48b7552 commit b0a1f05

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ export default /*@__PURE__*/_defineComponent({
517517
qux: { type: Function, required: false, default() { return 1 } },
518518
quux: { type: Function, required: false, default() { } },
519519
quuxx: { type: Promise, required: false, async default() { return await Promise.resolve('hi') } },
520+
quuux: { type: Number, required: false, default(a, [b, ...c], {d, ...e}, ...f) { return 1 } },
520521
fred: { type: String, required: false, get default() { return 'fred' } }
521522
},
522523
setup(__props: any, { expose: __expose }) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,14 @@ const props = defineProps({ foo: String })
387387
qux?(): number;
388388
quux?(): void
389389
quuxx?: Promise<string>;
390+
quuux?: number;
390391
fred?: string
391392
}>(), {
392393
foo: 'hi',
393394
qux() { return 1 },
394395
['quux']() { },
395396
async quuxx() { return await Promise.resolve('hi') },
397+
quuux(a, [b, ...c], {d, ...e}, ...f) { return 1 },
396398
get fred() { return 'fred' }
397399
})
398400
</script>
@@ -412,6 +414,9 @@ const props = defineProps({ foo: String })
412414
expect(content).toMatch(
413415
`quuxx: { type: Promise, required: false, async default() { return await Promise.resolve('hi') } }`,
414416
)
417+
expect(content).toMatch(
418+
`quuux: { type: Number, required: false, default(a, [b, ...c], {d, ...e}, ...f) { return 1 } }`,
419+
)
415420
expect(content).toMatch(
416421
`fred: { type: String, required: false, get default() { return 'fred' } }`,
417422
)
@@ -423,6 +428,7 @@ const props = defineProps({ foo: String })
423428
qux: BindingTypes.PROPS,
424429
quux: BindingTypes.PROPS,
425430
quuxx: BindingTypes.PROPS,
431+
quuux: BindingTypes.PROPS,
426432
fred: BindingTypes.PROPS,
427433
props: BindingTypes.SETUP_CONST,
428434
})

packages/compiler-sfc/src/script/defineProps.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,15 @@ function genRuntimePropFromType(
260260
// prop has corresponding static default value
261261
defaultString = `default: ${ctx.getString(prop.value)}`
262262
} else {
263+
let paramsString = ''
264+
if (prop.params.length) {
265+
const start = prop.params[0].start
266+
const end = prop.params[prop.params.length - 1].end
267+
paramsString = ctx.getString({ start, end } as Node)
268+
}
263269
defaultString = `${prop.async ? 'async ' : ''}${
264270
prop.kind !== 'method' ? `${prop.kind} ` : ''
265-
}default() ${ctx.getString(prop.body)}`
271+
}default(${paramsString}) ${ctx.getString(prop.body)}`
266272
}
267273
}
268274
}

0 commit comments

Comments
 (0)