Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler-sfc): supports reference a variable in withDefaults #6459

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,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();

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 @@ -1190,6 +1190,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
18 changes: 9 additions & 9 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export function compileScript(
let hasDefaultExportName = false
let hasDefaultExportRender = 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 @@ -528,16 +528,14 @@ export function compileScript(
node.callee
)
}
propsRuntimeDefaults = node.arguments[1] as ObjectExpression
if (
!propsRuntimeDefaults ||
propsRuntimeDefaults.type !== 'ObjectExpression'
) {
propsRuntimeDefaults = node.arguments[1]
if (!propsRuntimeDefaults) {
error(
`The 2nd argument of ${WITH_DEFAULTS} must be an object literal.`,
`The 2nd argument of ${WITH_DEFAULTS} is required.`,
propsRuntimeDefaults || node
)
}
checkInvalidScopeReference(propsRuntimeDefaults, WITH_DEFAULTS)
} else {
error(
`${WITH_DEFAULTS}' first argument must be a ${DEFINE_PROPS} call.`,
Expand Down Expand Up @@ -811,7 +809,9 @@ export function compileScript(
if (destructured) {
defaultString = `default: ${destructured}`
} 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 @@ -899,7 +899,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