Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optimising type judgement #12056

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/runtime-core/src/compat/compatConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ export function warnDeprecation(
const { message, link } = deprecationData[key]
warn(
`(deprecation ${key}) ${
typeof message === 'function' ? message(...args) : message
isFunction(message) ? message(...args) : message
}${link ? `\n Details: ${link}` : ``}`,
)
if (!isCompatEnabled(key, instance, true)) {
Expand Down
7 changes: 2 additions & 5 deletions packages/runtime-core/src/compat/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ function installCompatMount(
}

let container: Element
if (typeof selectorOrEl === 'string') {
if (isString(selectorOrEl)) {
// eslint-disable-next-line
const result = document.querySelector(selectorOrEl)
if (!result) {
Expand All @@ -512,10 +512,7 @@ function installCompatMount(

let namespace: ElementNamespace
if (container instanceof SVGElement) namespace = 'svg'
else if (
typeof MathMLElement === 'function' &&
container instanceof MathMLElement
)
else if (isFunction(MathMLElement) && container instanceof MathMLElement)
namespace = 'mathml'

// HMR root reload
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/compat/renderFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function compatH(
}

// to support v2 string component name look!up
if (typeof type === 'string') {
if (isString(type)) {
const t = hyphenate(type)
if (t === 'transition' || t === 'transition-group' || t === 'keep-alive') {
// since transition and transition-group are runtime-dom-specific,
Expand Down
5 changes: 3 additions & 2 deletions packages/runtime-core/src/compat/renderHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isArray,
isObject,
isReservedProp,
isString,
normalizeClass,
} from '@vue/shared'
import type { ComponentInternalInstance, Data } from '../component'
Expand Down Expand Up @@ -170,13 +171,13 @@ export function legacyMarkOnce(tree: VNode): VNode {
export function legacyBindDynamicKeys(props: any, values: any[]): any {
for (let i = 0; i < values.length; i += 2) {
const key = values[i]
if (typeof key === 'string' && key) {
if (isString(key) && key) {
props[values[i]] = values[i + 1]
}
}
return props
}

export function legacyPrependModifier(value: any, symbol: string): any {
return typeof value === 'string' ? symbol + value : value
return isString(value) ? symbol + value : value
}
4 changes: 2 additions & 2 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,10 +631,10 @@ function getType(ctor: Prop<any> | null): string {
}

// Avoid using regex for common cases by checking the type directly
if (typeof ctor === 'function') {
if (isFunction(ctor)) {
// Using name property to avoid converting function to string
return ctor.name || ''
} else if (typeof ctor === 'object') {
} else if (isObject(ctor)) {
// Attempting to directly access constructor name if possible
const name = ctor.constructor && ctor.constructor.name
return name || ''
Expand Down
10 changes: 8 additions & 2 deletions packages/runtime-core/src/components/Suspense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import {
normalizeVNode,
openBlock,
} from '../vnode'
import { ShapeFlags, isArray, isFunction, toNumber } from '@vue/shared'
import {
ShapeFlags,
isArray,
isFunction,
isNumber,
toNumber,
} from '@vue/shared'
import { type ComponentInternalInstance, handleSetupResult } from '../component'
import type { Slots } from '../componentSlots'
import {
Expand Down Expand Up @@ -501,7 +507,7 @@ function createSuspenseBoundary(
hiddenContainer,
deps: 0,
pendingId: suspenseId++,
timeout: typeof timeout === 'number' ? timeout : -1,
timeout: isNumber(timeout) ? timeout : -1,
activeBranch: null,
pendingBranch: null,
isInFallback: !isHydrating,
Expand Down
17 changes: 13 additions & 4 deletions packages/runtime-core/src/customFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ import {
isShallow,
toRaw,
} from '@vue/reactivity'
import { EMPTY_OBJ, extend, isArray, isFunction, isObject } from '@vue/shared'
import {
EMPTY_OBJ,
extend,
isArray,
isBoolean,
isFunction,
isNumber,
isObject,
isString,
} from '@vue/shared'
import type { ComponentInternalInstance, ComponentOptions } from './component'
import type { ComponentPublicInstance } from './componentPublicInstance'

Expand Down Expand Up @@ -146,11 +155,11 @@ export function initCustomFormatter(): void {
}

function formatValue(v: unknown, asRaw = true) {
if (typeof v === 'number') {
if (isNumber(v)) {
return ['span', numberStyle, v]
} else if (typeof v === 'string') {
} else if (isString(v)) {
return ['span', stringStyle, JSON.stringify(v)]
} else if (typeof v === 'boolean') {
} else if (isBoolean(v)) {
return ['span', keywordStyle, v]
} else if (isObject(v)) {
return ['object', { object: asRaw ? toRaw(v) : v }]
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/helpers/renderList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
shallowReadArray,
toReactive,
} from '@vue/reactivity'
import { isArray, isObject, isString } from '@vue/shared'
import { isArray, isNumber, isObject, isString } from '@vue/shared'
import { warn } from '../warning'

/**
Expand Down Expand Up @@ -82,7 +82,7 @@ export function renderList(
cached && cached[i],
)
}
} else if (typeof source === 'number') {
} else if (isNumber(source)) {
if (__DEV__ && !Number.isInteger(source)) {
warn(`The v-for range expect an integer value but got ${source}.`)
}
Expand Down
8 changes: 5 additions & 3 deletions packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
SlotFlags,
extend,
isArray,
isBoolean,
isFunction,
isNumber,
isObject,
isOn,
isString,
Expand Down Expand Up @@ -439,7 +441,7 @@ const normalizeRef = ({
ref_key,
ref_for,
}: VNodeProps): VNodeNormalizedRefAtom | null => {
if (typeof ref === 'number') {
if (isNumber(ref)) {
ref = '' + ref
}
return (
Expand Down Expand Up @@ -782,7 +784,7 @@ export function createCommentVNode(
}

export function normalizeVNode(child: VNodeChild): VNode {
if (child == null || typeof child === 'boolean') {
if (child == null || isBoolean(child)) {
// empty placeholder
return createVNode(Comment)
} else if (isArray(child)) {
Expand Down Expand Up @@ -818,7 +820,7 @@ export function normalizeChildren(vnode: VNode, children: unknown): void {
children = null
} else if (isArray(children)) {
type = ShapeFlags.ARRAY_CHILDREN
} else if (typeof children === 'object') {
} else if (isObject(children)) {
if (shapeFlag & (ShapeFlags.ELEMENT | ShapeFlags.TELEPORT)) {
// Normalize slot to plain children for plain element and Teleport
const slot = (children as any).default
Expand Down
10 changes: 3 additions & 7 deletions packages/runtime-core/src/warning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
type Data,
formatComponentName,
} from './component'
import { isFunction, isString } from '@vue/shared'
import { isBoolean, isFunction, isNumber, isString } from '@vue/shared'
import { isRef, pauseTracking, resetTracking, toRaw } from '@vue/reactivity'
import { ErrorCodes, callWithErrorHandling } from './errorHandling'

Expand Down Expand Up @@ -149,11 +149,7 @@ function formatProp(key: string, value: unknown, raw?: boolean): any {
if (isString(value)) {
value = JSON.stringify(value)
return raw ? value : [`${key}=${value}`]
} else if (
typeof value === 'number' ||
typeof value === 'boolean' ||
value == null
) {
} else if (isNumber(value) || isBoolean(value) || value == null) {
return raw ? value : [`${key}=${value}`]
} else if (isRef(value)) {
value = formatProp(key, toRaw(value.value), true)
Expand All @@ -173,7 +169,7 @@ export function assertNumber(val: unknown, type: string): void {
if (!__DEV__) return
if (val === undefined) {
return
} else if (typeof val !== 'number') {
} else if (!isNumber(val)) {
warn(`${type} is not a valid number - ` + `got ${JSON.stringify(val)}.`)
} else if (isNaN(val)) {
warn(`${type} is NaN - ` + 'the duration expression might be incorrect.')
Expand Down
4 changes: 3 additions & 1 deletion packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ import {
hasOwn,
hyphenate,
isArray,
isNumber,
isPlainObject,
isString,
toNumber,
} from '@vue/shared'
import { createApp, createSSRApp, render } from '.'
Expand Down Expand Up @@ -507,7 +509,7 @@ export class VueElement
if (shouldReflect) {
if (val === true) {
this.setAttribute(hyphenate(key), '')
} else if (typeof val === 'string' || typeof val === 'number') {
} else if (isString(val) || isNumber(val)) {
this.setAttribute(hyphenate(key), val + '')
} else if (!val) {
this.removeAttribute(hyphenate(key))
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-dom/src/directives/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
invokeArrayFns,
isArray,
isSet,
isString,
looseEqual,
looseIndexOf,
looseToNumber,
Expand Down Expand Up @@ -365,7 +366,7 @@ export function initVModelForSSR(): void {
}

vModelDynamic.getSSRProps = (binding, vnode) => {
if (typeof vnode.type !== 'string') {
if (!isString(vnode.type)) {
return
}
const modelToUse = resolveDynamicModel(
Expand Down
5 changes: 1 addition & 4 deletions packages/runtime-dom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,7 @@ function resolveRootNamespace(
if (container instanceof SVGElement) {
return 'svg'
}
if (
typeof MathMLElement === 'function' &&
container instanceof MathMLElement
) {
if (isFunction(MathMLElement) && container instanceof MathMLElement) {
return 'mathml'
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/server-renderer/src/helpers/ssrRenderList.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray, isObject, isString } from '@vue/shared'
import { isArray, isNumber, isObject, isString } from '@vue/shared'
import { warn } from '@vue/runtime-core'

export function ssrRenderList(
Expand All @@ -9,7 +9,7 @@ export function ssrRenderList(
for (let i = 0, l = source.length; i < l; i++) {
renderItem(source[i], i)
}
} else if (typeof source === 'number') {
} else if (isNumber(source)) {
if (__DEV__ && !Number.isInteger(source)) {
warn(`The v-for range expect an integer value but got ${source}.`)
return
Expand Down
5 changes: 4 additions & 1 deletion packages/shared/src/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export const isRegExp = (val: unknown): val is RegExp =>
export const isFunction = (val: unknown): val is Function =>
typeof val === 'function'
export const isString = (val: unknown): val is string => typeof val === 'string'
export const isNumber = (val: unknown): val is number => typeof val === 'number'
export const isBoolean = (val: unknown): val is boolean =>
typeof val === 'boolean'
export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol'
export const isObject = (val: unknown): val is Record<any, any> =>
val !== null && typeof val === 'object'
Expand Down Expand Up @@ -213,7 +216,7 @@ export function genCacheKey(source: string, options: any): string {
return (
source +
JSON.stringify(options, (_, val) =>
typeof val === 'function' ? val.toString() : val,
isFunction(val) ? val.toString() : val,
)
)
}
4 changes: 2 additions & 2 deletions packages/shared/src/normalizeProp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hyphenate, isArray, isObject, isString } from './general'
import { hyphenate, isArray, isNumber, isObject, isString } from './general'

export type NormalizedStyle = Record<string, string | number>

Expand Down Expand Up @@ -51,7 +51,7 @@ export function stringifyStyle(
}
for (const key in styles) {
const value = styles[key]
if (isString(value) || typeof value === 'number') {
if (isString(value) || isNumber(value)) {
const normalizedKey = key.startsWith(`--`) ? key : hyphenate(key)
// only render valid values
ret += `${normalizedKey}:${value};`
Expand Down