Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use reduce instead of filter+map in dependency_resolver #7522

Merged
merged 1 commit into from
Dec 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 37 additions & 25 deletions packages/jest-resolve-dependencies/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,27 @@ class DependencyResolver {
return [];
}

return dependencies
.map(dependency => {
if (this._resolver.isCoreModule(dependency)) {
return null;
}
try {
return this._resolver.resolveModule(file, dependency, options);
} catch (e) {}
return this._resolver.getMockModule(file, dependency);
})
.filter(Boolean);
return dependencies.reduce((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;
}, []);
}

resolveInverse(
Expand All @@ -64,20 +74,22 @@ class DependencyResolver {
const visitedModules = new Set();
while (changed.size) {
changed = new Set(
moduleMap
.filter(
module =>
!visitedModules.has(module.file) &&
module.dependencies.some(dep => dep && changed.has(dep)),
)
.map(module => {
const file = module.file;
if (filter(file)) {
relatedPaths.add(file);
}
visitedModules.add(file);
return module.file;
}),
moduleMap.reduce((acc, module) => {
if (
visitedModules.has(module.file) ||
!module.dependencies.some(dep => dep && changed.has(dep))
) {
return acc;
}

const file = module.file;
if (filter(file)) {
relatedPaths.add(file);
}
visitedModules.add(file);
acc.push(module.file);
return acc;
}, []),
);
}
return relatedPaths;
Expand Down