Skip to content

Commit

Permalink
Implicit instantiation codemod
Browse files Browse the repository at this point in the history
Summary:
This diff adds explicit type arguments to polymorphic function calls that do not constrain their types. This codemod will reduce the error burden that will come in a future version of flow.

This specific diff was generated by running:
```
flow codemod annotate-implicit-instantiations --write .
flow --json --pretty | jq '.errors | .[] | .message | .[] | .loc |.source' | sort | uniq | sed -e 's/"//g' | xargs hg revert
hg st -n | xargs grep "generated" | sed -e 's/:.*//g' | xargs hg revert
arc f
```

So these are the codemod results that introduced no new errors and no generated files.

Changelog: [Internal]

drop-conflicts

Reviewed By: SamChou19815

Differential Revision: D40413074

fbshipit-source-id: 42b52719978f1098169662b503dbcfd8cefdad53
  • Loading branch information
jbrown215 authored and facebook-github-bot committed Oct 19, 2022
1 parent 3b0b98d commit 97cddba
Show file tree
Hide file tree
Showing 31 changed files with 121 additions and 62 deletions.
4 changes: 2 additions & 2 deletions packages/metro-file-map/src/ModuleMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import H from './constants';
import * as fastPath from './lib/fast_path';

const EMPTY_OBJ: {[string]: ModuleMetaData} = {};
const EMPTY_MAP = new Map();
const EMPTY_MAP = new Map<'g' | 'native' | string, ?DuplicatesSet>();

