From 77a804b1d0d6a3f12fb3674cdceb85ebd6481e02 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 9 Feb 2024 08:47:06 +0800 Subject: [PATCH] fix(dx): warn against reserved keys as prop name close #10281 --- .../__tests__/componentProps.spec.ts | 19 +++++++++++++++++++ packages/runtime-core/src/componentProps.ts | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/componentProps.spec.ts b/packages/runtime-core/__tests__/componentProps.spec.ts index 6760a957f..6fc5d4dd2 100644 --- a/packages/runtime-core/__tests__/componentProps.spec.ts +++ b/packages/runtime-core/__tests__/componentProps.spec.ts @@ -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() + }) }) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 088f4da3c..d1822a163 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -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.`)