Skip to content

Commit 356848d

Browse files
nickhudkinsmeta-codesync[bot]
authored andcommitted
chore(types): auto-generate types for metro-file-map (#1611)
Summary: As I began working to hand-fix some types, the formidable robhogan [pointed me](#1608 (comment)) in the direction of `generateTypeScriptDefinitions.js`, which has been used here to generate types for `metro-file-map` Changelog: [Fix]: Auto generate types for `metro-file-map` Pull Request resolved: #1611 Test Plan: Automatically tested in CI, also peformed a good-ol eyeball check. Reviewed By: vzaidman Differential Revision: D90583383 Pulled By: robhogan fbshipit-source-id: 626c8fc63407b6d9aa7cb18993ad1096455bd202
1 parent fb93c16 commit 356848d

58 files changed

Lines changed: 1513 additions & 311 deletions

Some content is hidden

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

.eslintrc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,14 @@ module.exports = {
6060
'import/no-commonjs': 'off',
6161
},
6262
},
63+
{
64+
files: [
65+
// Uses FileMapPlugin<any,any> as input type
66+
'packages/metro-file-map/types/index.d.ts',
67+
],
68+
rules: {
69+
'@typescript-eslint/no-explicit-any': 'off',
70+
},
71+
},
6372
],
6473
};

packages/metro-file-map/src/Watcher.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @format
87
* @flow strict-local
8+
* @format
99
*/
1010

