Skip to content

Commit e73d0f2

Browse files
committed
Assert that currentlyRenderingFiber is always set
When accessed, this should always be set. This could enforced by storing this on the dispatcher for example.
1 parent cc1aa9d commit e73d0f2

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

packages/react-reconciler/src/ReactFiberHooks.js

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ type Dispatch<A> = A => void;
174174
let renderExpirationTime: ExpirationTime = NoWork;
175175
// The work-in-progress fiber. I've named it differently to distinguish it from
176176
// the work-in-progress hook.
177-
let currentlyRenderingFiber: Fiber | null = null;
177+
let currentlyRenderingFiber: Fiber = (null: any);
178178

179179
// Hooks are stored as a linked list on the fiber's memoizedState field. The
180180
// current hook list is the list that belongs to the current fiber. The
@@ -259,7 +259,7 @@ function checkDepsAreArrayDev(deps: mixed) {
259259
function warnOnHookMismatchInDev(currentHookName: HookType) {
260260
if (__DEV__) {
261261
const componentName = getComponentName(
262-
((currentlyRenderingFiber: any): Fiber).type,
262+
currentlyRenderingFiber.type,
263263
);
264264
if (!didWarnAboutMismatchedHooksForComponent.has(componentName)) {
265265
didWarnAboutMismatchedHooksForComponent.add(componentName);
@@ -483,7 +483,7 @@ export function renderWithHooks(
483483
currentHook !== null && currentHook.next !== null;
484484
485485
renderExpirationTime = NoWork;
486-
currentlyRenderingFiber = null;
486+
currentlyRenderingFiber = (null: any);
487487
488488
currentHook = null;
489489
workInProgressHook = null;
@@ -529,7 +529,7 @@ export function resetHooks(): void {
529529
// component is a module-style component.
530530
531531
renderExpirationTime = NoWork;
532-
currentlyRenderingFiber = null;
532+
currentlyRenderingFiber = (null: any);
533533
534534
currentHook = null;
535535
workInProgressHook = null;
@@ -558,8 +558,7 @@ function mountWorkInProgressHook(): Hook {
558558
559559
if (workInProgressHook === null) {
560560
// This is the first hook in the list
561-
let fiber = ((currentlyRenderingFiber: any): Fiber);
562-
fiber.memoizedState = workInProgressHook = hook;
561+
currentlyRenderingFiber.memoizedState = workInProgressHook = hook;
563562
} else {
564563
// Append to the end of the list
565564
workInProgressHook = workInProgressHook.next = hook;
@@ -575,8 +574,7 @@ function updateWorkInProgressHook(): Hook {
575574
// the dispatcher used for mounts.
576575
let nextCurrentHook: null | Hook;
577576
if (currentHook === null) {
578-
let fiber = ((currentlyRenderingFiber: any): Fiber);
579-
let current = fiber.alternate;
577+
let current = currentlyRenderingFiber.alternate;
580578
if (current !== null) {
581579
nextCurrentHook = current.memoizedState;
582580
} else {
@@ -588,8 +586,7 @@ function updateWorkInProgressHook(): Hook {
588586
589587
let nextWorkInProgressHook: null | Hook;
590588
if (workInProgressHook === null) {
591-
let fiber = ((currentlyRenderingFiber: any): Fiber);
592-
nextWorkInProgressHook = fiber.memoizedState;
589+
nextWorkInProgressHook = currentlyRenderingFiber.memoizedState;
593590
} else {
594591
nextWorkInProgressHook = workInProgressHook.next;
595592
}
@@ -621,8 +618,7 @@ function updateWorkInProgressHook(): Hook {
621618
622619
if (workInProgressHook === null) {
623620
// This is the first hook in the list.
624-
let fiber = ((currentlyRenderingFiber: any): Fiber);
625-
fiber.memoizedState = workInProgressHook = newHook;
621+
currentlyRenderingFiber.memoizedState = workInProgressHook = newHook;
626622
} else {
627623
// Append to the end of the list.
628624
workInProgressHook = workInProgressHook.next = newHook;
@@ -662,8 +658,7 @@ function mountReducer<S, I, A>(
662658
});
663659
const dispatch: Dispatch<A> = (queue.dispatch = (dispatchAction.bind(
664660
null,
665-
// Flow doesn't know this is non-null, but we do.
666-
((currentlyRenderingFiber: any): Fiber),
661+
currentlyRenderingFiber,
667662
queue,
668663
): any));
669664
return [hook.memoizedState, dispatch];
@@ -762,9 +757,8 @@ function updateReducer<S, I, A>(
762757
newBaseState = newState;
763758
}
764759
// Update the remaining priority in the queue.
765-
let fiber = ((currentlyRenderingFiber: any): Fiber);
766-
if (updateExpirationTime > fiber.expirationTime) {
767-
fiber.expirationTime = updateExpirationTime;
760+
if (updateExpirationTime > currentlyRenderingFiber.expirationTime) {
761+
currentlyRenderingFiber.expirationTime = updateExpirationTime;
768762
markUnprocessedUpdateTime(updateExpirationTime);
769763
}
770764
} else {
@@ -835,8 +829,7 @@ function mountState<S>(
835829
BasicStateAction<S>,
836830
> = (queue.dispatch = (dispatchAction.bind(
837831
null,
838-
// Flow doesn't know this is non-null, but we do.
839-
((currentlyRenderingFiber: any): Fiber),
832+
currentlyRenderingFiber,
840833
queue,
841834
): any));
842835
return [hook.memoizedState, dispatch];
@@ -857,10 +850,10 @@ function pushEffect(tag, create, destroy, deps) {
857850
// Circular
858851
next: (null: any),
859852
};
860-
let fiber = ((currentlyRenderingFiber: any): Fiber);
861-
let componentUpdateQueue: null | FunctionComponentUpdateQueue = (fiber.updateQueue: any);
853+
let componentUpdateQueue: null | FunctionComponentUpdateQueue = (currentlyRenderingFiber.updateQueue: any);
862854
if (componentUpdateQueue === null) {
863-
(fiber: any).updateQueue = componentUpdateQueue = createFunctionComponentUpdateQueue();
855+
componentUpdateQueue = createFunctionComponentUpdateQueue();
856+
currentlyRenderingFiber.updateQueue = (componentUpdateQueue: any);
864857
componentUpdateQueue.lastEffect = effect.next = effect;
865858
} else {
866859
const lastEffect = componentUpdateQueue.lastEffect;
@@ -894,8 +887,7 @@ function updateRef<T>(initialValue: T): {current: T} {
894887
function mountEffectImpl(fiberEffectTag, hookEffectTag, create, deps): void {
895888
const hook = mountWorkInProgressHook();
896889
const nextDeps = deps === undefined ? null : deps;
897-
let fiber = ((currentlyRenderingFiber: any): Fiber);
898-
fiber.effectTag |= fiberEffectTag;
890+
currentlyRenderingFiber.effectTag |= fiberEffectTag;
899891
hook.memoizedState = pushEffect(hookEffectTag, create, undefined, nextDeps);
900892
}
901893

@@ -916,8 +908,7 @@ function updateEffectImpl(fiberEffectTag, hookEffectTag, create, deps): void {
916908
}
917909
}
918910

919-
let fiber = ((currentlyRenderingFiber: any): Fiber);
920-
fiber.effectTag |= fiberEffectTag;
911+
currentlyRenderingFiber.effectTag |= fiberEffectTag;
921912

922913
hook.memoizedState = pushEffect(hookEffectTag, create, destroy, nextDeps);
923914
}
@@ -930,7 +921,7 @@ function mountEffect(
930921
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
931922
if ('undefined' !== typeof jest) {
932923
warnIfNotCurrentlyActingEffectsInDEV(
933-
((currentlyRenderingFiber: any): Fiber),
924+
currentlyRenderingFiber,
934925
);
935926
}
936927
}
@@ -950,7 +941,7 @@ function updateEffect(
950941
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
951942
if ('undefined' !== typeof jest) {
952943
warnIfNotCurrentlyActingEffectsInDEV(
953-
((currentlyRenderingFiber: any): Fiber),
944+
currentlyRenderingFiber,
954945
);
955946
}
956947
}

0 commit comments

Comments
 (0)