From 433a58ccb61c25512dcc3df155b8e285256917ef Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 25 Apr 2023 09:21:14 +0800 Subject: [PATCH] fix(compiler-sfc): props bindings should not override user declared bindings fix #8148 --- .../__tests__/compileScript/defineProps.spec.ts | 15 +++++++++++++++ packages/compiler-sfc/src/script/defineProps.ts | 8 ++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts b/packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts index cf61c98406c..f52307d41b3 100644 --- a/packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts @@ -571,6 +571,21 @@ const props = defineProps({ foo: String }) ).toMatch(`foo: { type: Number`) }) + // #8148 + test('should not override local bindings', () => { + const { bindings } = compile(` + + `) + expect(bindings).toStrictEqual({ + bar: BindingTypes.SETUP_MAYBE_REF, + computed: BindingTypes.SETUP_CONST + }) + }) + describe('errors', () => { test('w/ both type and non-type args', () => { expect(() => { diff --git a/packages/compiler-sfc/src/script/defineProps.ts b/packages/compiler-sfc/src/script/defineProps.ts index 16ea02fe3cf..dde415c21a1 100644 --- a/packages/compiler-sfc/src/script/defineProps.ts +++ b/packages/compiler-sfc/src/script/defineProps.ts @@ -58,7 +58,9 @@ export function processDefineProps( // register bindings if (ctx.propsRuntimeDecl) { for (const key of getObjectOrArrayExpressionKeys(ctx.propsRuntimeDecl)) { - ctx.bindingMetadata[key] = BindingTypes.PROPS + if (!(key in ctx.bindingMetadata)) { + ctx.bindingMetadata[key] = BindingTypes.PROPS + } } } @@ -170,7 +172,9 @@ function genRuntimePropsFromTypes(ctx: ScriptCompileContext) { for (const prop of props) { propStrings.push(genRuntimePropFromType(ctx, prop, hasStaticDefaults)) // register bindings - ctx.bindingMetadata[prop.key] = BindingTypes.PROPS + if (!(prop.key in ctx.bindingMetadata)) { + ctx.bindingMetadata[prop.key] = BindingTypes.PROPS + } } let propsDecls = `{