Skip to content

Commit 514183f

Browse files
authored
Try constant for undefined (#4552)
1 parent bedd413 commit 514183f

File tree

5 files changed

+25
-15
lines changed

5 files changed

+25
-15
lines changed

src/clone-element.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { assign, slice } from './util';
22
import { createVNode } from './create-element';
3+
import { UNDEFINED } from './constants';
34

45
/**
56
* Clones the given VNode, optionally adding attributes/props and replacing its
@@ -25,7 +26,7 @@ export function cloneElement(vnode, props, children) {
2526
for (i in props) {
2627
if (i == 'key') key = props[i];
2728
else if (i == 'ref') ref = props[i];
28-
else if (props[i] === undefined && defaultProps !== undefined) {
29+
else if (props[i] === UNDEFINED && defaultProps !== UNDEFINED) {
2930
normalizedProps[i] = defaultProps[i];
3031
} else {
3132
normalizedProps[i] = props[i];

src/constants.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const MATCHED = 1 << 17;
1010
/** Reset all mode flags */
1111
export const RESET_MODE = ~(MODE_HYDRATE | MODE_SUSPENDED);
1212

13+
export const UNDEFINED = undefined;
1314
export const EMPTY_OBJ = /** @type {any} */ ({});
1415
export const EMPTY_ARR = [];
1516
export const IS_NON_DIMENSIONAL =

src/create-element.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { slice } from './util';
22
import options from './options';
3+
import { UNDEFINED } from './constants';
34

45
let vnodeId = 0;
56

@@ -32,7 +33,7 @@ export function createElement(type, props, children) {
3233
// Note: type may be undefined in development, must never error here.
3334
if (typeof type == 'function' && type.defaultProps != null) {
3435
for (i in type.defaultProps) {
35-
if (normalizedProps[i] === undefined) {
36+
if (normalizedProps[i] === UNDEFINED) {
3637
normalizedProps[i] = type.defaultProps[i];
3738
}
3839
}
@@ -70,9 +71,9 @@ export function createVNode(type, props, key, ref, original) {
7071
// be set to dom.nextSibling which can return `null` and it is important
7172
// to be able to distinguish between an uninitialized _nextDom and
7273
// a _nextDom that has been set to `null`
73-
_nextDom: undefined,
74+
_nextDom: UNDEFINED,
7475
_component: null,
75-
constructor: undefined,
76+
constructor: UNDEFINED,
7677
_original: original == null ? ++vnodeId : original,
7778
_index: -1,
7879
_flags: 0
@@ -98,4 +99,4 @@ export function Fragment(props) {
9899
* @returns {vnode is VNode}
99100
*/
100101
export const isValidElement = vnode =>
101-
vnode != null && vnode.constructor == undefined;
102+
vnode != null && vnode.constructor == UNDEFINED;

src/diff/children.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { diff, unmount, applyRef } from './index';
22
import { createVNode, Fragment } from '../create-element';
3-
import { EMPTY_OBJ, EMPTY_ARR, INSERT_VNODE, MATCHED } from '../constants';
3+
import {
4+
EMPTY_OBJ,
5+
EMPTY_ARR,
6+
INSERT_VNODE,
7+
MATCHED,
8+
UNDEFINED
9+
} from '../constants';
410
import { isArray } from '../util';
511
import { getDomSibling } from '../component';
612

@@ -113,7 +119,7 @@ export function diffChildren(
113119
oldDom = insert(childVNode, oldDom, parentDom);
114120
} else if (
115121
typeof childVNode.type == 'function' &&
116-
childVNode._nextDom !== undefined
122+
childVNode._nextDom !== UNDEFINED
117123
) {
118124
// Since Fragments or components that return Fragment like VNodes can
119125
// contain multiple DOM nodes as the same level, continue the diff from
@@ -128,7 +134,7 @@ export function diffChildren(
128134
// after diffing Components and Fragments. Once we store it the nextDOM
129135
// local var, we can clean up the property. Also prevents us hanging on to
130136
// DOM nodes that may have been unmounted.
131-
childVNode._nextDom = undefined;
137+
childVNode._nextDom = UNDEFINED;
132138

133139
// Unset diffing flags
134140
childVNode._flags &= ~(INSERT_VNODE | MATCHED);
@@ -206,7 +212,7 @@ function constructNewChildrenArray(newParentVNode, renderResult, oldChildren) {
206212
null,
207213
null
208214
);
209-
} else if (childVNode.constructor === undefined && childVNode._depth > 0) {
215+
} else if (childVNode.constructor === UNDEFINED && childVNode._depth > 0) {
210216
// VNode is already in use, clone it. This can happen in the following
211217
// scenario:
212218
// const reuse = <div />

src/diff/index.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import {
22
EMPTY_OBJ,
33
MODE_HYDRATE,
44
MODE_SUSPENDED,
5-
RESET_MODE
5+
RESET_MODE,
6+
UNDEFINED
67
} from '../constants';
78
import { BaseComponent, getDomSibling } from '../component';
89
import { Fragment } from '../create-element';
@@ -47,7 +48,7 @@ export function diff(
4748

4849
// When passing through createElement it assigns the object
4950
// constructor as undefined. This to prevent JSON-injection.
50-
if (newVNode.constructor !== undefined) return null;
51+
if (newVNode.constructor !== UNDEFINED) return null;
5152

5253
// If the previous diff bailed out, resume creating/hydrating.
5354
if (oldVNode._flags & MODE_SUSPENDED) {
@@ -317,7 +318,7 @@ export function diff(
317318
* @param {VNode} root
318319
*/
319320
export function commitRoot(commitQueue, root, refQueue) {
320-
root._nextDom = undefined;
321+
root._nextDom = UNDEFINED;
321322

322323
for (let i = 0; i < refQueue.length; i++) {
323324
applyRef(refQueue[i], refQueue[++i], refQueue[++i]);
@@ -534,7 +535,7 @@ function diffElementNodes(
534535
if (nodeType === 'progress' && inputValue == null) {
535536
dom.removeAttribute('value');
536537
} else if (
537-
inputValue !== undefined &&
538+
inputValue !== UNDEFINED &&
538539
// #2756 For the <progress>-element the initial value is 0,
539540
// despite the attribute not being present. When the attribute
540541
// is missing the progress bar is treated as indeterminate.
@@ -550,7 +551,7 @@ function diffElementNodes(
550551
}
551552

552553
i = 'checked';
553-
if (checked !== undefined && checked !== dom[i]) {
554+
if (checked !== UNDEFINED && checked !== dom[i]) {
554555
setProperty(dom, i, checked, oldProps[i], namespace);
555556
}
556557
}
@@ -633,7 +634,7 @@ export function unmount(vnode, parentVNode, skipRemove) {
633634

634635
// Must be set to `undefined` to properly clean up `_nextDom`
635636
// for which `null` is a valid value. See comment in `create-element.js`
636-
vnode._component = vnode._parent = vnode._dom = vnode._nextDom = undefined;
637+
vnode._component = vnode._parent = vnode._dom = vnode._nextDom = UNDEFINED;
637638
}
638639

639640
/** The `.render()` method for a PFC backing instance. */

0 commit comments

Comments
 (0)