Browse Source

fix(compiler-sfc): props bindings should not override user declared bindings

fix #8148
pull/8154/head
Evan You 2 years ago
parent
commit
433a58ccb6
  1. 15
      packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts
  2. 8
      packages/compiler-sfc/src/script/defineProps.ts

15
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(`
<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(() => {

8
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 = `{

Loading…
Cancel
Save