Skip to content

Commit

Permalink
decentralize resolve types (#7966)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal authored and SimenB committed Feb 23, 2019
1 parent 7baf5fc commit fddf439
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 32 deletions.
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/moduleNameMapper.test.js.snap
Expand Up @@ -30,6 +30,6 @@ FAIL __tests__/index.js
12 | module.exports = () => 'test';
13 |
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:434:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:435:17)
at Object.require (index.js:10:1)
`;
Expand Up @@ -33,6 +33,6 @@ FAIL __tests__/test.js
| ^
4 |
at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:201:17)
at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:202:17)
at Object.require (index.js:3:18)
`;
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 @@ -23,7 +22,6 @@ export {
Matchers,
Mocks,
PrettyFormat,
Resolve,
Snapshot,
SourceMaps,
TestResult,
Expand Down

0 comments on commit fddf439

Please sign in to comment.