Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

changedFileKind.ts

107 lines3.5 KB

1import type { PullRequestChangeBreakdown } from "@t3tools/contracts";
2​
3export type ChangedFileKind = keyof NonNullable<PullRequestChangeBreakdown>;
4​
5export 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;
23​
24const 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]);
46const GENERATED_NAME =
47 /(\.generated\.|\.gen\.|\.g\.dart$|\.pb\.go$|_pb2(_grpc)?\.py$|\.pb\.(cc|h)$|\.min\.(js|css)$|\.(js|css)\.map$)/;
48const GENERATED_DIRS = new Set(["__generated__", "_generated", "generated"]);
49​
50const MOCK_NAME = /(\.snap$|\.(mocks?|fixtures?|stubs?|fakes?)\.[^.]+$)/i;
51// Case-sensitive after the prefix: `MockClock.ts` is a mock, `Mockup.ts` is not.
52const MOCK_PREFIX = /^([Mm]ocks?|[Ff]ixtures?|[Ss]tubs?|[Ff]akes?)([-_.A-Z]|$)/;
53const MOCK_DIRS = new Set([
54 "__fixtures__",
55 "__mocks__",
56 "__snapshots__",
57 "fakes",
58 "fixture",
59 "fixtures",
60 "mock",
61 "mocks",
62 "stubs",
63 "testdata",
64]);
65​
66const TEST_NAME =
67 /(\.(test|spec|e2e|eval|stories)\.[^.]+$|_(test|spec)\.[^.]+$|^test_.+\.py$|^conftest\.py$|^(vitest|jest)\.setup\.[^.]+$)/i;
68// Case-sensitive: `FooTest.java` is a test class, `Latest.java` is not.
69const CLASS_TEST_NAME = /(Tests?|Spec)\.(java|kt|kts|scala|groovy|cs|php|swift|m)$/;
70const TEST_DIRS = new Set([
71 "__tests__",
72 "__test__",
73 "cypress",
74 "e2e",
75 "evals",
76 "spec",
77 "specs",
78 "test",
79 "tests",
80]);
81​
82const DOCS_NAME = /\.(md|mdx|markdown|rst|adoc|txt)$/i;
83// Plain-text files that tools read as config or input, not prose.
84const NOT_DOCS_NAME = /^(requirements.*|constraints.*|CMakeLists|robots|llms)\.txt$/i;
85​
86/**
87 * Sorts a changed file by its path alone, using common naming conventions. Lockfiles and build
88 * output count as generated, and test support (mocks, fixtures, snapshots) is kept apart from the
89 * tests themselves, and prose (Markdown, plain text) is its own kind, so a reviewer can see how much of a change is logic.
90 */
91export function isTestChangedFile(path: string): boolean {
92 const kind = classifyChangedFile(path);
93 return kind === "tests" || kind === "mocks";
95​
96export function classifyChangedFile(path: string): ChangedFileKind {
97 const segments = path.replaceAll("\\", "/").split("/");
98 const name = segments.pop() ?? "";
99 const dirs = segments.map((segment) => segment.toLowerCase());
100 const inDir = (set: ReadonlySet<string>) => dirs.some((dir) => set.has(dir));
101 if (GENERATED_FILES.has(name.toLowerCase()) || GENERATED_NAME.test(name) || inDir(GENERATED_DIRS))
102 return "generated";
103 if (MOCK_NAME.test(name) || MOCK_PREFIX.test(name) || inDir(MOCK_DIRS)) return "mocks";
104 if (TEST_NAME.test(name) || CLASS_TEST_NAME.test(name) || inDir(TEST_DIRS)) return "tests";
105 if (DOCS_NAME.test(name) && !NOT_DOCS_NAME.test(name)) return "docs";
106 return "code";