Skip to content

Commit

Permalink
fix(compiler-sfc): no params were generated when using withDefaults
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn committed Feb 7, 2025
1 parent 119f18c commit ad7bf8f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ export default /*@__PURE__*/_defineComponent({
qux: { type: Function, required: false, default() { return 1 } },
quux: { type: Function, required: false, default() { } },
quuxx: { type: Promise, required: false, async default() { return await Promise.resolve('hi') } },
quuux: { type: Number, required: false, default(a, [b, ...c], {d, ...e}, ...f) { return 1 } },
fred: { type: String, required: false, get default() { return 'fred' } }
},
setup(__props: any, { expose: __expose }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,14 @@ const props = defineProps({ foo: String })
qux?(): number;
quux?(): void
quuxx?: Promise<string>;
quuux?: number;
fred?: string
}>(), {
foo: 'hi',
qux() { return 1 },
['quux']() { },
async quuxx() { return await Promise.resolve('hi') },
quuux(a, [b, ...c], {d, ...e}, ...f) { return 1 },
get fred() { return 'fred' }
})
</script>
Expand All @@ -412,6 +414,9 @@ const props = defineProps({ foo: String })
expect(content).toMatch(
`quuxx: { type: Promise, required: false, async default() { return await Promise.resolve('hi') } }`,
)
expect(content).toMatch(
`quuux: { type: Number, required: false, default(a, [b, ...c], {d, ...e}, ...f) { return 1 } }`,
)
expect(content).toMatch(
`fred: { type: String, required: false, get default() { return 'fred' } }`,
)
Expand All @@ -423,6 +428,7 @@ const props = defineProps({ foo: String })
qux: BindingTypes.PROPS,
quux: BindingTypes.PROPS,
quuxx: BindingTypes.PROPS,
quuux: BindingTypes.PROPS,
fred: BindingTypes.PROPS,
props: BindingTypes.SETUP_CONST,
})
Expand Down
8 changes: 7 additions & 1 deletion packages/compiler-sfc/src/script/defineProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,15 @@ function genRuntimePropFromType(
// prop has corresponding static default value
defaultString = `default: ${ctx.getString(prop.value)}`
} else {
let paramsString = ''
if (prop.params.length) {
const start = prop.params[0].start
const end = prop.params[prop.params.length - 1].end
paramsString = ctx.getString({ start, end } as Node)
}
defaultString = `${prop.async ? 'async ' : ''}${
prop.kind !== 'method' ? `${prop.kind} ` : ''
}default() ${ctx.getString(prop.body)}`
}default(${paramsString}) ${ctx.getString(prop.body)}`
}
}
}
Expand Down

0 comments on commit ad7bf8f

Please sign in to comment.