From 17f6912a44e2ce7b88a72488931428da58f19c56 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Fri, 2 Dec 2022 15:31:53 +0000 Subject: [PATCH] Add flow types to ReactFiberHooks (#25752) Increase type coverage in ReactFiberHooks file. --- .../react-reconciler/src/ReactFiberHooks.js | 81 +++++++++++-------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/packages/react-reconciler/src/ReactFiberHooks.js b/packages/react-reconciler/src/ReactFiberHooks.js index 8332587a1d444..cf3d34f4c4034 100644 --- a/packages/react-reconciler/src/ReactFiberHooks.js +++ b/packages/react-reconciler/src/ReactFiberHooks.js @@ -268,7 +268,7 @@ let hookTypesUpdateIndexDev: number = -1; // When true, such Hooks will always be "remounted". Only used during hot reload. let ignorePreviousDependencies: boolean = false; -function mountHookTypesDev() { +function mountHookTypesDev(): void { if (__DEV__) { const hookName = ((currentHookNameInDev: any): HookType); @@ -280,7 +280,7 @@ function mountHookTypesDev() { } } -function updateHookTypesDev() { +function updateHookTypesDev(): void { if (__DEV__) { const hookName = ((currentHookNameInDev: any): HookType); @@ -293,7 +293,7 @@ function updateHookTypesDev() { } } -function checkDepsAreArrayDev(deps: mixed) { +function checkDepsAreArrayDev(deps: mixed): void { if (__DEV__) { if (deps !== undefined && deps !== null && !isArray(deps)) { // Verify deps, but only on mount to avoid extra checks. @@ -308,7 +308,7 @@ function checkDepsAreArrayDev(deps: mixed) { } } -function warnOnHookMismatchInDev(currentHookName: HookType) { +function warnOnHookMismatchInDev(currentHookName: HookType): void { if (__DEV__) { const componentName = getComponentNameFromFiber(currentlyRenderingFiber); if (!didWarnAboutMismatchedHooksForComponent.has(componentName)) { @@ -369,7 +369,7 @@ function throwInvalidHookError() { function areHookInputsEqual( nextDeps: Array, prevDeps: Array | null, -) { +): boolean { if (__DEV__) { if (ignorePreviousDependencies) { // Only true when this component is being hot reloaded. @@ -681,7 +681,7 @@ function renderWithHooksAgain( Component: (p: Props, arg: SecondArg) => any, props: Props, secondArg: SecondArg, -) { +): any { // This is used to perform another render pass. It's used when setState is // called during render, and for double invoking components in Strict Mode // during development. @@ -745,7 +745,7 @@ export function bailoutHooks( current: Fiber, workInProgress: Fiber, lanes: Lanes, -) { +): void { workInProgress.updateQueue = current.updateQueue; // TODO: Don't need to reset the flags here, because they're reset in the // complete phase (bubbleProperties). @@ -1732,7 +1732,7 @@ function pushStoreConsistencyCheck( fiber: Fiber, getSnapshot: () => T, renderedSnapshot: T, -) { +): void { fiber.flags |= StoreConsistency; const check: StoreConsistencyCheck = { getSnapshot, @@ -1758,7 +1758,7 @@ function updateStoreInstance( inst: StoreInstance, nextSnapshot: T, getSnapshot: () => T, -) { +): void { // These are updated in the passive phase inst.value = nextSnapshot; inst.getSnapshot = getSnapshot; @@ -1773,7 +1773,11 @@ function updateStoreInstance( } } -function subscribeToStore(fiber, inst: StoreInstance, subscribe) { +function subscribeToStore( + fiber: Fiber, + inst: StoreInstance, + subscribe: (() => void) => () => void, +): any { const handleStoreChange = () => { // The store changed. Check if the snapshot changed since the last time we // read from the store. @@ -1843,7 +1847,12 @@ function rerenderState( return rerenderReducer(basicStateReducer, (initialState: any)); } -function pushEffect(tag, create, destroy, deps: Array | void | null) { +function pushEffect( + tag: HookFlags, + create: () => (() => void) | void, + destroy: (() => void) | void, + deps: Array | void | null, +): Effect { const effect: Effect = { tag, create, @@ -1963,9 +1972,9 @@ function updateRef(initialValue: T): {current: T} { } function mountEffectImpl( - fiberFlags, - hookFlags, - create, + fiberFlags: Flags, + hookFlags: HookFlags, + create: () => (() => void) | void, deps: Array | void | null, ): void { const hook = mountWorkInProgressHook(); @@ -1980,9 +1989,9 @@ function mountEffectImpl( } function updateEffectImpl( - fiberFlags, - hookFlags, - create, + fiberFlags: Flags, + hookFlags: HookFlags, + create: () => (() => void) | void, deps: Array | void | null, ): void { const hook = updateWorkInProgressHook(); @@ -2019,14 +2028,14 @@ function mountEffect( __DEV__ && (currentlyRenderingFiber.mode & StrictEffectsMode) !== NoMode ) { - return mountEffectImpl( + mountEffectImpl( MountPassiveDevEffect | PassiveEffect | PassiveStaticEffect, HookPassive, create, deps, ); } else { - return mountEffectImpl( + mountEffectImpl( PassiveEffect | PassiveStaticEffect, HookPassive, create, @@ -2039,7 +2048,7 @@ function updateEffect( create: () => (() => void) | void, deps: Array | void | null, ): void { - return updateEffectImpl(PassiveEffect, HookPassive, create, deps); + updateEffectImpl(PassiveEffect, HookPassive, create, deps); } function useEventImpl) => Return>( @@ -2099,7 +2108,7 @@ function mountInsertionEffect( create: () => (() => void) | void, deps: Array | void | null, ): void { - return mountEffectImpl(UpdateEffect, HookInsertion, create, deps); + mountEffectImpl(UpdateEffect, HookInsertion, create, deps); } function updateInsertionEffect( @@ -2133,7 +2142,7 @@ function updateLayoutEffect( function imperativeHandleEffect( create: () => T, ref: {current: T | null} | ((inst: T | null) => mixed) | null | void, -) { +): void | (() => void) { if (typeof ref === 'function') { const refCallback = ref; const inst = create(); @@ -2186,7 +2195,7 @@ function mountImperativeHandle( ) { fiberFlags |= MountLayoutDevEffect; } - return mountEffectImpl( + mountEffectImpl( fiberFlags, HookLayout, imperativeHandleEffect.bind(null, create, ref), @@ -2213,7 +2222,7 @@ function updateImperativeHandle( const effectDeps = deps !== null && deps !== undefined ? deps.concat([ref]) : null; - return updateEffectImpl( + updateEffectImpl( UpdateEffect, HookLayout, imperativeHandleEffect.bind(null, create, ref), @@ -2361,7 +2370,11 @@ function updateDeferredValueImpl(hook: Hook, prevValue: T, value: T): T { } } -function startTransition(setPending, callback, options) { +function startTransition( + setPending: boolean => void, + callback: () => void, + options?: StartTransitionOptions, +): void { const previousPriority = getCurrentUpdatePriority(); setCurrentUpdatePriority( higherEventPriority(previousPriority, ContinuousEventPriority), @@ -2483,7 +2496,7 @@ function updateId(): string { return id; } -function mountRefresh() { +function mountRefresh(): any { const hook = mountWorkInProgressHook(); const refresh = (hook.memoizedState = refreshCache.bind( null, @@ -2492,12 +2505,12 @@ function mountRefresh() { return refresh; } -function updateRefresh() { +function updateRefresh(): any { const hook = updateWorkInProgressHook(); return hook.memoizedState; } -function refreshCache(fiber: Fiber, seedKey: ?() => T, seedValue: T) { +function refreshCache(fiber: Fiber, seedKey: ?() => T, seedValue: T): void { if (!enableCache) { return; } @@ -2553,7 +2566,7 @@ function dispatchReducerAction( fiber: Fiber, queue: UpdateQueue, action: A, -) { +): void { if (__DEV__) { if (typeof arguments[3] === 'function') { console.error( @@ -2592,7 +2605,7 @@ function dispatchSetState( fiber: Fiber, queue: UpdateQueue, action: A, -) { +): void { if (__DEV__) { if (typeof arguments[3] === 'function') { console.error( @@ -2670,7 +2683,7 @@ function dispatchSetState( markUpdateInDevTools(fiber, lane, action); } -function isRenderPhaseUpdate(fiber: Fiber) { +function isRenderPhaseUpdate(fiber: Fiber): boolean { const alternate = fiber.alternate; return ( fiber === currentlyRenderingFiber || @@ -2681,7 +2694,7 @@ function isRenderPhaseUpdate(fiber: Fiber) { function enqueueRenderPhaseUpdate( queue: UpdateQueue, update: Update, -) { +): void { // This is a render phase update. Stash it in a lazily-created map of // queue -> linked list of updates. After this render pass, we'll restart // and apply the stashed updates on top of the work-in-progress hook. @@ -2702,7 +2715,7 @@ function entangleTransitionUpdate( root: FiberRoot, queue: UpdateQueue, lane: Lane, -) { +): void { if (isTransitionLane(lane)) { let queueLanes = queue.lanes; @@ -2723,7 +2736,7 @@ function entangleTransitionUpdate( } } -function markUpdateInDevTools(fiber, lane, action: A) { +function markUpdateInDevTools(fiber, lane, action: A): void { if (__DEV__) { if (enableDebugTracing) { if (fiber.mode & DebugTracingMode) {