Skip to content

Commit ba9a91c

Browse files
committed
refactor: remove null comparisons
1 parent 811f28a commit ba9a91c

File tree

18 files changed

+104
-119
lines changed

18 files changed

+104
-119
lines changed

packages/compiler-core/src/parse.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ function parseAttribute(
545545
{
546546
const pattern = /["'<]/g
547547
let m: RegExpExecArray | null
548-
while ((m = pattern.exec(name)) !== null) {
548+
while ((m = pattern.exec(name))) {
549549
emitError(
550550
context,
551551
ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
@@ -696,7 +696,7 @@ function parseAttributeValue(
696696
}
697697
const unexpectedChars = /["'<=`]/g
698698
let m: RegExpExecArray | null
699-
while ((m = unexpectedChars.exec(match[0])) !== null) {
699+
while ((m = unexpectedChars.exec(match[0]))) {
700700
emitError(
701701
context,
702702
ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,

packages/reactivity/src/effect.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export let activeEffect: ReactiveEffect | undefined
4646
export const ITERATE_KEY = Symbol('iterate')
4747

4848
export function isEffect(fn: any): fn is ReactiveEffect {
49-
return fn != null && fn._isEffect === true
49+
return fn && fn._isEffect === true
5050
}
5151

5252
export function effect<T = any>(

packages/runtime-core/src/apiWatch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ function doWatch(
229229
scheduler = invoke
230230
} else if (flush === 'pre') {
231231
scheduler = job => {
232-
if (!instance || instance.vnode.el != null) {
232+
if (!instance || instance.isMounted) {
233233
queueJob(job)
234234
} else {
235235
// with 'pre' option, the first call must happen before

packages/runtime-core/src/componentProps.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export function resolveProps(
9898
rawProps: Data | null,
9999
_options: ComponentPropsOptions | void
100100
) {
101-
const hasDeclaredProps = _options != null
101+
const hasDeclaredProps = !!_options
102102
if (!rawProps && !hasDeclaredProps) {
103103
return
104104
}
@@ -122,7 +122,7 @@ export function resolveProps(
122122
// allow mutation of propsProxy (which is readonly by default)
123123
unlock()
124124

125-
if (rawProps != null) {
125+
if (rawProps) {
126126
for (const key in rawProps) {
127127
const value = rawProps[key]
128128
// key, ref are reserved and never passed down
@@ -186,10 +186,7 @@ export function resolveProps(
186186
// in case of dynamic props, check if we need to delete keys from
187187
// the props proxy
188188
const { patchFlag } = instance.vnode
189-
if (
190-
propsProxy !== null &&
191-
(patchFlag === 0 || patchFlag & PatchFlags.FULL_PROPS)
192-
) {
189+
if (propsProxy && (patchFlag === 0 || patchFlag & PatchFlags.FULL_PROPS)) {
193190
const rawInitialProps = toRaw(propsProxy)
194191
for (const key in rawInitialProps) {
195192
if (!hasOwn(props, key)) {
@@ -250,7 +247,7 @@ function normalizePropsOptions(
250247
const opt = raw[key]
251248
const prop: NormalizedProp = (options[normalizedKey] =
252249
isArray(opt) || isFunction(opt) ? { type: opt } : opt)
253-
if (prop != null) {
250+
if (prop) {
254251
const booleanIndex = getTypeIndex(Boolean, prop.type)
255252
const stringIndex = getTypeIndex(String, prop.type)
256253
prop[BooleanFlags.shouldCast] = booleanIndex > -1

packages/runtime-core/src/componentProxy.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
109109
} else if (renderContext !== EMPTY_OBJ && hasOwn(renderContext, key)) {
110110
accessCache![key] = AccessTypes.CONTEXT
111111
return renderContext[key]
112-
} else if (type.props != null) {
112+
} else if (type.props) {
113113
// only cache other properties when instance has declared (this stable)
114114
// props
115115
if (hasOwn(props, key)) {
@@ -125,20 +125,20 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
125125
// public $xxx properties & user-attached properties (sink)
126126
const publicGetter = publicPropertiesMap[key]
127127
let cssModule
128-
if (publicGetter != null) {
128+
if (publicGetter) {
129129
if (__DEV__ && key === '$attrs') {
130130
markAttrsAccessed()
131131
}
132132
return publicGetter(target)
133133
} else if (
134134
__BUNDLER__ &&
135-
(cssModule = type.__cssModules) != null &&
135+
(cssModule = type.__cssModules) &&
136136
(cssModule = cssModule[key])
137137
) {
138138
return cssModule
139139
} else if (hasOwn(sink, key)) {
140140
return sink[key]
141-
} else if (__DEV__ && currentRenderingInstance != null) {
141+
} else if (__DEV__ && currentRenderingInstance) {
142142
warn(
143143
`Property ${JSON.stringify(key)} was accessed during render ` +
144144
`but is not defined on instance.`
@@ -152,7 +152,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
152152
accessCache![key] !== undefined ||
153153
(data !== EMPTY_OBJ && hasOwn(data, key)) ||
154154
hasOwn(renderContext, key) ||
155-
(type.props != null && hasOwn(type.props, key)) ||
155+
(type.props && hasOwn(type.props, key)) ||
156156
hasOwn(publicPropertiesMap, key) ||
157157
hasOwn(sink, key)
158158
)

packages/runtime-core/src/componentRenderUtils.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export function renderComponentRoot(
9090
result = cloneVNode(result, fallthroughAttrs)
9191
// If the child root node is a compiler optimized vnode, make sure it
9292
// force update full props to account for the merged attrs.
93-
if (result.dynamicChildren !== null) {
93+
if (result.dynamicChildren) {
9494
result.patchFlag |= PatchFlags.FULL_PROPS
9595
}
9696
} else if (__DEV__ && !accessedAttrs && result.type !== Comment) {
@@ -109,7 +109,7 @@ export function renderComponentRoot(
109109
result = cloneVNode(result, { [parentScopeId]: '' })
110110
}
111111
// inherit directives
112-
if (vnode.dirs != null) {
112+
if (vnode.dirs) {
113113
if (__DEV__ && !isElementRoot(result)) {
114114
warn(
115115
`Runtime directive used on component with non-element root node. ` +
@@ -119,7 +119,7 @@ export function renderComponentRoot(
119119
result.dirs = vnode.dirs
120120
}
121121
// inherit transition data
122-
if (vnode.transition != null) {
122+
if (vnode.transition) {
123123
if (__DEV__ && !isElementRoot(result)) {
124124
warn(
125125
`Component inside <Transition> renders non-element root node ` +
@@ -185,7 +185,7 @@ export function shouldUpdateComponent(
185185
}
186186

187187
// force child update on runtime directive usage on component vnode.
188-
if (nextVNode.dirs != null) {
188+
if (nextVNode.dirs) {
189189
return true
190190
}
191191

@@ -218,18 +218,18 @@ export function shouldUpdateComponent(
218218
} else if (!optimized) {
219219
// this path is only taken by manually written render functions
220220
// so presence of any children leads to a forced update
221-
if (prevChildren != null || nextChildren != null) {
222-
if (nextChildren == null || !(nextChildren as any).$stable) {
221+
if (prevChildren || nextChildren) {
222+
if (!nextChildren || !(nextChildren as any).$stable) {
223223
return true
224224
}
225225
}
226226
if (prevProps === nextProps) {
227227
return false
228228
}
229-
if (prevProps === null) {
230-
return nextProps !== null
229+
if (!prevProps) {
230+
return !!nextProps
231231
}
232-
if (nextProps === null) {
232+
if (!nextProps) {
233233
return true
234234
}
235235
return hasPropsChanged(prevProps, nextProps)

packages/runtime-core/src/componentSlots.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const normalizeSlot = (
4040
ctx: ComponentInternalInstance | null | undefined
4141
): Slot =>
4242
withCtx((props: any) => {
43-
if (__DEV__ && currentInstance != null) {
43+
if (__DEV__ && currentInstance) {
4444
warn(
4545
`Slot "${key}" invoked outside of the render function: ` +
4646
`this will not track dependencies used in the slot. ` +
@@ -80,7 +80,7 @@ export function resolveSlots(
8080
}
8181
}
8282
}
83-
} else if (children !== null) {
83+
} else if (children) {
8484
// non slot object children (direct value) passed to a component
8585
if (__DEV__ && !isKeepAlive(instance.vnode)) {
8686
warn(

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const KeepAliveImpl = {
8585
queuePostRenderEffect(() => {
8686
const component = vnode.component!
8787
component.isDeactivated = false
88-
if (component.a !== null) {
88+
if (component.a) {
8989
invokeHooks(component.a)
9090
}
9191
}, parentSuspense)
@@ -95,7 +95,7 @@ const KeepAliveImpl = {
9595
move(vnode, storageContainer, null, MoveType.LEAVE, parentSuspense)
9696
queuePostRenderEffect(() => {
9797
const component = vnode.component!
98-
if (component.da !== null) {
98+
if (component.da) {
9999
invokeHooks(component.da)
100100
}
101101
component.isDeactivated = true

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const PortalImpl = {
4444
const target = (n2.target = isString(targetSelector)
4545
? querySelector!(targetSelector)
4646
: targetSelector)
47-
if (target != null) {
47+
if (target) {
4848
if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
4949
setElementText(target, children as string)
5050
} else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
@@ -93,7 +93,7 @@ export const PortalImpl = {
9393
const nextTarget = (n2.target = isString(targetSelector)
9494
? querySelector!(targetSelector)
9595
: targetSelector)
96-
if (nextTarget != null) {
96+
if (nextTarget) {
9797
// move content
9898
if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
9999
setElementText(target, '')

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ export function queueEffectWithSuspense(
536536
fn: Function | Function[],
537537
suspense: SuspenseBoundary | null
538538
): void {
539-
if (suspense !== null && !suspense.isResolved) {
539+
if (suspense && !suspense.isResolved) {
540540
if (isArray(fn)) {
541541
suspense.effects.push(...fn)
542542
} else {

packages/runtime-core/src/errorHandling.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function callWithAsyncErrorHandling(
7979
): any[] {
8080
if (isFunction(fn)) {
8181
const res = callWithErrorHandling(fn, instance, type, args)
82-
if (res != null && !res._isVue && isPromise(res)) {
82+
if (res && !res._isVue && isPromise(res)) {
8383
res.catch(err => {
8484
handleError(err, instance, type)
8585
})
@@ -108,7 +108,7 @@ export function handleError(
108108
const errorInfo = __DEV__ ? ErrorTypeStrings[type] : type
109109
while (cur) {
110110
const errorCapturedHooks = cur.ec
111-
if (errorCapturedHooks !== null) {
111+
if (errorCapturedHooks) {
112112
for (let i = 0; i < errorCapturedHooks.length; i++) {
113113
if (errorCapturedHooks[i](err, exposedInstance, errorInfo)) {
114114
return

packages/runtime-core/src/hydration.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,12 @@ export function createHydrationFunctions(
199199
parentSuspense: SuspenseBoundary | null,
200200
optimized: boolean
201201
) => {
202-
optimized = optimized || vnode.dynamicChildren !== null
202+
optimized = optimized || !!vnode.dynamicChildren
203203
const { props, patchFlag, shapeFlag, dirs } = vnode
204204
// skip props & children if this is hoisted static nodes
205205
if (patchFlag !== PatchFlags.HOISTED) {
206206
// props
207-
if (props !== null) {
207+
if (props) {
208208
if (
209209
!optimized ||
210210
(patchFlag & PatchFlags.FULL_PROPS ||
@@ -215,24 +215,21 @@ export function createHydrationFunctions(
215215
patchProp(el, key, null, props[key])
216216
}
217217
}
218-
} else if (props.onClick != null) {
218+
} else if (props.onClick) {
219219
// Fast path for click listeners (which is most often) to avoid
220220
// iterating through props.
221221
patchProp(el, 'onClick', null, props.onClick)
222222
}
223223
}
224224
// vnode / directive hooks
225225
let vnodeHooks: VNodeHook | null | undefined
226-
if ((vnodeHooks = props && props.onVnodeBeforeMount) != null) {
226+
if ((vnodeHooks = props && props.onVnodeBeforeMount)) {
227227
invokeVNodeHook(vnodeHooks, parentComponent, vnode)
228228
}
229-
if (dirs != null) {
229+
if (dirs) {
230230
invokeDirectiveHook(vnode, null, parentComponent, 'beforeMount')
231231
}
232-
if (
233-
(vnodeHooks = props && props.onVnodeMounted) != null ||
234-
dirs != null
235-
) {
232+
if ((vnodeHooks = props && props.onVnodeMounted) || dirs) {
236233
queueEffectWithSuspense(() => {
237234
vnodeHooks && invokeVNodeHook(vnodeHooks, parentComponent, vnode)
238235
dirs && invokeDirectiveHook(vnode, null, parentComponent, 'mounted')
@@ -242,7 +239,7 @@ export function createHydrationFunctions(
242239
if (
243240
shapeFlag & ShapeFlags.ARRAY_CHILDREN &&
244241
// skip if element has innerHTML / textContent
245-
!(props !== null && (props.innerHTML || props.textContent))
242+
!(props && (props.innerHTML || props.textContent))
246243
) {
247244
let next = hydrateChildren(
248245
el.firstChild,
@@ -291,7 +288,7 @@ export function createHydrationFunctions(
291288
parentSuspense: SuspenseBoundary | null,
292289
optimized: boolean
293290
): Node | null => {
294-
optimized = optimized || vnode.dynamicChildren !== null
291+
optimized = optimized || !!vnode.dynamicChildren
295292
const children = vnode.children as VNode[]
296293
const l = children.length
297294
let hasWarned = false
@@ -369,7 +366,7 @@ export function createHydrationFunctions(
369366
const target = (vnode.target = isString(targetSelector)
370367
? document.querySelector(targetSelector)
371368
: targetSelector)
372-
if (target != null && vnode.shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
369+
if (target && vnode.shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
373370
hydrateChildren(
374371
target.firstChild,
375372
vnode,

0 commit comments

Comments
 (0)