Skip to content

Commit

Permalink
fix(compiler-sfc): props bindings should not override user declared b…
Browse files Browse the repository at this point in the history
…indings

fix #8148
  • Loading branch information
yyx990803 committed Apr 25, 2023
1 parent 01f43c1 commit 433a58c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
15 changes: 15 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
8 changes: 6 additions & 2 deletions packages/compiler-sfc/src/script/defineProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down Expand Up @@ -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 = `{
Expand Down

0 comments on commit 433a58c

Please sign in to comment.