Skip to content

Commit

Permalink
fix(compiler-sfc): check static default of computed key of object method
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jan 26, 2023
1 parent 0fbc19f commit 446498a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,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();

const props = __props as {
foo?: () => 'string'
};



return { props }
}

Expand Down
22 changes: 22 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,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">
Expand Down
5 changes: 2 additions & 3 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,9 +784,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'))
)
)
}
Expand Down

0 comments on commit 446498a

Please sign in to comment.