1111
import type {
@@ -51,7 +51,7 @@ type WatcherOptions = {
5151
extensions: ReadonlyArray<string>,
5252
forceNodeFilesystemAPI: boolean,
5353
healthCheckFilePrefix: string,
54-
ignoreForCrawl: string => boolean,
54+
ignoreForCrawl: (filePath: string) => boolean,
5555
ignorePatternForWatch: RegExp,
5656
previousState: CrawlerOptions['previousState'],
5757
perfLogger: ?PerfLogger,

packages/metro-file-map/src/__tests__/index-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @format
87
* @flow
8+
* @format
99
* @oncall react_native
1010
*/
1111

packages/metro-file-map/src/cache/DiskCacheManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export class DiskCacheManager implements CacheManager {
150150
}
151151
}
152152

153-
async end() {
153+
async end(): Promise<void> {
154154
// Clear any timers
155155
if (this.#debounceTimeout) {
156156
clearTimeout(this.#debounceTimeout);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
type WatchmanQuery = {[key: string]: unknown};
11+
type WatchmanQuerySince = unknown;
12+
13+
export declare function planQuery(
14+
args: Readonly<{
15+
since: WatchmanQuerySince;
16+
directoryFilters: ReadonlyArray<string>;
17+
extensions: ReadonlyArray<string>;
18+
includeSha1: boolean;
19+
includeSymlinks: boolean;
20+
}>,
21+
): {
22+
query: WatchmanQuery;
23+
queryGenerator: string;
24+
};

packages/metro-file-map/src/flow-types.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export type BuildResult = {
4040
export type CacheData = Readonly<{
4141
clocks: WatchmanClocks,
4242
fileSystemData: unknown,
43-
plugins: ReadonlyMap<string, V8Serializable>,
43+
plugins: ReadonlyMap<string, void | V8Serializable>,
4444
}>;
4545

4646
export interface CacheManager {
@@ -85,7 +85,7 @@ export type CacheManagerFactoryOptions = Readonly<{
8585
export type CacheManagerWriteOptions = Readonly<{
8686
changedSinceCacheRead: boolean,
8787
eventSource: CacheManagerEventSource,
88-
onWriteError: Error => void,
88+
onWriteError: (error: Error) => void,
8989
}>;
9090

9191
// A path that is
@@ -206,11 +206,11 @@ export type V8Serializable =
206206
| ReadonlyArray<V8Serializable>
207207
| ReadonlySet<V8Serializable>
208208
| ReadonlyMap<string, V8Serializable>
209-
| {[key: string]: V8Serializable};
209+
| Readonly<{[key: string]: V8Serializable}>;
210210

211211
export interface FileMapPlugin<
212-
SerializableState = V8Serializable,
213-
PerFileData = void,
212+
SerializableState: void | V8Serializable = void | V8Serializable,
213+
PerFileData: void | V8Serializable = void | V8Serializable,
214214
> {
215215
+name: string;
216216
initialize(
@@ -427,7 +427,9 @@ export type HasteMapData = Map<string, HasteMapItem>;
427427

428428
export type HasteMapItem = {
429429
[platform: string]: HasteMapItemMetadata,
430+
/*:: // Hide from TypeScript
430431
__proto__: null,
432+
*/
431433
};
432434
export type HasteMapItemMetadata = [/* path */ string, /* type */ number];
433435

@@ -465,8 +467,8 @@ export type ReadOnlyRawMockMap = Readonly<{
465467

466468
export interface WatcherBackend {
467469
getPauseReason(): ?string;
468-
onError((error: Error) => void): () => void;
469-
onFileEvent((event: WatcherBackendChangeEvent) => void): () => void;
470+
onError(listener: (error: Error) => void): () => void;
471+
onFileEvent(listener: (event: WatcherBackendChangeEvent) => void): () => void;
470472
startWatching(): Promise<void>;
471473
stopWatching(): Promise<void>;
472474
}

packages/metro-file-map/src/lib/RootPathUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @format
87
* @flow strict
8+
* @format
99
*/
1010

1111
import invariant from 'invariant';

packages/metro-file-map/src/lib/TreeFS.js

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @format
87
* @flow strict-local
8+
* @format
99
*/
1010

1111
import type {
@@ -42,6 +42,37 @@ type NormalizedSymlinkTarget = {
4242
startOfBasenameIdx: number,
4343
};
4444

45+
type DeserializedSnapshotInput = {
46+
rootDir: string,
47+
fileSystemData: DirectoryNode,
48+
processFile: ProcessFileFunction,
49+
};
50+
51+
type TreeFSOptions = {
52+
rootDir: Path,
53+
files?: FileData,
54+
processFile: ProcessFileFunction,
55+
};
56+
57+
type MatchFilesOptions = Readonly<{
58+
/* Filter relative paths against a pattern. */
59+
filter?: ?RegExp,
60+
/* `filter` is applied against absolute paths, vs rootDir-relative. (default: false) */
61+
filterCompareAbsolute?: boolean,
62+
/* `filter` is applied against posix-delimited paths, even on Windows. (default: false) */
63+
filterComparePosix?: boolean,
64+
/* Follow symlinks when enumerating paths. (default: false) */
65+
follow?: boolean,
66+
/* Should search for files recursively. (default: true) */
67+
recursive?: boolean,
68+
/* Match files under a given root, or null for all files */
69+
rootDir?: ?Path,
70+
}>;
71+
72+
type MetadataIteratorOptions = Readonly<{
73+
includeSymlinks: boolean,
74+
includeNodeModules: boolean,
75+
}>;
4576
/**
4677
* OVERVIEW:
4778
*
@@ -99,15 +130,8 @@ export default class TreeFS implements MutableFileSystem {
99130
+#rootDir: Path;
100131
#rootNode: DirectoryNode = new Map();
101132

102-
constructor({
103-
rootDir,
104-
files,
105-
processFile,
106-
}: {
107-
rootDir: Path,
108-
files?: FileData,
109-
processFile: ProcessFileFunction,
110-
}) {
133+
constructor(opts: TreeFSOptions) {
134+
const {rootDir, files, processFile} = opts;
111135
this.#rootDir = rootDir;
112136
this.#pathUtils = new RootPathUtils(rootDir);
113137
this.#processFile = processFile;
@@ -120,15 +144,8 @@ export default class TreeFS implements MutableFileSystem {
120144
return this.#cloneTree(this.#rootNode);
121145
}
122146

123-
static fromDeserializedSnapshot({
124-
rootDir,
125-
fileSystemData,
126-
processFile,
127-
}: {
128-
rootDir: string,
129-
fileSystemData: DirectoryNode,
130-
processFile: ProcessFileFunction,
131-
}): TreeFS {
147+
static fromDeserializedSnapshot(args: DeserializedSnapshotInput): TreeFS {
148+
const {rootDir, fileSystemData, processFile} = args;
132149
const tfs = new TreeFS({processFile, rootDir});
133150
tfs.#rootNode = fileSystemData;
134151
return tfs;
@@ -302,27 +319,15 @@ export default class TreeFS implements MutableFileSystem {
302319
* The query matches against normalized paths which start with `./`,
303320
* for example: `a/b.js` -> `./a/b.js`
304321
*/
305-
*matchFiles({
306-
filter = null,
307-
filterCompareAbsolute = false,
308-
filterComparePosix = false,
309-
follow = false,
310-
recursive = true,
311-
rootDir = null,
312-
}: Readonly<{
313-
/* Filter relative paths against a pattern. */
314-
filter?: ?RegExp,
315-
/* `filter` is applied against absolute paths, vs rootDir-relative. (default: false) */
316-
filterCompareAbsolute?: boolean,
317-
/* `filter` is applied against posix-delimited paths, even on Windows. (default: false) */
318-
filterComparePosix?: boolean,
319-
/* Follow symlinks when enumerating paths. (default: false) */
320-
follow?: boolean,
321-
/* Should search for files recursively. (default: true) */
322-
recursive?: boolean,
323-
/* Match files under a given root, or null for all files */
324-
rootDir?: ?Path,
325-
}>): Iterable<Path> {
322+
*matchFiles(opts: MatchFilesOptions): Iterable<Path> {
323+
const {
324+
filter = null,
325+
filterCompareAbsolute = false,
326+
filterComparePosix = false,
327+
follow = false,
328+
recursive = true,
329+
rootDir = null,
330+
} = opts;
326331
const normalRoot = rootDir == null ? '' : this.#normalizePath(rootDir);
327332
const contextRootResult = this.#lookupByNormalPath(normalRoot);
328333
if (!contextRootResult.exists) {
@@ -993,12 +998,7 @@ export default class TreeFS implements MutableFileSystem {
993998
return null;
994999
}
9951000

996-
*metadataIterator(
997-
opts: Readonly<{
998-
includeSymlinks: boolean,
999-
includeNodeModules: boolean,
1000-
}>,
1001-
): Iterator<{
1001+
*metadataIterator(opts: MetadataIteratorOptions): Iterator<{
10021002
baseName: string,
10031003
canonicalPath: string,
10041004
metadata: FileMetadata,

packages/metro-file-map/src/lib/checkWatchmanCapabilities.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @format
87
* @flow strict
8+
* @format
99
*/
1010

1111
import {execFile} from 'child_process';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
declare const dependencyExtractor: {
11+
extract: (code: string) => Set<string>;
12+
};
13+
14+
export = dependencyExtractor;

0 commit comments

Comments
 (0)