refactor(componentProps): extract validate prop name logic (#825)
This commit is contained in:
parent
703c9bc61f
commit
3d38e6faf3
@ -223,6 +223,15 @@ const normalizationMap = new WeakMap<
|
|||||||
NormalizedPropsOptions
|
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(
|
function normalizePropsOptions(
|
||||||
raw: ComponentPropsOptions | void
|
raw: ComponentPropsOptions | void
|
||||||
): NormalizedPropsOptions {
|
): NormalizedPropsOptions {
|
||||||
@ -240,10 +249,8 @@ function normalizePropsOptions(
|
|||||||
warn(`props must be strings when using array syntax.`, raw[i])
|
warn(`props must be strings when using array syntax.`, raw[i])
|
||||||
}
|
}
|
||||||
const normalizedKey = camelize(raw[i])
|
const normalizedKey = camelize(raw[i])
|
||||||
if (normalizedKey[0] !== '$') {
|
if (validatePropName(normalizedKey)) {
|
||||||
options[normalizedKey] = EMPTY_OBJ
|
options[normalizedKey] = EMPTY_OBJ
|
||||||
} else if (__DEV__) {
|
|
||||||
warn(`Invalid prop name: "${normalizedKey}" is a reserved property.`)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -252,7 +259,7 @@ function normalizePropsOptions(
|
|||||||
}
|
}
|
||||||
for (const key in raw) {
|
for (const key in raw) {
|
||||||
const normalizedKey = camelize(key)
|
const normalizedKey = camelize(key)
|
||||||
if (normalizedKey[0] !== '$') {
|
if (validatePropName(normalizedKey)) {
|
||||||
const opt = raw[key]
|
const opt = raw[key]
|
||||||
const prop: NormalizedProp = (options[normalizedKey] =
|
const prop: NormalizedProp = (options[normalizedKey] =
|
||||||
isArray(opt) || isFunction(opt) ? { type: opt } : opt)
|
isArray(opt) || isFunction(opt) ? { type: opt } : opt)
|
||||||
@ -267,8 +274,6 @@ function normalizePropsOptions(
|
|||||||
needCastKeys.push(normalizedKey)
|
needCastKeys.push(normalizedKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (__DEV__) {
|
|
||||||
warn(`Invalid prop name: "${normalizedKey}" is a reserved property.`)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user