Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,37 @@ describe("orderDiffFiles", () => {
).toEqual(["src/lib/index.ts", "src/a.ts"]);
});

it("resolves .mts and .cts modules from NodeNext and extensionless specifiers", () => {
expect(order([file("a.mts", ['import { z } from "./z.mjs";']), file("z.mts")])).toEqual([
"z.mts",
"a.mts",
]);
expect(order([file("a.ts", ['import { z } from "./z";']), file("z.cts")])).toEqual([
"z.cts",
"a.ts",
]);
});

it("falls back to the specifier's last segment when the path does not line up", () => {
expect(order([file("src/a.ts", ['import { b } from "~/lib/b";']), file("lib/b.ts")])).toEqual([
"lib/b.ts",
"src/a.ts",
]);
});

it("falls back to the whole dotted module name, not its first segment", () => {
expect(
order([file("src/a.ts", ['import { b } from "~/z/b.logic";']), file("z/b.logic.ts")]),
).toEqual(["z/b.logic.ts", "src/a.ts"]);
expect(
order([
file("src/a.ts", ['import { b } from "~/z/b.logic";']),
file("z/b.logic.ts"),
file("other/b.ts"),
]),
).toEqual(["other/b.ts", "z/b.logic.ts", "src/a.ts"]);
});

it("ignores an ambiguous last segment rather than guessing", () => {
expect(
order([
Expand All @@ -72,6 +96,18 @@ describe("orderDiffFiles", () => {
]);
});

it("strips a bundler query such as ?worker but keeps a # subpath import", () => {
expect(
order([file("src/a.js", ['import Worker from "./z.js?worker";']), file("src/z.js")]),
).toEqual(["src/z.js", "src/a.js"]);
expect(
order([file("a.ts", ['import url from "~/lib/b.logic?url";']), file("lib/b.logic.ts")]),
).toEqual(["lib/b.logic.ts", "a.ts"]);
expect(
order([file("src/a.ts", ['import { z } from "#internal/z";']), file("src/z.ts")]),
).toEqual(["src/z.ts", "src/a.ts"]);
});

it("reads require and bare imports too", () => {
expect(
order([
Expand Down
52 changes: 33 additions & 19 deletions apps/web/src/components/pullRequest/pullRequestFileOrder.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const GENERATED_DIRECTORIES = new Set([
"vendor",
]);
const TEST_DIRECTORIES = new Set(["__tests__", "tests", "test"]);
const MODULE_EXTENSIONS = [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs"];
// `.mts` also ends in `.ts`, so longer extensions come first.
const MODULE_EXTENSIONS = [".mts", ".cts", ".mjs", ".cjs", ".tsx", ".jsx", ".ts", ".js"];

function diffFileTier(path: string): DiffFileTier {
const segments = path.split("/");
Expand Down Expand Up @@ -56,6 +57,17 @@ function baseName(path: string): string {
return stripExtension(path.split("/").at(-1) ?? "");
}

// Import specifiers omit module extensions but keep dotted names such as `b.logic`, so module-map
// keys strip only the extensions an import could omit.
function stripModuleExtension(path: string): string {
const extension = MODULE_EXTENSIONS.find((candidate) => path.endsWith(candidate));
return extension === undefined ? path : path.slice(0, -extension.length);
}

function moduleName(path: string): string {
return stripModuleExtension(path.split("/").at(-1) ?? "");
}

function resolveRelative(fromPath: string, specifier: string): string {
const segments = fromPath.split("/").slice(0, -1);
for (const part of specifier.split("/")) {
Expand All @@ -72,25 +84,22 @@ function importedPaths(
path: string,
lines: ReadonlyArray<string>,
byModulePath: ReadonlyMap<string, string>,
byBaseName: ReadonlyMap<string, string>,
byModuleName: ReadonlyMap<string, string>,
): ReadonlySet<string> {
const imported = new Set<string>();
for (const line of lines) {
IMPORT_SPECIFIER.lastIndex = 0;
let match = IMPORT_SPECIFIER.exec(line);
while (match !== null) {
const specifier = match[1] ?? "";
const withExtension = specifier.startsWith(".")
? resolveRelative(path, specifier)
: specifier;
// Module-map keys omit extensions, which can differ from disk.
const extension = MODULE_EXTENSIONS.find((candidate) => withExtension.endsWith(candidate));
const resolved =
extension === undefined ? withExtension : withExtension.slice(0, -extension.length);
// Bundler queries such as `?worker` or `?url` name the same file.
const specifier = (match[1] ?? "").replace(/\?.*$/, "");
Comment thread
jancuk marked this conversation as resolved.
Resolved
const resolved = stripModuleExtension(
specifier.startsWith(".") ? resolveRelative(path, specifier) : specifier,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
);
const target =
byModulePath.get(resolved) ??
byModulePath.get(`${resolved}/index`) ??
byBaseName.get(baseName(resolved));
byModuleName.get(moduleName(resolved));
if (target !== undefined && target !== path) imported.add(target);
match = IMPORT_SPECIFIER.exec(line);
}
Expand Down Expand Up @@ -140,22 +149,27 @@ export function orderDiffFiles(

const sourcePaths = [...byPath.keys()].filter((path) => tiers.get(path) === "source");
const byModulePath = new Map<string, string>();
const ambiguousBaseNames = new Set<string>();
const byBaseName = new Map<string, string>();
const ambiguousModuleNames = new Set<string>();
const byModuleName = new Map<string, string>();
for (const path of sourcePaths) {
byModulePath.set(stripExtension(path), path);
const base = baseName(path);
if (byBaseName.has(base)) ambiguousBaseNames.add(base);
byBaseName.set(base, path);
byModulePath.set(stripModuleExtension(path), path);
const name = moduleName(path);
if (byModuleName.has(name)) ambiguousModuleNames.add(name);
byModuleName.set(name, path);
}
for (const base of ambiguousBaseNames) byBaseName.delete(base);
for (const name of ambiguousModuleNames) byModuleName.delete(name);

const imports = new Map<string, ReadonlySet<string>>();
for (const path of sourcePaths) {
const file = byPath.get(path)!;
imports.set(
path,
importedPaths(path, [...file.additionLines, ...file.deletionLines], byModulePath, byBaseName),
importedPaths(
path,
[...file.additionLines, ...file.deletionLines],
byModulePath,
byModuleName,
),
);
}
const orderedSource = orderByImports(sourcePaths, imports);
Expand Down
Loading