Skip to content

Commit

Permalink
[flow] enable enforce_local_inference_annotations (#25921)
Browse files Browse the repository at this point in the history
This setting is an incremental path to the next Flow version enforcing
type annotations on most functions (except some inline callbacks).

Used
```
node_modules/.bin/flow codemod annotate-functions-and-classes --write .
```
to add a majority of the types with some hand cleanup when for large
inferred objects that should just be `Fiber` or weird constructs
including `any`.

Suppressed the remaining issues.

Builds on #25918
  • Loading branch information
kassens authored Jan 9, 2023
1 parent 0b97441 commit 0b4f443
Show file tree
Hide file tree
Showing 144 changed files with 689 additions and 371 deletions.
2 changes: 1 addition & 1 deletion packages/jest-react/src/internalAct.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export function act<T>(scope: () => Thenable<T> | T): Thenable<T> {
}
}

function flushActWork(resolve, reject) {
function flushActWork(resolve: () => void, reject: (error: any) => void) {
if (Scheduler.unstable_hasPendingWork()) {
try {
Scheduler.unstable_flushUntilNextPaint();
Expand Down
7 changes: 4 additions & 3 deletions packages/react-cache/src/ReactCacheOld.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow
*/

import type {Thenable} from 'shared/ReactTypes';
import type {ReactContext, Thenable} from 'shared/ReactTypes';

import * as React from 'react';

Expand Down Expand Up @@ -48,7 +48,7 @@ const ReactCurrentDispatcher =
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.ReactCurrentDispatcher;

function readContext(Context) {
function readContext(Context: ReactContext<mixed>) {
const dispatcher = ReactCurrentDispatcher.current;
if (dispatcher === null) {
// This wasn't being minified but we're going to retire this package anyway.
Expand All @@ -62,6 +62,7 @@ function readContext(Context) {
return dispatcher.readContext(Context);
}

// $FlowFixMe[missing-local-annot]
function identityHashFn(input) {
if (__DEV__) {
if (
Expand Down Expand Up @@ -133,7 +134,7 @@ function accessResult<I, K, V>(
}
}

function deleteEntry(resource, key) {
function deleteEntry(resource: any, key: mixed) {
const entriesForResource = entries.get(resource);
if (entriesForResource !== undefined) {
entriesForResource.delete(key);
Expand Down
11 changes: 9 additions & 2 deletions packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type SomeChunk<T> =
| InitializedChunk<T>
| ErroredChunk<T>;

// $FlowFixMe[missing-this-annot]
function Chunk(status: any, value: any, reason: any, response: Response) {
this.status = status;
this.value = value;
Expand All @@ -104,6 +105,7 @@ function Chunk(status: any, value: any, reason: any, response: Response) {
Chunk.prototype = (Object.create(Promise.prototype): any);
// TODO: This doesn't return a new Promise chain unlike the real .then
Chunk.prototype.then = function<T>(
this: SomeChunk<T>,
resolve: (value: T) => mixed,
reject: (reason: mixed) => mixed,
) {
Expand Down Expand Up @@ -369,7 +371,11 @@ export function reportGlobalError(response: Response, error: Error): void {
});
}

function createElement(type, key, props): React$Element<any> {
function createElement(
type: mixed,
key: mixed,
props: mixed,
): React$Element<any> {
const element: any = {
// This tag allows us to uniquely identify this as a React Element
$$typeof: REACT_ELEMENT_TYPE,
Expand Down Expand Up @@ -446,6 +452,7 @@ function createModelResolver<T>(
value: null,
};
}
// $FlowFixMe[missing-local-annot]
return value => {
parentObject[key] = value;
blocked.deps--;
Expand All @@ -465,7 +472,7 @@ function createModelResolver<T>(
}

function createModelReject<T>(chunk: SomeChunk<T>) {
return error => triggerErrorOnChunk(chunk, error);
return (error: mixed) => triggerErrorOnChunk(chunk, error);
}

export function parseModelString(
Expand Down
1 change: 1 addition & 0 deletions packages/react-client/src/ReactFlightClientStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export function processBinaryChunk(
}

function createFromJSONCallback(response: Response) {
// $FlowFixMe[missing-this-annot]
return function(key: string, value: JSONValue) {
if (typeof value === 'string') {
// We can't use .bind here because we need the "this" value.
Expand Down
18 changes: 9 additions & 9 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ const Dispatcher: DispatcherType = {
// create a proxy to throw a custom error
// in case future versions of React adds more hooks
const DispatcherProxyHandler = {
get(target, prop) {
get(target: DispatcherType, prop: string) {
if (target.hasOwnProperty(prop)) {
return target[prop];
}
Expand Down Expand Up @@ -404,7 +404,7 @@ export type HooksTree = Array<HooksNode>;

let mostLikelyAncestorIndex = 0;

function findSharedIndex(hookStack, rootStack, rootIndex) {
function findSharedIndex(hookStack: any, rootStack: any, rootIndex: number) {
const source = rootStack[rootIndex].source;
hookSearch: for (let i = 0; i < hookStack.length; i++) {
if (hookStack[i].source === source) {
Expand All @@ -425,7 +425,7 @@ function findSharedIndex(hookStack, rootStack, rootIndex) {
return -1;
}

function findCommonAncestorIndex(rootStack, hookStack) {
function findCommonAncestorIndex(rootStack: any, hookStack: any) {
let rootIndex = findSharedIndex(
hookStack,
rootStack,
Expand All @@ -446,7 +446,7 @@ function findCommonAncestorIndex(rootStack, hookStack) {
return -1;
}

function isReactWrapper(functionName, primitiveName) {
function isReactWrapper(functionName: any, primitiveName: string) {
if (!functionName) {
return false;
}
Expand All @@ -460,7 +460,7 @@ function isReactWrapper(functionName, primitiveName) {
);
}

function findPrimitiveIndex(hookStack, hook) {
function findPrimitiveIndex(hookStack: any, hook: HookLogEntry) {
const stackCache = getPrimitiveStackCache();
const primitiveStack = stackCache.get(hook.primitive);
if (primitiveStack === undefined) {
Expand Down Expand Up @@ -488,7 +488,7 @@ function findPrimitiveIndex(hookStack, hook) {
return -1;
}

function parseTrimmedStack(rootStack, hook) {
function parseTrimmedStack(rootStack: any, hook: HookLogEntry) {
// Get the stack trace between the primitive hook function and
// the root function call. I.e. the stack frames of custom hooks.
const hookStack = ErrorStackParser.parse(hook.stackError);
Expand Down Expand Up @@ -520,8 +520,8 @@ function parseCustomHookName(functionName: void | string): string {
}

function buildTree(
rootStack,
readHookLog,
rootStack: any,
readHookLog: Array<HookLogEntry>,
includeHooksSource: boolean,
): HooksTree {
const rootChildren = [];
Expand Down Expand Up @@ -764,7 +764,7 @@ function inspectHooksOfForwardRef<Props, Ref>(
return buildTree(rootStack, readHookLog, includeHooksSource);
}

function resolveDefaultProps(Component, baseProps) {
function resolveDefaultProps(Component: any, baseProps: any) {
if (Component && Component.defaultProps) {
// Resolve default props. Taken from ReactElement
const props = assign({}, baseProps);
Expand Down
4 changes: 2 additions & 2 deletions packages/react-devtools-core/src/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const hook: ?DevToolsHook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;

let savedComponentFilters: Array<ComponentFilter> = getDefaultComponentFilters();

function debug(methodName: string, ...args) {
function debug(methodName: string, ...args: Array<mixed>) {
if (__DEBUG__) {
console.log(
`%c[core/backend] %c${methodName}`,
Expand Down Expand Up @@ -276,7 +276,7 @@ export function connectToDevTools(options: ?ConnectOptions) {
scheduleRetry();
}

function handleMessage(event) {
function handleMessage(event: MessageEvent) {
let data;
try {
if (typeof event.data === 'string') {
Expand Down
10 changes: 6 additions & 4 deletions packages/react-devtools-core/src/standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ let bridge: FrontendBridge | null = null;
let store: Store | null = null;
let root = null;

const log = (...args) => console.log('[React DevTools]', ...args);
log.warn = (...args) => console.warn('[React DevTools]', ...args);
log.error = (...args) => console.error('[React DevTools]', ...args);
const log = (...args: Array<mixed>) => console.log('[React DevTools]', ...args);
log.warn = (...args: Array<mixed>) => console.warn('[React DevTools]', ...args);
log.error = (...args: Array<mixed>) =>
console.error('[React DevTools]', ...args);

function debug(methodName: string, ...args) {
function debug(methodName: string, ...args: Array<mixed>) {
if (__DEBUG__) {
console.log(
`%c[core/standalone] %c${methodName}`,
Expand Down Expand Up @@ -166,6 +167,7 @@ function onDisconnected() {
disconnectedCallback();
}

// $FlowFixMe[missing-local-annot]
function onError({code, message}) {
safeUnmount();

Expand Down
4 changes: 3 additions & 1 deletion packages/react-devtools-extensions/src/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

let welcomeHasInitialized = false;

// $FlowFixMe[missing-local-annot]
function welcome(event) {
if (
event.source !== window ||
Expand Down Expand Up @@ -42,7 +43,7 @@ function welcome(event) {

window.addEventListener('message', welcome);

function setup(hook) {
function setup(hook: any) {
if (hook == null) {
// DevTools didn't get injected into this page (maybe b'c of the contentType).
return;
Expand All @@ -55,6 +56,7 @@ function setup(hook) {

const bridge = new Bridge({
listen(fn) {
// $FlowFixMe[missing-local-annot]
const listener = event => {
if (
event.source !== window ||
Expand Down
2 changes: 2 additions & 0 deletions packages/react-devtools-inline/src/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {BackendBridge} from 'react-devtools-shared/src/bridge';
import type {Wall} from 'react-devtools-shared/src/types';

function startActivation(contentWindow: any, bridge: BackendBridge) {
// $FlowFixMe[missing-local-annot]
const onSavedPreferences = data => {
// This is the only message we're listening for,
// so it's safe to cleanup after we've received it.
Expand Down Expand Up @@ -96,6 +97,7 @@ export function createBridge(contentWindow: any, wall?: Wall): BackendBridge {
if (wall == null) {
wall = {
listen(fn) {
// $FlowFixMe[missing-local-annot]
const onMessage = ({data}) => {
fn(data);
};
Expand Down
1 change: 1 addition & 0 deletions packages/react-devtools-inline/src/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function createBridge(contentWindow: any, wall?: Wall): FrontendBridge {
if (wall == null) {
wall = {
listen(fn) {
// $FlowFixMe[missing-local-annot]
const onMessage = ({data}) => {
fn(data);
};
Expand Down
2 changes: 1 addition & 1 deletion packages/react-devtools-shared/src/backend/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import type {ComponentFilter} from '../types';
import {isSynchronousXHRSupported} from './utils';
import type {BrowserTheme} from 'react-devtools-shared/src/devtools/views/DevTools';

const debug = (methodName, ...args) => {
const debug = (methodName: string, ...args: Array<string>) => {
if (__DEBUG__) {
console.log(
`%cAgent %c${methodName}`,
Expand Down
2 changes: 2 additions & 0 deletions packages/react-devtools-shared/src/backend/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export function patch({
? targetConsole[method].__REACT_DEVTOOLS_ORIGINAL_METHOD__
: targetConsole[method]);

// $FlowFixMe[missing-local-annot]
const overrideMethod = (...args) => {
let shouldAppendWarningStack = false;
if (method !== 'log') {
Expand Down Expand Up @@ -335,6 +336,7 @@ export function patchForStrictMode() {
? targetConsole[method].__REACT_DEVTOOLS_STRICT_MODE_ORIGINAL_METHOD__
: targetConsole[method]);

// $FlowFixMe[missing-local-annot]
const overrideMethod = (...args) => {
if (!consoleSettingsRef.hideConsoleLogsInStrictMode) {
// Dim the text color of the double logs if we're not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export function attach(
return ((internalInstanceToIDMap.get(internalInstance): any): number);
}

function areEqualArrays(a, b) {
function areEqualArrays(a: Array<any>, b: Array<any>) {
if (a.length !== b.length) {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/react-devtools-shared/src/backend/legacy/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {InternalInstance} from './renderer';

export function decorate(object: Object, attr: string, fn: Function): Function {
const old = object[attr];
// $FlowFixMe[missing-this-annot] webpack config needs to be updated to allow `this` type annotations
object[attr] = function(instance: InternalInstance) {
return fn.call(this, old, arguments);
};
Expand All @@ -34,6 +35,7 @@ export function restoreMany(source: Object, olds: Object): void {
}
}

// $FlowFixMe[missing-this-annot] webpack config needs to be updated to allow `this` type annotations
export function forceUpdate(instance: InternalInstance): void {
if (typeof instance.forceUpdate === 'function') {
instance.forceUpdate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export function createProfilingHooks({
}
}

function markAndClear(markName) {
function markAndClear(markName: string) {
// This method won't be called unless these functions are defined, so we can skip the extra typeof check.
((performanceTarget: any): Performance).mark(markName);
((performanceTarget: any): Performance).clearMarks(markName);
Expand Down
Loading

0 comments on commit 0b4f443

Please sign in to comment.