From 96b4d766bd2a69d9da48f3a1685a76255d2fcbfa Mon Sep 17 00:00:00 2001 From: baiwusanyu <740132583@qq.com> Date: Wed, 23 Nov 2022 20:26:57 +0800 Subject: [PATCH 1/4] fix(runtime-core): Custom type props warning --- packages/runtime-core/src/componentProps.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index d59a4e94699..b66c46f9406 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -557,7 +557,13 @@ function validatePropName(key: string) { // use function string name to check type constructors // so that it works across vms / iframes. function getType(ctor: Prop): string { - const match = ctor && ctor.toString().match(/^\s*function (\w+)/) + const ctorStr = ctor && ctor.toString() + let match: RegExpMatchArray | null + if (ctorStr.startsWith('class')) { + match = ctor && ctor.toString().match(/^\s*class (\w+)/) + } else { + match = ctor && ctor.toString().match(/^\s*function (\w+)/) + } return match ? match[1] : ctor === null ? 'null' : '' } From a028a57540e344990a9c59ecacf31ab60d3af251 Mon Sep 17 00:00:00 2001 From: baiwusanyu <740132583@qq.com> Date: Wed, 23 Nov 2022 20:39:01 +0800 Subject: [PATCH 2/4] fix(runtime-core): update --- packages/runtime-core/src/componentProps.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index b66c46f9406..da85e65b1a4 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -558,10 +558,11 @@ function validatePropName(key: string) { // so that it works across vms / iframes. function getType(ctor: Prop): string { const ctorStr = ctor && ctor.toString() - let match: RegExpMatchArray | null - if (ctorStr.startsWith('class')) { + let match: RegExpMatchArray | null = null + if (ctorStr && ctorStr.startsWith('class')) { match = ctor && ctor.toString().match(/^\s*class (\w+)/) - } else { + } + if (ctorStr && ctorStr.startsWith('function')) { match = ctor && ctor.toString().match(/^\s*function (\w+)/) } return match ? match[1] : ctor === null ? 'null' : '' From edca573eea5701b4d9c68eab4f0b7c341f963e1d Mon Sep 17 00:00:00 2001 From: baiwusanyu <740132583@qq.com> Date: Wed, 23 Nov 2022 21:03:56 +0800 Subject: [PATCH 3/4] fix(runtime-core): update reg --- packages/runtime-core/src/componentProps.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index da85e65b1a4..fa756fa32f2 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -557,15 +557,8 @@ function validatePropName(key: string) { // use function string name to check type constructors // so that it works across vms / iframes. function getType(ctor: Prop): string { - const ctorStr = ctor && ctor.toString() - let match: RegExpMatchArray | null = null - if (ctorStr && ctorStr.startsWith('class')) { - match = ctor && ctor.toString().match(/^\s*class (\w+)/) - } - if (ctorStr && ctorStr.startsWith('function')) { - match = ctor && ctor.toString().match(/^\s*function (\w+)/) - } - return match ? match[1] : ctor === null ? 'null' : '' + const match = ctor && ctor.toString().match(/^\s*(function|class) (\w+)/) + return match ? match[2] : ctor === null ? 'null' : '' } function isSameType(a: Prop, b: Prop): boolean { From b770ffba09d7bc33aad80e6467432d990e917db9 Mon Sep 17 00:00:00 2001 From: Thorsten Luenborg Date: Wed, 23 Nov 2022 17:16:36 +0100 Subject: [PATCH 4/4] test(runtime-core): add test case for warnings about prop type mismatches --- .../__tests__/componentProps.spec.ts | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/packages/runtime-core/__tests__/componentProps.spec.ts b/packages/runtime-core/__tests__/componentProps.spec.ts index cdb77838e31..195f352154c 100644 --- a/packages/runtime-core/__tests__/componentProps.spec.ts +++ b/packages/runtime-core/__tests__/componentProps.spec.ts @@ -321,6 +321,42 @@ describe('component props', () => { expect(`Missing required prop: "num"`).toHaveBeenWarned() }) + test('warn on type mismatch', () => { + class MyClass { + + } + const Comp = { + props: { + bool: { type: Boolean }, + str: { type: String }, + num: { type: Number }, + arr: { type: Array }, + obj: { type: Object }, + cls: { type: MyClass }, + fn: { type: Function }, + }, + setup() { + return () => null + } + } + render(h(Comp, { + bool: 'true', + str: 100, + num: '100', + arr: {}, + obj: 'false', + cls: {}, + fn: true, + }), nodeOps.createElement('div')) + expect(`Invalid prop: type check failed for prop "bool". Expected Boolean, got String`).toHaveBeenWarned() + expect(`Invalid prop: type check failed for prop "str". Expected String with value "100", got Number with value 100.`).toHaveBeenWarned() + expect(`Invalid prop: type check failed for prop "num". Expected Number with value 100, got String with value "100".`).toHaveBeenWarned() + expect(`Invalid prop: type check failed for prop "arr". Expected Array, got Object`).toHaveBeenWarned() + expect(`Invalid prop: type check failed for prop "obj". Expected Object, got String with value "false"`).toHaveBeenWarned() + expect(`Invalid prop: type check failed for prop "fn". Expected Function, got Boolean with value true.`).toHaveBeenWarned() + expect(`Invalid prop: type check failed for prop "cls". Expected MyClass, got Object`).toHaveBeenWarned() + }) + // #3495 test('should not warn required props using kebab-case', async () => { const Comp = {