export default class ModuleMap implements IModuleMap<SerializableModuleMap> {
static DuplicateHasteCandidatesError: Class<DuplicateHasteCandidatesError>;
Expand Down Expand Up @@ -191,7 +191,7 @@ export default class ModuleMap implements IModuleMap<SerializableModuleMap> {
}
// Force flow refinement
const previousSet = relativePathSet;
const duplicates = new Map();
const duplicates = new Map<string, number>();

for (const [relativePath, type] of previousSet) {
const duplicatePath = fastPath.resolve(this._raw.rootDir, relativePath);
Expand Down
3 changes: 2 additions & 1 deletion packages/metro-file-map/src/crawlers/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @oncall react_native
*/

import type {Path, FileMetaData} from '../flow-types';
import type {
CrawlerOptions,
FileData,
Expand Down Expand Up @@ -224,7 +225,7 @@ module.exports = async function nodeCrawl(options: CrawlerOptions): Promise<{

return new Promise(resolve => {
const callback = (list: Result) => {
const files = new Map();
const files = new Map<Path, FileMetaData>();
const removedFiles = new Map(data.files);
list.forEach(fileData => {
const [filePath, mtime, size] = fileData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ function compareObjectType(
right: ObjectTypeAnnotation,
isInFunctionReturn: boolean,
): Array<ComparisonResult> {
const leftProps = new Map();
const rightProps = new Map();
const leftProps = new Map<string, ObjectTypeProperty>();
const rightProps = new Map<string, ObjectTypeProperty>();
const finalResult = [];
left.properties.forEach(prop => {
leftProps.set(prop.name, prop);
Expand Down
2 changes: 1 addition & 1 deletion packages/metro-resolver/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type FileTreeNode = $ReadOnly<{
}>;

const CONTEXT: ResolutionContext = (() => {
const fileSet = new Set();
const fileSet = new Set<string>();
(function fillFileSet(fileTree: FileTreeNode, prefix: string) {
for (const entName in fileTree) {
const entPath = path.join(prefix, entName);
Expand Down
6 changes: 3 additions & 3 deletions packages/metro-runtime/src/modules/HMRClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ class HMRClient extends EventEmitter {
}

function mergeUpdates(base: HmrUpdate, next: HmrUpdate): HmrUpdate {
const addedIDs = new Set();
const deletedIDs = new Set();
const moduleMap = new Map();
const addedIDs = new Set<number>();
const deletedIDs = new Set<number>();
const moduleMap = new Map<number, HmrModule>();

// Fill in the temporary maps and sets from both updates in their order.
applyUpdateLocally(base);
Expand Down
14 changes: 7 additions & 7 deletions packages/metro-runtime/src/polyfills/require.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ if (__DEV__) {
}

const Refresh = requireRefresh();
const refreshBoundaryIDs = new Set();
const refreshBoundaryIDs = new Set<ModuleID>();

// In this loop, we will traverse the dependency tree upwards from the
// changed module. Updates "bubble" up to the closest accepted parent.
Expand Down Expand Up @@ -646,7 +646,7 @@ if (__DEV__) {

// If we reached here, it is likely that hot reload will be successful.
// Run the actual factories.
const seenModuleIDs = new Set();
const seenModuleIDs = new Set<ModuleID>();
for (let i = 0; i < updatedModuleIDs.length; i++) {
const updatedID = updatedModuleIDs[i];
if (seenModuleIDs.has(updatedID)) {
Expand Down Expand Up @@ -754,8 +754,8 @@ if (__DEV__) {
earlyStop: T => boolean,
): Array<T> {
const result = [];
const visited = new Set();
const stack = new Set();
const visited = new Set<mixed>();
const stack = new Set<mixed>();
function traverseDependentNodes(node: T) {
if (stack.has(node)) {
throw CYCLE_DETECTED;
Expand Down Expand Up @@ -902,7 +902,7 @@ if (__DEV__) {
if (key === '__esModule') {
continue;
}
const desc = Object.getOwnPropertyDescriptor(moduleExports, key);
const desc = Object.getOwnPropertyDescriptor<any>(moduleExports, key);
if (desc && desc.get) {
// Don't invoke getters as they may have side effects.
return false;
Expand Down Expand Up @@ -949,7 +949,7 @@ if (__DEV__) {
if (key === '__esModule') {
continue;
}
const desc = Object.getOwnPropertyDescriptor(moduleExports, key);
const desc = Object.getOwnPropertyDescriptor<any>(moduleExports, key);
if (desc && desc.get) {
continue;
}
Expand All @@ -972,7 +972,7 @@ if (__DEV__) {
return;
}
for (const key in moduleExports) {
const desc = Object.getOwnPropertyDescriptor(moduleExports, key);
const desc = Object.getOwnPropertyDescriptor<any>(moduleExports, key);
if (desc && desc.get) {
// Don't invoke getters as they may have side effects.
continue;
Expand Down
2 changes: 1 addition & 1 deletion packages/metro-source-map/src/Consumer/MappingsConsumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class MappingsConsumer extends AbstractConsumer implements IConsumer {

const {mappings: mappingsRaw, names} = this._sourceMap;
let next;
const vlqCache = new Map();
const vlqCache = new Map<string, any>();
for (let i = 0; i < mappingsRaw.length; i = next) {
switch (mappingsRaw[i]) {
case ';':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function importExportPlugin({types: t}: {types: Types, ...}): PluginObj<State> {

if (remote.type === 'StringLiteral') {
// https://babeljs.io/docs/en/babel-plugin-syntax-module-string-names
throw path.buildCodeFrameError(
throw path.buildCodeFrameError<$FlowFixMeEmpty>(
'Module string names are not supported',
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/metro/src/Assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function buildAssetMap(
const assets = files.map((file: string) =>
AssetPaths.tryParse(file, platforms),
);
const map = new Map();
const map = new Map<string, {files: Array<string>, scales: Array<number>}>();
assets.forEach(function (asset: ?AssetPath, i: number) {
if (asset == null) {
return;
Expand Down
2 changes: 1 addition & 1 deletion packages/metro/src/Bundler/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function createRamBundleGroups<T: ModuleTransportLike>(

if (ramGroups.length > 1) {
// build a map of all grouped module IDs to an array of group root IDs
const all = new ArrayMap();
const all = new ArrayMap<number, number>();
for (const [parent, children] of result) {
for (const module of children) {
all.get(module).push(parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async function getRamBundleInfo(
dependenciesByPath: Map<string, ModuleTransportLike>,
): Set<number> => {
const deps = getTransitiveDependencies(module.sourcePath, graph);
const output = new Set();
const output = new Set<number>();

for (const dependency of deps) {
const module = dependenciesByPath.get(dependency);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @oncall react_native
*/

import type {Module, Options} from '../types.flow';
import type {Module, Options, Dependency} from '../types.flow';
import type {Result} from '../Graph';
import CountingSet from '../../lib/CountingSet';
import {Graph} from '../Graph';
Expand Down Expand Up @@ -124,21 +124,21 @@ describe('DeltaCalculator', () => {
getSource: () => Buffer.of(),
};
barModule = {
dependencies: new Map(),
dependencies: new Map<string, Dependency>(),
inverseDependencies: new CountingSet(['/bundle']),
output: [],
path: '/bar',
getSource: () => Buffer.of(),
};
bazModule = {
dependencies: new Map(),
dependencies: new Map<string, Dependency>(),
inverseDependencies: new CountingSet(['/bundle']),
output: [],
path: '/baz',
getSource: () => Buffer.of(),
};
quxModule = {
dependencies: new Map(),
dependencies: new Map<string, Dependency>(),
inverseDependencies: new CountingSet(['/foo']),
output: [],
path: '/qux',
Expand Down Expand Up @@ -346,7 +346,7 @@ describe('DeltaCalculator', () => {
});

const quxModule: Module<$FlowFixMe> = {
dependencies: new Map(),
dependencies: new Map<string, Dependency>(),
inverseDependencies: new CountingSet(),
output: [],
path: '/qux',
Expand Down
16 changes: 8 additions & 8 deletions packages/metro/src/DeltaBundler/__tests__/Graph-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let mockedDependencyTree: Map<
}>,
>,
> = new Map();
const files = new Set();
const files = new Set<string>();
let graph: TestGraph;
let options;

Expand Down Expand Up @@ -190,9 +190,9 @@ function computeDelta(
modules2: ReadOnlyDependencies<>,
modifiedPaths: Set<string>,
) {
const added = new Set();
const modified = new Set();
const deleted = new Set();
const added = new Set<string>();
const modified = new Set<string>();
const deleted = new Set<string>();

for (const id of modules1.keys()) {
if (!modules2.has(id)) {
Expand All @@ -219,7 +219,7 @@ function computeInverseDependencies(
graph: ReadOnlyGraph<>,
options: Options<>,
) {
const allInverseDependencies = new Map();
const allInverseDependencies = new Map<string, Set<string>>();
for (const path of graph.dependencies.keys()) {
allInverseDependencies.set(path, new Set());
}
Expand Down Expand Up @@ -267,7 +267,7 @@ class TestGraph extends Graph<> {
this,
options,
);
const actualInverseDependencies = new Map();
const actualInverseDependencies = new Map<string, Set<string>>();
for (const [path, module] of graph.dependencies) {
actualInverseDependencies.set(path, new Set(module.inverseDependencies));
}
Expand All @@ -278,7 +278,7 @@ class TestGraph extends Graph<> {
}

function getMatchingContextModules<T>(graph: Graph<T>, filePath: string) {
const contextPaths = new Set();
const contextPaths = new Set<string>();
graph.markModifiedContextModules(filePath, contextPaths);
return contextPaths;
}
Expand Down Expand Up @@ -2816,7 +2816,7 @@ describe('optional dependencies', () => {
let localGraph;
let localOptions;
const getAllDependencies = () => {
const all = new Set();
const all = new Set<string>();
mockedDependencyTree.forEach(deps => {
deps.forEach(r => all.add(r.name));
});
Expand Down
6 changes: 3 additions & 3 deletions packages/metro/src/DeltaBundler/mergeDeltas.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ function mergeDeltas(delta1: DeltaBundle, delta2: DeltaBundle): DeltaBundle {
const added2 = new Map(delta2.added);
const modified2 = new Map(delta2.modified);
const deleted2 = new Set(delta2.deleted);
const added = new Map();
const modified = new Map();
const deleted = new Set();
const added = new Map<number, string>();
const modified = new Map<number, string>();
const deleted = new Set<number>();

for (const [id, code] of added1) {
if (!deleted2.has(id) && !modified2.has(id)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/metro/src/ModuleGraph/node-haste/HasteFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ module.exports = class HasteFS {
};

function buildDirectorySet(files: Array<string>): Set<string> {
const directories = new Set();
const directories = new Set<string>();
files.forEach((path: string) => {
const parsedPath = parse(path);
const root = parsedPath.root;
Expand All @@ -88,7 +88,7 @@ function buildDirectorySet(files: Array<string>): Set<string> {
function buildDirectoryEntries(
files: Array<pathParseResult>,
): Map<string, Array<string>> {
const directoryEntries = new Map();
const directoryEntries = new Map<string, Array<string>>();
files.forEach(({base, dir}) => {
const entries = directoryEntries.get(dir);
if (entries) {
Expand Down
2 changes: 1 addition & 1 deletion packages/metro/src/ModuleGraph/node-haste/node-haste.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const createModuleMap = ({
(platforms ?? defaults.platforms).concat([NATIVE_PLATFORM]),
);

const map = new Map();
const map = new Map<string, ModuleMapItem>();

files.forEach((filePath: string) => {
if (isNodeModules(filePath)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function asMultipleFilesRamBundle({
.join('\n');

// Write one file per module, wrapped with __d() call if it proceeds.
const extraFiles = new Map();
const extraFiles = new Map<string, string | Buffer>();
deferredModules.forEach(deferredModule => {
extraFiles.set(
path.join(JS_MODULES, deferredModule.id + '.js'),
Expand Down
2 changes: 1 addition & 1 deletion packages/metro/src/ModuleGraph/output/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ exports.concat = function* concat<T>(
// Creates an idempotent function that returns numeric IDs for objects based
// on their `path` property.
exports.createIdForPathFn = (): (({path: string, ...}) => number) => {
const seen = new Map();
const seen = new Map<string, number>();
let next = 0;
return ({path}) => {
let id = seen.get(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ function collectDependencies<TSplitCondition = void>(
ast: BabelNodeFile,
options: Options<TSplitCondition>,
): CollectedDependencies<TSplitCondition> {
const visited = new WeakSet();
const visited = new WeakSet<BabelNodeCallExpression>();

const state: State<TSplitCondition> = {
asyncRequireModulePathStringLiteral: null,
Expand Down
2 changes: 1 addition & 1 deletion packages/metro/src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ class Server {
return frame;
});
// In case of multiple bundles / HMR, some stack frames can have different URLs from others
const urls = new Set();
const urls = new Set<string>();

stack.forEach(frame => {
const sourceUrl = frame.file;
Expand Down

0 comments on commit 97cddba

Please sign in to comment.