Skip to content

Commit 0b4f443

Browse files
authored
[flow] enable enforce_local_inference_annotations (#25921)
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
1 parent 0b97441 commit 0b4f443

File tree

144 files changed

+689
-371
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+689
-371
lines changed

packages/jest-react/src/internalAct.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export function act<T>(scope: () => Thenable<T> | T): Thenable<T> {
122122
}
123123
}
124124

125-
function flushActWork(resolve, reject) {
125+
function flushActWork(resolve: () => void, reject: (error: any) => void) {
126126
if (Scheduler.unstable_hasPendingWork()) {
127127
try {
128128
Scheduler.unstable_flushUntilNextPaint();

packages/react-cache/src/ReactCacheOld.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @flow
88
*/
99

10-
import type {Thenable} from 'shared/ReactTypes';
10+
import type {ReactContext, Thenable} from 'shared/ReactTypes';
1111

1212
import * as React from 'react';
1313

@@ -48,7 +48,7 @@ const ReactCurrentDispatcher =
4848
React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
4949
.ReactCurrentDispatcher;
5050

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

65+
// $FlowFixMe[missing-local-annot]
6566
function identityHashFn(input) {
6667
if (__DEV__) {
6768
if (
@@ -133,7 +134,7 @@ function accessResult<I, K, V>(
133134
}
134135
}
135136

136-
function deleteEntry(resource, key) {
137+
function deleteEntry(resource: any, key: mixed) {
137138
const entriesForResource = entries.get(resource);
138139
if (entriesForResource !== undefined) {
139140
entriesForResource.delete(key);

packages/react-client/src/ReactFlightClient.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ type SomeChunk<T> =
9494
| InitializedChunk<T>
9595
| ErroredChunk<T>;
9696

97+
// $FlowFixMe[missing-this-annot]
9798
function Chunk(status: any, value: any, reason: any, response: Response) {
9899
this.status = status;
99100
this.value = value;
@@ -104,6 +105,7 @@ function Chunk(status: any, value: any, reason: any, response: Response) {
104105
Chunk.prototype = (Object.create(Promise.prototype): any);
105106
// TODO: This doesn't return a new Promise chain unlike the real .then
106107
Chunk.prototype.then = function<T>(
108+
this: SomeChunk<T>,
107109
resolve: (value: T) => mixed,
108110
reject: (reason: mixed) => mixed,
109111
) {
@@ -369,7 +371,11 @@ export function reportGlobalError(response: Response, error: Error): void {
369371
});
370372
}
371373

372-
function createElement(type, key, props): React$Element<any> {
374+
function createElement(
375+
type: mixed,
376+
key: mixed,
377+
props: mixed,
378+
): React$Element<any> {
373379
const element: any = {
374380
// This tag allows us to uniquely identify this as a React Element
375381
$$typeof: REACT_ELEMENT_TYPE,
@@ -446,6 +452,7 @@ function createModelResolver<T>(
446452
value: null,
447453
};
448454
}
455+
// $FlowFixMe[missing-local-annot]
449456
return value => {
450457
parentObject[key] = value;
451458
blocked.deps--;
@@ -465,7 +472,7 @@ function createModelResolver<T>(
465472
}
466473

467474
function createModelReject<T>(chunk: SomeChunk<T>) {
468-
return error => triggerErrorOnChunk(chunk, error);
475+
return (error: mixed) => triggerErrorOnChunk(chunk, error);
469476
}
470477

471478
export function parseModelString(

packages/react-client/src/ReactFlightClientStream.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export function processBinaryChunk(
122122
}
123123

124124
function createFromJSONCallback(response: Response) {
125+
// $FlowFixMe[missing-this-annot]
125126
return function(key: string, value: JSONValue) {
126127
if (typeof value === 'string') {
127128
// We can't use .bind here because we need the "this" value.

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ const Dispatcher: DispatcherType = {
350350
// create a proxy to throw a custom error
351351
// in case future versions of React adds more hooks
352352
const DispatcherProxyHandler = {
353-
get(target, prop) {
353+
get(target: DispatcherType, prop: string) {
354354
if (target.hasOwnProperty(prop)) {
355355
return target[prop];
356356
}
@@ -404,7 +404,7 @@ export type HooksTree = Array<HooksNode>;
404404

405405
let mostLikelyAncestorIndex = 0;
406406

407-
function findSharedIndex(hookStack, rootStack, rootIndex) {
407+
function findSharedIndex(hookStack: any, rootStack: any, rootIndex: number) {
408408
const source = rootStack[rootIndex].source;
409409
hookSearch: for (let i = 0; i < hookStack.length; i++) {
410410
if (hookStack[i].source === source) {
@@ -425,7 +425,7 @@ function findSharedIndex(hookStack, rootStack, rootIndex) {
425425
return -1;
426426
}
427427

428-
function findCommonAncestorIndex(rootStack, hookStack) {
428+
function findCommonAncestorIndex(rootStack: any, hookStack: any) {
429429
let rootIndex = findSharedIndex(
430430
hookStack,
431431
rootStack,
@@ -446,7 +446,7 @@ function findCommonAncestorIndex(rootStack, hookStack) {
446446
return -1;
447447
}
448448

449-
function isReactWrapper(functionName, primitiveName) {
449+
function isReactWrapper(functionName: any, primitiveName: string) {
450450
if (!functionName) {
451451
return false;
452452
}
@@ -460,7 +460,7 @@ function isReactWrapper(functionName, primitiveName) {
460460
);
461461
}
462462

463-
function findPrimitiveIndex(hookStack, hook) {
463+
function findPrimitiveIndex(hookStack: any, hook: HookLogEntry) {
464464
const stackCache = getPrimitiveStackCache();
465465
const primitiveStack = stackCache.get(hook.primitive);
466466
if (primitiveStack === undefined) {
@@ -488,7 +488,7 @@ function findPrimitiveIndex(hookStack, hook) {
488488
return -1;
489489
}
490490

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

522522
function buildTree(
523-
rootStack,
524-
readHookLog,
523+
rootStack: any,
524+
readHookLog: Array<HookLogEntry>,
525525
includeHooksSource: boolean,
526526
): HooksTree {
527527
const rootChildren = [];
@@ -764,7 +764,7 @@ function inspectHooksOfForwardRef<Props, Ref>(
764764
return buildTree(rootStack, readHookLog, includeHooksSource);
765765
}
766766

767-
function resolveDefaultProps(Component, baseProps) {
767+
function resolveDefaultProps(Component: any, baseProps: any) {
768768
if (Component && Component.defaultProps) {
769769
// Resolve default props. Taken from ReactElement
770770
const props = assign({}, baseProps);

packages/react-devtools-core/src/backend.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const hook: ?DevToolsHook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
4444

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

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

279-
function handleMessage(event) {
279+
function handleMessage(event: MessageEvent) {
280280
let data;
281281
try {
282282
if (typeof event.data === 'string') {

packages/react-devtools-core/src/standalone.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ let bridge: FrontendBridge | null = null;
8686
let store: Store | null = null;
8787
let root = null;
8888

89-
const log = (...args) => console.log('[React DevTools]', ...args);
90-
log.warn = (...args) => console.warn('[React DevTools]', ...args);
91-
log.error = (...args) => console.error('[React DevTools]', ...args);
89+
const log = (...args: Array<mixed>) => console.log('[React DevTools]', ...args);
90+
log.warn = (...args: Array<mixed>) => console.warn('[React DevTools]', ...args);
91+
log.error = (...args: Array<mixed>) =>
92+
console.error('[React DevTools]', ...args);
9293

93-
function debug(methodName: string, ...args) {
94+
function debug(methodName: string, ...args: Array<mixed>) {
9495
if (__DEBUG__) {
9596
console.log(
9697
`%c[core/standalone] %c${methodName}`,
@@ -166,6 +167,7 @@ function onDisconnected() {
166167
disconnectedCallback();
167168
}
168169

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

packages/react-devtools-extensions/src/backend.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
let welcomeHasInitialized = false;
1010

11+
// $FlowFixMe[missing-local-annot]
1112
function welcome(event) {
1213
if (
1314
event.source !== window ||
@@ -42,7 +43,7 @@ function welcome(event) {
4243

4344
window.addEventListener('message', welcome);
4445

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

5657
const bridge = new Bridge({
5758
listen(fn) {
59+
// $FlowFixMe[missing-local-annot]
5860
const listener = event => {
5961
if (
6062
event.source !== window ||

packages/react-devtools-inline/src/backend.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {BackendBridge} from 'react-devtools-shared/src/bridge';
1010
import type {Wall} from 'react-devtools-shared/src/types';
1111

1212
function startActivation(contentWindow: any, bridge: BackendBridge) {
13+
// $FlowFixMe[missing-local-annot]
1314
const onSavedPreferences = data => {
1415
// This is the only message we're listening for,
1516
// so it's safe to cleanup after we've received it.
@@ -96,6 +97,7 @@ export function createBridge(contentWindow: any, wall?: Wall): BackendBridge {
9697
if (wall == null) {
9798
wall = {
9899
listen(fn) {
100+
// $FlowFixMe[missing-local-annot]
99101
const onMessage = ({data}) => {
100102
fn(data);
101103
};

packages/react-devtools-inline/src/frontend.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export function createBridge(contentWindow: any, wall?: Wall): FrontendBridge {
3737
if (wall == null) {
3838
wall = {
3939
listen(fn) {
40+
// $FlowFixMe[missing-local-annot]
4041
const onMessage = ({data}) => {
4142
fn(data);
4243
};

packages/react-devtools-shared/src/backend/agent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import type {ComponentFilter} from '../types';
4242
import {isSynchronousXHRSupported} from './utils';
4343
import type {BrowserTheme} from 'react-devtools-shared/src/devtools/views/DevTools';
4444

45-
const debug = (methodName, ...args) => {
45+
const debug = (methodName: string, ...args: Array<string>) => {
4646
if (__DEBUG__) {
4747
console.log(
4848
`%cAgent %c${methodName}`,

packages/react-devtools-shared/src/backend/console.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ export function patch({
197197
? targetConsole[method].__REACT_DEVTOOLS_ORIGINAL_METHOD__
198198
: targetConsole[method]);
199199

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

339+
// $FlowFixMe[missing-local-annot]
338340
const overrideMethod = (...args) => {
339341
if (!consoleSettingsRef.hideConsoleLogsInStrictMode) {
340342
// Dim the text color of the double logs if we're not

packages/react-devtools-shared/src/backend/legacy/renderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export function attach(
195195
return ((internalInstanceToIDMap.get(internalInstance): any): number);
196196
}
197197

198-
function areEqualArrays(a, b) {
198+
function areEqualArrays(a: Array<any>, b: Array<any>) {
199199
if (a.length !== b.length) {
200200
return false;
201201
}

packages/react-devtools-shared/src/backend/legacy/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {InternalInstance} from './renderer';
1111

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

38+
// $FlowFixMe[missing-this-annot] webpack config needs to be updated to allow `this` type annotations
3739
export function forceUpdate(instance: InternalInstance): void {
3840
if (typeof instance.forceUpdate === 'function') {
3941
instance.forceUpdate();

packages/react-devtools-shared/src/backend/profilingHooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export function createProfilingHooks({
204204
}
205205
}
206206

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

0 commit comments

Comments
 (0)