From ba9a91c48cea390d0c88c8de868ab731d4b58eff Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 18 Mar 2020 18:14:51 -0400 Subject: [PATCH] refactor: remove null comparisons --- packages/compiler-core/src/parse.ts | 4 +- packages/reactivity/src/effect.ts | 2 +- packages/runtime-core/src/apiWatch.ts | 2 +- packages/runtime-core/src/componentProps.ts | 11 +- packages/runtime-core/src/componentProxy.ts | 10 +- .../runtime-core/src/componentRenderUtils.ts | 18 +-- packages/runtime-core/src/componentSlots.ts | 4 +- .../runtime-core/src/components/KeepAlive.ts | 4 +- .../runtime-core/src/components/Portal.ts | 4 +- .../runtime-core/src/components/Suspense.ts | 2 +- packages/runtime-core/src/errorHandling.ts | 4 +- packages/runtime-core/src/hydration.ts | 21 ++-- packages/runtime-core/src/renderer.ts | 105 ++++++++---------- packages/runtime-core/src/vnode.ts | 12 +- packages/runtime-dom/src/modules/props.ts | 2 +- packages/runtime-dom/src/nodeOps.ts | 4 +- packages/runtime-test/src/nodeOps.ts | 4 +- .../server-renderer/src/renderToString.ts | 10 +- 18 files changed, 104 insertions(+), 119 deletions(-) diff --git a/packages/compiler-core/src/parse.ts b/packages/compiler-core/src/parse.ts index dbadeff7..648511c8 100644 --- a/packages/compiler-core/src/parse.ts +++ b/packages/compiler-core/src/parse.ts @@ -545,7 +545,7 @@ function parseAttribute( { const pattern = /["'<]/g let m: RegExpExecArray | null - while ((m = pattern.exec(name)) !== null) { + while ((m = pattern.exec(name))) { emitError( context, ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME, @@ -696,7 +696,7 @@ function parseAttributeValue( } const unexpectedChars = /["'<=`]/g let m: RegExpExecArray | null - while ((m = unexpectedChars.exec(match[0])) !== null) { + while ((m = unexpectedChars.exec(match[0]))) { emitError( context, ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE, diff --git a/packages/reactivity/src/effect.ts b/packages/reactivity/src/effect.ts index ae339be9..7b8ec4d4 100644 --- a/packages/reactivity/src/effect.ts +++ b/packages/reactivity/src/effect.ts @@ -46,7 +46,7 @@ export let activeEffect: ReactiveEffect | undefined export const ITERATE_KEY = Symbol('iterate') export function isEffect(fn: any): fn is ReactiveEffect { - return fn != null && fn._isEffect === true + return fn && fn._isEffect === true } export function effect( diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index 889821a7..329b639c 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -229,7 +229,7 @@ function doWatch( scheduler = invoke } else if (flush === 'pre') { scheduler = job => { - if (!instance || instance.vnode.el != null) { + if (!instance || instance.isMounted) { queueJob(job) } else { // with 'pre' option, the first call must happen before diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index d2f35da2..0e55d022 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -98,7 +98,7 @@ export function resolveProps( rawProps: Data | null, _options: ComponentPropsOptions | void ) { - const hasDeclaredProps = _options != null + const hasDeclaredProps = !!_options if (!rawProps && !hasDeclaredProps) { return } @@ -122,7 +122,7 @@ export function resolveProps( // allow mutation of propsProxy (which is readonly by default) unlock() - if (rawProps != null) { + if (rawProps) { for (const key in rawProps) { const value = rawProps[key] // key, ref are reserved and never passed down @@ -186,10 +186,7 @@ export function resolveProps( // in case of dynamic props, check if we need to delete keys from // the props proxy const { patchFlag } = instance.vnode - if ( - propsProxy !== null && - (patchFlag === 0 || patchFlag & PatchFlags.FULL_PROPS) - ) { + if (propsProxy && (patchFlag === 0 || patchFlag & PatchFlags.FULL_PROPS)) { const rawInitialProps = toRaw(propsProxy) for (const key in rawInitialProps) { if (!hasOwn(props, key)) { @@ -250,7 +247,7 @@ function normalizePropsOptions( const opt = raw[key] const prop: NormalizedProp = (options[normalizedKey] = isArray(opt) || isFunction(opt) ? { type: opt } : opt) - if (prop != null) { + if (prop) { const booleanIndex = getTypeIndex(Boolean, prop.type) const stringIndex = getTypeIndex(String, prop.type) prop[BooleanFlags.shouldCast] = booleanIndex > -1 diff --git a/packages/runtime-core/src/componentProxy.ts b/packages/runtime-core/src/componentProxy.ts index fcbc4064..eb496f65 100644 --- a/packages/runtime-core/src/componentProxy.ts +++ b/packages/runtime-core/src/componentProxy.ts @@ -109,7 +109,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler = { } else if (renderContext !== EMPTY_OBJ && hasOwn(renderContext, key)) { accessCache![key] = AccessTypes.CONTEXT return renderContext[key] - } else if (type.props != null) { + } else if (type.props) { // only cache other properties when instance has declared (this stable) // props if (hasOwn(props, key)) { @@ -125,20 +125,20 @@ export const PublicInstanceProxyHandlers: ProxyHandler = { // public $xxx properties & user-attached properties (sink) const publicGetter = publicPropertiesMap[key] let cssModule - if (publicGetter != null) { + if (publicGetter) { if (__DEV__ && key === '$attrs') { markAttrsAccessed() } return publicGetter(target) } else if ( __BUNDLER__ && - (cssModule = type.__cssModules) != null && + (cssModule = type.__cssModules) && (cssModule = cssModule[key]) ) { return cssModule } else if (hasOwn(sink, key)) { return sink[key] - } else if (__DEV__ && currentRenderingInstance != null) { + } else if (__DEV__ && currentRenderingInstance) { warn( `Property ${JSON.stringify(key)} was accessed during render ` + `but is not defined on instance.` @@ -152,7 +152,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler = { accessCache![key] !== undefined || (data !== EMPTY_OBJ && hasOwn(data, key)) || hasOwn(renderContext, key) || - (type.props != null && hasOwn(type.props, key)) || + (type.props && hasOwn(type.props, key)) || hasOwn(publicPropertiesMap, key) || hasOwn(sink, key) ) diff --git a/packages/runtime-core/src/componentRenderUtils.ts b/packages/runtime-core/src/componentRenderUtils.ts index 316bb80a..571128b1 100644 --- a/packages/runtime-core/src/componentRenderUtils.ts +++ b/packages/runtime-core/src/componentRenderUtils.ts @@ -90,7 +90,7 @@ export function renderComponentRoot( result = cloneVNode(result, fallthroughAttrs) // If the child root node is a compiler optimized vnode, make sure it // force update full props to account for the merged attrs. - if (result.dynamicChildren !== null) { + if (result.dynamicChildren) { result.patchFlag |= PatchFlags.FULL_PROPS } } else if (__DEV__ && !accessedAttrs && result.type !== Comment) { @@ -109,7 +109,7 @@ export function renderComponentRoot( result = cloneVNode(result, { [parentScopeId]: '' }) } // inherit directives - if (vnode.dirs != null) { + if (vnode.dirs) { if (__DEV__ && !isElementRoot(result)) { warn( `Runtime directive used on component with non-element root node. ` + @@ -119,7 +119,7 @@ export function renderComponentRoot( result.dirs = vnode.dirs } // inherit transition data - if (vnode.transition != null) { + if (vnode.transition) { if (__DEV__ && !isElementRoot(result)) { warn( `Component inside renders non-element root node ` + @@ -185,7 +185,7 @@ export function shouldUpdateComponent( } // force child update on runtime directive usage on component vnode. - if (nextVNode.dirs != null) { + if (nextVNode.dirs) { return true } @@ -218,18 +218,18 @@ export function shouldUpdateComponent( } else if (!optimized) { // this path is only taken by manually written render functions // so presence of any children leads to a forced update - if (prevChildren != null || nextChildren != null) { - if (nextChildren == null || !(nextChildren as any).$stable) { + if (prevChildren || nextChildren) { + if (!nextChildren || !(nextChildren as any).$stable) { return true } } if (prevProps === nextProps) { return false } - if (prevProps === null) { - return nextProps !== null + if (!prevProps) { + return !!nextProps } - if (nextProps === null) { + if (!nextProps) { return true } return hasPropsChanged(prevProps, nextProps) diff --git a/packages/runtime-core/src/componentSlots.ts b/packages/runtime-core/src/componentSlots.ts index db049246..e9459963 100644 --- a/packages/runtime-core/src/componentSlots.ts +++ b/packages/runtime-core/src/componentSlots.ts @@ -40,7 +40,7 @@ const normalizeSlot = ( ctx: ComponentInternalInstance | null | undefined ): Slot => withCtx((props: any) => { - if (__DEV__ && currentInstance != null) { + if (__DEV__ && currentInstance) { warn( `Slot "${key}" invoked outside of the render function: ` + `this will not track dependencies used in the slot. ` + @@ -80,7 +80,7 @@ export function resolveSlots( } } } - } else if (children !== null) { + } else if (children) { // non slot object children (direct value) passed to a component if (__DEV__ && !isKeepAlive(instance.vnode)) { warn( diff --git a/packages/runtime-core/src/components/KeepAlive.ts b/packages/runtime-core/src/components/KeepAlive.ts index 8aae9b4e..c7ecb3e8 100644 --- a/packages/runtime-core/src/components/KeepAlive.ts +++ b/packages/runtime-core/src/components/KeepAlive.ts @@ -85,7 +85,7 @@ const KeepAliveImpl = { queuePostRenderEffect(() => { const component = vnode.component! component.isDeactivated = false - if (component.a !== null) { + if (component.a) { invokeHooks(component.a) } }, parentSuspense) @@ -95,7 +95,7 @@ const KeepAliveImpl = { move(vnode, storageContainer, null, MoveType.LEAVE, parentSuspense) queuePostRenderEffect(() => { const component = vnode.component! - if (component.da !== null) { + if (component.da) { invokeHooks(component.da) } component.isDeactivated = true diff --git a/packages/runtime-core/src/components/Portal.ts b/packages/runtime-core/src/components/Portal.ts index 93676867..2a091cd3 100644 --- a/packages/runtime-core/src/components/Portal.ts +++ b/packages/runtime-core/src/components/Portal.ts @@ -44,7 +44,7 @@ export const PortalImpl = { const target = (n2.target = isString(targetSelector) ? querySelector!(targetSelector) : targetSelector) - if (target != null) { + if (target) { if (shapeFlag & ShapeFlags.TEXT_CHILDREN) { setElementText(target, children as string) } else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) { @@ -93,7 +93,7 @@ export const PortalImpl = { const nextTarget = (n2.target = isString(targetSelector) ? querySelector!(targetSelector) : targetSelector) - if (nextTarget != null) { + if (nextTarget) { // move content if (shapeFlag & ShapeFlags.TEXT_CHILDREN) { setElementText(target, '') diff --git a/packages/runtime-core/src/components/Suspense.ts b/packages/runtime-core/src/components/Suspense.ts index bdb44907..a7dd7e01 100644 --- a/packages/runtime-core/src/components/Suspense.ts +++ b/packages/runtime-core/src/components/Suspense.ts @@ -536,7 +536,7 @@ export function queueEffectWithSuspense( fn: Function | Function[], suspense: SuspenseBoundary | null ): void { - if (suspense !== null && !suspense.isResolved) { + if (suspense && !suspense.isResolved) { if (isArray(fn)) { suspense.effects.push(...fn) } else { diff --git a/packages/runtime-core/src/errorHandling.ts b/packages/runtime-core/src/errorHandling.ts index f027f0c3..c57fd8a9 100644 --- a/packages/runtime-core/src/errorHandling.ts +++ b/packages/runtime-core/src/errorHandling.ts @@ -79,7 +79,7 @@ export function callWithAsyncErrorHandling( ): any[] { if (isFunction(fn)) { const res = callWithErrorHandling(fn, instance, type, args) - if (res != null && !res._isVue && isPromise(res)) { + if (res && !res._isVue && isPromise(res)) { res.catch(err => { handleError(err, instance, type) }) @@ -108,7 +108,7 @@ export function handleError( const errorInfo = __DEV__ ? ErrorTypeStrings[type] : type while (cur) { const errorCapturedHooks = cur.ec - if (errorCapturedHooks !== null) { + if (errorCapturedHooks) { for (let i = 0; i < errorCapturedHooks.length; i++) { if (errorCapturedHooks[i](err, exposedInstance, errorInfo)) { return diff --git a/packages/runtime-core/src/hydration.ts b/packages/runtime-core/src/hydration.ts index d81e6abd..aa6c81b7 100644 --- a/packages/runtime-core/src/hydration.ts +++ b/packages/runtime-core/src/hydration.ts @@ -199,12 +199,12 @@ export function createHydrationFunctions( parentSuspense: SuspenseBoundary | null, optimized: boolean ) => { - optimized = optimized || vnode.dynamicChildren !== null + optimized = optimized || !!vnode.dynamicChildren const { props, patchFlag, shapeFlag, dirs } = vnode // skip props & children if this is hoisted static nodes if (patchFlag !== PatchFlags.HOISTED) { // props - if (props !== null) { + if (props) { if ( !optimized || (patchFlag & PatchFlags.FULL_PROPS || @@ -215,7 +215,7 @@ export function createHydrationFunctions( patchProp(el, key, null, props[key]) } } - } else if (props.onClick != null) { + } else if (props.onClick) { // Fast path for click listeners (which is most often) to avoid // iterating through props. patchProp(el, 'onClick', null, props.onClick) @@ -223,16 +223,13 @@ export function createHydrationFunctions( } // vnode / directive hooks let vnodeHooks: VNodeHook | null | undefined - if ((vnodeHooks = props && props.onVnodeBeforeMount) != null) { + if ((vnodeHooks = props && props.onVnodeBeforeMount)) { invokeVNodeHook(vnodeHooks, parentComponent, vnode) } - if (dirs != null) { + if (dirs) { invokeDirectiveHook(vnode, null, parentComponent, 'beforeMount') } - if ( - (vnodeHooks = props && props.onVnodeMounted) != null || - dirs != null - ) { + if ((vnodeHooks = props && props.onVnodeMounted) || dirs) { queueEffectWithSuspense(() => { vnodeHooks && invokeVNodeHook(vnodeHooks, parentComponent, vnode) dirs && invokeDirectiveHook(vnode, null, parentComponent, 'mounted') @@ -242,7 +239,7 @@ export function createHydrationFunctions( if ( shapeFlag & ShapeFlags.ARRAY_CHILDREN && // skip if element has innerHTML / textContent - !(props !== null && (props.innerHTML || props.textContent)) + !(props && (props.innerHTML || props.textContent)) ) { let next = hydrateChildren( el.firstChild, @@ -291,7 +288,7 @@ export function createHydrationFunctions( parentSuspense: SuspenseBoundary | null, optimized: boolean ): Node | null => { - optimized = optimized || vnode.dynamicChildren !== null + optimized = optimized || !!vnode.dynamicChildren const children = vnode.children as VNode[] const l = children.length let hasWarned = false @@ -369,7 +366,7 @@ export function createHydrationFunctions( const target = (vnode.target = isString(targetSelector) ? document.querySelector(targetSelector) : targetSelector) - if (target != null && vnode.shapeFlag & ShapeFlags.ARRAY_CHILDREN) { + if (target && vnode.shapeFlag & ShapeFlags.ARRAY_CHILDREN) { hydrateChildren( target.firstChild, vnode, diff --git a/packages/runtime-core/src/renderer.ts b/packages/runtime-core/src/renderer.ts index c8788352..942bedb5 100644 --- a/packages/runtime-core/src/renderer.ts +++ b/packages/runtime-core/src/renderer.ts @@ -348,7 +348,7 @@ function baseCreateRenderer< optimized = false ) => { // patching & not same type, unmount old tree - if (n1 != null && !isSameVNodeType(n1, n2)) { + if (n1 && !isSameVNodeType(n1, n2)) { anchor = getNextHostNode(n1) unmount(n1, parentComponent, parentSuspense, true) n1 = null @@ -476,7 +476,7 @@ function baseCreateRenderer< anchor: HostNode | null, isSVG: boolean ) => { - if (n2.el != null && hostCloneNode !== undefined) { + if (n2.el && hostCloneNode !== undefined) { hostInsert(hostCloneNode(n2.el), container, anchor) } else { // static nodes are only present when used with compiler-dom/runtime-dom @@ -514,7 +514,7 @@ function baseCreateRenderer< } else { patchElement(n1, n2, parentComponent, parentSuspense, isSVG, optimized) } - if (n2.ref !== null && parentComponent !== null) { + if (n2.ref != null && parentComponent) { setRef(n2.ref, n1 && n1.ref, parentComponent, n2.el) } } @@ -540,7 +540,7 @@ function baseCreateRenderer< dirs } = vnode if ( - vnode.el !== null && + vnode.el && hostCloneNode !== undefined && patchFlag === PatchFlags.HOISTED ) { @@ -551,29 +551,29 @@ function baseCreateRenderer< } else { el = vnode.el = hostCreateElement(vnode.type as string, isSVG) // props - if (props != null) { + if (props) { for (const key in props) { if (!isReservedProp(key)) { hostPatchProp(el, key, null, props[key], isSVG) } } - if ((vnodeHook = props.onVnodeBeforeMount) != null) { + if ((vnodeHook = props.onVnodeBeforeMount)) { invokeVNodeHook(vnodeHook, parentComponent, vnode) } } - if (dirs != null) { + if (dirs) { invokeDirectiveHook(vnode, null, parentComponent, 'beforeMount') } // scopeId if (__BUNDLER__) { - if (scopeId !== null) { + if (scopeId) { hostSetScopeId(el, scopeId) } const treeOwnerId = parentComponent && parentComponent.type.__scopeId // vnode's own scopeId and the current patched component's scopeId is // different - this is a slot content node. - if (treeOwnerId != null && treeOwnerId !== scopeId) { + if (treeOwnerId && treeOwnerId !== scopeId) { hostSetScopeId(el, treeOwnerId + '-s') } } @@ -589,19 +589,19 @@ function baseCreateRenderer< parentComponent, parentSuspense, isSVG && type !== 'foreignObject', - optimized || vnode.dynamicChildren !== null + optimized || !!vnode.dynamicChildren ) } - if (transition != null && !transition.persisted) { + if (transition && !transition.persisted) { transition.beforeEnter(el) } } hostInsert(el, container, anchor) if ( - (vnodeHook = props && props.onVnodeMounted) != null || - (transition != null && !transition.persisted) || - dirs != null + (vnodeHook = props && props.onVnodeMounted) || + (transition && !transition.persisted) || + dirs ) { queuePostRenderEffect(() => { vnodeHook && invokeVNodeHook(vnodeHook, parentComponent, vnode) @@ -652,10 +652,10 @@ function baseCreateRenderer< const newProps = n2.props || EMPTY_OBJ let vnodeHook: VNodeHook | undefined | null - if ((vnodeHook = newProps.onVnodeBeforeUpdate) != null) { + if ((vnodeHook = newProps.onVnodeBeforeUpdate)) { invokeVNodeHook(vnodeHook, parentComponent, n2, n1) } - if (dirs != null) { + if (dirs) { invokeDirectiveHook(n2, n1, parentComponent, 'beforeUpdate') } @@ -748,7 +748,7 @@ function baseCreateRenderer< } const areChildrenSVG = isSVG && n2.type !== 'foreignObject' - if (dynamicChildren != null) { + if (dynamicChildren) { patchBlockChildren( n1.dynamicChildren!, dynamicChildren, @@ -770,7 +770,7 @@ function baseCreateRenderer< ) } - if ((vnodeHook = newProps.onVnodeUpdated) != null || dirs != null) { + if ((vnodeHook = newProps.onVnodeUpdated) || dirs) { queuePostRenderEffect(() => { vnodeHook && invokeVNodeHook(vnodeHook, parentComponent, n2, n1) dirs && invokeDirectiveHook(n2, n1, parentComponent, 'updated') @@ -906,7 +906,7 @@ function baseCreateRenderer< optimized ) } else { - if (patchFlag & PatchFlags.STABLE_FRAGMENT && dynamicChildren != null) { + if (patchFlag & PatchFlags.STABLE_FRAGMENT && dynamicChildren) { // a stable fragment (template root or