@devries-cameron20 / t3code-1 / commits / aace48bfe4
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): PR file order follows alias imports of dotted modules (#32364)

Co-authored-by: azhar <azhar.amir@amartha.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

azhar committed Nov 15, 2026 at 12:51 UTC aace48bfe4a8df0194d28909eadeed7400d196f4
2 files changed +69 -19
apps/web/src/components/pullRequest/pullRequestFileOrder.logic.test.ts
+36
@@ -48,6 +48,17 @@ describe("orderDiffFiles", () => {
48 ).toEqual(["src/lib/index.ts", "src/a.ts"]);
49 });
50
51 + it("resolves .mts and .cts modules from NodeNext and extensionless specifiers", () => {
52 + expect(order([file("a.mts", ['import { z } from "./z.mjs";']), file("z.mts")])).toEqual([
53 + "z.mts",
54 + "a.mts",
55 + ]);
56 + expect(order([file("a.ts", ['import { z } from "./z";']), file("z.cts")])).toEqual([
57 + "z.cts",
58 + "a.ts",
59 + ]);
60 + });
61 +
62 it("falls back to the specifier's last segment when the path does not line up", () => {
63 expect(order([file("src/a.ts", ['import { b } from "~/lib/b";']), file("lib/b.ts")])).toEqual([
64 "lib/b.ts",
@@ -55,6 +66,19 @@ describe("orderDiffFiles", () => {
66 ]);
67 });
68
69 + it("falls back to the whole dotted module name, not its first segment", () => {
70 + expect(
71 + order([file("src/a.ts", ['import { b } from "~/z/b.logic";']), file("z/b.logic.ts")]),
72 + ).toEqual(["z/b.logic.ts", "src/a.ts"]);
73 + expect(
74 + order([
75 + file("src/a.ts", ['import { b } from "~/z/b.logic";']),
76 + file("z/b.logic.ts"),
77 + file("other/b.ts"),
78 + ]),
79 + ).toEqual(["other/b.ts", "z/b.logic.ts", "src/a.ts"]);
80 + });
81 +
82 it("ignores an ambiguous last segment rather than guessing", () => {
83 expect(
84 order([
@@ -72,6 +96,18 @@ describe("orderDiffFiles", () => {
96 ]);
97 });
98
99 + it("strips a bundler query such as ?worker but keeps a # subpath import", () => {
100 + expect(
101 + order([file("src/a.js", ['import Worker from "./z.js?worker";']), file("src/z.js")]),
102 + ).toEqual(["src/z.js", "src/a.js"]);
103 + expect(
104 + order([file("a.ts", ['import url from "~/lib/b.logic?url";']), file("lib/b.logic.ts")]),
105 + ).toEqual(["lib/b.logic.ts", "a.ts"]);
106 + expect(
107 + order([file("src/a.ts", ['import { z } from "#internal/z";']), file("src/z.ts")]),
108 + ).toEqual(["src/z.ts", "src/a.ts"]);
109 + });
110 +
111 it("reads require and bare imports too", () => {
112 expect(
113 order([
apps/web/src/components/pullRequest/pullRequestFileOrder.logic.ts
+33 -19
@@ -22,7 +22,8 @@ const GENERATED_DIRECTORIES = new Set([
22 "vendor",
23 ]);
24 const TEST_DIRECTORIES = new Set(["__tests__", "tests", "test"]);
25 -const MODULE_EXTENSIONS = [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs"];
25 +// `.mts` also ends in `.ts`, so longer extensions come first.
26 +const MODULE_EXTENSIONS = [".mts", ".cts", ".mjs", ".cjs", ".tsx", ".jsx", ".ts", ".js"];
27
28 function diffFileTier(path: string): DiffFileTier {
29 const segments = path.split("/");
@@ -56,6 +57,17 @@ function baseName(path: string): string {
57 return stripExtension(path.split("/").at(-1) ?? "");
58 }
59
60 +// Import specifiers omit module extensions but keep dotted names such as `b.logic`, so module-map
61 +// keys strip only the extensions an import could omit.
62 +function stripModuleExtension(path: string): string {
63 + const extension = MODULE_EXTENSIONS.find((candidate) => path.endsWith(candidate));
64 + return extension === undefined ? path : path.slice(0, -extension.length);
65 +}
66 +
67 +function moduleName(path: string): string {
68 + return stripModuleExtension(path.split("/").at(-1) ?? "");
69 +}
70 +
71 function resolveRelative(fromPath: string, specifier: string): string {
72 const segments = fromPath.split("/").slice(0, -1);
73 for (const part of specifier.split("/")) {
@@ -72,25 +84,22 @@ function importedPaths(
84 path: string,
85 lines: ReadonlyArray<string>,
86 byModulePath: ReadonlyMap<string, string>,
75 - byBaseName: ReadonlyMap<string, string>,
87 + byModuleName: ReadonlyMap<string, string>,
88 ): ReadonlySet<string> {
89 const imported = new Set<string>();
90 for (const line of lines) {
91 IMPORT_SPECIFIER.lastIndex = 0;
92 let match = IMPORT_SPECIFIER.exec(line);
93 while (match !== null) {
82 - const specifier = match[1] ?? "";
83 - const withExtension = specifier.startsWith(".")
84 - ? resolveRelative(path, specifier)
85 - : specifier;
86 - // Module-map keys omit extensions, which can differ from disk.
87 - const extension = MODULE_EXTENSIONS.find((candidate) => withExtension.endsWith(candidate));
88 - const resolved =
89 - extension === undefined ? withExtension : withExtension.slice(0, -extension.length);
94 + // Bundler queries such as `?worker` or `?url` name the same file.
95 + const specifier = (match[1] ?? "").replace(/\?.*$/, "");
96 + const resolved = stripModuleExtension(
97 + specifier.startsWith(".") ? resolveRelative(path, specifier) : specifier,
98 + );
99 const target =
100 byModulePath.get(resolved) ??
101 byModulePath.get(`${resolved}/index`) ??
93 - byBaseName.get(baseName(resolved));
102 + byModuleName.get(moduleName(resolved));
103 if (target !== undefined && target !== path) imported.add(target);
104 match = IMPORT_SPECIFIER.exec(line);
105 }
@@ -140,22 +149,27 @@ export function orderDiffFiles(
149
150 const sourcePaths = [...byPath.keys()].filter((path) => tiers.get(path) === "source");
151 const byModulePath = new Map<string, string>();
143 - const ambiguousBaseNames = new Set<string>();
144 - const byBaseName = new Map<string, string>();
152 + const ambiguousModuleNames = new Set<string>();
153 + const byModuleName = new Map<string, string>();
154 for (const path of sourcePaths) {
146 - byModulePath.set(stripExtension(path), path);
147 - const base = baseName(path);
148 - if (byBaseName.has(base)) ambiguousBaseNames.add(base);
149 - byBaseName.set(base, path);
155 + byModulePath.set(stripModuleExtension(path), path);
156 + const name = moduleName(path);
157 + if (byModuleName.has(name)) ambiguousModuleNames.add(name);
158 + byModuleName.set(name, path);
159 }
151 - for (const base of ambiguousBaseNames) byBaseName.delete(base);
160 + for (const name of ambiguousModuleNames) byModuleName.delete(name);
161
162 const imports = new Map<string, ReadonlySet<string>>();
163 for (const path of sourcePaths) {
164 const file = byPath.get(path)!;
165 imports.set(
166 path,
158 - importedPaths(path, [...file.additionLines, ...file.deletionLines], byModulePath, byBaseName),
167 + importedPaths(
168 + path,
169 + [...file.additionLines, ...file.deletionLines],
170 + byModulePath,
171 + byModuleName,
172 + ),
173 );
174 }
175 const orderedSource = orderByImports(sourcePaths, imports);