@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 106 lines 3.33 KB
Raw
1 import type { PullRequestChangeBreakdown } from "@t3tools/contracts";
2
3 export type ChangedFileKind = keyof NonNullable<PullRequestChangeBreakdown>;
4
5 export function summarizeChangedFiles(
6 files: ReadonlyArray<{ path: string; additions: number; deletions: number }>,
7 ): NonNullable<PullRequestChangeBreakdown> {
8 const empty = () => ({ additions: 0, deletions: 0 });
9 const breakdown = {
10 code: empty(),
11 tests: empty(),
12 mocks: empty(),
13 generated: empty(),
14 docs: empty(),
15 };
16 for (const file of files) {
17 const bucket = breakdown[classifyChangedFile(file.path)];
18 bucket.additions += file.additions;
19 bucket.deletions += file.deletions;
20 }
21 return breakdown;
22 }
23
24 const GENERATED_FILES = new Set([
25 "bun.lock",
26 "bun.lockb",
27 "cargo.lock",
28 "composer.lock",
29 "deno.lock",
30 "flake.lock",
31 "gemfile.lock",
32 "go.sum",
33 "gradle.lockfile",
34 "mix.lock",
35 "npm-shrinkwrap.json",
36 "package-lock.json",
37 "packages.lock.json",
38 "pipfile.lock",
39 "pnpm-lock.yaml",
40 "podfile.lock",
41 "poetry.lock",
42 "pubspec.lock",
43 "uv.lock",
44 "yarn.lock",
45 ]);
46 const GENERATED_NAME =
47 /(\.generated\.|\.gen\.|\.g\.dart$|\.pb\.go$|_pb2(_grpc)?\.py$|\.pb\.(cc|h)$|\.min\.(js|css)$|\.(js|css)\.map$)/;
48 const GENERATED_DIRS = new Set(["__generated__", "_generated", "generated"]);
49
50 const MOCK_NAME =
51 /(\.snap$|\.(mocks?|fixtures?|stubs?|fakes?)\.[^.]+$|^(mocks?|fixtures?|stubs?|fakes?)([-_.A-Z]|$))/;
52 const MOCK_DIRS = new Set([
53 "__fixtures__",
54 "__mocks__",
55 "__snapshots__",
56 "fakes",
57 "fixture",
58 "fixtures",
59 "mock",
60 "mocks",
61 "stubs",
62 "testdata",
63 ]);
64
65 const TEST_NAME =
66 /(\.(test|spec|e2e|eval|stories)\.[^.]+$|_(test|spec)\.[^.]+$|^test_.+\.py$|^conftest\.py$|^(vitest|jest)\.setup\.[^.]+$)/i;
67 // Case-sensitive: `FooTest.java` is a test class, `Latest.java` is not.
68 const CLASS_TEST_NAME = /(Tests?|Spec)\.(java|kt|kts|scala|groovy|cs|php|swift|m)$/;
69 const TEST_DIRS = new Set([
70 "__tests__",
71 "__test__",
72 "cypress",
73 "e2e",
74 "evals",
75 "spec",
76 "specs",
77 "test",
78 "tests",
79 ]);
80
81 const DOCS_NAME = /\.(md|mdx|markdown|rst|adoc|txt)$/i;
82 // Plain-text files that tools read as config or input, not prose.
83 const NOT_DOCS_NAME = /^(requirements.*|constraints.*|CMakeLists|robots|llms)\.txt$/i;
84
85 /**
86 * Sorts a changed file by its path alone, using common naming conventions. Lockfiles and build
87 * output count as generated, and test support (mocks, fixtures, snapshots) is kept apart from the
88 * tests themselves, and prose (Markdown, plain text) is its own kind, so a reviewer can see how much of a change is logic.
89 */
90 export function isTestChangedFile(path: string): boolean {
91 const kind = classifyChangedFile(path);
92 return kind === "tests" || kind === "mocks";
93 }
94
95 export function classifyChangedFile(path: string): ChangedFileKind {
96 const segments = path.replaceAll("\\", "/").split("/");
97 const name = segments.pop() ?? "";
98 const dirs = segments.map((segment) => segment.toLowerCase());
99 const inDir = (set: ReadonlySet<string>) => dirs.some((dir) => set.has(dir));
100 if (GENERATED_FILES.has(name.toLowerCase()) || GENERATED_NAME.test(name) || inDir(GENERATED_DIRS))
101 return "generated";
102 if (MOCK_NAME.test(name) || inDir(MOCK_DIRS)) return "mocks";
103 if (TEST_NAME.test(name) || CLASS_TEST_NAME.test(name) || inDir(TEST_DIRS)) return "tests";
104 if (DOCS_NAME.test(name) && !NOT_DOCS_NAME.test(name)) return "docs";
105 return "code";
106 }