Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"prepublishOnly": "npm run test"
},
"dependencies": {
"eslint-import-context": "^0.2.0",
"is-bun-module": "^2.0.0",
"js-yaml": "^4.1.0",
"oxc-resolver": "^11.6.1",
Expand Down
31 changes: 31 additions & 0 deletions pnpm-lock.yaml

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

7 changes: 3 additions & 4 deletions src/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import process from "node:process";

import { isBunBuiltin } from "is-bun-module";

import { defaultOptions } from "./constants.js";
Expand All @@ -8,6 +6,7 @@ import type { NextImportResolver, Options, ResolvedResult } from "./types.js";
import {
findClosestPackageRoot,
findWorkspacePackages,
getResolveRoots,
hasBunPrefix,
hasNodePrefix,
isNodeBuiltin,
Expand Down Expand Up @@ -74,7 +73,7 @@ export function resolve(
return resolveRelativePath(sourceFile, modulePath, restOptions);
}

const resolveRoots = roots?.length ? roots : [process.cwd()];
const resolveRoots = getResolveRoots(roots);

const workspacePackages = findWorkspacePackages(resolveRoots, packages);

Expand Down Expand Up @@ -111,7 +110,7 @@ export function createNextImportResolver(
...config,
};

const resolveRoots = roots?.length ? roots : [process.cwd()];
const resolveRoots = getResolveRoots(roots);

const workspacePackages = findWorkspacePackages(resolveRoots, packages);

Expand Down
15 changes: 15 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import fs from "node:fs";
import module from "node:module";
import path from "node:path";
import process from "node:process";

import { useRuleContext } from "eslint-import-context";
import yaml from "js-yaml";
import type { TsconfigOptions } from "oxc-resolver";
import { stableHash } from "stable-hash-x";
Expand Down Expand Up @@ -536,3 +538,16 @@ export function findWorkspacePackages(

return sortPathsByDepth([...roots]);
}

/**
* Get the resolve roots. if `roots` is not provided, it will use the current working directory.
*
* @param roots {string[]} - the root directories
* @returns {string[]} - the resolve roots
*/
export function getResolveRoots(roots?: string[]): string[] {
if (roots?.length) return [...roots];

const context = useRuleContext();
return [context?.cwd ?? process.cwd()];
}
48 changes: 48 additions & 0 deletions tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import process from "node:process";

import {
hasBunPrefix,
hashObject,
Expand Down Expand Up @@ -207,4 +209,50 @@ describe("utils", () => {
expect(isNodeBuiltin("bun:fs")).toBe(false);
});
});

describe("test getResolveRoots", () => {
const originalProcessCwd = process.cwd.bind(null);
let mockCwd: string;

beforeEach(() => {
mockCwd = "/mock/cwd";
process.cwd = () => mockCwd;
});

afterEach(() => {
process.cwd = originalProcessCwd;
vi.resetModules();
vi.clearAllMocks();
});

it("returns the provided roots if given", async () => {
const { getResolveRoots } = await import("@/utils.js");
const roots = ["/foo/bar", "/baz"];
expect(getResolveRoots(roots)).toEqual(roots);
});

it("returns context.cwd if available (mock eslint-import-context)", async () => {
vi.doMock("eslint-import-context", () => ({
useRuleContext: () => ({ cwd: "/context/cwd" }),
}));
const { getResolveRoots } = await import("@/utils.js");
expect(getResolveRoots()).toEqual(["/context/cwd"]);
});

it("returns process.cwd() if context is undefined (mock eslint-import-context)", async () => {
vi.doMock("eslint-import-context", () => ({
useRuleContext: () => undefined,
}));
const { getResolveRoots } = await import("@/utils.js");
expect(getResolveRoots()).toEqual([mockCwd]);
});

it("returns process.cwd() if cwd is not available (mock eslint-import-context)", async () => {
vi.doMock("eslint-import-context", () => ({
useRuleContext: () => ({}),
}));
const { getResolveRoots } = await import("@/utils.js");
expect(getResolveRoots()).toEqual([mockCwd]);
});
});
});
Loading