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

decentralize resolve types #7966

Merged
merged 2 commits into from Feb 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/jest-haste-map/src/index.ts
Expand Up @@ -221,6 +221,7 @@ const getWhiteList = (list: Array<string> | undefined): RegExp | null => {
* Worker processes can directly access the cache through `HasteMap.read()`.
*
*/
/* eslint-disable-next-line no-redeclare */
class HasteMap extends EventEmitter {
private _buildPromise: Promise<HasteMapObject> | null;
private _cachePath: Config.Path;
Expand Down
24 changes: 16 additions & 8 deletions packages/jest-resolve-dependencies/src/index.ts
Expand Up @@ -5,15 +5,23 @@
* LICENSE file in the root directory of this source tree.
*/

import {Config, Resolve, Snapshot} from '@jest/types';
import {Config, Snapshot} from '@jest/types';
import {FS as HasteFS} from 'jest-haste-map';
import Resolver from 'jest-resolve';
import {isSnapshotPath} from 'jest-snapshot';

namespace DependencyResolver {
export type ResolvedModule = {
file: Config.Path;
dependencies: Config.Path[];
};
}

/**
* DependencyResolver is used to resolve the direct dependencies of a module or
* to retrieve a list of all transitive inverse dependencies.
*/
/* eslint-disable-next-line no-redeclare */
class DependencyResolver {
private _hasteFS: HasteFS;
private _resolver: Resolver;
Expand All @@ -31,7 +39,7 @@ class DependencyResolver {

resolve(
file: Config.Path,
options?: Resolve.ResolveModuleConfig,
options?: Resolver.ResolveModuleConfig,
): Array<Config.Path> {
const dependencies = this._hasteFS.getDependencies(file);
if (!dependencies) {
Expand Down Expand Up @@ -64,19 +72,19 @@ class DependencyResolver {
resolveInverseModuleMap(
paths: Set<Config.Path>,
filter: (file: Config.Path) => boolean,
options?: Resolve.ResolveModuleConfig,
): Array<Resolve.ResolvedModule> {
options?: Resolver.ResolveModuleConfig,
): Array<DependencyResolver.ResolvedModule> {
if (!paths.size) {
return [];
}

const collectModules = (
related: Set<Config.Path>,
moduleMap: Array<Resolve.ResolvedModule>,
moduleMap: Array<DependencyResolver.ResolvedModule>,
changed: Set<Config.Path>,
) => {
const visitedModules = new Set();
const result: Array<Resolve.ResolvedModule> = [];
const result: Array<DependencyResolver.ResolvedModule> = [];
while (changed.size) {
changed = new Set(
moduleMap.reduce<Array<Config.Path>>((acc, module) => {
Expand Down Expand Up @@ -116,7 +124,7 @@ class DependencyResolver {
}
}
}
const modules: Array<Resolve.ResolvedModule> = [];
const modules: Array<DependencyResolver.ResolvedModule> = [];
for (const file of this._hasteFS.getAbsoluteFileIterator()) {
modules.push({
dependencies: this.resolve(file, options),
Expand All @@ -129,7 +137,7 @@ class DependencyResolver {
resolveInverse(
paths: Set<Config.Path>,
filter: (file: Config.Path) => boolean,
options?: Resolve.ResolveModuleConfig,
options?: Resolver.ResolveModuleConfig,
): Array<Config.Path> {
return this.resolveInverseModuleMap(paths, filter, options).map(
module => module.file,
Expand Down
14 changes: 11 additions & 3 deletions packages/jest-resolve/src/index.ts
Expand Up @@ -6,7 +6,7 @@
*/

import path from 'path';
import {Config, Resolve} from '@jest/types';
import {Config} from '@jest/types';
import {ModuleMap} from 'jest-haste-map';
import {sync as realpath} from 'realpath-native';
import chalk from 'chalk';
Expand All @@ -27,6 +27,13 @@ type FindNodeModuleConfig = {

type BooleanObject = {[key: string]: boolean};

namespace Resolver {
export type ResolveModuleConfig = {
skipNodeResolution?: boolean;
paths?: Config.Path[];
};
}

const NATIVE_PLATFORM = 'native';

// We might be inside a symlink.
Expand All @@ -39,6 +46,7 @@ const nodePaths = process.env.NODE_PATH
.map(p => path.resolve(resolvedCwd, p))
: null;

/* eslint-disable-next-line no-redeclare */
class Resolver {
private readonly _options: ResolverConfig;
private readonly _moduleMap: ModuleMap;
Expand Down Expand Up @@ -97,7 +105,7 @@ class Resolver {
resolveModuleFromDirIfExists(
dirname: Config.Path,
moduleName: string,
options?: Resolve.ResolveModuleConfig,
options?: Resolver.ResolveModuleConfig,
): Config.Path | null {
const paths = (options && options.paths) || this._options.modulePaths;
const moduleDirectory = this._options.moduleDirectories;
Expand Down Expand Up @@ -180,7 +188,7 @@ class Resolver {
resolveModule(
from: Config.Path,
moduleName: string,
options?: Resolve.ResolveModuleConfig,
options?: Resolver.ResolveModuleConfig,
): Config.Path {
const dirname = path.dirname(from);
const module = this.resolveModuleFromDirIfExists(
Expand Down
17 changes: 0 additions & 17 deletions packages/jest-types/src/Resolve.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/jest-types/src/index.ts
Expand Up @@ -10,7 +10,6 @@ import * as Console from './Console';
import * as Matchers from './Matchers';
import * as Mocks from './Mocks';
import * as PrettyFormat from './PrettyFormat';
import * as Resolve from './Resolve';
import * as Snapshot from './Snapshot';
import * as SourceMaps from './SourceMaps';
import * as TestResult from './TestResult';
Expand All @@ -21,7 +20,6 @@ export {
Matchers,
Mocks,
PrettyFormat,
Resolve,
Snapshot,
SourceMaps,
TestResult,
Expand Down