Skip to content

Commit e2424f3

Browse files
authored
[flow] enable exact_empty_objects (#25973)
This enables the "exact_empty_objects" setting for Flow which makes empty objects exact instead of building up the type as properties are added in code below. This is in preparation to Flow 191 which makes this the default and removes the config. More about the change in the Flow blog [here](https://medium.com/flow-type/improved-handling-of-the-empty-object-in-flow-ead91887e40c).
1 parent 0b4f443 commit e2424f3

Some content is hidden

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

49 files changed

+182
-135
lines changed

packages/react-client/src/ReactFlightClient.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,9 @@ function createElement(
393393
// We don't really need to add any of these but keeping them for good measure.
394394
// Unfortunately, _store is enumerable in jest matchers so for equality to
395395
// work, I need to keep it or make _store non-enumerable in the other file.
396-
element._store = {};
396+
element._store = ({}: {
397+
validated?: boolean,
398+
});
397399
Object.defineProperty(element._store, 'validated', {
398400
configurable: false,
399401
enumerable: false,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ function buildTree(
524524
readHookLog: Array<HookLogEntry>,
525525
includeHooksSource: boolean,
526526
): HooksTree {
527-
const rootChildren = [];
527+
const rootChildren: Array<HooksNode> = [];
528528
let prevStack = null;
529529
let levelChildren = rootChildren;
530530
let nativeHookID = 0;
@@ -557,7 +557,7 @@ function buildTree(
557557
// The remaining part of the new stack are custom hooks. Push them
558558
// to the tree.
559559
for (let j = stack.length - commonSteps - 1; j >= 1; j--) {
560-
const children = [];
560+
const children: Array<HooksNode> = [];
561561
const stackFrame = stack[j];
562562
const levelChild: HooksNode = {
563563
id: null,

packages/react-devtools-shared/src/backend/NativeStyleEditor/setupNativeStyleEditor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function measureStyle(
170170
}
171171

172172
function shallowClone(object: Object): Object {
173-
const cloned = {};
173+
const cloned: {[string]: $FlowFixMe} = {};
174174
for (const n in object) {
175175
cloned[n] = object[n];
176176
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const injectedRenderers: Map<
7979
> = new Map();
8080

8181
let targetConsole: Object = console;
82-
let targetConsoleMethods = {};
82+
let targetConsoleMethods: {[string]: $FlowFixMe} = {};
8383
for (const method in console) {
8484
targetConsoleMethods[method] = console[method];
8585
}
@@ -97,7 +97,7 @@ export function dangerous_setTargetConsoleForTesting(
9797
): void {
9898
targetConsole = targetConsoleForTesting;
9999

100-
targetConsoleMethods = {};
100+
targetConsoleMethods = ({}: {[string]: $FlowFixMe});
101101
for (const method in targetConsole) {
102102
targetConsoleMethods[method] = console[method];
103103
}
@@ -179,7 +179,7 @@ export function patch({
179179
return;
180180
}
181181

182-
const originalConsoleMethods = {};
182+
const originalConsoleMethods: {[string]: $FlowFixMe} = {};
183183

184184
unpatchFn = () => {
185185
for (const method in originalConsoleMethods) {
@@ -318,7 +318,7 @@ export function patchForStrictMode() {
318318
return;
319319
}
320320

321-
const originalConsoleMethods = {};
321+
const originalConsoleMethods: {[string]: $FlowFixMe} = {};
322322

323323
unpatchForStrictModeFn = () => {
324324
for (const method in originalConsoleMethods) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ export function attach(
785785

786786
let owner = element._owner;
787787
if (owner) {
788-
owners = [];
788+
owners = ([]: Array<SerializedElement>);
789789
while (owner != null) {
790790
owners.push({
791791
displayName: getData(owner).displayName || 'Unknown',
@@ -807,8 +807,8 @@ export function attach(
807807
}
808808

809809
// Not implemented
810-
const errors = [];
811-
const warnings = [];
810+
const errors: Array<[string, number]> = [];
811+
const warnings: Array<[string, number]> = [];
812812

813813
return {
814814
id,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function decorateMany(
2222
source: Object,
2323
fns: {[attr: string]: Function, ...},
2424
): Object {
25-
const olds = {};
25+
const olds: {[string]: $FlowFixMe} = {};
2626
for (const name in fns) {
2727
olds[name] = decorate(source, name, fns[name]);
2828
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ let supportsUserTiming =
5252
let supportsUserTimingV3 = false;
5353
if (supportsUserTiming) {
5454
const CHECK_V3_MARK = '__v3';
55-
const markOptions = {};
55+
const markOptions = ({}: {startTime?: number});
5656
Object.defineProperty(markOptions, 'startTime', {
5757
get: function() {
5858
supportsUserTimingV3 = true;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3252,7 +3252,7 @@ export function attach(
32523252

32533253
let owners = null;
32543254
if (_debugOwner) {
3255-
owners = [];
3255+
owners = ([]: Array<SerializedElement>);
32563256
let owner: null | Fiber = _debugOwner;
32573257
while (owner !== null) {
32583258
owners.push(fiberToSerializedElement(owner));
@@ -3265,7 +3265,7 @@ export function attach(
32653265

32663266
let hooks = null;
32673267
if (usesHooks) {
3268-
const originalConsoleMethods = {};
3268+
const originalConsoleMethods: {[string]: $FlowFixMe} = {};
32693269

32703270
// Temporarily disable all console logging before re-running the hook.
32713271
for (const method in console) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export function cleanForBridge(
2020
path?: Array<string | number> = [],
2121
): DehydratedData | null {
2222
if (data !== null) {
23-
const cleanedPaths = [];
24-
const unserializablePaths = [];
23+
const cleanedPaths: Array<Array<string | number>> = [];
24+
const unserializablePaths: Array<Array<string | number>> = [];
2525
const cleanedData = dehydrate(
2626
data,
2727
cleanedPaths,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ const STACK_SOURCE_LOCATION = /([^\s]+) \((.+):(.+):(.+)\)/;
198198
export function stackToComponentSources(
199199
stack: string,
200200
): Array<[string, ?Stack]> {
201-
const out = [];
201+
const out: Array<[string, ?Stack]> = [];
202202
stack
203203
.split(STACK_DELIMETER)
204204
.slice(1)

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,16 @@ export type InspectedElement = {
125125

126126
// TODO: Add profiling type
127127

128+
type Data =
129+
| string
130+
| Dehydrated
131+
| Unserializable
132+
| Array<Dehydrated>
133+
| Array<Unserializable>
134+
| {[string]: Data};
135+
128136
export type DehydratedData = {
129137
cleaned: Array<Array<string | number>>,
130-
data:
131-
| string
132-
| Dehydrated
133-
| Unserializable
134-
| Array<Dehydrated>
135-
| Array<Unserializable>
136-
| {[key: string]: string | Dehydrated | Unserializable, ...},
138+
data: Data,
137139
unserializable: Array<Array<string | number>>,
138140
};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ function updateTree(
150150

151151
// Clone nodes before mutating them so edits don't affect them.
152152
const getClonedNode = (id: number): CommitTreeNode => {
153+
// $FlowFixMe[prop-missing] - recommended fix is to use object spread operator
153154
const clonedNode = ((Object.assign(
154155
{},
155156
nodes.get(id),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export function serializeDataForCopy(props: Object): string {
134134

135135
export function serializeHooksForCopy(hooks: HooksTree | null): string {
136136
// $FlowFixMe "HooksTree is not an object"
137-
const cloned = Object.assign([], hooks);
137+
const cloned = Object.assign(([]: Array<any>), hooks);
138138

139139
const queue = [...cloned];
140140

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function installHook(target: any): DevToolsHook | null {
2929
}
3030

3131
let targetConsole: Object = console;
32-
let targetConsoleMethods = {};
32+
let targetConsoleMethods: {[string]: $FlowFixMe} = {};
3333
for (const method in console) {
3434
targetConsoleMethods[method] = console[method];
3535
}
@@ -39,7 +39,7 @@ export function installHook(target: any): DevToolsHook | null {
3939
): void {
4040
targetConsole = targetConsoleForTesting;
4141

42-
targetConsoleMethods = {};
42+
targetConsoleMethods = ({}: {[string]: $FlowFixMe});
4343
for (const method in targetConsole) {
4444
targetConsoleMethods[method] = console[method];
4545
}
@@ -250,7 +250,7 @@ export function installHook(target: any): DevToolsHook | null {
250250
return;
251251
}
252252

253-
const originalConsoleMethods = {};
253+
const originalConsoleMethods: {[string]: $FlowFixMe} = {};
254254

255255
unpatchFn = () => {
256256
for (const method in originalConsoleMethods) {
@@ -516,9 +516,9 @@ export function installHook(target: any): DevToolsHook | null {
516516
}
517517

518518
// TODO: More meaningful names for "rendererInterfaces" and "renderers".
519-
const fiberRoots = {};
519+
const fiberRoots: {[RendererID]: Set<mixed>} = {};
520520
const rendererInterfaces = new Map();
521-
const listeners = {};
521+
const listeners: {[string]: Array<Handler>} = {};
522522
const renderers = new Map();
523523

524524
const hook: DevToolsHook = {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ function getPotentialHookDeclarationsFromAST(sourceAST: File): NodePath[] {
362362
export function getHookNamesMappingFromAST(
363363
sourceAST: File,
364364
): $ReadOnlyArray<{name: string, start: Position}> {
365-
const hookStack = [];
365+
const hookStack: Array<{name: string, start: $FlowFixMe}> = [];
366366
const hookNames = [];
367367
const pushFrame = (name: string, node: Node) => {
368368
const nameInfo = {name, start: {...node.loc.start}};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function generateHookMap(sourceAST: File): HookMap {
6363
const hookNamesMapping = getHookNamesMappingFromAST(sourceAST);
6464
const namesMap: Map<string, number> = new Map();
6565
const names = [];
66-
const mappings = [];
66+
const mappings: Array<HookMapLine> = [];
6767

6868
let currentLine = null;
6969
hookNamesMapping.forEach(({name, start}) => {

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
setInObject,
1717
} from './utils';
1818

19-
import type {DehydratedData} from 'react-devtools-shared/src/devtools/views/Components/types';
19+
import type {DehydratedData} from './devtools/views/Components/types';
2020

2121
export const meta = {
2222
inspectable: (Symbol('inspectable'): symbol),
@@ -124,13 +124,7 @@ export function dehydrate(
124124
path: Array<string | number>,
125125
isPathAllowed: (path: Array<string | number>) => boolean,
126126
level?: number = 0,
127-
):
128-
| string
129-
| Dehydrated
130-
| Unserializable
131-
| Array<Dehydrated>
132-
| Array<Unserializable>
133-
| {[key: string]: string | Dehydrated | Unserializable, ...} {
127+
): $PropertyType<DehydratedData, 'data'> {
134128
const type = getDataType(data);
135129

136130
let isPathAllowedCheck;
@@ -304,7 +298,9 @@ export function dehydrate(
304298
if (level >= LEVEL_THRESHOLD && !isPathAllowedCheck) {
305299
return createDehydrated(type, true, data, cleaned, path);
306300
} else {
307-
const object = {};
301+
const object: {
302+
[string]: $PropertyType<DehydratedData, 'data'>,
303+
} = {};
308304
getAllEnumerableKeys(data).forEach(key => {
309305
const name = key.toString();
310306
object[name] = dehydrate(

packages/react-devtools-shell/src/app/InspectableElements/CircularReferences.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@ const arrayTwo = [];
1414
arrayTwo.push(arrayOne);
1515
arrayOne.push(arrayTwo);
1616

17-
const objectOne = {};
18-
const objectTwo = {objectOne};
17+
type ObjectOne = {
18+
objectTwo?: ObjectTwo,
19+
};
20+
type ObjectTwo = {
21+
objectOne: ObjectOne,
22+
};
23+
24+
const objectOne: ObjectOne = {};
25+
const objectTwo: ObjectTwo = {objectOne};
1926
objectOne.objectTwo = objectTwo;
2027

2128
export default function CircularReferences(): React.Node {

packages/react-devtools-shell/src/app/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ ignoreErrors([
3333
ignoreWarnings(['Warning: componentWillReceiveProps has been renamed']);
3434
ignoreLogs([]);
3535

36-
const unmountFunctions = [];
36+
const unmountFunctions: Array<() => void | boolean> = [];
3737

3838
function createContainer() {
3939
const container = document.createElement('div');

packages/react-devtools-timeline/src/TimelineSearchContext.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type Action =
4040

4141
type Dispatch = (action: Action) => void;
4242

43-
const EMPTY_ARRAY = [];
43+
const EMPTY_ARRAY: Array<ReactComponentMeasure> = [];
4444

4545
function reducer(state: State, action: Action): State {
4646
let {searchIndex, searchRegExp, searchResults, searchText} = state;

packages/react-dom-bindings/src/client/ReactDOMComponent.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ export function diffProperties(
672672
for (styleName in lastStyle) {
673673
if (lastStyle.hasOwnProperty(styleName)) {
674674
if (!styleUpdates) {
675-
styleUpdates = {};
675+
styleUpdates = ({}: {[string]: $FlowFixMe});
676676
}
677677
styleUpdates[styleName] = '';
678678
}
@@ -725,7 +725,7 @@ export function diffProperties(
725725
(!nextProp || !nextProp.hasOwnProperty(styleName))
726726
) {
727727
if (!styleUpdates) {
728-
styleUpdates = {};
728+
styleUpdates = ({}: {[string]: string});
729729
}
730730
styleUpdates[styleName] = '';
731731
}
@@ -737,7 +737,7 @@ export function diffProperties(
737737
lastProp[styleName] !== nextProp[styleName]
738738
) {
739739
if (!styleUpdates) {
740-
styleUpdates = {};
740+
styleUpdates = ({}: {[string]: $FlowFixMe});
741741
}
742742
styleUpdates[styleName] = nextProp[styleName];
743743
}

packages/react-dom-bindings/src/client/ReactDOMFloatClient.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,7 @@ export function getResource(
792792
function preloadPropsFromRawProps(
793793
rawBorrowedProps: PreloadQualifyingProps,
794794
): PreloadProps {
795+
// $FlowFixMe[prop-missing] - recommended fix is to use object spread operator
795796
return Object.assign({}, rawBorrowedProps);
796797
}
797798

@@ -805,6 +806,7 @@ function titlePropsFromRawProps(
805806
}
806807

807808
function stylePropsFromRawProps(rawProps: StyleQualifyingProps): StyleProps {
809+
// $FlowFixMe[prop-missing] - recommended fix is to use object spread operator
808810
const props: StyleProps = Object.assign({}, rawProps);
809811
props['data-precedence'] = rawProps.precedence;
810812
props.precedence = null;
@@ -813,6 +815,7 @@ function stylePropsFromRawProps(rawProps: StyleQualifyingProps): StyleProps {
813815
}
814816

815817
function scriptPropsFromRawProps(rawProps: ScriptQualifyingProps): ScriptProps {
818+
// $FlowFixMe[prop-missing] - recommended fix is to use object spread operator
816819
const props: ScriptProps = Object.assign({}, rawProps);
817820
return props;
818821
}
@@ -1318,7 +1321,9 @@ function acquireScriptResource(resource: ScriptResource): Instance {
13181321
}
13191322

13201323
function attachLoadListeners(instance: Instance, resource: StyleResource) {
1321-
const listeners = {};
1324+
const listeners: {
1325+
[string]: () => mixed,
1326+
} = {};
13221327
listeners.load = onResourceLoad.bind(
13231328
null,
13241329
instance,

packages/react-dom-bindings/src/client/ReactDOMSelect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function updateOptions(
7777

7878
if (multiple) {
7979
const selectedValues = (propValue: Array<string>);
80-
const selectedValue = {};
80+
const selectedValue: {[string]: boolean} = {};
8181
for (let i = 0; i < selectedValues.length; i++) {
8282
// Prefix to avoid chaos with special keys.
8383
selectedValue['$' + selectedValues[i]] = true;

packages/react-dom-bindings/src/client/validateDOMNesting.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ if (__DEV__) {
434434
return null;
435435
};
436436

437-
const didWarn = {};
437+
const didWarn: {[string]: boolean} = {};
438438

439439
validateDOMNesting = function(
440440
childTag: ?string,

0 commit comments

Comments
 (0)