Skip to content

Commit 482f2e3

Browse files
committed
fix(compiler-sfc): use dynamic defaults merging for methods with computed keys
ref #7113
1 parent fe61944 commit 482f2e3

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

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

+24
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,30 @@ const props = __props as { foo: () => void, bar: boolean, baz: boolean | (() =>
20012001

20022002

20032003

2004+
return { props }
2005+
}
2006+
2007+
})"
2008+
`;
2009+
2010+
exports[`SFC compile <script setup> > with TypeScript > withDefaults w/ dynamic object method 1`] = `
2011+
"import { mergeDefaults as _mergeDefaults, defineComponent as _defineComponent } from 'vue'
2012+
2013+
export default /*#__PURE__*/_defineComponent({
2014+
props: _mergeDefaults({
2015+
foo: { type: Function, required: false }
2016+
}, {
2017+
['fo' + 'o']() { return 'foo' }
2018+
}),
2019+
setup(__props: any, { expose: __expose }) {
2020+
__expose();
2021+
2022+
const props = __props as {
2023+
foo?: () => 'string'
2024+
};
2025+
2026+
2027+
20042028
return { props }
20052029
}
20062030

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

+22
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,28 @@ const emit = defineEmits(['a', 'b'])
14301430
)
14311431
})
14321432

1433+
test('withDefaults w/ dynamic object method', () => {
1434+
const { content } = compile(`
1435+
<script setup lang="ts">
1436+
const props = withDefaults(defineProps<{
1437+
foo?: () => 'string'
1438+
}>(), {
1439+
['fo' + 'o']() { return 'foo' }
1440+
})
1441+
</script>
1442+
`)
1443+
assertCode(content)
1444+
expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
1445+
expect(content).toMatch(
1446+
`
1447+
_mergeDefaults({
1448+
foo: { type: Function, required: false }
1449+
}, {
1450+
['fo' + 'o']() { return 'foo' }
1451+
})`.trim()
1452+
)
1453+
})
1454+
14331455
test('defineEmits w/ type', () => {
14341456
const { content } = compile(`
14351457
<script setup lang="ts">

packages/compiler-sfc/src/compileScript.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,8 @@ export function compileScript(
842842
propsRuntimeDefaults.type === 'ObjectExpression' &&
843843
propsRuntimeDefaults.properties.every(
844844
node =>
845-
(node.type === 'ObjectProperty' &&
846-
(!node.computed || node.key.type.endsWith('Literal'))) ||
847-
node.type === 'ObjectMethod'
845+
node.type !== 'SpreadElement' &&
846+
(!node.computed || node.key.type.endsWith('Literal'))
848847
)
849848
)
850849
}

0 commit comments

Comments
 (0)