Skip to content

Commit ee18800

Browse files
committed
refactor: replace inline type checks with shared utils
1 parent 3f27c58 commit ee18800

35 files changed

+166
-111
lines changed

packages/runtime-core/src/apiAsyncComponent.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
currentInstance,
77
isInSSRComponentSetup,
88
} from './component'
9-
import { isFunction, isObject } from '@vue/shared'
9+
import { isFunction, isNullish, isObject } from '@vue/shared'
1010
import type { ComponentPublicInstance } from './componentPublicInstance'
1111
import { type VNode, createVNode } from './vnode'
1212
import { defineComponent } from './apiDefineComponent'
@@ -191,7 +191,7 @@ export function defineAsyncComponent<
191191
}, delay)
192192
}
193193

194-
if (timeout != null) {
194+
if (!isNullish(timeout)) {
195195
setTimeout(() => {
196196
if (!loaded.value && !error.value) {
197197
const err = new Error(

packages/runtime-core/src/apiCreateApp.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { warn } from './warning'
2222
import { type VNode, cloneVNode, createVNode } from './vnode'
2323
import type { RootHydrateFunction } from './hydration'
2424
import { devtoolsInitApp, devtoolsUnmountApp } from './devtools'
25-
import { NO, extend, isFunction, isObject } from '@vue/shared'
25+
import { NO, extend, isFunction, isNullish, isObject } from '@vue/shared'
2626
import { version } from '.'
2727
import { installAppCompatProperties } from './compat/global'
2828
import type { NormalizedPropsOptions } from './componentProps'
@@ -259,7 +259,7 @@ export function createAppAPI<HostElement>(
259259
rootComponent = extend({}, rootComponent)
260260
}
261261

262-
if (rootProps != null && !isObject(rootProps)) {
262+
if (!isNullish(rootProps) && !isObject(rootProps)) {
263263
__DEV__ && warn(`root props passed to app.mount() must be an object.`)
264264
rootProps = null
265265
}

packages/runtime-core/src/apiInject.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isFunction } from '@vue/shared'
1+
import { isFunction, isNullish } from '@vue/shared'
22
import { currentInstance } from './component'
33
import { currentRenderingInstance } from './componentRenderContext'
44
import { currentApp } from './apiCreateApp'
@@ -62,7 +62,7 @@ export function inject(
6262
const provides = currentApp
6363
? currentApp._context.provides
6464
: instance
65-
? instance.parent == null
65+
? isNullish(instance.parent)
6666
? instance.vnode.appContext && instance.vnode.appContext.provides
6767
: instance.parent.provides
6868
: undefined

packages/runtime-core/src/component.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import {
6868
getGlobalThis,
6969
isArray,
7070
isFunction,
71+
isNullish,
7172
isObject,
7273
isPromise,
7374
makeMap,
@@ -1127,7 +1128,7 @@ export function createSetupContext(
11271128
if (instance.exposed) {
11281129
warn(`expose() should be called only once per setup().`)
11291130
}
1130-
if (exposed != null) {
1131+
if (!isNullish(exposed)) {
11311132
let exposedType: string = typeof exposed
11321133
if (exposedType === 'object') {
11331134
if (isArray(exposed)) {

packages/runtime-core/src/componentOptions.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
extend,
1616
isArray,
1717
isFunction,
18+
isNullish,
1819
isObject,
1920
isPromise,
2021
isString,
@@ -768,7 +769,7 @@ export function applyOptions(instance: ComponentInternalInstance): void {
768769
if (render && instance.render === NOOP) {
769770
instance.render = render as InternalRenderFunction
770771
}
771-
if (inheritAttrs != null) {
772+
if (!isNullish(inheritAttrs)) {
772773
instance.inheritAttrs = inheritAttrs
773774
}
774775

packages/runtime-core/src/componentProps.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
hyphenate,
1818
isArray,
1919
isFunction,
20+
isNullish,
2021
isObject,
2122
isOn,
2223
isReservedProp,
@@ -455,7 +456,7 @@ function resolvePropValue(
455456
isAbsent: boolean,
456457
) {
457458
const opt = options[key]
458-
if (opt != null) {
459+
if (!isNullish(opt)) {
459460
const hasDefault = hasOwn(opt, 'default')
460461
// default values
461462
if (hasDefault && value === undefined) {
@@ -631,7 +632,7 @@ function getType(ctor: Prop<any> | null): string {
631632
}
632633

633634
// Avoid using regex for common cases by checking the type directly
634-
if (typeof ctor === 'function') {
635+
if (isFunction(ctor)) {
635636
// Using name property to avoid converting function to string
636637
return ctor.name || ''
637638
} else if (typeof ctor === 'object') {
@@ -657,7 +658,7 @@ function validateProps(
657658
const camelizePropsKey = Object.keys(rawProps).map(key => camelize(key))
658659
for (const key in options) {
659660
let opt = options[key]
660-
if (opt == null) continue
661+
if (isNullish(opt)) continue
661662
validateProp(
662663
key,
663664
resolvedValues[key],
@@ -685,11 +686,11 @@ function validateProp(
685686
return
686687
}
687688
// missing but optional
688-
if (value == null && !required) {
689+
if (isNullish(value) && !required) {
689690
return
690691
}
691692
// type check
692-
if (type != null && type !== true && !skipCheck) {
693+
if (!isNullish(type) && type !== true && !skipCheck) {
693694
let isValid = false
694695
const types = isArray(type) ? type : [type]
695696
const expectedTypes = []

packages/runtime-core/src/componentPublicInstance.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
hasOwn,
2323
isFunction,
2424
isGloballyAllowed,
25+
isNullish,
2526
isString,
2627
} from '@vue/shared'
2728
import {
@@ -596,7 +597,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
596597
key: string,
597598
descriptor: PropertyDescriptor,
598599
) {
599-
if (descriptor.get != null) {
600+
if (!isNullish(descriptor.get)) {
600601
// invalidate key cache of a getter based property #5417
601602
target._.accessCache![key] = 0
602603
} else if (hasOwn(descriptor, 'value')) {

packages/runtime-core/src/componentSlots.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
def,
1515
isArray,
1616
isFunction,
17+
isNullish,
1718
} from '@vue/shared'
1819
import { warn } from './warning'
1920
import { isKeepAlive } from './components/KeepAlive'
@@ -133,7 +134,7 @@ const normalizeObjectSlots = (
133134
const value = rawSlots[key]
134135
if (isFunction(value)) {
135136
slots[key] = normalizeSlot(key, value, ctx)
136-
} else if (value != null) {
137+
} else if (!isNullish(value)) {
137138
if (
138139
__DEV__ &&
139140
!(
@@ -248,7 +249,7 @@ export const updateSlots = (
248249
// delete stale slots
249250
if (needDeletionCheck) {
250251
for (const key in slots) {
251-
if (!isInternalKey(key) && deletionComparisonTarget[key] == null) {
252+
if (!isInternalKey(key) && isNullish(deletionComparisonTarget[key])) {
252253
delete slots[key]
253254
}
254255
}

packages/runtime-core/src/components/BaseTransition.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ import { warn } from '../warning'
1616
import { isKeepAlive } from './KeepAlive'
1717
import { toRaw } from '@vue/reactivity'
1818
import { ErrorCodes, callWithAsyncErrorHandling } from '../errorHandling'
19-
import { PatchFlags, ShapeFlags, isArray, isFunction } from '@vue/shared'
19+
import {
20+
PatchFlags,
21+
ShapeFlags,
22+
isArray,
23+
isFunction,
24+
isNullish,
25+
} from '@vue/shared'
2026
import { onBeforeUnmount, onMounted } from '../apiLifecycle'
2127
import { isTeleport } from './Teleport'
2228
import type { RendererElement } from '../renderer'
@@ -545,10 +551,9 @@ export function getTransitionRawChildren(
545551
for (let i = 0; i < children.length; i++) {
546552
let child = children[i]
547553
// #5360 inherit parent key in case of <template v-for>
548-
const key =
549-
parentKey == null
550-
? child.key
551-
: String(parentKey) + String(child.key != null ? child.key : i)
554+
const key = isNullish(parentKey)
555+
? child.key
556+
: String(parentKey) + String(!isNullish(child.key) ? child.key : i)
552557
// handle fragment children case, e.g. v-for
553558
if (child.type === Fragment) {
554559
if (child.patchFlag & PatchFlags.KEYED_FRAGMENT) keyedFragmentCount++
@@ -558,7 +563,7 @@ export function getTransitionRawChildren(
558563
}
559564
// comment placeholders should be skipped, e.g. v-if
560565
else if (keepComment || child.type !== Comment) {
561-
ret.push(key != null ? cloneVNode(child, { key }) : child)
566+
ret.push(!isNullish(key) ? cloneVNode(child, { key }) : child)
562567
}
563568
}
564569
// #1126 if a transition children list contains multiple sub fragments, these

packages/runtime-core/src/components/KeepAlive.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
ShapeFlags,
2929
invokeArrayFns,
3030
isArray,
31+
isNullish,
3132
isRegExp,
3233
isString,
3334
remove,
@@ -237,7 +238,7 @@ const KeepAliveImpl: ComponentOptions = {
237238
let pendingCacheKey: CacheKey | null = null
238239
const cacheSubtree = () => {
239240
// fix #1621, the pendingCacheKey could be 0
240-
if (pendingCacheKey != null) {
241+
if (!isNullish(pendingCacheKey)) {
241242
// if KeepAlive child is a Suspense, it needs to be cached after Suspense resolves
242243
// avoid caching vnode that not been mounted
243244
if (isSuspense(instance.subTree.type)) {
@@ -321,7 +322,7 @@ const KeepAliveImpl: ComponentOptions = {
321322
return rawVNode
322323
}
323324

324-
const key = vnode.key == null ? comp : vnode.key
325+
const key = isNullish(vnode.key) ? comp : vnode.key
325326
const cachedVNode = cache.get(key)
326327

327328
// clone vnode if it's reused because we are going to mutate it

packages/runtime-core/src/components/Suspense.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ import {
1010
normalizeVNode,
1111
openBlock,
1212
} from '../vnode'
13-
import { ShapeFlags, isArray, isFunction, toNumber } from '@vue/shared'
13+
import {
14+
ShapeFlags,
15+
isArray,
16+
isFunction,
17+
isNullish,
18+
isNumber,
19+
toNumber,
20+
} from '@vue/shared'
1421
import { type ComponentInternalInstance, handleSetupResult } from '../component'
1522
import type { Slots } from '../componentSlots'
1623
import {
@@ -78,7 +85,7 @@ export const SuspenseImpl = {
7885
// platform-specific impl passed from renderer
7986
rendererInternals: RendererInternals,
8087
): void {
81-
if (n1 == null) {
88+
if (isNullish(n1)) {
8289
mountSuspense(
8390
n2,
8491
container,
@@ -501,7 +508,7 @@ function createSuspenseBoundary(
501508
hiddenContainer,
502509
deps: 0,
503510
pendingId: suspenseId++,
504-
timeout: typeof timeout === 'number' ? timeout : -1,
511+
timeout: isNumber(timeout) ? timeout : -1,
505512
activeBranch: null,
506513
pendingBranch: null,
507514
isInFallback: !isHydrating,
@@ -900,5 +907,5 @@ function setActiveBranch(suspense: SuspenseBoundary, branch: VNode) {
900907

901908
function isVNodeSuspensible(vnode: VNode) {
902909
const suspensible = vnode.props && vnode.props.suspensible
903-
return suspensible != null && suspensible !== false
910+
return !isNullish(suspensible) && suspensible !== false
904911
}

packages/runtime-core/src/components/Teleport.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
traverseStaticChildren,
1212
} from '../renderer'
1313
import type { VNode, VNodeArrayChildren, VNodeProps } from '../vnode'
14-
import { ShapeFlags, isString } from '@vue/shared'
14+
import { ShapeFlags, isFunction, isNullish, isString } from '@vue/shared'
1515
import { warn } from '../warning'
1616
import { isHmrUpdating } from '../hmr'
1717

@@ -37,7 +37,7 @@ const isTargetSVG = (target: RendererElement): boolean =>
3737
typeof SVGElement !== 'undefined' && target instanceof SVGElement
3838

3939
const isTargetMathML = (target: RendererElement): boolean =>
40-
typeof MathMLElement === 'function' && target instanceof MathMLElement
40+
isFunction(MathMLElement) && target instanceof MathMLElement
4141

4242
const resolveTarget = <T = RendererElement>(
4343
props: TeleportProps | null,
@@ -104,7 +104,7 @@ export const TeleportImpl = {
104104
dynamicChildren = null
105105
}
106106

107-
if (n1 == null) {
107+
if (isNullish(n1)) {
108108
// insert anchors in the main view
109109
const placeholder = (n2.el = __DEV__
110110
? createComment('teleport start')

packages/runtime-core/src/customFormatter.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@ import {
88
resetTracking,
99
toRaw,
1010
} from '@vue/reactivity'
11-
import { EMPTY_OBJ, extend, isArray, isFunction, isObject } from '@vue/shared'
11+
import {
12+
EMPTY_OBJ,
13+
extend,
14+
isArray,
15+
isBoolean,
16+
isFunction,
17+
isNumber,
18+
isObject,
19+
isString,
20+
} from '@vue/shared'
1221
import type { ComponentInternalInstance, ComponentOptions } from './component'
1322
import type { ComponentPublicInstance } from './componentPublicInstance'
1423

@@ -151,11 +160,11 @@ export function initCustomFormatter(): void {
151160
}
152161

153162
function formatValue(v: unknown, asRaw = true) {
154-
if (typeof v === 'number') {
163+
if (isNumber(v)) {
155164
return ['span', numberStyle, v]
156-
} else if (typeof v === 'string') {
165+
} else if (isString(v)) {
157166
return ['span', stringStyle, JSON.stringify(v)]
158-
} else if (typeof v === 'boolean') {
167+
} else if (isBoolean(v)) {
159168
return ['span', keywordStyle, v]
160169
} else if (isObject(v)) {
161170
return ['object', { object: asRaw ? toRaw(v) : v }]

packages/runtime-core/src/devtools.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import type { App } from './apiCreateApp'
33
import { Comment, Fragment, Static, Text } from './vnode'
44
import type { ComponentInternalInstance } from './component'
5+
import { isFunction } from '@vue/shared'
56

67
interface AppRecord {
78
id: number
@@ -115,7 +116,7 @@ export const devtoolsComponentRemoved = (
115116
): void => {
116117
if (
117118
devtools &&
118-
typeof devtools.cleanupBuffer === 'function' &&
119+
isFunction(devtools.cleanupBuffer) &&
119120
// remove the component if it wasn't buffered
120121
!devtools.cleanupBuffer(component)
121122
) {

packages/runtime-core/src/helpers/renderList.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
toReactive,
88
toReadonly,
99
} from '@vue/reactivity'
10-
import { isArray, isObject, isString } from '@vue/shared'
10+
import { isArray, isNumber, isObject, isString } from '@vue/shared'
1111
import { warn } from '../warning'
1212

1313
/**
@@ -90,7 +90,7 @@ export function renderList(
9090
cached && cached[i],
9191
)
9292
}
93-
} else if (typeof source === 'number') {
93+
} else if (isNumber(source)) {
9494
if (__DEV__ && !Number.isInteger(source)) {
9595
warn(`The v-for range expect an integer value but got ${source}.`)
9696
}

0 commit comments

Comments
 (0)