fix(compiler-sfc): use dynamic defaults merging for methods with computed keys

ref #7113
This commit is contained in:
Evan You 2023-03-31 09:08:23 +08:00
parent fe619443d2
commit 482f2e3434
3 changed files with 48 additions and 3 deletions

View File

@ -2001,6 +2001,30 @@ const props = __props as { foo: () => void, bar: boolean, baz: boolean | (() =>
return { props }
}
})"
`;
exports[`SFC compile <script setup> > with TypeScript > withDefaults w/ dynamic object method 1`] = `
"import { mergeDefaults as _mergeDefaults, defineComponent as _defineComponent } from 'vue'
export default /*#__PURE__*/_defineComponent({
props: _mergeDefaults({
foo: { type: Function, required: false }
}, {
['fo' + 'o']() { return 'foo' }
}),
setup(__props: any, { expose: __expose }) {
__expose();
const props = __props as {
foo?: () => 'string'
};
return { props }
}

View File

@ -1430,6 +1430,28 @@ const emit = defineEmits(['a', 'b'])
)
})
test('withDefaults w/ dynamic object method', () => {
const { content } = compile(`
<script setup lang="ts">
const props = withDefaults(defineProps<{
foo?: () => 'string'
}>(), {
['fo' + 'o']() { return 'foo' }
})
</script>
`)
assertCode(content)
expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
expect(content).toMatch(
`
_mergeDefaults({
foo: { type: Function, required: false }
}, {
['fo' + 'o']() { return 'foo' }
})`.trim()
)
})
test('defineEmits w/ type', () => {
const { content } = compile(`
<script setup lang="ts">

View File

@ -842,9 +842,8 @@ export function compileScript(
propsRuntimeDefaults.type === 'ObjectExpression' &&
propsRuntimeDefaults.properties.every(
node =>
(node.type === 'ObjectProperty' &&
(!node.computed || node.key.type.endsWith('Literal'))) ||
node.type === 'ObjectMethod'
node.type !== 'SpreadElement' &&
(!node.computed || node.key.type.endsWith('Literal'))
)
)
}