From bcc05671fcdb159016fb64ab6f0b8f137fa3862c Mon Sep 17 00:00:00 2001 From: Jan Kassens Date: Mon, 3 Oct 2022 20:57:34 -0400 Subject: [PATCH] Flow: types first in shared (#25343) --- .../react/src/ReactServerContextRegistry.js | 9 +++++ packages/shared/ReactErrorUtils.js | 2 +- packages/shared/ReactServerContextRegistry.js | 6 ++- packages/shared/enqueueTask.js | 2 +- packages/shared/invokeGuardedCallbackImpl.js | 39 ++++++------------- scripts/flow/config/flowconfig | 2 +- 6 files changed, 29 insertions(+), 31 deletions(-) diff --git a/packages/react/src/ReactServerContextRegistry.js b/packages/react/src/ReactServerContextRegistry.js index dda738ac7439c..d0c1e67d5995e 100644 --- a/packages/react/src/ReactServerContextRegistry.js +++ b/packages/react/src/ReactServerContextRegistry.js @@ -1,3 +1,12 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + */ + import type {ReactServerContext} from 'shared/ReactTypes'; export const ContextRegistry: { diff --git a/packages/shared/ReactErrorUtils.js b/packages/shared/ReactErrorUtils.js index 5a33bfceae1c0..d1d9660c6ea3b 100644 --- a/packages/shared/ReactErrorUtils.js +++ b/packages/shared/ReactErrorUtils.js @@ -109,7 +109,7 @@ export function hasCaughtError(): boolean { return hasError; } -export function clearCaughtError() { +export function clearCaughtError(): mixed { if (hasError) { const error = caughtError; hasError = false; diff --git a/packages/shared/ReactServerContextRegistry.js b/packages/shared/ReactServerContextRegistry.js index d8f7c7082940e..9729865a238c1 100644 --- a/packages/shared/ReactServerContextRegistry.js +++ b/packages/shared/ReactServerContextRegistry.js @@ -7,13 +7,17 @@ * @flow */ +import type {ReactServerContext} from 'shared/ReactTypes'; + import {REACT_SERVER_CONTEXT_DEFAULT_VALUE_NOT_LOADED} from 'shared/ReactSymbols'; import ReactSharedInternals from 'shared/ReactSharedInternals'; import {createServerContext} from 'react'; const ContextRegistry = ReactSharedInternals.ContextRegistry; -export function getOrCreateServerContext(globalName: string) { +export function getOrCreateServerContext( + globalName: string, +): ReactServerContext { if (!ContextRegistry[globalName]) { ContextRegistry[globalName] = createServerContext( globalName, diff --git a/packages/shared/enqueueTask.js b/packages/shared/enqueueTask.js index d8fc646db2b26..95d11d613f5be 100644 --- a/packages/shared/enqueueTask.js +++ b/packages/shared/enqueueTask.js @@ -10,7 +10,7 @@ let didWarnAboutMessageChannel = false; let enqueueTaskImpl = null; -export default function enqueueTask(task: () => void) { +export default function enqueueTask(task: () => void): void { if (enqueueTaskImpl === null) { try { // read require off the module object to get around the bundlers. diff --git a/packages/shared/invokeGuardedCallbackImpl.js b/packages/shared/invokeGuardedCallbackImpl.js index 9fcfbaaef0e93..74f8bd6454f5c 100644 --- a/packages/shared/invokeGuardedCallbackImpl.js +++ b/packages/shared/invokeGuardedCallbackImpl.js @@ -7,26 +7,25 @@ * @flow */ -function invokeGuardedCallbackProd( +function invokeGuardedCallbackProd, Context>( name: string | null, - func: (a: A, b: B, c: C, d: D, e: E, f: F) => mixed, + func: (...Args) => mixed, context: Context, - a: A, - b: B, - c: C, - d: D, - e: E, - f: F, -) { +): void { const funcArgs = Array.prototype.slice.call(arguments, 3); try { + // $FlowFixMe[incompatible-call] Flow doesn't understand the arguments splicing. func.apply(context, funcArgs); } catch (error) { this.onError(error); } } -let invokeGuardedCallbackImpl = invokeGuardedCallbackProd; +let invokeGuardedCallbackImpl: , Context>( + name: string | null, + func: (...Args) => mixed, + context: Context, +) => void = invokeGuardedCallbackProd; if (__DEV__) { // In DEV mode, we swap out invokeGuardedCallback for a special version @@ -59,24 +58,9 @@ if (__DEV__) { const fakeNode = document.createElement('react'); invokeGuardedCallbackImpl = function invokeGuardedCallbackDev< - A, - B, - C, - D, - E, - F, + Args: Array, Context, - >( - name: string | null, - func: (a: A, b: B, c: C, d: D, e: E, f: F) => mixed, - context: Context, - a: A, - b: B, - c: C, - d: D, - e: E, - f: F, - ) { + >(name: string | null, func: (...Args) => mixed, context: Context): void { // If document doesn't exist we know for sure we will crash in this method // when we call document.createEvent(). However this can cause confusing // errors: https://github.com/facebook/create-react-app/issues/3482 @@ -142,6 +126,7 @@ if (__DEV__) { function callCallback() { didCall = true; restoreAfterDispatch(); + // $FlowFixMe[incompatible-call] Flow doesn't understand the arguments splicing. func.apply(context, funcArgs); didError = false; } diff --git a/scripts/flow/config/flowconfig b/scripts/flow/config/flowconfig index 404ac2d8b315e..68e1aef4a3408 100644 --- a/scripts/flow/config/flowconfig +++ b/scripts/flow/config/flowconfig @@ -82,7 +82,7 @@ well_formed_exports.includes=/packages/react-server-native-relay well_formed_exports.includes=/packages/react-suspense-test-utils ; well_formed_exports.includes=/packages/react-test-renderer well_formed_exports.includes=/packages/scheduler -; well_formed_exports.includes=/packages/shared +well_formed_exports.includes=/packages/shared well_formed_exports.includes=/packages/use-subscription well_formed_exports.includes=/packages/use-sync-external-store