Skip to content

Commit

Permalink
[flow] enable exact_empty_objects (#25973)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
kassens committed Jan 9, 2023
1 parent 0b4f443 commit e2424f3
Show file tree
Hide file tree
Showing 49 changed files with 182 additions and 135 deletions.
4 changes: 3 additions & 1 deletion packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ function createElement(
// We don't really need to add any of these but keeping them for good measure.
// Unfortunately, _store is enumerable in jest matchers so for equality to
// work, I need to keep it or make _store non-enumerable in the other file.
element._store = {};
element._store = ({}: {
validated?: boolean,
});
Object.defineProperty(element._store, 'validated', {
configurable: false,
enumerable: false,
Expand Down
4 changes: 2 additions & 2 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ function buildTree(
readHookLog: Array<HookLogEntry>,
includeHooksSource: boolean,
): HooksTree {
const rootChildren = [];
const rootChildren: Array<HooksNode> = [];
let prevStack = null;
let levelChildren = rootChildren;
let nativeHookID = 0;
Expand Down Expand Up @@ -557,7 +557,7 @@ function buildTree(
// The remaining part of the new stack are custom hooks. Push them
// to the tree.
for (let j = stack.length - commonSteps - 1; j >= 1; j--) {
const children = [];
const children: Array<HooksNode> = [];
const stackFrame = stack[j];
const levelChild: HooksNode = {
id: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function measureStyle(
}

function shallowClone(object: Object): Object {
const cloned = {};
const cloned: {[string]: $FlowFixMe} = {};
for (const n in object) {
cloned[n] = object[n];
}
Expand Down
8 changes: 4 additions & 4 deletions packages/react-devtools-shared/src/backend/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const injectedRenderers: Map<
> = new Map();

let targetConsole: Object = console;
let targetConsoleMethods = {};
let targetConsoleMethods: {[string]: $FlowFixMe} = {};
for (const method in console) {
targetConsoleMethods[method] = console[method];
}
Expand All @@ -97,7 +97,7 @@ export function dangerous_setTargetConsoleForTesting(
): void {
targetConsole = targetConsoleForTesting;

targetConsoleMethods = {};
targetConsoleMethods = ({}: {[string]: $FlowFixMe});
for (const method in targetConsole) {
targetConsoleMethods[method] = console[method];
}
Expand Down Expand Up @@ -179,7 +179,7 @@ export function patch({
return;
}

const originalConsoleMethods = {};
const originalConsoleMethods: {[string]: $FlowFixMe} = {};

unpatchFn = () => {
for (const method in originalConsoleMethods) {
Expand Down Expand Up @@ -318,7 +318,7 @@ export function patchForStrictMode() {
return;
}

const originalConsoleMethods = {};
const originalConsoleMethods: {[string]: $FlowFixMe} = {};

unpatchForStrictModeFn = () => {
for (const method in originalConsoleMethods) {
Expand Down
6 changes: 3 additions & 3 deletions packages/react-devtools-shared/src/backend/legacy/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ export function attach(

let owner = element._owner;
if (owner) {
owners = [];
owners = ([]: Array<SerializedElement>);
while (owner != null) {
owners.push({
displayName: getData(owner).displayName || 'Unknown',
Expand All @@ -807,8 +807,8 @@ export function attach(
}

// Not implemented
const errors = [];
const warnings = [];
const errors: Array<[string, number]> = [];
const warnings: Array<[string, number]> = [];

return {
id,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-devtools-shared/src/backend/legacy/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function decorateMany(
source: Object,
fns: {[attr: string]: Function, ...},
): Object {
const olds = {};
const olds: {[string]: $FlowFixMe} = {};
for (const name in fns) {
olds[name] = decorate(source, name, fns[name]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ let supportsUserTiming =
let supportsUserTimingV3 = false;
if (supportsUserTiming) {
const CHECK_V3_MARK = '__v3';
const markOptions = {};
const markOptions = ({}: {startTime?: number});
Object.defineProperty(markOptions, 'startTime', {
get: function() {
supportsUserTimingV3 = true;
Expand Down
4 changes: 2 additions & 2 deletions packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3252,7 +3252,7 @@ export function attach(

let owners = null;
if (_debugOwner) {
owners = [];
owners = ([]: Array<SerializedElement>);
let owner: null | Fiber = _debugOwner;
while (owner !== null) {
owners.push(fiberToSerializedElement(owner));
Expand All @@ -3265,7 +3265,7 @@ export function attach(

let hooks = null;
if (usesHooks) {
const originalConsoleMethods = {};
const originalConsoleMethods: {[string]: $FlowFixMe} = {};

// Temporarily disable all console logging before re-running the hook.
for (const method in console) {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-devtools-shared/src/backend/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export function cleanForBridge(
path?: Array<string | number> = [],
): DehydratedData | null {
if (data !== null) {
const cleanedPaths = [];
const unserializablePaths = [];
const cleanedPaths: Array<Array<string | number>> = [];
const unserializablePaths: Array<Array<string | number>> = [];
const cleanedData = dehydrate(
data,
cleanedPaths,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-devtools-shared/src/devtools/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const STACK_SOURCE_LOCATION = /([^\s]+) \((.+):(.+):(.+)\)/;
export function stackToComponentSources(
stack: string,
): Array<[string, ?Stack]> {
const out = [];
const out: Array<[string, ?Stack]> = [];
stack
.split(STACK_DELIMETER)
.slice(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,16 @@ export type InspectedElement = {

// TODO: Add profiling type

type Data =
| string
| Dehydrated
| Unserializable
| Array<Dehydrated>
| Array<Unserializable>
| {[string]: Data};

export type DehydratedData = {
cleaned: Array<Array<string | number>>,
data:
| string
| Dehydrated
| Unserializable
| Array<Dehydrated>
| Array<Unserializable>
| {[key: string]: string | Dehydrated | Unserializable, ...},
data: Data,
unserializable: Array<Array<string | number>>,
};
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ function updateTree(

// Clone nodes before mutating them so edits don't affect them.
const getClonedNode = (id: number): CommitTreeNode => {
// $FlowFixMe[prop-missing] - recommended fix is to use object spread operator
const clonedNode = ((Object.assign(
{},
nodes.get(id),
Expand Down
2 changes: 1 addition & 1 deletion packages/react-devtools-shared/src/devtools/views/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export function serializeDataForCopy(props: Object): string {

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

const queue = [...cloned];

Expand Down
10 changes: 5 additions & 5 deletions packages/react-devtools-shared/src/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function installHook(target: any): DevToolsHook | null {
}

let targetConsole: Object = console;
let targetConsoleMethods = {};
let targetConsoleMethods: {[string]: $FlowFixMe} = {};
for (const method in console) {
targetConsoleMethods[method] = console[method];
}
Expand All @@ -39,7 +39,7 @@ export function installHook(target: any): DevToolsHook | null {
): void {
targetConsole = targetConsoleForTesting;

targetConsoleMethods = {};
targetConsoleMethods = ({}: {[string]: $FlowFixMe});
for (const method in targetConsole) {
targetConsoleMethods[method] = console[method];
}
Expand Down Expand Up @@ -250,7 +250,7 @@ export function installHook(target: any): DevToolsHook | null {
return;
}

const originalConsoleMethods = {};
const originalConsoleMethods: {[string]: $FlowFixMe} = {};

unpatchFn = () => {
for (const method in originalConsoleMethods) {
Expand Down Expand Up @@ -516,9 +516,9 @@ export function installHook(target: any): DevToolsHook | null {
}

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

const hook: DevToolsHook = {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-devtools-shared/src/hooks/astUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ function getPotentialHookDeclarationsFromAST(sourceAST: File): NodePath[] {
export function getHookNamesMappingFromAST(
sourceAST: File,
): $ReadOnlyArray<{name: string, start: Position}> {
const hookStack = [];
const hookStack: Array<{name: string, start: $FlowFixMe}> = [];
const hookNames = [];
const pushFrame = (name: string, node: Node) => {
const nameInfo = {name, start: {...node.loc.start}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function generateHookMap(sourceAST: File): HookMap {
const hookNamesMapping = getHookNamesMappingFromAST(sourceAST);
const namesMap: Map<string, number> = new Map();
const names = [];
const mappings = [];
const mappings: Array<HookMapLine> = [];

let currentLine = null;
hookNamesMapping.forEach(({name, start}) => {
Expand Down
14 changes: 5 additions & 9 deletions packages/react-devtools-shared/src/hydration.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
setInObject,
} from './utils';

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

export const meta = {
inspectable: (Symbol('inspectable'): symbol),
Expand Down Expand Up @@ -124,13 +124,7 @@ export function dehydrate(
path: Array<string | number>,
isPathAllowed: (path: Array<string | number>) => boolean,
level?: number = 0,
):
| string
| Dehydrated
| Unserializable
| Array<Dehydrated>
| Array<Unserializable>
| {[key: string]: string | Dehydrated | Unserializable, ...} {
): $PropertyType<DehydratedData, 'data'> {
const type = getDataType(data);

let isPathAllowedCheck;
Expand Down Expand Up @@ -304,7 +298,9 @@ export function dehydrate(
if (level >= LEVEL_THRESHOLD && !isPathAllowedCheck) {
return createDehydrated(type, true, data, cleaned, path);
} else {
const object = {};
const object: {
[string]: $PropertyType<DehydratedData, 'data'>,
} = {};
getAllEnumerableKeys(data).forEach(key => {
const name = key.toString();
object[name] = dehydrate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ const arrayTwo = [];
arrayTwo.push(arrayOne);
arrayOne.push(arrayTwo);

const objectOne = {};
const objectTwo = {objectOne};
type ObjectOne = {
objectTwo?: ObjectTwo,
};
type ObjectTwo = {
objectOne: ObjectOne,
};

const objectOne: ObjectOne = {};
const objectTwo: ObjectTwo = {objectOne};
objectOne.objectTwo = objectTwo;

export default function CircularReferences(): React.Node {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-devtools-shell/src/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ignoreErrors([
ignoreWarnings(['Warning: componentWillReceiveProps has been renamed']);
ignoreLogs([]);

const unmountFunctions = [];
const unmountFunctions: Array<() => void | boolean> = [];

function createContainer() {
const container = document.createElement('div');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Action =

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

const EMPTY_ARRAY = [];
const EMPTY_ARRAY: Array<ReactComponentMeasure> = [];

function reducer(state: State, action: Action): State {
let {searchIndex, searchRegExp, searchResults, searchText} = state;
Expand Down
6 changes: 3 additions & 3 deletions packages/react-dom-bindings/src/client/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ export function diffProperties(
for (styleName in lastStyle) {
if (lastStyle.hasOwnProperty(styleName)) {
if (!styleUpdates) {
styleUpdates = {};
styleUpdates = ({}: {[string]: $FlowFixMe});
}
styleUpdates[styleName] = '';
}
Expand Down Expand Up @@ -725,7 +725,7 @@ export function diffProperties(
(!nextProp || !nextProp.hasOwnProperty(styleName))
) {
if (!styleUpdates) {
styleUpdates = {};
styleUpdates = ({}: {[string]: string});
}
styleUpdates[styleName] = '';
}
Expand All @@ -737,7 +737,7 @@ export function diffProperties(
lastProp[styleName] !== nextProp[styleName]
) {
if (!styleUpdates) {
styleUpdates = {};
styleUpdates = ({}: {[string]: $FlowFixMe});
}
styleUpdates[styleName] = nextProp[styleName];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ export function getResource(
function preloadPropsFromRawProps(
rawBorrowedProps: PreloadQualifyingProps,
): PreloadProps {
// $FlowFixMe[prop-missing] - recommended fix is to use object spread operator
return Object.assign({}, rawBorrowedProps);
}

Expand All @@ -805,6 +806,7 @@ function titlePropsFromRawProps(
}

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

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

function attachLoadListeners(instance: Instance, resource: StyleResource) {
const listeners = {};
const listeners: {
[string]: () => mixed,
} = {};
listeners.load = onResourceLoad.bind(
null,
instance,
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom-bindings/src/client/ReactDOMSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function updateOptions(

if (multiple) {
const selectedValues = (propValue: Array<string>);
const selectedValue = {};
const selectedValue: {[string]: boolean} = {};
for (let i = 0; i < selectedValues.length; i++) {
// Prefix to avoid chaos with special keys.
selectedValue['$' + selectedValues[i]] = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ if (__DEV__) {
return null;
};

const didWarn = {};
const didWarn: {[string]: boolean} = {};

validateDOMNesting = function(
childTag: ?string,
Expand Down
Loading

0 comments on commit e2424f3

Please sign in to comment.