/
index.ts
140 lines (127 loc) · 3.79 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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {Config, Resolve, Snapshot} from '@jest/types';
import {FS as HasteFS} from 'jest-haste-map';
import Resolver from 'jest-resolve';
import {isSnapshotPath} from 'jest-snapshot';
/**
* DependencyResolver is used to resolve the direct dependencies of a module or
* to retrieve a list of all transitive inverse dependencies.
*/
class DependencyResolver {
private _hasteFS: HasteFS;
private _resolver: Resolver;
private _snapshotResolver: Snapshot.SnapshotResolver;
constructor(
resolver: Resolver,
hasteFS: HasteFS,
snapshotResolver: Snapshot.SnapshotResolver,
) {
this._resolver = resolver;
this._hasteFS = hasteFS;
this._snapshotResolver = snapshotResolver;
}
resolve(
file: Config.Path,
options?: Resolve.ResolveModuleConfig,
): Array<Config.Path> {
const dependencies = this._hasteFS.getDependencies(file);
if (!dependencies) {
return [];
}
return dependencies.reduce<Array<Config.Path>>((acc, dependency) => {
if (this._resolver.isCoreModule(dependency)) {
return acc;
}
let resolvedDependency;
try {
resolvedDependency = this._resolver.resolveModule(
file,
dependency,
options,
);
} catch (e) {
resolvedDependency = this._resolver.getMockModule(file, dependency);
}
if (resolvedDependency) {
acc.push(resolvedDependency);
}
return acc;
}, []);
}
resolveInverseModuleMap(
paths: Set<Config.Path>,
filter: (file: Config.Path) => boolean,
options?: Resolve.ResolveModuleConfig,
): Array<Resolve.ResolvedModule> {
if (!paths.size) {
return [];
}
const collectModules = (
related: Set<Config.Path>,
moduleMap: Array<Resolve.ResolvedModule>,
changed: Set<Config.Path>,
) => {
const visitedModules = new Set();
const result: Array<Resolve.ResolvedModule> = [];
while (changed.size) {
changed = new Set(
moduleMap.reduce<Array<Config.Path>>((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(module.file);
return acc;
}, []),
);
}
return result.concat(
Array.from(related).map(file => ({dependencies: [], file})),
);
};
const relatedPaths = new Set<Config.Path>();
const changed: Set<Config.Path> = new Set();
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<Resolve.ResolvedModule> = [];
for (const file of this._hasteFS.getAbsoluteFileIterator()) {
modules.push({
dependencies: this.resolve(file, options),
file,
});
}
return collectModules(relatedPaths, modules, changed);
}
resolveInverse(
paths: Set<Config.Path>,
filter: (file: Config.Path) => boolean,
options?: Resolve.ResolveModuleConfig,
): Array<Config.Path> {
return this.resolveInverseModuleMap(paths, filter, options).map(
module => module.file,
);
}
}
export = DependencyResolver;