refactor: remove unnecessary normalization
This commit is contained in:
parent
b946a6b7f7
commit
7b7ae57388
@ -22,10 +22,6 @@ export type ComponentPropsOptions<P = Data> = {
|
|||||||
[K in keyof P]: PropValidator<P[K]>
|
[K in keyof P]: PropValidator<P[K]>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type NormalizedPropsOptions<P = Data> = {
|
|
||||||
[K in keyof P]: PropOptions<P[K]>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Prop<T> = { (): T } | { new (...args: any[]): T & object }
|
export type Prop<T> = { (): T } | { new (...args: any[]): T & object }
|
||||||
|
|
||||||
export type PropType<T> = Prop<T> | Prop<T>[]
|
export type PropType<T> = Prop<T> | Prop<T>[]
|
||||||
|
@ -9,7 +9,6 @@ import { immutable, unwrap, lock, unlock } from '@vue/observer'
|
|||||||
import {
|
import {
|
||||||
Data,
|
Data,
|
||||||
ComponentPropsOptions,
|
ComponentPropsOptions,
|
||||||
NormalizedPropsOptions,
|
|
||||||
PropValidator,
|
PropValidator,
|
||||||
PropOptions
|
PropOptions
|
||||||
} from './componentOptions'
|
} from './componentOptions'
|
||||||
@ -77,10 +76,7 @@ export function resolveProps(
|
|||||||
Component: ComponentClass | FunctionalComponent
|
Component: ComponentClass | FunctionalComponent
|
||||||
): { props: Data; attrs?: Data } {
|
): { props: Data; attrs?: Data } {
|
||||||
const hasDeclaredProps = rawOptions !== void 0
|
const hasDeclaredProps = rawOptions !== void 0
|
||||||
const options = (hasDeclaredProps &&
|
const options = rawOptions as ComponentPropsOptions
|
||||||
normalizePropsOptions(
|
|
||||||
rawOptions as ComponentPropsOptions
|
|
||||||
)) as NormalizedPropsOptions
|
|
||||||
if (!rawData && !hasDeclaredProps) {
|
if (!rawData && !hasDeclaredProps) {
|
||||||
return EMPTY_PROPS
|
return EMPTY_PROPS
|
||||||
}
|
}
|
||||||
@ -120,7 +116,7 @@ export function resolveProps(
|
|||||||
if (props[key] === void 0) {
|
if (props[key] === void 0) {
|
||||||
const opt = options[key]
|
const opt = options[key]
|
||||||
if (opt != null && opt.hasOwnProperty('default')) {
|
if (opt != null && opt.hasOwnProperty('default')) {
|
||||||
const defaultValue = opt.default
|
const defaultValue = (opt as PropOptions).default
|
||||||
props[key] =
|
props[key] =
|
||||||
typeof defaultValue === 'function' ? defaultValue() : defaultValue
|
typeof defaultValue === 'function' ? defaultValue() : defaultValue
|
||||||
}
|
}
|
||||||
@ -130,28 +126,6 @@ export function resolveProps(
|
|||||||
return { props, attrs }
|
return { props, attrs }
|
||||||
}
|
}
|
||||||
|
|
||||||
const normalizeCache = new WeakMap<
|
|
||||||
ComponentPropsOptions,
|
|
||||||
NormalizedPropsOptions
|
|
||||||
>()
|
|
||||||
|
|
||||||
function normalizePropsOptions(
|
|
||||||
raw: ComponentPropsOptions
|
|
||||||
): NormalizedPropsOptions {
|
|
||||||
let cached = normalizeCache.get(raw)
|
|
||||||
if (cached) {
|
|
||||||
return cached
|
|
||||||
}
|
|
||||||
const normalized: NormalizedPropsOptions = {}
|
|
||||||
for (const key in raw) {
|
|
||||||
const opt = raw[key]
|
|
||||||
normalized[key] =
|
|
||||||
typeof opt === 'function' ? { type: opt } : (opt as PropOptions)
|
|
||||||
}
|
|
||||||
normalizeCache.set(raw, normalized)
|
|
||||||
return normalized
|
|
||||||
}
|
|
||||||
|
|
||||||
function validateProp(
|
function validateProp(
|
||||||
key: string,
|
key: string,
|
||||||
value: any,
|
value: any,
|
||||||
|
Loading…
Reference in New Issue
Block a user