2019-08-23 21:38:32 +08:00
|
|
|
import { readonly, toRaw, lock, unlock } from '@vue/reactivity'
|
2019-05-28 18:06:00 +08:00
|
|
|
import {
|
|
|
|
EMPTY_OBJ,
|
|
|
|
camelize,
|
|
|
|
hyphenate,
|
|
|
|
capitalize,
|
|
|
|
isString,
|
|
|
|
isFunction,
|
|
|
|
isArray,
|
2019-06-03 13:44:45 +08:00
|
|
|
isObject,
|
|
|
|
isReservedProp
|
2019-05-28 18:06:00 +08:00
|
|
|
} from '@vue/shared'
|
|
|
|
import { warn } from './warning'
|
2019-05-28 19:36:15 +08:00
|
|
|
import { Data, ComponentInstance } from './component'
|
2019-08-22 23:12:37 +08:00
|
|
|
import { PatchFlags } from './patchFlags'
|
2019-05-28 18:06:00 +08:00
|
|
|
|
|
|
|
export type ComponentPropsOptions<P = Data> = {
|
2019-06-01 00:47:05 +08:00
|
|
|
[K in keyof P]: Prop<P[K]> | null
|
2019-05-28 18:06:00 +08:00
|
|
|
}
|
|
|
|
|
2019-06-01 00:47:05 +08:00
|
|
|
type Prop<T> = PropOptions<T> | PropType<T>
|
2019-05-28 18:06:00 +08:00
|
|
|
|
2019-05-31 18:07:43 +08:00
|
|
|
interface PropOptions<T = any> {
|
2019-05-28 18:06:00 +08:00
|
|
|
type?: PropType<T> | true | null
|
|
|
|
required?: boolean
|
|
|
|
default?: T | null | undefined | (() => T | null | undefined)
|
2019-05-31 18:07:43 +08:00
|
|
|
validator?(value: any): boolean
|
|
|
|
}
|
|
|
|
|
2019-06-01 00:47:05 +08:00
|
|
|
export type PropType<T> = PropConstructor<T> | PropConstructor<T>[]
|
|
|
|
|
|
|
|
type PropConstructor<T> = { new (...args: any[]): T & object } | { (): T }
|
|
|
|
|
2019-06-12 15:43:19 +08:00
|
|
|
type RequiredKeys<T, MakeDefautRequired> = {
|
|
|
|
[K in keyof T]: T[K] extends
|
|
|
|
| { required: true }
|
|
|
|
| (MakeDefautRequired extends true ? { default: any } : never)
|
|
|
|
? K
|
|
|
|
: never
|
2019-06-01 00:47:05 +08:00
|
|
|
}[keyof T]
|
|
|
|
|
2019-06-12 15:43:19 +08:00
|
|
|
type OptionalKeys<T, MakeDefautRequired> = Exclude<
|
|
|
|
keyof T,
|
|
|
|
RequiredKeys<T, MakeDefautRequired>
|
|
|
|
>
|
2019-06-01 00:47:05 +08:00
|
|
|
|
|
|
|
type InferPropType<T> = T extends null
|
2019-06-01 17:43:41 +08:00
|
|
|
? any // null & true would fail to infer
|
|
|
|
: T extends { type: null | true }
|
|
|
|
? any // somehow `ObjectContructor` when inferred from { (): T } becomes `any`
|
|
|
|
: T extends ObjectConstructor | { type: ObjectConstructor }
|
2019-06-01 00:47:05 +08:00
|
|
|
? { [key: string]: any }
|
|
|
|
: T extends Prop<infer V> ? V : T
|
|
|
|
|
2019-06-12 15:43:19 +08:00
|
|
|
export type ExtractPropTypes<
|
|
|
|
O,
|
|
|
|
MakeDefautRequired extends boolean = true
|
|
|
|
> = O extends object
|
|
|
|
? {
|
|
|
|
readonly [K in RequiredKeys<O, MakeDefautRequired>]: InferPropType<O[K]>
|
|
|
|
} &
|
|
|
|
{
|
|
|
|
readonly [K in OptionalKeys<O, MakeDefautRequired>]?: InferPropType<
|
|
|
|
O[K]
|
|
|
|
>
|
|
|
|
}
|
2019-06-01 00:47:05 +08:00
|
|
|
: { [K in string]: any }
|
2019-05-28 18:06:00 +08:00
|
|
|
|
|
|
|
const enum BooleanFlags {
|
|
|
|
shouldCast = '1',
|
|
|
|
shouldCastTrue = '2'
|
|
|
|
}
|
|
|
|
|
2019-06-01 00:47:05 +08:00
|
|
|
type NormalizedProp =
|
|
|
|
| null
|
|
|
|
| (PropOptions & {
|
|
|
|
[BooleanFlags.shouldCast]?: boolean
|
|
|
|
[BooleanFlags.shouldCastTrue]?: boolean
|
|
|
|
})
|
2019-05-28 18:06:00 +08:00
|
|
|
|
|
|
|
type NormalizedPropsOptions = Record<string, NormalizedProp>
|
|
|
|
|
|
|
|
// resolve raw VNode data.
|
|
|
|
// - filter out reserved keys (key, ref, slots)
|
|
|
|
// - extract class and style into $attrs (to be merged onto child
|
|
|
|
// component root)
|
|
|
|
// - for the rest:
|
|
|
|
// - if has declared props: put declared ones in `props`, the rest in `attrs`
|
|
|
|
// - else: everything goes in `props`.
|
|
|
|
|
|
|
|
export function resolveProps(
|
2019-05-29 11:36:16 +08:00
|
|
|
instance: ComponentInstance,
|
2019-05-29 09:18:45 +08:00
|
|
|
rawProps: any,
|
2019-05-28 18:06:00 +08:00
|
|
|
_options: ComponentPropsOptions | void
|
2019-05-29 11:36:16 +08:00
|
|
|
) {
|
2019-05-28 18:06:00 +08:00
|
|
|
const hasDeclaredProps = _options != null
|
|
|
|
const options = normalizePropsOptions(_options) as NormalizedPropsOptions
|
2019-05-29 09:18:45 +08:00
|
|
|
if (!rawProps && !hasDeclaredProps) {
|
2019-05-29 11:36:16 +08:00
|
|
|
return
|
2019-05-28 18:06:00 +08:00
|
|
|
}
|
2019-05-30 23:16:15 +08:00
|
|
|
|
2019-05-28 18:06:00 +08:00
|
|
|
const props: any = {}
|
|
|
|
let attrs: any = void 0
|
2019-05-30 23:16:15 +08:00
|
|
|
|
|
|
|
// update the instance propsProxy (passed to setup()) to trigger potential
|
|
|
|
// changes
|
|
|
|
const propsProxy = instance.propsProxy
|
|
|
|
const setProp = propsProxy
|
|
|
|
? (key: string, val: any) => {
|
|
|
|
props[key] = val
|
|
|
|
propsProxy[key] = val
|
|
|
|
}
|
|
|
|
: (key: string, val: any) => {
|
|
|
|
props[key] = val
|
|
|
|
}
|
|
|
|
|
2019-08-23 21:38:32 +08:00
|
|
|
// allow mutation of propsProxy (which is readonly by default)
|
2019-05-30 23:16:15 +08:00
|
|
|
unlock()
|
|
|
|
|
2019-05-29 09:18:45 +08:00
|
|
|
if (rawProps != null) {
|
|
|
|
for (const key in rawProps) {
|
2019-06-03 13:44:45 +08:00
|
|
|
// key, ref are reserved
|
|
|
|
if (isReservedProp(key)) continue
|
2019-05-28 18:06:00 +08:00
|
|
|
// any non-declared data are put into a separate `attrs` object
|
|
|
|
// for spreading
|
|
|
|
if (hasDeclaredProps && !options.hasOwnProperty(key)) {
|
2019-05-29 09:18:45 +08:00
|
|
|
;(attrs || (attrs = {}))[key] = rawProps[key]
|
2019-05-28 18:06:00 +08:00
|
|
|
} else {
|
2019-05-30 23:16:15 +08:00
|
|
|
setProp(key, rawProps[key])
|
2019-05-28 18:06:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// set default values, cast booleans & run validators
|
|
|
|
if (hasDeclaredProps) {
|
|
|
|
for (const key in options) {
|
|
|
|
let opt = options[key]
|
|
|
|
if (opt == null) continue
|
|
|
|
const isAbsent = !props.hasOwnProperty(key)
|
|
|
|
const hasDefault = opt.hasOwnProperty('default')
|
|
|
|
const currentValue = props[key]
|
|
|
|
// default values
|
|
|
|
if (hasDefault && currentValue === undefined) {
|
|
|
|
const defaultValue = opt.default
|
2019-05-30 23:16:15 +08:00
|
|
|
setProp(key, isFunction(defaultValue) ? defaultValue() : defaultValue)
|
2019-05-28 18:06:00 +08:00
|
|
|
}
|
|
|
|
// boolean casting
|
|
|
|
if (opt[BooleanFlags.shouldCast]) {
|
|
|
|
if (isAbsent && !hasDefault) {
|
2019-05-30 23:16:15 +08:00
|
|
|
setProp(key, false)
|
2019-05-28 18:06:00 +08:00
|
|
|
} else if (
|
|
|
|
opt[BooleanFlags.shouldCastTrue] &&
|
|
|
|
(currentValue === '' || currentValue === hyphenate(key))
|
|
|
|
) {
|
2019-05-30 23:16:15 +08:00
|
|
|
setProp(key, true)
|
2019-05-28 18:06:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// runtime validation
|
2019-05-29 09:18:45 +08:00
|
|
|
if (__DEV__ && rawProps) {
|
2019-06-11 23:50:28 +08:00
|
|
|
validateProp(key, toRaw(rawProps[key]), opt, isAbsent)
|
2019-05-28 18:06:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if component has no declared props, $attrs === $props
|
|
|
|
attrs = props
|
|
|
|
}
|
2019-05-29 11:36:16 +08:00
|
|
|
|
2019-06-05 21:38:32 +08:00
|
|
|
// in case of dynamic props, check if we need to delete keys from
|
|
|
|
// the props proxy
|
|
|
|
const { patchFlag } = instance.vnode
|
2019-08-22 23:12:37 +08:00
|
|
|
if (
|
|
|
|
propsProxy !== null &&
|
|
|
|
(patchFlag === 0 || patchFlag & PatchFlags.FULL_PROPS)
|
|
|
|
) {
|
2019-06-11 23:50:28 +08:00
|
|
|
const rawInitialProps = toRaw(propsProxy)
|
2019-06-05 21:38:32 +08:00
|
|
|
for (const key in rawInitialProps) {
|
|
|
|
if (!props.hasOwnProperty(key)) {
|
|
|
|
delete propsProxy[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 21:38:32 +08:00
|
|
|
// lock readonly
|
2019-05-30 23:16:15 +08:00
|
|
|
lock()
|
|
|
|
|
2019-08-23 21:38:32 +08:00
|
|
|
instance.props = __DEV__ ? readonly(props) : props
|
2019-05-29 11:36:16 +08:00
|
|
|
instance.attrs = options
|
|
|
|
? __DEV__
|
2019-08-23 21:38:32 +08:00
|
|
|
? readonly(attrs)
|
2019-05-29 11:36:16 +08:00
|
|
|
: attrs
|
|
|
|
: instance.props
|
2019-05-28 18:06:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const normalizationMap = new WeakMap()
|
|
|
|
|
|
|
|
function normalizePropsOptions(
|
|
|
|
raw: ComponentPropsOptions | void
|
|
|
|
): NormalizedPropsOptions | void {
|
|
|
|
if (!raw) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (normalizationMap.has(raw)) {
|
|
|
|
return normalizationMap.get(raw)
|
|
|
|
}
|
|
|
|
const normalized: NormalizedPropsOptions = {}
|
|
|
|
normalizationMap.set(raw, normalized)
|
|
|
|
if (isArray(raw)) {
|
|
|
|
for (let i = 0; i < raw.length; i++) {
|
|
|
|
if (__DEV__ && !isString(raw[i])) {
|
|
|
|
warn(`props must be strings when using array syntax.`, raw[i])
|
|
|
|
}
|
|
|
|
const normalizedKey = camelize(raw[i])
|
2019-06-01 17:43:41 +08:00
|
|
|
if (normalizedKey[0] !== '$') {
|
2019-05-28 18:06:00 +08:00
|
|
|
normalized[normalizedKey] = EMPTY_OBJ
|
|
|
|
} else if (__DEV__) {
|
|
|
|
warn(`Invalid prop name: "${normalizedKey}" is a reserved property.`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (__DEV__ && !isObject(raw)) {
|
|
|
|
warn(`invalid props options`, raw)
|
|
|
|
}
|
|
|
|
for (const key in raw) {
|
|
|
|
const normalizedKey = camelize(key)
|
2019-06-01 17:43:41 +08:00
|
|
|
if (normalizedKey[0] !== '$') {
|
2019-05-28 18:06:00 +08:00
|
|
|
const opt = raw[key]
|
2019-06-01 00:47:05 +08:00
|
|
|
const prop: NormalizedProp = (normalized[normalizedKey] =
|
2019-05-28 18:06:00 +08:00
|
|
|
isArray(opt) || isFunction(opt) ? { type: opt } : opt)
|
2019-06-01 00:47:05 +08:00
|
|
|
if (prop != null) {
|
2019-05-28 18:06:00 +08:00
|
|
|
const booleanIndex = getTypeIndex(Boolean, prop.type)
|
|
|
|
const stringIndex = getTypeIndex(String, prop.type)
|
2019-06-01 00:47:05 +08:00
|
|
|
prop[BooleanFlags.shouldCast] = booleanIndex > -1
|
|
|
|
prop[BooleanFlags.shouldCastTrue] = booleanIndex < stringIndex
|
2019-05-28 18:06:00 +08:00
|
|
|
}
|
|
|
|
} else if (__DEV__) {
|
|
|
|
warn(`Invalid prop name: "${normalizedKey}" is a reserved property.`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return normalized
|
|
|
|
}
|
|
|
|
|
|
|
|
// use function string name to check type constructors
|
|
|
|
// so that it works across vms / iframes.
|
|
|
|
function getType(ctor: Prop<any>): string {
|
|
|
|
const match = ctor && ctor.toString().match(/^\s*function (\w+)/)
|
|
|
|
return match ? match[1] : ''
|
|
|
|
}
|
|
|
|
|
|
|
|
function isSameType(a: Prop<any>, b: Prop<any>): boolean {
|
|
|
|
return getType(a) === getType(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTypeIndex(
|
|
|
|
type: Prop<any>,
|
|
|
|
expectedTypes: PropType<any> | void | null | true
|
|
|
|
): number {
|
|
|
|
if (isArray(expectedTypes)) {
|
|
|
|
for (let i = 0, len = expectedTypes.length; i < len; i++) {
|
|
|
|
if (isSameType(expectedTypes[i], type)) {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (isObject(expectedTypes)) {
|
|
|
|
return isSameType(expectedTypes, type) ? 0 : -1
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
type AssertionResult = {
|
|
|
|
valid: boolean
|
|
|
|
expectedType: string
|
|
|
|
}
|
|
|
|
|
|
|
|
function validateProp(
|
|
|
|
name: string,
|
|
|
|
value: any,
|
|
|
|
prop: PropOptions<any>,
|
|
|
|
isAbsent: boolean
|
|
|
|
) {
|
|
|
|
const { type, required, validator } = prop
|
|
|
|
// required!
|
|
|
|
if (required && isAbsent) {
|
|
|
|
warn('Missing required prop: "' + name + '"')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// missing but optional
|
|
|
|
if (value == null && !prop.required) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// type check
|
|
|
|
if (type != null && type !== true) {
|
|
|
|
let isValid = false
|
|
|
|
const types = isArray(type) ? type : [type]
|
|
|
|
const expectedTypes = []
|
|
|
|
// value is valid as long as one of the specified types match
|
|
|
|
for (let i = 0; i < types.length && !isValid; i++) {
|
|
|
|
const { valid, expectedType } = assertType(value, types[i])
|
|
|
|
expectedTypes.push(expectedType || '')
|
|
|
|
isValid = valid
|
|
|
|
}
|
|
|
|
if (!isValid) {
|
|
|
|
warn(getInvalidTypeMessage(name, value, expectedTypes))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// custom validator
|
|
|
|
if (validator && !validator(value)) {
|
|
|
|
warn('Invalid prop: custom validator check failed for prop "' + name + '".')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const simpleCheckRE = /^(String|Number|Boolean|Function|Symbol)$/
|
|
|
|
|
2019-06-01 00:47:05 +08:00
|
|
|
function assertType(value: any, type: PropConstructor<any>): AssertionResult {
|
2019-05-28 18:06:00 +08:00
|
|
|
let valid
|
|
|
|
const expectedType = getType(type)
|
|
|
|
if (simpleCheckRE.test(expectedType)) {
|
|
|
|
const t = typeof value
|
|
|
|
valid = t === expectedType.toLowerCase()
|
|
|
|
// for primitive wrapper objects
|
|
|
|
if (!valid && t === 'object') {
|
|
|
|
valid = value instanceof type
|
|
|
|
}
|
|
|
|
} else if (expectedType === 'Object') {
|
|
|
|
valid = toRawType(value) === 'Object'
|
|
|
|
} else if (expectedType === 'Array') {
|
|
|
|
valid = isArray(value)
|
|
|
|
} else {
|
|
|
|
valid = value instanceof type
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
valid,
|
|
|
|
expectedType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getInvalidTypeMessage(
|
|
|
|
name: string,
|
|
|
|
value: any,
|
|
|
|
expectedTypes: string[]
|
|
|
|
): string {
|
|
|
|
let message =
|
|
|
|
`Invalid prop: type check failed for prop "${name}".` +
|
|
|
|
` Expected ${expectedTypes.map(capitalize).join(', ')}`
|
|
|
|
const expectedType = expectedTypes[0]
|
|
|
|
const receivedType = toRawType(value)
|
|
|
|
const expectedValue = styleValue(value, expectedType)
|
|
|
|
const receivedValue = styleValue(value, receivedType)
|
|
|
|
// check if we need to specify expected value
|
|
|
|
if (
|
|
|
|
expectedTypes.length === 1 &&
|
|
|
|
isExplicable(expectedType) &&
|
|
|
|
!isBoolean(expectedType, receivedType)
|
|
|
|
) {
|
|
|
|
message += ` with value ${expectedValue}`
|
|
|
|
}
|
|
|
|
message += `, got ${receivedType} `
|
|
|
|
// check if we need to specify received value
|
|
|
|
if (isExplicable(receivedType)) {
|
|
|
|
message += `with value ${receivedValue}.`
|
|
|
|
}
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
|
|
|
function styleValue(value: any, type: string): string {
|
|
|
|
if (type === 'String') {
|
|
|
|
return `"${value}"`
|
|
|
|
} else if (type === 'Number') {
|
|
|
|
return `${Number(value)}`
|
|
|
|
} else {
|
|
|
|
return `${value}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function toRawType(value: any): string {
|
|
|
|
return Object.prototype.toString.call(value).slice(8, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isExplicable(type: string): boolean {
|
|
|
|
const explicitTypes = ['string', 'number', 'boolean']
|
|
|
|
return explicitTypes.some(elem => type.toLowerCase() === elem)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isBoolean(...args: string[]): boolean {
|
|
|
|
return args.some(elem => elem.toLowerCase() === 'boolean')
|
|
|
|
}
|