]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): props bindings should not override user declared bindings
authorEvan You <yyx990803@gmail.com>
Tue, 25 Apr 2023 01:21:14 +0000 (09:21 +0800)
committerEvan You <yyx990803@gmail.com>
Tue, 25 Apr 2023 01:21:14 +0000 (09:21 +0800)
fix #8148

packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts
packages/compiler-sfc/src/script/defineProps.ts

index cf61c98406ca6e580618b0f82ecd5554c8b162ee..f52307d41b32bd90a79036f78ba2e5f71acf7fa1 100644 (file)
@@ -571,6 +571,21 @@ const props = defineProps({ foo: String })
     ).toMatch(`foo: { type: Number`)
   })
 
+  // #8148
+  test('should not override local bindings', () => {
+    const { bindings } = compile(`
+    <script setup lang="ts">
+    import { computed } from 'vue'
+    defineProps<{ bar: string }>()
+    const bar = computed(() => 1)
+    </script>
+  `)
+    expect(bindings).toStrictEqual({
+      bar: BindingTypes.SETUP_MAYBE_REF,
+      computed: BindingTypes.SETUP_CONST
+    })
+  })
+
   describe('errors', () => {
     test('w/ both type and non-type args', () => {
       expect(() => {
index 16ea02fe3cf1383b2c0bbae82ba286763a11f512..dde415c21a1078905b65d7189458b4a8a2bff65f 100644 (file)
@@ -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 = `{