UNPKG

6.6 kBTypeScriptView Raw
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/// <reference types="node" />
8
9import type {Config} from '@jest/types';
10import type {Stats} from 'graceful-fs';
11
12declare type ChangeEvent = {
13 eventsQueue: EventsQueue;
14 hasteFS: HasteFS;
15 moduleMap: ModuleMap_2;
16};
17
18export declare class DuplicateError extends Error {
19 mockPath1: string;
20 mockPath2: string;
21 constructor(mockPath1: string, mockPath2: string);
22}
23
24declare class DuplicateHasteCandidatesError extends Error {
25 hasteName: string;
26 platform: string | null;
27 supportsNativePlatform: boolean;
28 duplicatesSet: DuplicatesSet;
29 constructor(
30 name: string,
31 platform: string,
32 supportsNativePlatform: boolean,
33 duplicatesSet: DuplicatesSet,
34 );
35}
36
37declare type DuplicatesIndex = Map<string, Map<string, DuplicatesSet>>;
38
39declare type DuplicatesSet = Map<string, /* type */ number>;
40
41declare type EventsQueue = Array<{
42 filePath: string;
43 stat: Stats | undefined;
44 type: string;
45}>;
46
47declare type FileData = Map<string, FileMetaData>;
48
49declare type FileMetaData = [
50 id: string,
51 mtime: number,
52 size: number,
53 visited: 0 | 1,
54 dependencies: string,
55 sha1: string | null | undefined,
56];
57
58declare class HasteFS implements IHasteFS {
59 private readonly _rootDir;
60 private readonly _files;
61 constructor({rootDir, files}: {rootDir: string; files: FileData});
62 getModuleName(file: string): string | null;
63 getSize(file: string): number | null;
64 getDependencies(file: string): Array<string> | null;
65 getSha1(file: string): string | null;
66 exists(file: string): boolean;
67 getAllFiles(): Array<string>;
68 getFileIterator(): Iterable<string>;
69 getAbsoluteFileIterator(): Iterable<string>;
70 matchFiles(pattern: RegExp | string): Array<string>;
71 matchFilesWithGlob(globs: Array<string>, root: string | null): Set<string>;
72 private _getFileData;
73}
74
75declare type HasteMapStatic<S = SerializableModuleMap> = {
76 getCacheFilePath(
77 tmpdir: string,
78 name: string,
79 ...extra: Array<string>
80 ): string;
81 getModuleMapFromJSON(json: S): IModuleMap<S>;
82};
83
84declare type HasteRegExp = RegExp | ((str: string) => boolean);
85
86declare type HType = {
87 ID: 0;
88 MTIME: 1;
89 SIZE: 2;
90 VISITED: 3;
91 DEPENDENCIES: 4;
92 SHA1: 5;
93 PATH: 0;
94 TYPE: 1;
95 MODULE: 0;
96 PACKAGE: 1;
97 GENERIC_PLATFORM: 'g';
98 NATIVE_PLATFORM: 'native';
99 DEPENDENCY_DELIM: '\0';
100};
101
102declare type HTypeValue = HType[keyof HType];
103
104export declare interface IHasteFS {
105 exists(path: string): boolean;
106 getAbsoluteFileIterator(): Iterable<string>;
107 getAllFiles(): Array<string>;
108 getDependencies(file: string): Array<string> | null;
109 getSize(path: string): number | null;
110 matchFiles(pattern: RegExp | string): Array<string>;
111 matchFilesWithGlob(
112 globs: ReadonlyArray<string>,
113 root: string | null,
114 ): Set<string>;
115}
116
117export declare interface IHasteMap {
118 on(eventType: 'change', handler: (event: ChangeEvent) => void): void;
119 build(): Promise<{
120 hasteFS: IHasteFS;
121 moduleMap: IModuleMap;
122 }>;
123}
124
125declare type IJestHasteMap = HasteMapStatic & {
126 create(options: Options): Promise<IHasteMap>;
127 getStatic(config: Config.ProjectConfig): HasteMapStatic;
128};
129
130export declare interface IModuleMap<S = SerializableModuleMap> {
131 getModule(
132 name: string,
133 platform?: string | null,
134 supportsNativePlatform?: boolean | null,
135 type?: HTypeValue | null,
136 ): string | null;
137 getPackage(
138 name: string,
139 platform: string | null | undefined,
140 _supportsNativePlatform: boolean | null,
141 ): string | null;
142 getMockModule(name: string): string | undefined;
143 getRawModuleMap(): RawModuleMap;
144 toJSON(): S;
145}
146
147declare const JestHasteMap: IJestHasteMap;
148export default JestHasteMap;
149
150declare type MockData = Map<string, string>;
151
152export declare const ModuleMap: {
153 create: (rootPath: string) => IModuleMap;
154};
155
156declare class ModuleMap_2 implements IModuleMap {
157 static DuplicateHasteCandidatesError: typeof DuplicateHasteCandidatesError;
158 private readonly _raw;
159 private json;
160 private static mapToArrayRecursive;
161 private static mapFromArrayRecursive;
162 constructor(raw: RawModuleMap);
163 getModule(
164 name: string,
165 platform?: string | null,
166 supportsNativePlatform?: boolean | null,
167 type?: HTypeValue | null,
168 ): string | null;
169 getPackage(
170 name: string,
171 platform: string | null | undefined,
172 _supportsNativePlatform: boolean | null,
173 ): string | null;
174 getMockModule(name: string): string | undefined;
175 getRawModuleMap(): RawModuleMap;
176 toJSON(): SerializableModuleMap;
177 static fromJSON(serializableModuleMap: SerializableModuleMap): ModuleMap_2;
178 /**
179 * When looking up a module's data, we walk through each eligible platform for
180 * the query. For each platform, we want to check if there are known
181 * duplicates for that name+platform pair. The duplication logic normally
182 * removes elements from the `map` object, but we want to check upfront to be
183 * extra sure. If metadata exists both in the `duplicates` object and the
184 * `map`, this would be a bug.
185 */
186 private _getModuleMetadata;
187 private _assertNoDuplicates;
188 static create(rootDir: string): ModuleMap_2;
189}
190
191declare type ModuleMapData = Map<string, ModuleMapItem>;
192
193declare type ModuleMapItem = {
194 [platform: string]: ModuleMetaData;
195};
196
197declare type ModuleMetaData = [path: string, type: number];
198
199declare type Options = {
200 cacheDirectory?: string;
201 computeDependencies?: boolean;
202 computeSha1?: boolean;
203 console?: Console;
204 dependencyExtractor?: string | null;
205 enableSymlinks?: boolean;
206 extensions: Array<string>;
207 forceNodeFilesystemAPI?: boolean;
208 hasteImplModulePath?: string;
209 hasteMapModulePath?: string;
210 id: string;
211 ignorePattern?: HasteRegExp;
212 maxWorkers: number;
213 mocksPattern?: string;
214 platforms: Array<string>;
215 resetCache?: boolean;
216 retainAllFiles: boolean;
217 rootDir: string;
218 roots: Array<string>;
219 skipPackageJson?: boolean;
220 throwOnModuleCollision?: boolean;
221 useWatchman?: boolean;
222 watch?: boolean;
223 workerThreads?: boolean;
224};
225
226declare type RawModuleMap = {
227 rootDir: string;
228 duplicates: DuplicatesIndex;
229 map: ModuleMapData;
230 mocks: MockData;
231};
232
233export declare type SerializableModuleMap = {
234 duplicates: ReadonlyArray<[string, [string, [string, [string, number]]]]>;
235 map: ReadonlyArray<[string, ValueType<ModuleMapData>]>;
236 mocks: ReadonlyArray<[string, ValueType<MockData>]>;
237 rootDir: string;
238};
239
240declare type ValueType<T> = T extends Map<string, infer V> ? V : never;
241
242export {};