From e502aa6c00a9d8c5aad1cd6f5173943b9a57a4d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Sat, 13 Aug 2022 02:57:38 +0800 Subject: [PATCH 1/2] feat(compiler-sfc): withDefaults supports reference a variable --- packages/compiler-sfc/src/compileScript.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index b7e4c0ea778..11653031fa7 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -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 @@ -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.`, From 301940d802423c28b70d2aa03fe849ffd29f4bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Sat, 13 Aug 2022 03:30:03 +0800 Subject: [PATCH 2/2] test: add tests --- .../__snapshots__/compileScript.spec.ts.snap | 27 +++++++++++++++++++ .../__tests__/compileScript.spec.ts | 23 ++++++++++++++++ packages/compiler-sfc/src/compileScript.ts | 6 +++-- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 649b92d17f0..5c192c36609 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -1776,6 +1776,33 @@ const props = __props as { +return { props, get defaults() { return defaults } } +} + +})" +`; + +exports[`SFC compile + `) + 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( diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 11653031fa7..3f33d0c8940 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -809,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 @@ -897,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) ===