Skip to content

Commit 3b6826e

Browse files
committed
Flow: inference_mode=constrain_writes
This mode is going to be the new default in Flow going forward. There was an unfortuante large number of suppressions in this update. More on the changes can be found in this [Flow blog post](https://medium.com/flow-type/new-flow-language-rule-constrained-writes-4c70e375d190). Added some of the required annotations using the provided codemod: ```sh node_modules/.bin/flow codemod annotate-declarations --write . ``` ghstack-source-id: 0b168e1 Pull Request resolved: #25422
1 parent af9afe9 commit 3b6826e

File tree

70 files changed

+207
-88
lines changed

Some content is hidden

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

70 files changed

+207
-88
lines changed

packages/react-cache/src/LRU.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function createLRU<T>(limit: number): LRU<T> {
5757
// Delete entries from the cache, starting from the end of the list.
5858
if (first !== null) {
5959
const resolvedFirst: Entry<T> = (first: any);
60-
let last = resolvedFirst.previous;
60+
let last: null | Entry<T> = resolvedFirst.previous;
6161
while (size > targetSize && last !== null) {
6262
const onDelete = last.onDelete;
6363
const previous = last.previous;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ function useImperativeHandle<T>(
217217
// and if there is a ref callback it might not store it but if it does we
218218
// have no way of knowing where. So let's only enable introspection of the
219219
// ref itself if it is using the object form.
220-
let instance = undefined;
220+
let instance: ?T = undefined;
221221
if (ref !== null && typeof ref === 'object') {
222222
instance = ref.current;
223223
}
@@ -716,14 +716,15 @@ export function inspectHooks<Props>(
716716
} finally {
717717
readHookLog = hookLog;
718718
hookLog = [];
719+
// $FlowFixMe[incompatible-use] found when upgrading Flow
719720
currentDispatcher.current = previousDispatcher;
720721
}
721722
const rootStack = ErrorStackParser.parse(ancestorStackError);
722723
return buildTree(rootStack, readHookLog, includeHooksSource);
723724
}
724725

725726
function setupContexts(contextMap: Map<ReactContext<any>, any>, fiber: Fiber) {
726-
let current = fiber;
727+
let current: null | Fiber = fiber;
727728
while (current) {
728729
if (current.tag === ContextProvider) {
729730
const providerType: ReactProviderType<any> = current.type;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export function connectToDevTools(options: ?ConnectOptions) {
146146
}
147147
},
148148
);
149+
// $FlowFixMe[incompatible-use] found when upgrading Flow
149150
bridge.addListener(
150151
'updateComponentFilters',
151152
(componentFilters: Array<ComponentFilter>) => {
@@ -165,10 +166,12 @@ export function connectToDevTools(options: ?ConnectOptions) {
165166
// Ideally the backend would save the filters itself, but RN doesn't provide a sync storage solution.
166167
// So for now we just fall back to using the default filters...
167168
if (window.__REACT_DEVTOOLS_COMPONENT_FILTERS__ == null) {
169+
// $FlowFixMe[incompatible-use] found when upgrading Flow
168170
bridge.send('overrideComponentFilters', savedComponentFilters);
169171
}
170172

171173
// TODO (npm-packages) Warn if "isBackendStorageAPISupported"
174+
// $FlowFixMe[incompatible-call] found when upgrading Flow
172175
const agent = new Agent(bridge);
173176
agent.addListener('shutdown', () => {
174177
// If we received 'shutdown' from `agent`, we assume the `bridge` is already shutting down,
@@ -181,6 +184,7 @@ export function connectToDevTools(options: ?ConnectOptions) {
181184
// Setup React Native style editor if the environment supports it.
182185
if (resolveRNStyle != null || hook.resolveRNStyle != null) {
183186
setupNativeStyleEditor(
187+
// $FlowFixMe[incompatible-call] found when upgrading Flow
184188
bridge,
185189
agent,
186190
((resolveRNStyle || hook.resolveRNStyle: any): ResolveNativeStyle),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ function initialize(socket: WebSocket) {
256256
socket.close();
257257
});
258258

259+
// $FlowFixMe[incompatible-call] found when upgrading Flow
259260
store = new Store(bridge, {
260261
checkBridgeProtocolCompatibility: true,
261262
supportsNativeInspection: true,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ export function getStackByFiberInDevAndProd(
8484
): string {
8585
try {
8686
let info = '';
87-
let node = workInProgress;
87+
let node: Fiber = workInProgress;
8888
do {
8989
info += describeFiber(workTagMap, node, currentDispatcherRef);
90+
// $FlowFixMe[incompatible-type] we bail out when we get a null
9091
node = node.return;
9192
} while (node);
9293
return info;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ export function attach(
248248
parentIDStack.pop();
249249
return result;
250250
} catch (err) {
251+
// $FlowFixMe[incompatible-type] found when upgrading Flow
251252
parentIDStack = [];
252253
throw err;
253254
} finally {
@@ -284,6 +285,7 @@ export function attach(
284285
parentIDStack.pop();
285286
return result;
286287
} catch (err) {
288+
// $FlowFixMe[incompatible-type] found when upgrading Flow
287289
parentIDStack = [];
288290
throw err;
289291
} finally {
@@ -320,6 +322,7 @@ export function attach(
320322
parentIDStack.pop();
321323
return result;
322324
} catch (err) {
325+
// $FlowFixMe[incompatible-type] found when upgrading Flow
323326
parentIDStack = [];
324327
throw err;
325328
} finally {
@@ -351,6 +354,7 @@ export function attach(
351354

352355
return result;
353356
} catch (err) {
357+
// $FlowFixMe[incompatible-type] found when upgrading Flow
354358
parentIDStack = [];
355359
throw err;
356360
} finally {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ export function createProfilingHooks({
799799

800800
function getParentFibers(fiber: Fiber): Array<Fiber> {
801801
const parents = [];
802-
let parent = fiber;
802+
let parent: null | Fiber = fiber;
803803
while (parent !== null) {
804804
parents.push(parent);
805805
parent = parent.return;
@@ -824,6 +824,7 @@ export function createProfilingHooks({
824824
warning: null,
825825
};
826826
currentFiberStacks.set(event, getParentFibers(fiber));
827+
// $FlowFixMe[incompatible-use] found when upgrading Flow
827828
currentTimelineData.schedulingEvents.push(event);
828829
}
829830
}

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,7 +2269,7 @@ export function attach(
22692269

22702270
// This is a naive implementation that shallowly recourses children.
22712271
// We might want to revisit this if it proves to be too inefficient.
2272-
let child = childSet;
2272+
let child: null | Fiber = childSet;
22732273
while (child !== null) {
22742274
findReorderedChildrenRecursively(child, nextChildren);
22752275
child = child.sibling;
@@ -2846,11 +2846,11 @@ export function attach(
28462846
// https://github.com/facebook/react/blob/main/packages/react-reconciler/src/ReactFiberTreeReflection.js
28472847
function getNearestMountedFiber(fiber: Fiber): null | Fiber {
28482848
let node = fiber;
2849-
let nearestMounted = fiber;
2849+
let nearestMounted: null | Fiber = fiber;
28502850
if (!fiber.alternate) {
28512851
// If there is no alternate, this might be a new tree that isn't inserted
28522852
// yet. If it is, then it will have a pending insertion effect on it.
2853-
let nextNode = node;
2853+
let nextNode: Fiber = node;
28542854
do {
28552855
node = nextNode;
28562856
if ((node.flags & (Placement | Hydrating)) !== NoFlags) {
@@ -2859,6 +2859,7 @@ export function attach(
28592859
// if that one is still mounted.
28602860
nearestMounted = node.return;
28612861
}
2862+
// $FlowFixMe[incompatible-type] we bail out when we get a null
28622863
nextNode = node.return;
28632864
} while (nextNode);
28642865
} else {
@@ -3097,7 +3098,7 @@ export function attach(
30973098
const owners: Array<SerializedElement> = [fiberToSerializedElement(fiber)];
30983099

30993100
if (_debugOwner) {
3100-
let owner = _debugOwner;
3101+
let owner: null | Fiber = _debugOwner;
31013102
while (owner !== null) {
31023103
owners.unshift(fiberToSerializedElement(owner));
31033104
owner = owner._debugOwner || null;
@@ -3256,7 +3257,7 @@ export function attach(
32563257
let owners = null;
32573258
if (_debugOwner) {
32583259
owners = [];
3259-
let owner = _debugOwner;
3260+
let owner: null | Fiber = _debugOwner;
32603261
while (owner !== null) {
32613262
owners.push(fiberToSerializedElement(owner));
32623263
owner = owner._debugOwner || null;
@@ -3687,18 +3688,22 @@ export function attach(
36873688
// This will enable us to send patches without re-inspecting if hydrated paths are requested.
36883689
// (Reducing how often we shallow-render is a better DX for function components that use hooks.)
36893690
const cleanedInspectedElement = {...mostRecentlyInspectedElement};
3691+
// $FlowFixMe[prop-missing] found when upgrading Flow
36903692
cleanedInspectedElement.context = cleanForBridge(
36913693
cleanedInspectedElement.context,
36923694
createIsPathAllowed('context', null),
36933695
);
3696+
// $FlowFixMe[prop-missing] found when upgrading Flow
36943697
cleanedInspectedElement.hooks = cleanForBridge(
36953698
cleanedInspectedElement.hooks,
36963699
createIsPathAllowed('hooks', 'hooks'),
36973700
);
3701+
// $FlowFixMe[prop-missing] found when upgrading Flow
36983702
cleanedInspectedElement.props = cleanForBridge(
36993703
cleanedInspectedElement.props,
37003704
createIsPathAllowed('props', null),
37013705
);
3706+
// $FlowFixMe[prop-missing] found when upgrading Flow
37023707
cleanedInspectedElement.state = cleanForBridge(
37033708
cleanedInspectedElement.state,
37043709
createIsPathAllowed('state', null),
@@ -3709,6 +3714,7 @@ export function attach(
37093714
responseID: requestID,
37103715
type: 'full-data',
37113716
// $FlowFixMe[incompatible-return] found when upgrading Flow
3717+
// $FlowFixMe[prop-missing] found when upgrading Flow
37123718
value: cleanedInspectedElement,
37133719
};
37143720
}
@@ -4420,13 +4426,15 @@ export function attach(
44204426
// The return path will contain Fibers that are "invisible" to the store
44214427
// because their keys and indexes are important to restoring the selection.
44224428
function getPathForElement(id: number): Array<PathFrame> | null {
4423-
let fiber = idToArbitraryFiberMap.get(id);
4429+
let fiber: ?Fiber = idToArbitraryFiberMap.get(id);
44244430
if (fiber == null) {
44254431
return null;
44264432
}
44274433
const keyPath = [];
44284434
while (fiber !== null) {
4435+
// $FlowFixMe[incompatible-call] found when upgrading Flow
44294436
keyPath.push(getPathFrame(fiber));
4437+
// $FlowFixMe[incompatible-use] found when upgrading Flow
44304438
fiber = fiber.return;
44314439
}
44324440
keyPath.reverse();
@@ -4443,7 +4451,7 @@ export function attach(
44434451
return null;
44444452
}
44454453
// Find the closest Fiber store is aware of.
4446-
let fiber = trackedPathMatchFiber;
4454+
let fiber: null | Fiber = trackedPathMatchFiber;
44474455
while (fiber !== null && shouldFilterFiber(fiber)) {
44484456
fiber = fiber.return;
44494457
}

packages/react-devtools-shared/src/backend/views/Highlighter/Overlay.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ function findTipPos(dims, bounds, tipSize) {
282282
const tipWidth = Math.max(tipSize.width, 60);
283283
const margin = 5;
284284

285-
let top;
285+
let top: number | string;
286286
if (dims.top + dims.height + tipHeight <= bounds.top + bounds.height) {
287287
if (dims.top + dims.height < bounds.top + 0) {
288288
top = bounds.top + margin;
@@ -299,7 +299,7 @@ function findTipPos(dims, bounds, tipSize) {
299299
top = bounds.top + bounds.height - tipHeight - margin;
300300
}
301301

302-
let left = dims.left + margin;
302+
let left: number | string = dims.left + margin;
303303
if (dims.left < bounds.left) {
304304
left = bounds.left + margin;
305305
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export function getNestedBoundingClientRect(
8484
const ownerIframe = getOwnerIframe(node);
8585
if (ownerIframe && ownerIframe !== boundaryWindow) {
8686
const rects = [node.getBoundingClientRect()];
87-
let currentIframe = ownerIframe;
87+
let currentIframe: null | HTMLElement = ownerIframe;
8888
let onlyOneMore = false;
8989
while (currentIframe) {
9090
const rect = getBoundingClientRectWithBorderOffset(currentIframe);

packages/react-devtools-shared/src/devtools/cache.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ const resourceConfigs: Map<Resource<any, any, any>, Config> = new Map();
8888
function getEntriesForResource(
8989
resource: any,
9090
): Map<any, any> | WeakMap<any, any> {
91-
let entriesForResource = ((entries.get(resource): any): Map<any, any>);
91+
let entriesForResource: Map<any, any> | WeakMap<any, any> = ((entries.get(
92+
resource,
93+
): any): Map<any, any>);
9294
if (entriesForResource === undefined) {
9395
const config = resourceConfigs.get(resource);
9496
entriesForResource =

packages/react-devtools-shared/src/devtools/store.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ export default class Store extends EventEmitter<{
763763

764764
const weightDelta = 1 - element.weight;
765765

766-
let parentElement = ((this._idToElement.get(
766+
let parentElement: void | Element = ((this._idToElement.get(
767767
element.parentID,
768768
): any): Element);
769769
while (parentElement != null) {
@@ -789,7 +789,7 @@ export default class Store extends EventEmitter<{
789789
: currentElement.weight;
790790
const weightDelta = newWeight - oldWeight;
791791

792-
let parentElement = ((this._idToElement.get(
792+
let parentElement: void | Element = ((this._idToElement.get(
793793
currentElement.parentID,
794794
): any): Element);
795795
while (parentElement != null) {
@@ -806,8 +806,10 @@ export default class Store extends EventEmitter<{
806806

807807
currentElement =
808808
currentElement.parentID !== 0
809-
? this.getElementByID(currentElement.parentID)
810-
: null;
809+
? // $FlowFixMe[incompatible-type] found when upgrading Flow
810+
this.getElementByID(currentElement.parentID)
811+
: // $FlowFixMe[incompatible-type] found when upgrading Flow
812+
null;
811813
}
812814
}
813815

packages/react-devtools-shared/src/devtools/views/Components/NativeStyleEditor/context.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ const resource: Resource<
6666
return request.promise;
6767
}
6868

69-
let resolveFn = ((null: any): ResolveFn);
69+
let resolveFn:
70+
| ResolveFn
71+
| ((
72+
result: Promise<StyleAndLayoutFrontend> | StyleAndLayoutFrontend,
73+
) => void) = ((null: any): ResolveFn);
7074
const promise = new Promise(resolve => {
7175
resolveFn = resolve;
7276
});

packages/react-devtools-shared/src/devtools/views/Components/NewKeyValue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default function NewKeyValue({
5757
const {id} = inspectedElement;
5858
const rendererID = store.getRendererIDForElement(id);
5959
if (rendererID !== null) {
60-
let basePath = newPath;
60+
let basePath: Array<string | number> = newPath;
6161
if (hookID != null) {
6262
basePath = parseHookPathForEdit(basePath);
6363
}

packages/react-devtools-shared/src/devtools/views/Components/OwnersListContext.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ const resource: Resource<
4949
return request.promise;
5050
}
5151

52-
let resolveFn = ((null: any): ResolveFn);
52+
let resolveFn:
53+
| ResolveFn
54+
| ((
55+
result: Promise<Array<SerializedElement>> | Array<SerializedElement>,
56+
) => void) = ((null: any): ResolveFn);
5357
const promise = new Promise(resolve => {
5458
resolveFn = resolve;
5559
});

packages/react-devtools-shared/src/devtools/views/Components/SelectedTreeHighlight.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @flow
88
*/
99

10+
import type {Element} from './types';
1011
import * as React from 'react';
1112
import {useContext, useMemo} from 'react';
1213
import {TreeStateContext} from './TreeContext';
@@ -53,7 +54,7 @@ export default function SelectedTreeHighlight(_: {}): React.Node {
5354
}
5455

5556
let stopIndex = null;
56-
let current = element;
57+
let current: null | Element = element;
5758
while (current !== null) {
5859
if (current.isCollapsed || current.children.length === 0) {
5960
// We've found the last/deepest descendant.

packages/react-devtools-shared/src/devtools/views/Components/TreeContext.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ function reduceTreeState(store: Store, state: State, action: Action): State {
294294
) {
295295
const leafElement = store.getElementByID(ownerSubtreeLeafElementID);
296296
if (leafElement !== null) {
297-
let currentElement = leafElement;
297+
let currentElement: null | Element = leafElement;
298298
while (currentElement !== null) {
299299
if (currentElement.ownerID === selectedElementID) {
300300
selectedElementIndex = store.getIndexOfElementID(

packages/react-devtools-shared/src/devtools/views/Profiler/Tooltip.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ const TOOLTIP_OFFSET = 5;
5757
// Method used to find the position of the tooltip based on current mouse position
5858
function getTooltipPosition(element, mousePosition) {
5959
const {height, mouseX, mouseY, width} = mousePosition;
60-
let top = 0;
61-
let left = 0;
60+
let top: number | string = 0;
61+
let left: number | string = 0;
6262

6363
if (mouseY + TOOLTIP_OFFSET + element.offsetHeight >= height) {
6464
if (mouseY - TOOLTIP_OFFSET - element.offsetHeight > 0) {

packages/react-devtools-shared/src/devtools/views/hooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ export function useModalDismissSignal(
238238
// Delay until after the current call stack is empty,
239239
// in case this effect is being run while an event is currently bubbling.
240240
// In that case, we don't want to listen to the pre-existing event.
241-
let timeoutID = setTimeout(() => {
241+
let timeoutID: null | TimeoutID = setTimeout(() => {
242242
timeoutID = null;
243243

244244
// It's important to listen to the ownerDocument to support the browser extension.

packages/react-devtools-shared/src/dynamicImportCache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export function loadModule(moduleLoaderFunction: ModuleLoaderFunction): Module {
136136
);
137137

138138
// Eventually timeout and stop trying to load the module.
139-
let timeoutID = setTimeout(function onTimeout() {
139+
let timeoutID: null | TimeoutID = setTimeout(function onTimeout() {
140140
if (__DEBUG__) {
141141
console.log(
142142
`[dynamicImportCache] loadModule("${moduleLoaderFunction.name}") onTimeout()`,

0 commit comments

Comments
 (0)