Skip to content

Commit 25aa2e5

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 25aa2e5

File tree

1 file changed

+19
-34
lines changed

1 file changed

+19
-34
lines changed

packages/react-reconciler/src/ReactFiberHooks.js

Lines changed: 19 additions & 34 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
@@ -258,9 +258,7 @@ function checkDepsAreArrayDev(deps: mixed) {
258258

259259
function warnOnHookMismatchInDev(currentHookName: HookType) {
260260
if (__DEV__) {
261-
const componentName = getComponentName(
262-
((currentlyRenderingFiber: any): Fiber).type,
263-
);
261+
const componentName = getComponentName(currentlyRenderingFiber.type);
264262
if (!didWarnAboutMismatchedHooksForComponent.has(componentName)) {
265263
didWarnAboutMismatchedHooksForComponent.add(componentName);
266264

@@ -483,7 +481,7 @@ export function renderWithHooks(
483481
currentHook !== null && currentHook.next !== null;
484482
485483
renderExpirationTime = NoWork;
486-
currentlyRenderingFiber = null;
484+
currentlyRenderingFiber = (null: any);
487485
488486
currentHook = null;
489487
workInProgressHook = null;
@@ -529,7 +527,7 @@ export function resetHooks(): void {
529527
// component is a module-style component.
530528
531529
renderExpirationTime = NoWork;
532-
currentlyRenderingFiber = null;
530+
currentlyRenderingFiber = (null: any);
533531
534532
currentHook = null;
535533
workInProgressHook = null;
@@ -558,8 +556,7 @@ function mountWorkInProgressHook(): Hook {
558556
559557
if (workInProgressHook === null) {
560558
// This is the first hook in the list
561-
let fiber = ((currentlyRenderingFiber: any): Fiber);
562-
fiber.memoizedState = workInProgressHook = hook;
559+
currentlyRenderingFiber.memoizedState = workInProgressHook = hook;
563560
} else {
564561
// Append to the end of the list
565562
workInProgressHook = workInProgressHook.next = hook;
@@ -575,8 +572,7 @@ function updateWorkInProgressHook(): Hook {
575572
// the dispatcher used for mounts.
576573
let nextCurrentHook: null | Hook;
577574
if (currentHook === null) {
578-
let fiber = ((currentlyRenderingFiber: any): Fiber);
579-
let current = fiber.alternate;
575+
let current = currentlyRenderingFiber.alternate;
580576
if (current !== null) {
581577
nextCurrentHook = current.memoizedState;
582578
} else {
@@ -588,8 +584,7 @@ function updateWorkInProgressHook(): Hook {
588584
589585
let nextWorkInProgressHook: null | Hook;
590586
if (workInProgressHook === null) {
591-
let fiber = ((currentlyRenderingFiber: any): Fiber);
592-
nextWorkInProgressHook = fiber.memoizedState;
587+
nextWorkInProgressHook = currentlyRenderingFiber.memoizedState;
593588
} else {
594589
nextWorkInProgressHook = workInProgressHook.next;
595590
}
@@ -621,8 +616,7 @@ function updateWorkInProgressHook(): Hook {
621616
622617
if (workInProgressHook === null) {
623618
// This is the first hook in the list.
624-
let fiber = ((currentlyRenderingFiber: any): Fiber);
625-
fiber.memoizedState = workInProgressHook = newHook;
619+
currentlyRenderingFiber.memoizedState = workInProgressHook = newHook;
626620
} else {
627621
// Append to the end of the list.
628622
workInProgressHook = workInProgressHook.next = newHook;
@@ -662,8 +656,7 @@ function mountReducer<S, I, A>(
662656
});
663657
const dispatch: Dispatch<A> = (queue.dispatch = (dispatchAction.bind(
664658
null,
665-
// Flow doesn't know this is non-null, but we do.
666-
((currentlyRenderingFiber: any): Fiber),
659+
currentlyRenderingFiber,
667660
queue,
668661
): any));
669662
return [hook.memoizedState, dispatch];
@@ -762,9 +755,8 @@ function updateReducer<S, I, A>(
762755
newBaseState = newState;
763756
}
764757
// Update the remaining priority in the queue.
765-
let fiber = ((currentlyRenderingFiber: any): Fiber);
766-
if (updateExpirationTime > fiber.expirationTime) {
767-
fiber.expirationTime = updateExpirationTime;
758+
if (updateExpirationTime > currentlyRenderingFiber.expirationTime) {
759+
currentlyRenderingFiber.expirationTime = updateExpirationTime;
768760
markUnprocessedUpdateTime(updateExpirationTime);
769761
}
770762
} else {
@@ -835,8 +827,7 @@ function mountState<S>(
835827
BasicStateAction<S>,
836828
> = (queue.dispatch = (dispatchAction.bind(
837829
null,
838-
// Flow doesn't know this is non-null, but we do.
839-
((currentlyRenderingFiber: any): Fiber),
830+
currentlyRenderingFiber,
840831
queue,
841832
): any));
842833
return [hook.memoizedState, dispatch];
@@ -857,10 +848,10 @@ function pushEffect(tag, create, destroy, deps) {
857848
// Circular
858849
next: (null: any),
859850
};
860-
let fiber = ((currentlyRenderingFiber: any): Fiber);
861-
let componentUpdateQueue: null | FunctionComponentUpdateQueue = (fiber.updateQueue: any);
851+
let componentUpdateQueue: null | FunctionComponentUpdateQueue = (currentlyRenderingFiber.updateQueue: any);
862852
if (componentUpdateQueue === null) {
863-
(fiber: any).updateQueue = componentUpdateQueue = createFunctionComponentUpdateQueue();
853+
componentUpdateQueue = createFunctionComponentUpdateQueue();
854+
currentlyRenderingFiber.updateQueue = (componentUpdateQueue: any);
864855
componentUpdateQueue.lastEffect = effect.next = effect;
865856
} else {
866857
const lastEffect = componentUpdateQueue.lastEffect;
@@ -894,8 +885,7 @@ function updateRef<T>(initialValue: T): {current: T} {
894885
function mountEffectImpl(fiberEffectTag, hookEffectTag, create, deps): void {
895886
const hook = mountWorkInProgressHook();
896887
const nextDeps = deps === undefined ? null : deps;
897-
let fiber = ((currentlyRenderingFiber: any): Fiber);
898-
fiber.effectTag |= fiberEffectTag;
888+
currentlyRenderingFiber.effectTag |= fiberEffectTag;
899889
hook.memoizedState = pushEffect(hookEffectTag, create, undefined, nextDeps);
900890
}
901891

@@ -916,8 +906,7 @@ function updateEffectImpl(fiberEffectTag, hookEffectTag, create, deps): void {
916906
}
917907
}
918908

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

922911
hook.memoizedState = pushEffect(hookEffectTag, create, destroy, nextDeps);
923912
}
@@ -929,9 +918,7 @@ function mountEffect(
929918
if (__DEV__) {
930919
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
931920
if ('undefined' !== typeof jest) {
932-
warnIfNotCurrentlyActingEffectsInDEV(
933-
((currentlyRenderingFiber: any): Fiber),
934-
);
921+
warnIfNotCurrentlyActingEffectsInDEV(currentlyRenderingFiber);
935922
}
936923
}
937924
return mountEffectImpl(
@@ -949,9 +936,7 @@ function updateEffect(
949936
if (__DEV__) {
950937
// $FlowExpectedError - jest isn't a global, and isn't recognized outside of tests
951938
if ('undefined' !== typeof jest) {
952-
warnIfNotCurrentlyActingEffectsInDEV(
953-
((currentlyRenderingFiber: any): Fiber),
954-
);
939+
warnIfNotCurrentlyActingEffectsInDEV(currentlyRenderingFiber);
955940
}
956941
}
957942
return updateEffectImpl(

0 commit comments

Comments
 (0)