diff --git a/apps/web/src/components/pullRequest/pullRequestFileOrder.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestFileOrder.logic.test.ts index 899aff44f1934..0626e463d751a 100644 --- a/apps/web/src/components/pullRequest/pullRequestFileOrder.logic.test.ts +++ b/apps/web/src/components/pullRequest/pullRequestFileOrder.logic.test.ts @@ -48,6 +48,17 @@ 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", @@ -55,6 +66,19 @@ describe("orderDiffFiles", () => { ]); }); + 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([ @@ -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([ diff --git a/apps/web/src/components/pullRequest/pullRequestFileOrder.logic.ts b/apps/web/src/components/pullRequest/pullRequestFileOrder.logic.ts index 737206d96e508..41ee82ebdf85e 100644 --- a/apps/web/src/components/pullRequest/pullRequestFileOrder.logic.ts +++ b/apps/web/src/components/pullRequest/pullRequestFileOrder.logic.ts @@ -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("/"); @@ -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("/")) { @@ -72,25 +84,22 @@ function importedPaths( path: string, lines: ReadonlyArray, byModulePath: ReadonlyMap, - byBaseName: ReadonlyMap, + byModuleName: ReadonlyMap, ): ReadonlySet { const imported = new Set(); 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(/\?.*$/, ""); + const resolved = stripModuleExtension( + specifier.startsWith(".") ? resolveRelative(path, specifier) : specifier, + ); 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); } @@ -140,22 +149,27 @@ export function orderDiffFiles( const sourcePaths = [...byPath.keys()].filter((path) => tiers.get(path) === "source"); const byModulePath = new Map(); - const ambiguousBaseNames = new Set(); - const byBaseName = new Map(); + const ambiguousModuleNames = new Set(); + const byModuleName = new Map(); 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>(); 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);