Skip to content

Commit 6621706

Browse files
authored
Merge pull request #8978 from acdlite/nooptionaltypes
[Fiber] Use `T | null` instead of `?T` types
2 parents 7958c1d + 350d736 commit 6621706

12 files changed

+263
-271
lines changed

src/renderers/shared/fiber/ReactChildFiber.js

Lines changed: 82 additions & 86 deletions
Large diffs are not rendered by default.

src/renderers/shared/fiber/ReactDebugCurrentFiber.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ if (__DEV__) {
2222
function getCurrentFiberOwnerName() : string | null {
2323
if (__DEV__) {
2424
const fiber = ReactDebugCurrentFiber.current;
25-
if (fiber == null) {
25+
if (fiber === null) {
2626
return null;
2727
}
2828
if (fiber._debugOwner != null) {
@@ -35,7 +35,7 @@ function getCurrentFiberOwnerName() : string | null {
3535
function getCurrentFiberStackAddendum() : string | null {
3636
if (__DEV__) {
3737
const fiber = ReactDebugCurrentFiber.current;
38-
if (fiber == null) {
38+
if (fiber === null) {
3939
return null;
4040
}
4141
// Safe because if current fiber exists, we are reconciling,

src/renderers/shared/fiber/ReactFiber.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ export type Fiber = {
9393
// This is effectively the parent, but there can be multiple parents (two)
9494
// so this is only the parent of the thing we're currently processing.
9595
// It is conceptually the same as the return address of a stack frame.
96-
return: ?Fiber,
96+
return: Fiber | null,
9797

9898
// Singly Linked List Tree Structure.
99-
child: ?Fiber,
100-
sibling: ?Fiber,
99+
child: Fiber | null,
100+
sibling: Fiber | null,
101101
index: number,
102102

103103
// The ref last used to attach this node.
@@ -119,13 +119,13 @@ export type Fiber = {
119119
effectTag: TypeOfSideEffect,
120120

121121
// Singly linked list fast path to the next fiber with side-effects.
122-
nextEffect: ?Fiber,
122+
nextEffect: Fiber | null,
123123

124124
// The first and last fiber with side-effect within this subtree. This allows
125125
// us to reuse a slice of the linked list when we reuse the work done within
126126
// this fiber.
127-
firstEffect: ?Fiber,
128-
lastEffect: ?Fiber,
127+
firstEffect: Fiber | null,
128+
lastEffect: Fiber | null,
129129

130130
// This will be used to quickly determine if a subtree has no pending changes.
131131
pendingWorkPriority: PriorityLevel,
@@ -139,19 +139,19 @@ export type Fiber = {
139139
// priority, then we need to store the progressed work somewhere. This holds
140140
// the started child set until we need to get back to working on it. It may
141141
// or may not be the same as the "current" child.
142-
progressedChild: ?Fiber,
142+
progressedChild: Fiber | null,
143143

144144
// When we reconcile children onto progressedChild it is possible that we have
145145
// to delete some child fibers. We need to keep track of this side-effects so
146146
// that if we continue later on, we have to include those effects. Deletions
147147
// are added in the reverse order from sibling pointers.
148-
progressedFirstDeletion: ?Fiber,
149-
progressedLastDeletion: ?Fiber,
148+
progressedFirstDeletion: Fiber | null,
149+
progressedLastDeletion: Fiber | null,
150150

151151
// This is a pooled version of a Fiber. Every fiber that gets updated will
152152
// eventually have a pair. There are cases when we can clean up pairs to save
153153
// memory if we need to.
154-
alternate: ?Fiber,
154+
alternate: Fiber | null,
155155

156156
// Conceptual aliases
157157
// workInProgress : Fiber -> alternate The alternate used for reuse happens
@@ -249,7 +249,7 @@ exports.cloneFiber = function(fiber : Fiber, priorityLevel : PriorityLevel) : Fi
249249
// objects for things that are never updated. It also allow us to reclaim the
250250
// extra memory if needed.
251251
let alt = fiber.alternate;
252-
if (alt) {
252+
if (alt !== null) {
253253
// If we clone, then we do so from the "current" state. The current state
254254
// can't have any side-effects that are still valid so we reset just to be
255255
// sure.

src/renderers/shared/fiber/ReactFiberBeginWork.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
102102
// We now have clones. Let's store them as the currently progressed work.
103103
workInProgress.progressedChild = workInProgress.child;
104104
workInProgress.progressedPriority = priorityLevel;
105-
if (current) {
105+
if (current !== null) {
106106
// We also store it on the current. When the alternate swaps in we can
107107
// continue from this point.
108108
current.progressedChild = workInProgress.progressedChild;
@@ -131,7 +131,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
131131
// At this point any memoization is no longer valid since we'll have changed
132132
// the children.
133133
workInProgress.memoizedProps = null;
134-
if (!current) {
134+
if (current === null) {
135135
// If this is a fresh new component that hasn't been rendered yet, we
136136
// won't update its child set by applying minimal side-effects. Instead,
137137
// we will add them all to the child before it gets rendered. That means
@@ -191,9 +191,9 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
191191
return workInProgress.child;
192192
}
193193

194-
function markRef(current : ?Fiber, workInProgress : Fiber) {
194+
function markRef(current : Fiber | null, workInProgress : Fiber) {
195195
const ref = workInProgress.ref;
196-
if (ref && (!current || current.ref !== ref)) {
196+
if (ref !== null && (!current || current.ref !== ref)) {
197197
// Schedule a Ref effect
198198
workInProgress.effectTag |= Ref;
199199
}
@@ -211,7 +211,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
211211
nextProps = memoizedProps;
212212
}
213213
} else {
214-
if (nextProps == null || memoizedProps === nextProps) {
214+
if (nextProps === null || memoizedProps === nextProps) {
215215
return bailoutOnAlreadyFinishedWork(current, workInProgress);
216216
}
217217
// TODO: Disable this before release, since it is not part of the public API
@@ -240,14 +240,14 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
240240
return workInProgress.child;
241241
}
242242

243-
function updateClassComponent(current : ?Fiber, workInProgress : Fiber, priorityLevel : PriorityLevel) {
243+
function updateClassComponent(current : Fiber | null, workInProgress : Fiber, priorityLevel : PriorityLevel) {
244244
// Push context providers early to prevent context stack mismatches.
245245
// During mounting we don't know the child context yet as the instance doesn't exist.
246246
// We will invalidate the child context in finishClassComponent() right after rendering.
247247
const hasContext = pushContextProvider(workInProgress);
248248

249249
let shouldUpdate;
250-
if (!current) {
250+
if (current === null) {
251251
if (!workInProgress.stateNode) {
252252
// In the initial pass we might need to construct the instance.
253253
constructClassInstance(workInProgress);
@@ -264,7 +264,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
264264
}
265265

266266
function finishClassComponent(
267-
current : ?Fiber,
267+
current : Fiber | null,
268268
workInProgress : Fiber,
269269
shouldUpdate : boolean,
270270
hasContext : boolean,
@@ -314,7 +314,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
314314
pushHostContainer(workInProgress, root.containerInfo);
315315

316316
const updateQueue = workInProgress.updateQueue;
317-
if (updateQueue) {
317+
if (updateQueue !== null) {
318318
const prevState = workInProgress.memoizedState;
319319
const state = beginUpdateQueue(workInProgress, updateQueue, null, prevState, null, priorityLevel);
320320
if (prevState === state) {
@@ -335,7 +335,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
335335
pushHostContext(workInProgress);
336336

337337
let nextProps = workInProgress.pendingProps;
338-
const prevProps = current ? current.memoizedProps : null;
338+
const prevProps = current !== null ? current.memoizedProps : null;
339339
const memoizedProps = workInProgress.memoizedProps;
340340
if (hasContextChanged()) {
341341
// Normally we can bail out on props equality but if context has changed
@@ -359,7 +359,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
359359
// that is a bit tricky since workInProgress and current can have
360360
// different "hidden" settings.
361361
let child = workInProgress.progressedChild;
362-
while (child) {
362+
while (child !== null) {
363363
// To ensure that this subtree gets its priority reset, the children
364364
// need to be reset.
365365
child.pendingWorkPriority = OffscreenPriority;
@@ -409,16 +409,16 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
409409
// Reconcile the children and stash them for later work.
410410
reconcileChildrenAtPriority(current, workInProgress, nextChildren, OffscreenPriority);
411411
memoizeProps(workInProgress, nextProps);
412-
workInProgress.child = current ? current.child : null;
412+
workInProgress.child = current !== null ? current.child : null;
413413

414-
if (!current) {
414+
if (current === null) {
415415
// If this doesn't have a current we won't track it for placement
416416
// effects. However, when we come back around to this we have already
417417
// inserted the parent which means that we'll infact need to make this a
418418
// placement.
419419
// TODO: There has to be a better solution to this problem.
420420
let child = workInProgress.progressedChild;
421-
while (child) {
421+
while (child !== null) {
422422
child.effectTag = Placement;
423423
child = child.sibling;
424424
}
@@ -464,7 +464,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
464464
value = fn(props, context);
465465
}
466466

467-
if (typeof value === 'object' && value && typeof value.render === 'function') {
467+
if (typeof value === 'object' && value !== null && typeof value.render === 'function') {
468468
// Proceed under the assumption that this is a class instance
469469
workInProgress.tag = ClassComponent;
470470

@@ -479,7 +479,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
479479
// Proceed under the assumption that this is a functional component
480480
workInProgress.tag = FunctionalComponent;
481481
if (__DEV__) {
482-
if (workInProgress.ref != null) {
482+
if (workInProgress.ref !== null) {
483483
let info = '';
484484
const ownerName = ReactDebugCurrentFiber.getCurrentFiberOwnerName();
485485
if (ownerName) {
@@ -517,7 +517,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
517517
if (nextCoroutine === null) {
518518
nextCoroutine = current && current.memoizedProps;
519519
invariant(
520-
nextCoroutine != null,
520+
nextCoroutine !== null,
521521
'We should always have pending or current props. This error is ' +
522522
'likely caused by a bug in React. Please file an issue.'
523523
);
@@ -538,7 +538,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
538538
// At this point any memoization is no longer valid since we'll have changed
539539
// the children.
540540
workInProgress.memoizedProps = null;
541-
if (!current) {
541+
if (current === null) {
542542
workInProgress.stateNode = mountChildFibersInPlace(
543543
workInProgress,
544544
workInProgress.stateNode,
@@ -592,7 +592,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
592592
return bailoutOnAlreadyFinishedWork(current, workInProgress);
593593
}
594594

595-
if (!current) {
595+
if (current === null) {
596596
// Portals are special because we don't append the children during mount
597597
// but at commit. Therefore we need to track insertions which the normal
598598
// flow doesn't do during mount. This doesn't happen at the root because
@@ -632,7 +632,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
632632
}
633633
*/
634634

635-
function bailoutOnAlreadyFinishedWork(current, workInProgress : Fiber) : ?Fiber {
635+
function bailoutOnAlreadyFinishedWork(current, workInProgress : Fiber) : Fiber | null {
636636
const priorityLevel = workInProgress.pendingWorkPriority;
637637
// TODO: We should ideally be able to bail out early if the children have no
638638
// more work to do. However, since we don't have a separation of this
@@ -687,7 +687,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
687687
// is handled by beginUpdateQueue.
688688
}
689689

690-
function beginWork(current : ?Fiber, workInProgress : Fiber, priorityLevel : PriorityLevel) : ?Fiber {
690+
function beginWork(current : Fiber | null, workInProgress : Fiber, priorityLevel : PriorityLevel) : Fiber | null {
691691
if (workInProgress.pendingWorkPriority === NoWork ||
692692
workInProgress.pendingWorkPriority > priorityLevel) {
693693
return bailoutOnLowPriority(current, workInProgress);
@@ -744,7 +744,7 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>(
744744
}
745745
}
746746

747-
function beginFailedWork(current : ?Fiber, workInProgress : Fiber, priorityLevel : PriorityLevel) {
747+
function beginFailedWork(current : Fiber | null, workInProgress : Fiber, priorityLevel : PriorityLevel) {
748748
invariant(
749749
workInProgress.tag === ClassComponent ||
750750
workInProgress.tag === HostRoot,

src/renderers/shared/fiber/ReactFiberClassComponent.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module.exports = function(
7171
};
7272

7373
function checkShouldComponentUpdate(workInProgress, oldProps, newProps, oldState, newState, newContext) {
74-
if (oldProps === null || (workInProgress.updateQueue && workInProgress.updateQueue.hasForceUpdate)) {
74+
if (oldProps === null || (workInProgress.updateQueue !== null && workInProgress.updateQueue.hasForceUpdate)) {
7575
// If the workInProgress already has an Update effect, return true
7676
return true;
7777
}
@@ -208,10 +208,10 @@ module.exports = function(
208208
workInProgress.effectTag |= Update;
209209
}
210210

211-
function markUpdateIfAlreadyInProgress(current: ?Fiber, workInProgress : Fiber) {
211+
function markUpdateIfAlreadyInProgress(current: Fiber | null, workInProgress : Fiber) {
212212
// If an update was already in progress, we should schedule an Update
213213
// effect even though we're bailing out, so that cWU/cDU are called.
214-
if (current) {
214+
if (current !== null) {
215215
if (workInProgress.memoizedProps !== current.memoizedProps ||
216216
workInProgress.memoizedState !== current.memoizedState) {
217217
markUpdate(workInProgress);
@@ -274,7 +274,7 @@ module.exports = function(
274274
// If we had additional state updates during this life-cycle, let's
275275
// process them now.
276276
const updateQueue = workInProgress.updateQueue;
277-
if (updateQueue) {
277+
if (updateQueue !== null) {
278278
instance.state = beginUpdateQueue(
279279
workInProgress,
280280
updateQueue,
@@ -343,7 +343,7 @@ module.exports = function(
343343
// They may be from componentWillMount() or from error boundary's setState()
344344
// during initial mounting.
345345
const newUpdateQueue = workInProgress.updateQueue;
346-
if (newUpdateQueue) {
346+
if (newUpdateQueue !== null) {
347347
newInstance.state = beginUpdateQueue(
348348
workInProgress,
349349
newUpdateQueue,
@@ -392,7 +392,7 @@ module.exports = function(
392392
const oldState = workInProgress.memoizedState;
393393
// TODO: Previous state can be null.
394394
let newState;
395-
if (updateQueue) {
395+
if (updateQueue !== null) {
396396
newState = beginUpdateQueue(
397397
workInProgress,
398398
updateQueue,
@@ -408,7 +408,7 @@ module.exports = function(
408408
if (oldProps === newProps &&
409409
oldState === newState &&
410410
!hasContextChanged() &&
411-
!(updateQueue && updateQueue.hasForceUpdate)) {
411+
!(updateQueue !== null && updateQueue.hasForceUpdate)) {
412412
markUpdateIfAlreadyInProgress(current, workInProgress);
413413
return false;
414414
}

0 commit comments

Comments
 (0)