Skip to content

Commit ff3b892

Browse files
committed
refactor: Inline NULL & UNDEFINED
1 parent af53275 commit ff3b892

File tree

10 files changed

+102
-118
lines changed

10 files changed

+102
-118
lines changed

src/clone-element.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { assign, slice } from './util';
22
import { createVNode } from './create-element';
3-
import { NULL } from './constants';
43

54
/**
65
* Clones the given VNode, optionally adding attributes/props and replacing its
@@ -33,6 +32,6 @@ export function cloneElement(vnode, props, children) {
3332
normalizedProps,
3433
key || vnode.key,
3534
ref || vnode.ref,
36-
NULL
35+
null
3736
);
3837
}

src/component.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { assign } from './util';
22
import { diff, commitRoot } from './diff/index';
33
import options from './options';
44
import { Fragment } from './create-element';
5-
import { MODE_HYDRATE, NULL } from './constants';
5+
import { MODE_HYDRATE } from './constants';
66

77
/**
88
* Base Component class. Provides `setState()` and `forceUpdate()`, which
@@ -28,7 +28,7 @@ export function BaseComponent(props, context) {
2828
BaseComponent.prototype.setState = function (update, callback) {
2929
// only clone state when copying to nextState the first time.
3030
let s;
31-
if (this._nextState != NULL && this._nextState != this.state) {
31+
if (this._nextState != null && this._nextState != this.state) {
3232
s = this._nextState;
3333
} else {
3434
s = this._nextState = assign({}, this.state);
@@ -45,7 +45,7 @@ BaseComponent.prototype.setState = function (update, callback) {
4545
}
4646

4747
// Skip update if updater function returned null
48-
if (update == NULL) return;
48+
if (update == null) return;
4949

5050
if (this._vnode) {
5151
if (callback) {
@@ -89,18 +89,18 @@ BaseComponent.prototype.render = Fragment;
8989
* @param {number | null} [childIndex]
9090
*/
9191
export function getDomSibling(vnode, childIndex) {
92-
if (childIndex == NULL) {
92+
if (childIndex == null) {
9393
// Use childIndex==null as a signal to resume the search from the vnode's sibling
9494
return vnode._parent
9595
? getDomSibling(vnode._parent, vnode._index + 1)
96-
: NULL;
96+
: null;
9797
}
9898

9999
let sibling;
100100
for (; childIndex < vnode._children.length; childIndex++) {
101101
sibling = vnode._children[childIndex];
102102

103-
if (sibling != NULL && sibling._dom != NULL) {
103+
if (sibling != null && sibling._dom != null) {
104104
// Since updateParentDomPointers keeps _dom pointer correct,
105105
// we can rely on _dom to tell us if this subtree contains a
106106
// rendered DOM node, and what the first rendered DOM node is
@@ -113,7 +113,7 @@ export function getDomSibling(vnode, childIndex) {
113113
// Only climb up and search the parent if we aren't searching through a DOM
114114
// VNode (meaning we reached the DOM parent of the original vnode that began
115115
// the search)
116-
return typeof vnode.type == 'function' ? getDomSibling(vnode) : NULL;
116+
return typeof vnode.type == 'function' ? getDomSibling(vnode) : null;
117117
}
118118

119119
/**
@@ -137,9 +137,9 @@ function renderComponent(component) {
137137
oldVNode,
138138
component._globalContext,
139139
component._parentDom.namespaceURI,
140-
oldVNode._flags & MODE_HYDRATE ? [oldDom] : NULL,
140+
oldVNode._flags & MODE_HYDRATE ? [oldDom] : null,
141141
commitQueue,
142-
oldDom == NULL ? getDomSibling(oldVNode) : oldDom,
142+
oldDom == null ? getDomSibling(oldVNode) : oldDom,
143143
!!(oldVNode._flags & MODE_HYDRATE),
144144
refQueue
145145
);
@@ -158,11 +158,11 @@ function renderComponent(component) {
158158
* @param {import('./internal').VNode} vnode
159159
*/
160160
function updateParentDomPointers(vnode) {
161-
if ((vnode = vnode._parent) != NULL && vnode._component != NULL) {
162-
vnode._dom = NULL;
161+
if ((vnode = vnode._parent) != null && vnode._component != null) {
162+
vnode._dom = null;
163163
for (let i = 0; i < vnode._children.length; i++) {
164164
let child = vnode._children[i];
165-
if (child != NULL && child._dom != NULL) {
165+
if (child != null && child._dom != null) {
166166
vnode._dom = child._dom;
167167
break;
168168
}

src/constants.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,5 @@ export const SKIP_CHILDREN = 1 << 3;
1212
/** Reset all mode flags */
1313
export const RESET_MODE = ~(MODE_HYDRATE | MODE_SUSPENDED);
1414

15-
export const NULL = null;
16-
export const UNDEFINED = undefined;
1715
export const EMPTY_OBJ = /** @type {any} */ ({});
1816
export const EMPTY_ARR = [];

src/create-context.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { enqueueRender } from './component';
2-
import { NULL } from './constants';
32

43
export let i = 0;
54

@@ -14,7 +13,7 @@ export function createContext(defaultValue) {
1413
this.getChildContext = () => ctx;
1514

1615
this.componentWillUnmount = () => {
17-
subs = NULL;
16+
subs = null;
1817
};
1918

2019
this.shouldComponentUpdate = function (_props) {

src/create-element.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { slice } from './util';
22
import options from './options';
3-
import { NULL, UNDEFINED } from './constants';
43

54
let vnodeId = 0;
65

@@ -29,7 +28,7 @@ export function createElement(type, props, children) {
2928
arguments.length > 3 ? slice.call(arguments, 2) : children;
3029
}
3130

32-
return createVNode(type, normalizedProps, key, ref, NULL);
31+
return createVNode(type, normalizedProps, key, ref, null);
3332
}
3433

3534
/**
@@ -53,25 +52,25 @@ export function createVNode(type, props, key, ref, original) {
5352
props,
5453
key,
5554
ref,
56-
_children: NULL,
57-
_parent: NULL,
55+
_children: null,
56+
_parent: null,
5857
_depth: 0,
59-
_dom: NULL,
60-
_component: NULL,
61-
constructor: UNDEFINED,
62-
_original: original == NULL ? ++vnodeId : original,
58+
_dom: null,
59+
_component: null,
60+
constructor: void 0,
61+
_original: original == null ? ++vnodeId : original,
6362
_index: -1,
6463
_flags: 0
6564
};
6665

6766
// Only invoke the vnode hook if this was *not* a direct copy:
68-
if (original == NULL && options.vnode != NULL) options.vnode(vnode);
67+
if (original == null && options.vnode != null) options.vnode(vnode);
6968

7069
return vnode;
7170
}
7271

7372
export function createRef() {
74-
return { current: NULL };
73+
return { current: null };
7574
}
7675

7776
export function Fragment(props) {
@@ -84,4 +83,4 @@ export function Fragment(props) {
8483
* @returns {vnode is VNode}
8584
*/
8685
export const isValidElement = vnode =>
87-
vnode != NULL && vnode.constructor == UNDEFINED;
86+
vnode != null && vnode.constructor == void 0;

src/diff/catch-error.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { NULL } from '../constants';
2-
31
/**
42
* Find the closest error boundary to a thrown error and call it
53
* @param {object} error The thrown value
@@ -22,12 +20,12 @@ export function _catchError(error, vnode, oldVNode, errorInfo) {
2220
try {
2321
ctor = component.constructor;
2422

25-
if (ctor && ctor.getDerivedStateFromError != NULL) {
23+
if (ctor && ctor.getDerivedStateFromError != null) {
2624
component.setState(ctor.getDerivedStateFromError(error));
2725
handled = component._dirty;
2826
}
2927

30-
if (component.componentDidCatch != NULL) {
28+
if (component.componentDidCatch != null) {
3129
component.componentDidCatch(error, errorInfo || {});
3230
handled = component._dirty;
3331
}

src/diff/children.js

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import { diff, unmount, applyRef } from './index';
22
import { createVNode, Fragment } from '../create-element';
3-
import {
4-
EMPTY_OBJ,
5-
EMPTY_ARR,
6-
INSERT_VNODE,
7-
MATCHED,
8-
UNDEFINED,
9-
NULL
10-
} from '../constants';
3+
import { EMPTY_OBJ, EMPTY_ARR, INSERT_VNODE, MATCHED } from '../constants';
114
import { isArray } from '../util';
125
import { getDomSibling } from '../component';
136

@@ -80,7 +73,7 @@ export function diffChildren(
8073

8174
for (i = 0; i < newChildrenLength; i++) {
8275
childVNode = newParentVNode._children[i];
83-
if (childVNode == NULL) continue;
76+
if (childVNode == null) continue;
8477

8578
// At this point, constructNewChildrenArray has assigned _index to be the
8679
// matchingIndex for this VNode's oldVNode (or -1 if there is no oldVNode).
@@ -111,7 +104,7 @@ export function diffChildren(
111104
newDom = childVNode._dom;
112105
if (childVNode.ref && oldVNode.ref != childVNode.ref) {
113106
if (oldVNode.ref) {
114-
applyRef(oldVNode.ref, NULL, childVNode);
107+
applyRef(oldVNode.ref, null, childVNode);
115108
}
116109
refQueue.push(
117110
childVNode.ref,
@@ -120,7 +113,7 @@ export function diffChildren(
120113
);
121114
}
122115

123-
if (firstChildDom == NULL && newDom != NULL) {
116+
if (firstChildDom == null && newDom != null) {
124117
firstChildDom = newDom;
125118
}
126119

@@ -129,7 +122,7 @@ export function diffChildren(
129122
oldVNode._children === childVNode._children
130123
) {
131124
oldDom = insert(childVNode, oldDom, parentDom);
132-
} else if (typeof childVNode.type == 'function' && result !== UNDEFINED) {
125+
} else if (typeof childVNode.type == 'function' && result !== void 0) {
133126
oldDom = result;
134127
} else if (newDom) {
135128
oldDom = newDom.nextSibling;
@@ -175,11 +168,11 @@ function constructNewChildrenArray(
175168
childVNode = renderResult[i];
176169

177170
if (
178-
childVNode == NULL ||
171+
childVNode == null ||
179172
typeof childVNode == 'boolean' ||
180173
typeof childVNode == 'function'
181174
) {
182-
newParentVNode._children[i] = NULL;
175+
newParentVNode._children[i] = null;
183176
continue;
184177
}
185178
// If this newVNode is being reused (e.g. <div>{reuse}{reuse}</div>) in the same diff,
@@ -193,21 +186,21 @@ function constructNewChildrenArray(
193186
childVNode.constructor == String
194187
) {
195188
childVNode = newParentVNode._children[i] = createVNode(
196-
NULL,
189+
null,
197190
childVNode,
198-
NULL,
199-
NULL,
200-
NULL
191+
null,
192+
null,
193+
null
201194
);
202195
} else if (isArray(childVNode)) {
203196
childVNode = newParentVNode._children[i] = createVNode(
204197
Fragment,
205198
{ children: childVNode },
206-
NULL,
207-
NULL,
208-
NULL
199+
null,
200+
null,
201+
null
209202
);
210-
} else if (childVNode.constructor == UNDEFINED && childVNode._depth > 0) {
203+
} else if (childVNode.constructor == void 0 && childVNode._depth > 0) {
211204
// VNode is already in use, clone it. This can happen in the following
212205
// scenario:
213206
// const reuse = <div />
@@ -216,7 +209,7 @@ function constructNewChildrenArray(
216209
childVNode.type,
217210
childVNode.props,
218211
childVNode.key,
219-
childVNode.ref ? childVNode.ref : NULL,
212+
childVNode.ref ? childVNode.ref : null,
220213
childVNode._original
221214
);
222215
} else {
@@ -237,7 +230,7 @@ function constructNewChildrenArray(
237230
remainingOldChildren
238231
));
239232

240-
oldVNode = NULL;
233+
oldVNode = null;
241234
if (matchingIndex != -1) {
242235
oldVNode = oldChildren[matchingIndex];
243236
remainingOldChildren--;
@@ -249,7 +242,7 @@ function constructNewChildrenArray(
249242
// Here, we define isMounting for the purposes of the skew diffing
250243
// algorithm. Nodes that are unsuspending are considered mounting and we detect
251244
// this by checking if oldVNode._original == null
252-
const isMounting = oldVNode == NULL || oldVNode._original == NULL;
245+
const isMounting = oldVNode == null || oldVNode._original == null;
253246

254247
if (isMounting) {
255248
if (matchingIndex == -1) {
@@ -321,7 +314,7 @@ function constructNewChildrenArray(
321314
if (remainingOldChildren) {
322315
for (i = 0; i < oldChildrenLength; i++) {
323316
oldVNode = oldChildren[i];
324-
if (oldVNode != NULL && (oldVNode._flags & MATCHED) == 0) {
317+
if (oldVNode != null && (oldVNode._flags & MATCHED) == 0) {
325318
if (oldVNode._dom == oldDom) {
326319
oldDom = getDomSibling(oldVNode);
327320
}
@@ -361,13 +354,13 @@ function insert(parentVNode, oldDom, parentDom) {
361354
if (oldDom && parentVNode.type && !oldDom.parentNode) {
362355
oldDom = getDomSibling(parentVNode);
363356
}
364-
parentDom.insertBefore(parentVNode._dom, oldDom || NULL);
357+
parentDom.insertBefore(parentVNode._dom, oldDom || null);
365358
oldDom = parentVNode._dom;
366359
}
367360

368361
do {
369362
oldDom = oldDom && oldDom.nextSibling;
370-
} while (oldDom != NULL && oldDom.nodeType == 8);
363+
} while (oldDom != null && oldDom.nodeType == 8);
371364

372365
return oldDom;
373366
}
@@ -380,7 +373,7 @@ function insert(parentVNode, oldDom, parentDom) {
380373
*/
381374
export function toChildArray(children, out) {
382375
out = out || [];
383-
if (children == NULL || typeof children == 'boolean') {
376+
if (children == null || typeof children == 'boolean') {
384377
} else if (isArray(children)) {
385378
children.some(child => {
386379
toChildArray(child, out);
@@ -422,10 +415,10 @@ function findMatchingIndex(
422415
let shouldSearch =
423416
// (typeof type != 'function' || type === Fragment || key) &&
424417
remainingOldChildren >
425-
(oldVNode != NULL && (oldVNode._flags & MATCHED) == 0 ? 1 : 0);
418+
(oldVNode != null && (oldVNode._flags & MATCHED) == 0 ? 1 : 0);
426419

427420
if (
428-
(oldVNode === NULL && childVNode.key == null) ||
421+
(oldVNode === null && childVNode.key == null) ||
429422
(oldVNode &&
430423
key == oldVNode.key &&
431424
type == oldVNode.type &&

0 commit comments

Comments
 (0)