Skip to content

Commit

Permalink
feat(compiler-sfc): support arbitrary expression as withDefaults argu…
Browse files Browse the repository at this point in the history
…ment

ref #6459
  • Loading branch information
yyx990803 committed Mar 31, 2023
1 parent 566748c commit fe61944
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,33 @@ const props = __props as {



return { props, get defaults() { return defaults } }
}

})"
`;

exports[`SFC compile <script setup> > with TypeScript > withDefaults (reference) 1`] = `
"import { mergeDefaults as _mergeDefaults, defineComponent as _defineComponent } from 'vue'
import { defaults } from './foo'

export default /*#__PURE__*/_defineComponent({
props: _mergeDefaults({
foo: { type: String, required: false },
bar: { type: Number, required: false },
baz: { type: Boolean, required: true }
}, defaults),
setup(__props: any, { expose: __expose }) {
__expose();

const props = __props as {
foo?: string
bar?: number
baz: boolean
};



return { props, get defaults() { return defaults } }
}

Expand Down
23 changes: 23 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,29 @@ const emit = defineEmits(['a', 'b'])
)
})

test('withDefaults (reference)', () => {
const { content } = compile(`
<script setup lang="ts">
import { defaults } from './foo'
const props = withDefaults(defineProps<{
foo?: string
bar?: number
baz: boolean
}>(), defaults)
</script>
`)
assertCode(content)
expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
expect(content).toMatch(
`
_mergeDefaults({
foo: { type: String, required: false },
bar: { type: Number, required: false },
baz: { type: Boolean, required: true }
}, defaults)`.trim()
)
})

// #7111
test('withDefaults (dynamic) w/ production mode', () => {
const { content } = compile(
Expand Down
20 changes: 8 additions & 12 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ export function compileScript(
let hasDefaultExportRender = false
let hasDefineOptionsCall = false
let propsRuntimeDecl: Node | undefined
let propsRuntimeDefaults: ObjectExpression | undefined
let propsRuntimeDefaults: Node | undefined
let propsDestructureDecl: Node | undefined
let propsDestructureRestId: string | undefined
let propsTypeDecl: PropsDeclType | undefined
Expand Down Expand Up @@ -534,15 +534,9 @@ export function compileScript(
node.callee
)
}
propsRuntimeDefaults = node.arguments[1] as ObjectExpression
if (
!propsRuntimeDefaults ||
propsRuntimeDefaults.type !== 'ObjectExpression'
) {
error(
`The 2nd argument of ${WITH_DEFAULTS} must be an object literal.`,
propsRuntimeDefaults || node
)
propsRuntimeDefaults = node.arguments[1]
if (!propsRuntimeDefaults) {
error(`The 2nd argument of ${WITH_DEFAULTS} is required.`, node)
}
} else {
error(
Expand Down Expand Up @@ -872,7 +866,9 @@ export function compileScript(
destructured.needSkipFactory ? `, skipFactory: true` : ``
}`
} else if (hasStaticDefaults) {
const prop = propsRuntimeDefaults!.properties.find(node => {
const prop = (
propsRuntimeDefaults as ObjectExpression
).properties.find(node => {
if (node.type === 'SpreadElement') return false
return resolveObjectKey(node.key, node.computed) === key
}) as ObjectProperty | ObjectMethod
Expand Down Expand Up @@ -1001,7 +997,7 @@ export function compileScript(
m.key.type === 'Identifier'
) {
if (
propsRuntimeDefaults!.properties.some(p => {
(propsRuntimeDefaults as ObjectExpression).properties.some(p => {
if (p.type === 'SpreadElement') return false
return (
resolveObjectKey(p.key, p.computed) ===
Expand Down

0 comments on commit fe61944

Please sign in to comment.