|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const GENERATED_PREFIXES = [ |
| 4 | + "gui/dist/", |
| 5 | + "dist/", |
| 6 | + "coverage/", |
| 7 | + ".next/", |
| 8 | + "node_modules/", |
| 9 | +]; |
| 10 | +const BEHAVIOR_PREFIXES = ["src/", "gui/src/"]; |
| 11 | +const TEST_PREFIXES = ["tests/"]; |
| 12 | +const TEST_FILE_PATTERN = /(?:^|\/)(?:__tests__\/.*|[^/]+\.(?:test|spec)\.[^.]+)$/; |
| 13 | +const SUPPRESSION_PATTERN = /(?:@ts-ignore|@ts-nocheck|eslint-disable|biome-ignore|prettier-ignore)/; |
| 14 | +const FOCUSED_TEST_PATTERN = /\b(?:describe|it|test)\.(?:only|skip)\s*\(/; |
| 15 | + |
| 16 | +function addedLines(patch) { |
| 17 | + if (typeof patch !== "string") return []; |
| 18 | + return patch |
| 19 | + .split("\n") |
| 20 | + .filter((line) => line.startsWith("+") && !line.startsWith("+++")) |
| 21 | + .map((line) => line.slice(1)); |
| 22 | +} |
| 23 | + |
| 24 | +function hasDeletions(patch) { |
| 25 | + if (typeof patch !== "string") return false; |
| 26 | + return patch |
| 27 | + .split("\n") |
| 28 | + .some((line) => line.startsWith("-") && !line.startsWith("---")); |
| 29 | +} |
| 30 | + |
| 31 | +// Lines that survive in the result of a hunk: additions plus context. Used for |
| 32 | +// empty-catch detection when the hunk also deletes lines, so deleting a catch |
| 33 | +// body cannot bypass the check. |
| 34 | +// |
| 35 | +// Returned per hunk, never as one flat list. Hunks are disjoint windows onto the |
| 36 | +// file, so concatenating them puts unrelated lines next to each other: a hunk |
| 37 | +// ending at `} catch (e) {` followed by one starting at `}` reads as an empty |
| 38 | +// catch that does not exist anywhere in the file. |
| 39 | +function resultLinesByHunk(patch) { |
| 40 | + if (typeof patch !== "string") return []; |
| 41 | + const hunks = []; |
| 42 | + let current = null; |
| 43 | + for (const line of patch.split("\n")) { |
| 44 | + if (line.startsWith("@@")) { |
| 45 | + current = []; |
| 46 | + hunks.push(current); |
| 47 | + continue; |
| 48 | + } |
| 49 | + if (current === null) { |
| 50 | + // A patch without a hunk header (some API shapes omit it) is one window. |
| 51 | + current = []; |
| 52 | + hunks.push(current); |
| 53 | + } |
| 54 | + if ((line.startsWith("+") && !line.startsWith("+++")) || line.startsWith(" ")) { |
| 55 | + current.push(line.slice(1)); |
| 56 | + } |
| 57 | + } |
| 58 | + return hunks; |
| 59 | +} |
| 60 | + |
| 61 | +// Flat form, kept for callers that only need the surviving text of a patch. |
| 62 | +function resultLines(patch) { |
| 63 | + return resultLinesByHunk(patch).flat(); |
| 64 | +} |
| 65 | + |
| 66 | +function isGeneratedPath(path) { |
| 67 | + return GENERATED_PREFIXES.some((prefix) => path.startsWith(prefix)); |
| 68 | +} |
| 69 | + |
| 70 | +function isBehaviorPath(path) { |
| 71 | + return BEHAVIOR_PREFIXES.some((prefix) => path.startsWith(prefix)); |
| 72 | +} |
| 73 | + |
| 74 | +function isTestPath(path) { |
| 75 | + return TEST_PREFIXES.some((prefix) => path.startsWith(prefix)) || TEST_FILE_PATTERN.test(path); |
| 76 | +} |
| 77 | + |
| 78 | +// A hunk whose surviving and removed lines are all comments or blank changed no |
| 79 | +// behavior, so it cannot owe a regression test. This matters because the review |
| 80 | +// standard here asks for dense explanatory comments in the source: a PR that |
| 81 | +// only sharpens a comment about WHY something fails closed would otherwise be |
| 82 | +// told to add a test for a change it did not make, and the only escape would be |
| 83 | +// a maintainer label — which trains contributors to ask for the label instead of |
| 84 | +// writing tests, weakening the gate everywhere it actually matters. |
| 85 | +// |
| 86 | +// Deliberately narrow: a single non-comment line anywhere in the file's patch |
| 87 | +// makes the whole file count as behavior again. Block-comment CONTINUATION |
| 88 | +// lines are recognized only in the common leading-asterisk form; anything more |
| 89 | +// clever than that reads as code and keeps the requirement. |
| 90 | +function isCommentOnlyChange(patch) { |
| 91 | + if (typeof patch !== "string") return false; |
| 92 | + const changed = patch |
| 93 | + .split("\n") |
| 94 | + .filter( |
| 95 | + (line) => |
| 96 | + (line.startsWith("+") && !line.startsWith("+++")) || |
| 97 | + (line.startsWith("-") && !line.startsWith("---")), |
| 98 | + ) |
| 99 | + .map((line) => line.slice(1).trim()); |
| 100 | + if (changed.length === 0) return false; |
| 101 | + return changed.every( |
| 102 | + (line) => |
| 103 | + line === "" || |
| 104 | + line.startsWith("//") || |
| 105 | + line.startsWith("/*") || |
| 106 | + line.startsWith("*") || |
| 107 | + line.startsWith("#"), |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +function hasEmptyCatch(lines) { |
| 112 | + const text = lines.join("\n"); |
| 113 | + return /catch\s*(?:\([^)]*\))?\s*\{\s*\}/m.test(text); |
| 114 | +} |
| 115 | + |
| 116 | +function assessHygiene({ files = [], labels = [] }) { |
| 117 | + const labelSet = new Set(labels); |
| 118 | + const failures = []; |
| 119 | + const filenames = files.map((file) => file.filename); |
| 120 | + const removedFilenames = new Set( |
| 121 | + files |
| 122 | + .filter((file) => file.status === "removed") |
| 123 | + .map((file) => file.filename), |
| 124 | + ); |
| 125 | + // Renames are classified on both sides: moving a behavior or generated file |
| 126 | + // to a documentation path must not bypass the hygiene gates. |
| 127 | + const previousFilenames = files.flatMap((file) => |
| 128 | + file.previous_filename ? [file.previous_filename] : [], |
| 129 | + ); |
| 130 | + const allPaths = [...new Set([...filenames, ...previousFilenames])]; |
| 131 | + // A file whose patch is entirely comments changed no behavior. Renamed-from |
| 132 | + // paths carry no patch of their own, so they are judged by the file that |
| 133 | + // carries them. |
| 134 | + const commentOnlyPaths = new Set( |
| 135 | + files |
| 136 | + .filter((file) => isCommentOnlyChange(file.patch)) |
| 137 | + .flatMap((file) => |
| 138 | + file.previous_filename |
| 139 | + ? [file.filename, file.previous_filename] |
| 140 | + : [file.filename], |
| 141 | + ), |
| 142 | + ); |
| 143 | + const behaviorChanged = allPaths.some( |
| 144 | + (path) => isBehaviorPath(path) && !commentOnlyPaths.has(path), |
| 145 | + ); |
| 146 | + // Deleted tests add no coverage and must not satisfy the regression gate. |
| 147 | + const testsChanged = allPaths.some( |
| 148 | + (path) => isTestPath(path) && !removedFilenames.has(path), |
| 149 | + ); |
| 150 | + |
| 151 | + if ( |
| 152 | + behaviorChanged && |
| 153 | + !testsChanged && |
| 154 | + !labelSet.has("test-exception-approved") |
| 155 | + ) { |
| 156 | + failures.push({ code: "missing_regression_test" }); |
| 157 | + } |
| 158 | + |
| 159 | + const generated = allPaths.filter( |
| 160 | + (path) => isGeneratedPath(path) && !removedFilenames.has(path), |
| 161 | + ); |
| 162 | + if ( |
| 163 | + generated.length > 0 && |
| 164 | + !labelSet.has("generated-change-approved") |
| 165 | + ) { |
| 166 | + failures.push({ code: "generated_output", paths: generated }); |
| 167 | + } |
| 168 | + |
| 169 | + // A lockfile that MOVED with no manifest beside it is still orphaned, so both |
| 170 | + // sides of a rename count. A lockfile that was DELETED is not: dropping |
| 171 | + // `bun.lock` adds no dependency, which is why the generated-output and |
| 172 | + // regression-test checks above exclude removals the same way. |
| 173 | + if ( |
| 174 | + allPaths.includes("bun.lock") && |
| 175 | + !removedFilenames.has("bun.lock") && |
| 176 | + !allPaths.includes("package.json") && |
| 177 | + !labelSet.has("dependency-change-approved") |
| 178 | + ) { |
| 179 | + failures.push({ code: "orphan_lockfile" }); |
| 180 | + } |
| 181 | + |
| 182 | + const suppressions = []; |
| 183 | + const focusedTests = []; |
| 184 | + const emptyCatches = []; |
| 185 | + for (const file of files) { |
| 186 | + const lines = addedLines(file.patch); |
| 187 | + if (lines.some((line) => SUPPRESSION_PATTERN.test(line))) { |
| 188 | + suppressions.push(file.filename); |
| 189 | + } |
| 190 | + if (lines.some((line) => FOCUSED_TEST_PATTERN.test(line))) { |
| 191 | + focusedTests.push(file.filename); |
| 192 | + } |
| 193 | + // Scan hunk by hunk: an empty catch has to be empty within one window. |
| 194 | + const catchWindows = hasDeletions(file.patch) |
| 195 | + ? resultLinesByHunk(file.patch) |
| 196 | + : [lines]; |
| 197 | + if (catchWindows.some((window) => hasEmptyCatch(window))) { |
| 198 | + emptyCatches.push(file.filename); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + if ( |
| 203 | + suppressions.length > 0 && |
| 204 | + !labelSet.has("suppression-approved") |
| 205 | + ) { |
| 206 | + failures.push({ code: "new_suppression", paths: suppressions }); |
| 207 | + } |
| 208 | + if ( |
| 209 | + focusedTests.length > 0 && |
| 210 | + !labelSet.has("test-exception-approved") |
| 211 | + ) { |
| 212 | + failures.push({ code: "focused_or_skipped_test", paths: focusedTests }); |
| 213 | + } |
| 214 | + if (emptyCatches.length > 0) { |
| 215 | + failures.push({ code: "empty_catch", paths: emptyCatches }); |
| 216 | + } |
| 217 | + |
| 218 | + return failures; |
| 219 | +} |
| 220 | + |
| 221 | +module.exports = { |
| 222 | + addedLines, |
| 223 | + assessHygiene, |
| 224 | + hasEmptyCatch, |
| 225 | + hasDeletions, |
| 226 | + isBehaviorPath, |
| 227 | + isGeneratedPath, |
| 228 | + isTestPath, |
| 229 | + resultLines, |
| 230 | + resultLinesByHunk, |
| 231 | +}; |
0 commit comments