From: yang <504575307@qq.com> Date: Mon, 16 Mar 2020 22:45:08 +0000 (+0800) Subject: refactor(componentProps): extract validate prop name logic (#825) X-Git-Tag: v3.0.0-alpha.9~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3d38e6faf34b0191b1d4354569d5904ecf181780;p=thirdparty%2Fvuejs%2Fcore.git refactor(componentProps): extract validate prop name logic (#825) --- diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 67ba81b9b0..c2cbb473d6 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -223,6 +223,15 @@ const normalizationMap = new WeakMap< NormalizedPropsOptions >() +function validatePropName(key: string) { + if (key[0] !== '$') { + return true + } else if (__DEV__) { + warn(`Invalid prop name: "${key}" is a reserved property.`) + } + return false +} + function normalizePropsOptions( raw: ComponentPropsOptions | void ): NormalizedPropsOptions { @@ -240,10 +249,8 @@ function normalizePropsOptions( warn(`props must be strings when using array syntax.`, raw[i]) } const normalizedKey = camelize(raw[i]) - if (normalizedKey[0] !== '$') { + if (validatePropName(normalizedKey)) { options[normalizedKey] = EMPTY_OBJ - } else if (__DEV__) { - warn(`Invalid prop name: "${normalizedKey}" is a reserved property.`) } } } else { @@ -252,7 +259,7 @@ function normalizePropsOptions( } for (const key in raw) { const normalizedKey = camelize(key) - if (normalizedKey[0] !== '$') { + if (validatePropName(normalizedKey)) { const opt = raw[key] const prop: NormalizedProp = (options[normalizedKey] = isArray(opt) || isFunction(opt) ? { type: opt } : opt) @@ -267,8 +274,6 @@ function normalizePropsOptions( needCastKeys.push(normalizedKey) } } - } else if (__DEV__) { - warn(`Invalid prop name: "${normalizedKey}" is a reserved property.`) } } }