Skip to content

Commit

Permalink
remove Path
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 22, 2022
1 parent 23cc87c commit 7344e67
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 123 deletions.
5 changes: 2 additions & 3 deletions packages/jest-resolve/src/ModuleNotFoundError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

import * as path from 'path';
import slash = require('slash');
import type {Config} from '@jest/types';

export default class ModuleNotFoundError extends Error {
public code = 'MODULE_NOT_FOUND';
public hint?: string;
public requireStack?: Array<Config.Path>;
public requireStack?: Array<string>;
public siblingWithSimilarExtensionFound?: boolean;
public moduleName?: string;

Expand All @@ -24,7 +23,7 @@ export default class ModuleNotFoundError extends Error {
this.moduleName = moduleName;
}

public buildMessage(rootDir: Config.Path): void {
public buildMessage(rootDir: string): void {
if (!this._originalMessage) {
this._originalMessage = this.message || '';
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/jest-resolve/src/__mocks__/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "__mocks__",
"version": "1.0.0",
"dependencies": {
}
"dependencies": {}
}
29 changes: 14 additions & 15 deletions packages/jest-resolve/src/defaultResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
resolve as resolveExports,
} from 'resolve.exports';
import slash = require('slash');
import type {Config} from '@jest/types';
import {
PkgJson,
isDirectoryAsync,
Expand All @@ -30,14 +29,14 @@ const resolveSync = resolveAsync.sync;
// copy from `resolve`'s types so we don't have their types in our definition
// files
export interface ResolverOptions {
basedir: Config.Path;
basedir: string;
browser?: boolean;
conditions?: Array<string>;
defaultResolver: typeof defaultResolver;
extensions?: Array<string>;
moduleDirectory?: Array<string>;
paths?: Array<Config.Path>;
rootDir?: Config.Path;
paths?: Array<string>;
rootDir?: string;
packageFilter?: (pkg: PkgJson, dir: string) => PkgJson;
pathFilter?: (pkg: PkgJson, path: string, relativePath: string) => string;
}
Expand All @@ -56,9 +55,9 @@ declare global {
}

export function defaultResolver(
path: Config.Path,
path: string,
options: ResolverOptions,
): Config.Path {
): string {
// Yarn 2 adds support to `resolve` automatically so the pnpResolver is only
// needed for Yarn 1 which implements version 1 of the pnp spec
if (process.versions.pnp === '1') {
Expand All @@ -73,9 +72,9 @@ export function defaultResolver(
}

export async function defaultResolverAsync(
path: Config.Path,
path: string,
options: ResolverOptionsAsync,
): Promise<Config.Path> {
): Promise<string> {
// Yarn 2 adds support to `resolve` automatically so the pnpResolver is only
// needed for Yarn 1 which implements version 1 of the pnp spec
if (process.versions.pnp === '1') {
Expand All @@ -101,7 +100,7 @@ export async function defaultResolverAsync(
* getSyncResolveOptions returns resolution options that are used synchronously.
*/
function getSyncResolveOptions(
path: Config.Path,
path: string,
options: ResolverOptions,
): resolveAsync.SyncOpts {
return {
Expand All @@ -120,7 +119,7 @@ function getSyncResolveOptions(
* getAsyncResolveOptions returns resolution options that are used asynchronously.
*/
function getAsyncResolveOptions(
path: Config.Path,
path: string,
options: ResolverOptionsAsync,
): resolveAsync.AsyncOpts {
return {
Expand All @@ -139,13 +138,13 @@ function getAsyncResolveOptions(
* helper functions
*/

function readPackageSync(_: unknown, file: Config.Path): PkgJson {
function readPackageSync(_: unknown, file: string): PkgJson {
return readPackageCached(file);
}

function readPackageAsync(
_: unknown,
pkgfile: Config.Path,
pkgfile: string,
cb: (err: Error | null, pkgJson?: PkgJson) => void,
): void {
try {
Expand All @@ -158,7 +157,7 @@ function readPackageAsync(
}

function createPackageFilter(
originalPath: Config.Path,
originalPath: string,
userFilter?: ResolverOptions['packageFilter'],
): ResolverOptions['packageFilter'] {
if (shouldIgnoreRequestForExports(originalPath)) {
Expand Down Expand Up @@ -186,7 +185,7 @@ function createPackageFilter(
}

function createPathFilter(
originalPath: Config.Path,
originalPath: string,
conditions?: Array<string>,
userFilter?: ResolverOptions['pathFilter'],
): ResolverOptions['pathFilter'] {
Expand Down Expand Up @@ -221,5 +220,5 @@ function createPathFilter(
}

// if it's a relative import or an absolute path, exports are ignored
const shouldIgnoreRequestForExports = (path: Config.Path) =>
const shouldIgnoreRequestForExports = (path: string) =>
path.startsWith('.') || isAbsolute(path);
23 changes: 10 additions & 13 deletions packages/jest-resolve/src/fileWalkers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import {dirname, resolve} from 'path';
import * as fs from 'graceful-fs';
import type {Config} from '@jest/types';
import {tryRealpath} from 'jest-util';

export function clearFsCache(): void {
Expand Down Expand Up @@ -53,7 +52,7 @@ function statSyncCached(path: string): IPathType {
}

const checkedRealpathPaths = new Map<string, string>();
function realpathCached(path: Config.Path): Config.Path {
function realpathCached(path: string): string {
let result = checkedRealpathPaths.get(path);

if (result != null) {
Expand All @@ -75,7 +74,7 @@ function realpathCached(path: Config.Path): Config.Path {
export type PkgJson = Record<string, unknown>;

const packageContents = new Map<string, PkgJson>();
export function readPackageCached(path: Config.Path): PkgJson {
export function readPackageCached(path: string): PkgJson {
let result = packageContents.get(path);

if (result != null) {
Expand All @@ -92,9 +91,7 @@ export function readPackageCached(path: Config.Path): PkgJson {
// adapted from
// https://github.com/lukeed/escalade/blob/2477005062cdbd8407afc90d3f48f4930354252b/src/sync.js
// to use cached `fs` calls
export function findClosestPackageJson(
start: Config.Path,
): Config.Path | undefined {
export function findClosestPackageJson(start: string): string | undefined {
let dir = resolve('.', start);
if (!isDirectorySync(dir)) {
dir = dirname(dir);
Expand All @@ -120,12 +117,12 @@ export function findClosestPackageJson(
/*
* helper functions
*/
export function isFileSync(file: Config.Path): boolean {
export function isFileSync(file: string): boolean {
return statSyncCached(file) === IPathType.FILE;
}

export function isFileAsync(
file: Config.Path,
file: string,
cb: (err: Error | null, isFile?: boolean) => void,
): void {
try {
Expand All @@ -137,12 +134,12 @@ export function isFileAsync(
}
}

export function isDirectorySync(dir: Config.Path): boolean {
export function isDirectorySync(dir: string): boolean {
return statSyncCached(dir) === IPathType.DIRECTORY;
}

export function isDirectoryAsync(
dir: Config.Path,
dir: string,
cb: (err: Error | null, isDir?: boolean) => void,
): void {
try {
Expand All @@ -154,13 +151,13 @@ export function isDirectoryAsync(
}
}

export function realpathSync(file: Config.Path): Config.Path {
export function realpathSync(file: string): string {
return realpathCached(file);
}

export function realpathAsync(
file: Config.Path,
cb: (err: Error | null, resolved?: Config.Path) => void,
file: string,
cb: (err: Error | null, resolved?: string) => void,
): void {
try {
// TODO: create an async version of realpathCached
Expand Down
11 changes: 5 additions & 6 deletions packages/jest-resolve/src/nodeModulesPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@
*/

import * as path from 'path';
import type {Config} from '@jest/types';
import {tryRealpath} from 'jest-util';

type NodeModulesPathsOptions = {
moduleDirectory?: Array<string>;
paths?: Array<Config.Path>;
paths?: Array<string>;
};

export default function nodeModulesPaths(
basedir: Config.Path,
basedir: string,
options: NodeModulesPathsOptions,
): Array<Config.Path> {
): Array<string> {
const modules =
options && options.moduleDirectory
? Array.from(options.moduleDirectory)
Expand All @@ -46,15 +45,15 @@ export default function nodeModulesPaths(
physicalBasedir = basedirAbs;
}

const paths: Array<Config.Path> = [physicalBasedir];
const paths: Array<string> = [physicalBasedir];
let parsed = path.parse(physicalBasedir);
while (parsed.dir !== paths[paths.length - 1]) {
paths.push(parsed.dir);
parsed = path.parse(parsed.dir);
}

const dirs = paths
.reduce<Array<Config.Path>>(
.reduce<Array<string>>(
(dirs, aPath) =>
dirs.concat(
modules.map(moduleDir =>
Expand Down
Loading

0 comments on commit 7344e67

Please sign in to comment.