index.d.ts 6.4 KB

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