@samitouri / QOS-React-2 / commits / 17f6912a44

Add flow types to ReactFiberHooks (#25752)

Increase type coverage in ReactFiberHooks file.

Samuel Susla committed Dec 2, 2022 at 15:31 UTC 17f6912a44e2ce7b88a72488931428da58f19c56
1 file changed +47 -34
packages/react-reconciler/src/ReactFiberHooks.js
+47 -34
@@ -268,7 +268,7 @@ let hookTypesUpdateIndexDev: number = -1;
268 // When true, such Hooks will always be "remounted". Only used during hot reload.
269 let ignorePreviousDependencies: boolean = false;
270
271 -function mountHookTypesDev() {
271 +function mountHookTypesDev(): void {
272 if (__DEV__) {
273 const hookName = ((currentHookNameInDev: any): HookType);
274
@@ -280,7 +280,7 @@ function mountHookTypesDev() {
280 }
281 }
282
283 -function updateHookTypesDev() {
283 +function updateHookTypesDev(): void {
284 if (__DEV__) {
285 const hookName = ((currentHookNameInDev: any): HookType);
286
@@ -293,7 +293,7 @@ function updateHookTypesDev() {
293 }
294 }
295
296 -function checkDepsAreArrayDev(deps: mixed) {
296 +function checkDepsAreArrayDev(deps: mixed): void {
297 if (__DEV__) {
298 if (deps !== undefined && deps !== null && !isArray(deps)) {
299 // Verify deps, but only on mount to avoid extra checks.
@@ -308,7 +308,7 @@ function checkDepsAreArrayDev(deps: mixed) {
308 }
309 }
310
311 -function warnOnHookMismatchInDev(currentHookName: HookType) {
311 +function warnOnHookMismatchInDev(currentHookName: HookType): void {
312 if (__DEV__) {
313 const componentName = getComponentNameFromFiber(currentlyRenderingFiber);
314 if (!didWarnAboutMismatchedHooksForComponent.has(componentName)) {
@@ -369,7 +369,7 @@ function throwInvalidHookError() {
369 function areHookInputsEqual(
370 nextDeps: Array<mixed>,
371 prevDeps: Array<mixed> | null,
372 -) {
372 +): boolean {
373 if (__DEV__) {
374 if (ignorePreviousDependencies) {
375 // Only true when this component is being hot reloaded.
@@ -681,7 +681,7 @@ function renderWithHooksAgain<Props, SecondArg>(
681 Component: (p: Props, arg: SecondArg) => any,
682 props: Props,
683 secondArg: SecondArg,
684 -) {
684 +): any {
685 // This is used to perform another render pass. It's used when setState is
686 // called during render, and for double invoking components in Strict Mode
687 // during development.
@@ -745,7 +745,7 @@ export function bailoutHooks(
745 current: Fiber,
746 workInProgress: Fiber,
747 lanes: Lanes,
748 -) {
748 +): void {
749 workInProgress.updateQueue = current.updateQueue;
750 // TODO: Don't need to reset the flags here, because they're reset in the
751 // complete phase (bubbleProperties).
@@ -1732,7 +1732,7 @@ function pushStoreConsistencyCheck<T>(
1732 fiber: Fiber,
1733 getSnapshot: () => T,
1734 renderedSnapshot: T,
1735 -) {
1735 +): void {
1736 fiber.flags |= StoreConsistency;
1737 const check: StoreConsistencyCheck<T> = {
1738 getSnapshot,
@@ -1758,7 +1758,7 @@ function updateStoreInstance<T>(
1758 inst: StoreInstance<T>,
1759 nextSnapshot: T,
1760 getSnapshot: () => T,
1761 -) {
1761 +): void {
1762 // These are updated in the passive phase
1763 inst.value = nextSnapshot;
1764 inst.getSnapshot = getSnapshot;
@@ -1773,7 +1773,11 @@ function updateStoreInstance<T>(
1773 }
1774 }
1775
1776 -function subscribeToStore<T>(fiber, inst: StoreInstance<T>, subscribe) {
1776 +function subscribeToStore<T>(
1777 + fiber: Fiber,
1778 + inst: StoreInstance<T>,
1779 + subscribe: (() => void) => () => void,
1780 +): any {
1781 const handleStoreChange = () => {
1782 // The store changed. Check if the snapshot changed since the last time we
1783 // read from the store.
@@ -1843,7 +1847,12 @@ function rerenderState<S>(
1847 return rerenderReducer(basicStateReducer, (initialState: any));
1848 }
1849
1846 -function pushEffect(tag, create, destroy, deps: Array<mixed> | void | null) {
1850 +function pushEffect(
1851 + tag: HookFlags,
1852 + create: () => (() => void) | void,
1853 + destroy: (() => void) | void,
1854 + deps: Array<mixed> | void | null,
1855 +): Effect {
1856 const effect: Effect = {
1857 tag,
1858 create,
@@ -1963,9 +1972,9 @@ function updateRef<T>(initialValue: T): {current: T} {
1972 }
1973
1974 function mountEffectImpl(
1966 - fiberFlags,
1967 - hookFlags,
1968 - create,
1975 + fiberFlags: Flags,
1976 + hookFlags: HookFlags,
1977 + create: () => (() => void) | void,
1978 deps: Array<mixed> | void | null,
1979 ): void {
1980 const hook = mountWorkInProgressHook();
@@ -1980,9 +1989,9 @@ function mountEffectImpl(
1989 }
1990
1991 function updateEffectImpl(
1983 - fiberFlags,
1984 - hookFlags,
1985 - create,
1992 + fiberFlags: Flags,
1993 + hookFlags: HookFlags,
1994 + create: () => (() => void) | void,
1995 deps: Array<mixed> | void | null,
1996 ): void {
1997 const hook = updateWorkInProgressHook();
@@ -2019,14 +2028,14 @@ function mountEffect(
2028 __DEV__ &&
2029 (currentlyRenderingFiber.mode & StrictEffectsMode) !== NoMode
2030 ) {
2022 - return mountEffectImpl(
2031 + mountEffectImpl(
2032 MountPassiveDevEffect | PassiveEffect | PassiveStaticEffect,
2033 HookPassive,
2034 create,
2035 deps,
2036 );
2037 } else {
2029 - return mountEffectImpl(
2038 + mountEffectImpl(
2039 PassiveEffect | PassiveStaticEffect,
2040 HookPassive,
2041 create,
@@ -2039,7 +2048,7 @@ function updateEffect(
2048 create: () => (() => void) | void,
2049 deps: Array<mixed> | void | null,
2050 ): void {
2042 - return updateEffectImpl(PassiveEffect, HookPassive, create, deps);
2051 + updateEffectImpl(PassiveEffect, HookPassive, create, deps);
2052 }
2053
2054 function useEventImpl<Args, Return, F: (...Array<Args>) => Return>(
@@ -2099,7 +2108,7 @@ function mountInsertionEffect(
2108 create: () => (() => void) | void,
2109 deps: Array<mixed> | void | null,
2110 ): void {
2102 - return mountEffectImpl(UpdateEffect, HookInsertion, create, deps);
2111 + mountEffectImpl(UpdateEffect, HookInsertion, create, deps);
2112 }
2113
2114 function updateInsertionEffect(
@@ -2133,7 +2142,7 @@ function updateLayoutEffect(
2142 function imperativeHandleEffect<T>(
2143 create: () => T,
2144 ref: {current: T | null} | ((inst: T | null) => mixed) | null | void,
2136 -) {
2145 +): void | (() => void) {
2146 if (typeof ref === 'function') {
2147 const refCallback = ref;
2148 const inst = create();
@@ -2186,7 +2195,7 @@ function mountImperativeHandle<T>(
2195 ) {
2196 fiberFlags |= MountLayoutDevEffect;
2197 }
2189 - return mountEffectImpl(
2198 + mountEffectImpl(
2199 fiberFlags,
2200 HookLayout,
2201 imperativeHandleEffect.bind(null, create, ref),
@@ -2213,7 +2222,7 @@ function updateImperativeHandle<T>(
2222 const effectDeps =
2223 deps !== null && deps !== undefined ? deps.concat([ref]) : null;
2224
2216 - return updateEffectImpl(
2225 + updateEffectImpl(
2226 UpdateEffect,
2227 HookLayout,
2228 imperativeHandleEffect.bind(null, create, ref),
@@ -2361,7 +2370,11 @@ function updateDeferredValueImpl<T>(hook: Hook, prevValue: T, value: T): T {
2370 }
2371 }
2372
2364 -function startTransition(setPending, callback, options) {
2373 +function startTransition(
2374 + setPending: boolean => void,
2375 + callback: () => void,
2376 + options?: StartTransitionOptions,
2377 +): void {
2378 const previousPriority = getCurrentUpdatePriority();
2379 setCurrentUpdatePriority(
2380 higherEventPriority(previousPriority, ContinuousEventPriority),
@@ -2483,7 +2496,7 @@ function updateId(): string {
2496 return id;
2497 }
2498
2486 -function mountRefresh() {
2499 +function mountRefresh(): any {
2500 const hook = mountWorkInProgressHook();
2501 const refresh = (hook.memoizedState = refreshCache.bind(
2502 null,
@@ -2492,12 +2505,12 @@ function mountRefresh() {
2505 return refresh;
2506 }
2507
2495 -function updateRefresh() {
2508 +function updateRefresh(): any {
2509 const hook = updateWorkInProgressHook();
2510 return hook.memoizedState;
2511 }
2512
2500 -function refreshCache<T>(fiber: Fiber, seedKey: ?() => T, seedValue: T) {
2513 +function refreshCache<T>(fiber: Fiber, seedKey: ?() => T, seedValue: T): void {
2514 if (!enableCache) {
2515 return;
2516 }
@@ -2553,7 +2566,7 @@ function dispatchReducerAction<S, A>(
2566 fiber: Fiber,
2567 queue: UpdateQueue<S, A>,
2568 action: A,
2556 -) {
2569 +): void {
2570 if (__DEV__) {
2571 if (typeof arguments[3] === 'function') {
2572 console.error(
@@ -2592,7 +2605,7 @@ function dispatchSetState<S, A>(
2605 fiber: Fiber,
2606 queue: UpdateQueue<S, A>,
2607 action: A,
2595 -) {
2608 +): void {
2609 if (__DEV__) {
2610 if (typeof arguments[3] === 'function') {
2611 console.error(
@@ -2670,7 +2683,7 @@ function dispatchSetState<S, A>(
2683 markUpdateInDevTools(fiber, lane, action);
2684 }
2685
2673 -function isRenderPhaseUpdate(fiber: Fiber) {
2686 +function isRenderPhaseUpdate(fiber: Fiber): boolean {
2687 const alternate = fiber.alternate;
2688 return (
2689 fiber === currentlyRenderingFiber ||
@@ -2681,7 +2694,7 @@ function isRenderPhaseUpdate(fiber: Fiber) {
2694 function enqueueRenderPhaseUpdate<S, A>(
2695 queue: UpdateQueue<S, A>,
2696 update: Update<S, A>,
2684 -) {
2697 +): void {
2698 // This is a render phase update. Stash it in a lazily-created map of
2699 // queue -> linked list of updates. After this render pass, we'll restart
2700 // and apply the stashed updates on top of the work-in-progress hook.
@@ -2702,7 +2715,7 @@ function entangleTransitionUpdate<S, A>(
2715 root: FiberRoot,
2716 queue: UpdateQueue<S, A>,
2717 lane: Lane,
2705 -) {
2718 +): void {
2719 if (isTransitionLane(lane)) {
2720 let queueLanes = queue.lanes;
2721
@@ -2723,7 +2736,7 @@ function entangleTransitionUpdate<S, A>(
2736 }
2737 }
2738
2726 -function markUpdateInDevTools<A>(fiber, lane, action: A) {
2739 +function markUpdateInDevTools<A>(fiber, lane, action: A): void {
2740 if (__DEV__) {
2741 if (enableDebugTracing) {
2742 if (fiber.mode & DebugTracingMode) {