Skip to content

Commit

Permalink
fix: The withDefaults function loses arguments when assigning a funct…
Browse files Browse the repository at this point in the history
…ion default value vuejs#5253
  • Loading branch information
hcg1023 committed Mar 23, 2022
1 parent 1070f12 commit 3a3c9c5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1420,12 +1420,14 @@ export default /*#__PURE__*/_defineComponent({
foo: { type: String, required: false, default: 'hi' },
bar: { type: Number, required: false },
baz: { type: Boolean, required: true },
qux: { type: Function, required: false, default() { return 1 } }
qux: { type: Function, required: false, default() { return 1 } },
toLowerCase: { type: Function, required: true, default: (str: string) => str.toLowerCase() },
toUpperCase: { type: Function, required: true, default(str: string) { return str.toUpperCase() } }
},
setup(__props: any, { expose }) {
expose();
const props = __props as { foo: string, bar?: number, baz: boolean, qux(): number }
const props = __props as { foo: string, bar?: number, baz: boolean, qux(): number, toLowerCase: (str: string) => string, toUpperCase: (str: string) => string }
Expand Down
18 changes: 15 additions & 3 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,10 +902,14 @@ const emit = defineEmits(['a', 'b'])
foo?: string
bar?: number;
baz: boolean;
qux?(): number
qux?(): number;
toLowerCase: (str: string) => string
toUpperCase: (str: string) => string
}>(), {
foo: 'hi',
qux() { return 1 }
qux() { return 1 },
toLowerCase: (str: string) => str.toLowerCase(),
toUpperCase(str: string){ return str.toUpperCase() }
})
</script>
`)
Expand All @@ -918,15 +922,23 @@ const emit = defineEmits(['a', 'b'])
expect(content).toMatch(
`qux: { type: Function, required: false, default() { return 1 } }`
)
expect(content).toMatch(
`toLowerCase: { type: Function, required: true, default: (str: string) => str.toLowerCase() }`
)
expect(content).toMatch(
`toUpperCase: { type: Function, required: true, default(str: string) { return str.toUpperCase() } }`
)
expect(content).toMatch(
`{ foo: string, bar?: number, baz: boolean, qux(): number }`
`{ foo: string, bar?: number, baz: boolean, qux(): number, toLowerCase: (str: string) => string, toUpperCase: (str: string) => string }`
)
expect(content).toMatch(`const props = __props`)
expect(bindings).toStrictEqual({
foo: BindingTypes.PROPS,
bar: BindingTypes.PROPS,
baz: BindingTypes.PROPS,
qux: BindingTypes.PROPS,
toLowerCase: BindingTypes.PROPS,
toUpperCase: BindingTypes.PROPS,
props: BindingTypes.SETUP_CONST
})
})
Expand Down
11 changes: 10 additions & 1 deletion packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,15 @@ export function compileScript(
)
}

function genRuntimeFunctionPropParameters(params: ObjectMethod['params'], source: string) {
if (!params.length) {
return ''
}
return params.reduce((result, paramNode) => {
return `${result}${result ? ',' : ''}${ source.slice(paramNode.start!, paramNode.end!)}`
}, '')
}

function genRuntimeProps(props: Record<string, PropTypeData>) {
const keys = Object.keys(props)
if (!keys.length) {
Expand All @@ -686,7 +695,7 @@ export function compileScript(
prop.value.end!
)}`
} else {
defaultString = `default() ${scriptSetupSource.slice(
defaultString = `default(${genRuntimeFunctionPropParameters(prop.params, scriptSetupSource)}) ${scriptSetupSource.slice(
prop.body.start!,
prop.body.end!
)}`
Expand Down

0 comments on commit 3a3c9c5

Please sign in to comment.