vue3-yuanma/packages/runtime-core/src/componentProps.ts

621 lines
16 KiB
TypeScript
Raw Normal View History

import {
toRaw,
shallowReactive,
trigger,
TriggerOpTypes
} 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,
hasOwn,
toRawType,
PatchFlags,
makeMap,
isReservedProp,
EMPTY_ARR,
def,
extend
2019-05-28 18:06:00 +08:00
} from '@vue/shared'
import { warn } from './warning'
import {
Data,
ComponentInternalInstance,
ComponentOptions,
ConcreteComponent,
setCurrentInstance
} from './component'
import { isEmitListener } from './componentEmits'
import { InternalObjectKey } from './vnode'
import { AppContext } from './apiCreateApp'
2019-05-28 18:06:00 +08:00
2019-10-08 21:26:09 +08:00
export type ComponentPropsOptions<P = Data> =
| ComponentObjectPropsOptions<P>
| string[]
export type ComponentObjectPropsOptions<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
}
export type Prop<T, D = T> = PropOptions<T, D> | PropType<T>
2019-05-28 18:06:00 +08:00
type DefaultFactory<T> = (props: Data) => T | null | undefined
interface PropOptions<T = any, D = T> {
2019-05-28 18:06:00 +08:00
type?: PropType<T> | true | null
required?: boolean
default?: D | DefaultFactory<D> | null | undefined | object
2019-10-22 23:26:48 +08:00
validator?(value: unknown): boolean
2019-05-31 18:07:43 +08:00
}
2019-06-01 00:47:05 +08:00
export type PropType<T> = PropConstructor<T> | PropConstructor<T>[]
type PropConstructor<T = any> =
| { new (...args: any[]): T & {} }
| { (): T }
| PropMethod<T>
type PropMethod<T, TConstructor = any> = [T] extends [(...args: any) => any] // if is function with args
? { new (): TConstructor; (): T; readonly prototype: TConstructor } // Create Function like constructor
: never
2019-06-01 00:47:05 +08:00
type RequiredKeys<T> = {
[K in keyof T]: T[K] extends
| { required: true }
| { default: any }
// don't mark Boolean props as undefined
| BooleanConstructor
| { type: BooleanConstructor }
? K
: never
2019-06-01 00:47:05 +08:00
}[keyof T]
type OptionalKeys<T> = Exclude<keyof T, RequiredKeys<T>>
type DefaultKeys<T> = {
[K in keyof T]: T[K] extends
| { default: any }
// Boolean implicitly defaults to false
| BooleanConstructor
| { type: BooleanConstructor }
? T[K] extends { type: BooleanConstructor; required: true } // not default if Boolean is marked as required
? never
: K
: never
}[keyof T]
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 // As TS issue https://github.com/Microsoft/TypeScript/issues/14829 // somehow `ObjectConstructor` when inferred from { (): T } becomes `any` // `BooleanConstructor` when inferred from PropConstructor(with PropMethod) becomes `Boolean`
: [T] extends [ObjectConstructor | { type: ObjectConstructor }]
? Record<string, any>
: [T] extends [BooleanConstructor | { type: BooleanConstructor }]
? boolean
: [T] extends [DateConstructor | { type: DateConstructor }]
? Date
: [T] extends [Prop<infer V, infer D>]
? (unknown extends V ? D : V)
: T
2019-06-01 00:47:05 +08:00
export type ExtractPropTypes<O> = O extends object
? { [K in RequiredKeys<O>]: InferPropType<O[K]> } &
{ [K in OptionalKeys<O>]?: 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 {
2019-11-25 05:00:46 +08:00
shouldCast,
shouldCastTrue
2019-05-28 18:06:00 +08:00
}
// extract props which defined with default from prop options
export type ExtractDefaultPropTypes<O> = O extends object
? { [K in DefaultKeys<O>]: InferPropType<O[K]> }
: {}
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
// normalized value is a tuple of the actual normalized options
// and an array of prop keys that need value casting (booleans and defaults)
export type NormalizedProps = Record<string, NormalizedProp>
export type NormalizedPropsOptions = [NormalizedProps, string[]] | []
2019-05-28 18:06:00 +08:00
export function initProps(
2019-09-07 00:58:31 +08:00
instance: ComponentInternalInstance,
rawProps: Data | null,
isStateful: number, // result of bitwise flag comparison
isSSR = false
2019-05-29 11:36:16 +08:00
) {
const props: Data = {}
const attrs: Data = {}
def(attrs, InternalObjectKey, 1)
instance.propsDefaults = Object.create(null)
setFullProps(instance, rawProps, props, attrs)
// validation
if (__DEV__) {
validateProps(rawProps || {}, props, instance)
2019-05-28 18:06:00 +08:00
}
2019-05-30 23:16:15 +08:00
if (isStateful) {
// stateful
instance.props = isSSR ? props : shallowReactive(props)
} else {
if (!instance.type.props) {
// functional w/ optional props, props === attrs
instance.props = attrs
} else {
// functional w/ declared props
instance.props = props
}
}
instance.attrs = attrs
}
2019-05-30 23:16:15 +08:00
export function updateProps(
instance: ComponentInternalInstance,
rawProps: Data | null,
rawPrevProps: Data | null,
optimized: boolean
) {
const {
props,
attrs,
vnode: { patchFlag }
} = instance
const rawCurrentProps = toRaw(props)
const [options] = instance.propsOptions
if (
// always force full diff in dev
// - #1942 if hmr is enabled with sfc component
// - vite#872 non-sfc component used by sfc component
!(
__DEV__ &&
(instance.type.__hmrId ||
(instance.parent && instance.parent.type.__hmrId))
) &&
(optimized || patchFlag > 0) &&
!(patchFlag & PatchFlags.FULL_PROPS)
) {
if (patchFlag & PatchFlags.PROPS) {
// Compiler-generated props & no keys change, just set the updated
// the props.
const propsToUpdate = instance.vnode.dynamicProps!
for (let i = 0; i < propsToUpdate.length; i++) {
const key = propsToUpdate[i]
// PROPS flag guarantees rawProps to be non-null
const value = rawProps![key]
if (options) {
// attr / props separation was done on init and will be consistent
// in this code path, so just check if attrs have it.
if (hasOwn(attrs, key)) {
attrs[key] = value
} else {
const camelizedKey = camelize(key)
props[camelizedKey] = resolvePropValue(
options,
rawCurrentProps,
camelizedKey,
value,
instance
)
}
} else {
attrs[key] = value
}
}
}
} else {
// full props update.
setFullProps(instance, rawProps, props, attrs)
// in case of dynamic props, check if we need to delete keys from
// the props object
let kebabKey: string
for (const key in rawCurrentProps) {
if (
!rawProps ||
// for camelCase
(!hasOwn(rawProps, key) &&
// it's possible the original props was passed in as kebab-case
// and converted to camelCase (#955)
((kebabKey = hyphenate(key)) === key || !hasOwn(rawProps, kebabKey)))
) {
if (options) {
if (
rawPrevProps &&
// for camelCase
(rawPrevProps[key] !== undefined ||
// for kebab-case
rawPrevProps[kebabKey!] !== undefined)
) {
props[key] = resolvePropValue(
options,
rawProps || EMPTY_OBJ,
key,
undefined,
instance
)
}
} else {
delete props[key]
}
}
}
// in the case of functional component w/o props declaration, props and
// attrs point to the same object so it should already have been updated.
if (attrs !== rawCurrentProps) {
for (const key in attrs) {
if (!rawProps || !hasOwn(rawProps, key)) {
delete attrs[key]
}
}
}
}
// trigger updates for $attrs in case it's used in component slots
trigger(instance, TriggerOpTypes.SET, '$attrs')
if (__DEV__) {
validateProps(rawProps || {}, props, instance)
}
}
function setFullProps(
instance: ComponentInternalInstance,
rawProps: Data | null,
props: Data,
attrs: Data
) {
const [options, needCastKeys] = instance.propsOptions
2020-03-19 06:14:51 +08:00
if (rawProps) {
2019-05-29 09:18:45 +08:00
for (const key in rawProps) {
const value = rawProps[key]
// key, ref are reserved and never passed down
if (isReservedProp(key)) {
continue
}
// prop option names are camelized during normalization, so to support
// kebab -> camel conversion here we need to camelize the key.
let camelKey
if (options && hasOwn(options, (camelKey = camelize(key)))) {
props[camelKey] = value
} else if (!isEmitListener(instance.emitsOptions, key)) {
// Any non-declared (either as a prop or an emitted event) props are put
// into a separate `attrs` object for spreading. Make sure to preserve
// original key casing
attrs[key] = value
2019-05-28 18:06:00 +08:00
}
}
}
if (needCastKeys) {
const rawCurrentProps = toRaw(props)
for (let i = 0; i < needCastKeys.length; i++) {
const key = needCastKeys[i]
props[key] = resolvePropValue(
options!,
rawCurrentProps,
key,
rawCurrentProps[key],
instance
)
2019-05-28 18:06:00 +08:00
}
}
}
2019-05-29 11:36:16 +08:00
function resolvePropValue(
options: NormalizedProps,
props: Data,
key: string,
value: unknown,
instance: ComponentInternalInstance
) {
const opt = options[key]
if (opt != null) {
const hasDefault = hasOwn(opt, 'default')
// default values
if (hasDefault && value === undefined) {
const defaultValue = opt.default
if (opt.type !== Function && isFunction(defaultValue)) {
const { propsDefaults } = instance
if (key in propsDefaults) {
value = propsDefaults[key]
} else {
setCurrentInstance(instance)
value = propsDefaults[key] = defaultValue(props)
setCurrentInstance(null)
}
} else {
value = defaultValue
}
}
// boolean casting
if (opt[BooleanFlags.shouldCast]) {
if (!hasOwn(props, key) && !hasDefault) {
value = false
} else if (
opt[BooleanFlags.shouldCastTrue] &&
(value === '' || value === hyphenate(key))
) {
value = true
}
}
}
return value
}
export function normalizePropsOptions(
comp: ConcreteComponent,
appContext: AppContext,
asMixin = false
): NormalizedPropsOptions {
if (!appContext.deopt && comp.__props) {
return comp.__props
2019-05-28 18:06:00 +08:00
}
const raw = comp.props
const normalized: NormalizedPropsOptions[0] = {}
const needCastKeys: NormalizedPropsOptions[1] = []
// apply mixin/extends props
let hasExtends = false
if (__FEATURE_OPTIONS_API__ && !isFunction(comp)) {
const extendProps = (raw: ComponentOptions) => {
hasExtends = true
const [props, keys] = normalizePropsOptions(raw, appContext, true)
extend(normalized, props)
if (keys) needCastKeys.push(...keys)
}
if (!asMixin && appContext.mixins.length) {
appContext.mixins.forEach(extendProps)
}
if (comp.extends) {
extendProps(comp.extends)
}
if (comp.mixins) {
comp.mixins.forEach(extendProps)
}
}
if (!raw && !hasExtends) {
return (comp.__props = EMPTY_ARR as any)
}
2019-05-28 18:06:00 +08:00
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])
if (validatePropName(normalizedKey)) {
normalized[normalizedKey] = EMPTY_OBJ
2019-05-28 18:06:00 +08:00
}
}
} else if (raw) {
2019-05-28 18:06:00 +08:00
if (__DEV__ && !isObject(raw)) {
warn(`invalid props options`, raw)
}
for (const key in raw) {
const normalizedKey = camelize(key)
if (validatePropName(normalizedKey)) {
2019-05-28 18:06:00 +08:00
const opt = raw[key]
const prop: NormalizedProp = (normalized[normalizedKey] =
2019-05-28 18:06:00 +08:00
isArray(opt) || isFunction(opt) ? { type: opt } : opt)
2020-03-19 06:14:51 +08:00
if (prop) {
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] =
stringIndex < 0 || booleanIndex < stringIndex
// if the prop needs boolean casting or default value
if (booleanIndex > -1 || hasOwn(prop, 'default')) {
needCastKeys.push(normalizedKey)
}
2019-05-28 18:06:00 +08:00
}
}
}
}
return (comp.__props = [normalized, needCastKeys])
}
function validatePropName(key: string) {
if (key[0] !== '$') {
return true
} else if (__DEV__) {
warn(`Invalid prop name: "${key}" is a reserved property.`)
}
return false
}
2019-05-28 18:06:00 +08:00
// 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 (isFunction(expectedTypes)) {
2019-05-28 18:06:00 +08:00
return isSameType(expectedTypes, type) ? 0 : -1
}
return -1
}
/**
* dev only
*/
function validateProps(
rawProps: Data,
props: Data,
instance: ComponentInternalInstance
) {
const resolvedValues = toRaw(props)
const options = instance.propsOptions[0]
for (const key in options) {
let opt = options[key]
if (opt == null) continue
validateProp(key, resolvedValues[key], opt, !hasOwn(rawProps, key))
}
}
/**
* dev only
*/
2019-05-28 18:06:00 +08:00
function validateProp(
name: string,
2019-10-22 23:26:48 +08:00
value: unknown,
prop: PropOptions,
2019-05-28 18:06:00 +08:00
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 isSimpleType = /*#__PURE__*/ makeMap(
'String,Number,Boolean,Function,Symbol,BigInt'
)
2019-05-28 18:06:00 +08:00
type AssertionResult = {
valid: boolean
expectedType: string
}
/**
* dev only
*/
2019-10-22 23:26:48 +08:00
function assertType(value: unknown, type: PropConstructor): AssertionResult {
2019-05-28 18:06:00 +08:00
let valid
const expectedType = getType(type)
if (isSimpleType(expectedType)) {
2019-05-28 18:06:00 +08:00
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 = isObject(value)
2019-05-28 18:06:00 +08:00
} else if (expectedType === 'Array') {
valid = isArray(value)
} else {
valid = value instanceof type
}
return {
valid,
expectedType
}
}
/**
* dev only
*/
2019-05-28 18:06:00 +08:00
function getInvalidTypeMessage(
name: string,
2019-10-22 23:26:48 +08:00
value: unknown,
2019-05-28 18:06:00 +08:00
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
}
/**
* dev only
*/
2019-10-22 23:26:48 +08:00
function styleValue(value: unknown, type: string): string {
2019-05-28 18:06:00 +08:00
if (type === 'String') {
return `"${value}"`
} else if (type === 'Number') {
return `${Number(value)}`
} else {
return `${value}`
}
}
/**
* dev only
*/
2019-05-28 18:06:00 +08:00
function isExplicable(type: string): boolean {
const explicitTypes = ['string', 'number', 'boolean']
return explicitTypes.some(elem => type.toLowerCase() === elem)
}
/**
* dev only
*/
2019-05-28 18:06:00 +08:00
function isBoolean(...args: string[]): boolean {
return args.some(elem => elem.toLowerCase() === 'boolean')
}