Skip to content

Commit

Permalink
fix(dx): warn against reserved keys as prop name
Browse files Browse the repository at this point in the history
close #10281
  • Loading branch information
yyx990803 committed Feb 9, 2024
1 parent dfb271a commit 77a804b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/runtime-core/__tests__/componentProps.spec.ts
Expand Up @@ -729,4 +729,23 @@ describe('component props', () => {

expect(Object.keys(props.msg).length).toBe(1)
})

test('should warn against reserved prop names', () => {
const Comp = defineComponent({
props: {
key: String,
ref: String,
$foo: String,
},
render() {},
})

const root = nodeOps.createElement('div')

render(h(Comp, { msg: 'test' }), root)

expect(`Invalid prop name: "key"`).toHaveBeenWarned()
expect(`Invalid prop name: "ref"`).toHaveBeenWarned()
expect(`Invalid prop name: "$foo"`).toHaveBeenWarned()
})
})
2 changes: 1 addition & 1 deletion packages/runtime-core/src/componentProps.ts
Expand Up @@ -586,7 +586,7 @@ export function normalizePropsOptions(
}

function validatePropName(key: string) {
if (key[0] !== '$') {
if (key[0] !== '$' && !isReservedProp(key)) {
return true
} else if (__DEV__) {
warn(`Invalid prop name: "${key}" is a reserved property.`)
Expand Down

0 comments on commit 77a804b

Please sign in to comment.