-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
index.ts
174 lines (155 loc) · 4.71 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as path from 'path';
import type {IHasteFS} from 'jest-haste-map';
import type {ResolveModuleConfig, default as Resolver} from 'jest-resolve';
import {type SnapshotResolver, isSnapshotPath} from 'jest-snapshot';
export type ResolvedModule = {
file: string;
dependencies: Array<string>;
};
/**
* DependencyResolver is used to resolve the direct dependencies of a module or
* to retrieve a list of all transitive inverse dependencies.
*/
export class DependencyResolver {
private readonly _hasteFS: IHasteFS;
private readonly _resolver: Resolver;
private readonly _snapshotResolver: SnapshotResolver;
constructor(
resolver: Resolver,
hasteFS: IHasteFS,
snapshotResolver: SnapshotResolver,
) {
this._resolver = resolver;
this._hasteFS = hasteFS;
this._snapshotResolver = snapshotResolver;
}
resolve(file: string, options?: ResolveModuleConfig): Array<string> {
const dependencies = this._hasteFS.getDependencies(file);
if (!dependencies) {
return [];
}
return dependencies.reduce<Array<string>>((acc, dependency) => {
if (this._resolver.isCoreModule(dependency)) {
return acc;
}
let resolvedDependency;
let resolvedMockDependency;
try {
resolvedDependency = this._resolver.resolveModule(
file,
dependency,
options,
);
} catch {
try {
resolvedDependency = this._resolver.getMockModule(file, dependency);
} catch {
// leave resolvedDependency as undefined if nothing can be found
}
}
if (resolvedDependency == null) {
return acc;
}
acc.push(resolvedDependency);
// If we resolve a dependency, then look for a mock dependency
// of the same name in that dependency's directory.
try {
resolvedMockDependency = this._resolver.getMockModule(
resolvedDependency,
path.basename(dependency),
);
} catch {
// leave resolvedMockDependency as undefined if nothing can be found
}
if (resolvedMockDependency != null) {
const dependencyMockDir = path.resolve(
path.dirname(resolvedDependency),
'__mocks__',
);
resolvedMockDependency = path.resolve(resolvedMockDependency);
// make sure mock is in the correct directory
if (dependencyMockDir === path.dirname(resolvedMockDependency)) {
acc.push(resolvedMockDependency);
}
}
return acc;
}, []);
}
resolveInverseModuleMap(
paths: Set<string>,
filter: (file: string) => boolean,
options?: ResolveModuleConfig,
): Array<ResolvedModule> {
if (paths.size === 0) {
return [];
}
const collectModules = (
related: Set<string>,
moduleMap: Array<ResolvedModule>,
changed: Set<string>,
) => {
const visitedModules = new Set();
const result: Array<ResolvedModule> = [];
while (changed.size > 0) {
changed = new Set(
moduleMap.reduce<Array<string>>((acc, module) => {
if (
visitedModules.has(module.file) ||
!module.dependencies.some(dep => changed.has(dep))
) {
return acc;
}
const file = module.file;
if (filter(file)) {
result.push(module);
related.delete(file);
}
visitedModules.add(file);
acc.push(file);
return acc;
}, []),
);
}
return [
...result,
...[...related].map(file => ({dependencies: [], file})),
];
};
const relatedPaths = new Set<string>();
const changed = new Set<string>();
for (const path of paths) {
if (this._hasteFS.exists(path)) {
const modulePath = isSnapshotPath(path)
? this._snapshotResolver.resolveTestPath(path)
: path;
changed.add(modulePath);
if (filter(modulePath)) {
relatedPaths.add(modulePath);
}
}
}
const modules: Array<ResolvedModule> = [];
for (const file of this._hasteFS.getAbsoluteFileIterator()) {
modules.push({
dependencies: this.resolve(file, options),
file,
});
}
return collectModules(relatedPaths, modules, changed);
}
resolveInverse(
paths: Set<string>,
filter: (file: string) => boolean,
options?: ResolveModuleConfig,
): Array<string> {
return this.resolveInverseModuleMap(paths, filter, options).map(
module => module.file,
);
}
}