From 269b342bd533598b301540c3dc93456bdc3c7710 Mon Sep 17 00:00:00 2001 From: edison Date: Thu, 9 May 2024 07:43:17 +0800 Subject: [PATCH] fix(compile-sfc): register props destructure rest id as setup bindings (#10888) close #10885 --- .../definePropsDestructure.spec.ts.snap | 16 ++++++++++++++ .../definePropsDestructure.spec.ts | 21 +++++++++++++++++++ packages/compiler-sfc/src/compileScript.ts | 8 ++++++- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap b/packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap index 8b5325cc3c5..7bf0597cc39 100644 --- a/packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap @@ -304,3 +304,19 @@ return () => {} }" `; + +exports[`sfc reactive props destructure > rest spread non-inline 1`] = ` +"import { createPropsRestProxy as _createPropsRestProxy } from 'vue' + +export default { + props: ['foo', 'bar'], + setup(__props, { expose: __expose }) { + __expose(); + + const rest = _createPropsRestProxy(__props, ["foo"]) + +return { rest } +} + +}" +`; diff --git a/packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts b/packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts index 3843ef92190..ecc7a09d011 100644 --- a/packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts @@ -265,6 +265,27 @@ describe('sfc reactive props destructure', () => { }) }) + test('rest spread non-inline', () => { + const { content, bindings } = compile( + ` + + + `, + { inlineTemplate: false }, + ) + expect(content).toMatch( + `const rest = _createPropsRestProxy(__props, ["foo"])`, + ) + assertCode(content) + expect(bindings).toStrictEqual({ + foo: BindingTypes.PROPS, + bar: BindingTypes.PROPS, + rest: BindingTypes.SETUP_REACTIVE_CONST, + }) + }) + // #6960 test('computed static key', () => { const { content, bindings } = compile(` diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index d4131d5c61d..31bee3af74d 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -523,8 +523,14 @@ export function compileScript( ) } - // defineProps / defineEmits + // defineProps const isDefineProps = processDefineProps(ctx, init, decl.id) + if (ctx.propsDestructureRestId) { + setupBindings[ctx.propsDestructureRestId] = + BindingTypes.SETUP_REACTIVE_CONST + } + + // defineEmits const isDefineEmits = !isDefineProps && processDefineEmits(ctx, init, decl.id) !isDefineEmits &&