Public: anyone can read this; the crew can change it.
Gate: a pull adds no stray blank line before a closing bracket or inside the import block (#16369) #16406
Check · Review · Land · Live — The access wall holds it for a person to decide: a trusted source's pull touches release machinery. Allow it (merge) or send it back.
Green for 4 days, not landed: it waits for a person: the access wall holds it for a person to decide: a trusted source's pull touches release machinery. Allow it (merge) or send it back. To unblock: a person who writes the repository merges it or requests changes.
grokbot/forge:fix/16369-stray-blank-lines → main 141 commits fork
Combined with main
The preview is built from combined/16406 (92eee71f17): main merged into this pull's head. Merging lands that branch.
The checks line says which commit the check ran on.
scripts/gate.ts +13 −2
| @@ -763,8 +763,19 @@ export async function addedRefusal(cwd: string = process.cwd()): Promise<string[ | ||
| 763 | 763 | const { addedProblems, parseAdded } = await import("../src/kernel/review_rules.ts"); |
| 764 | 764 | const base = baseRef(cwd); |
| 765 | 765 | if (!base) return []; |
| 766 | const diff = git(["diff", "-U0", "--no-color", "--no-ext-diff", `${base}..${judgedHead(cwd)}`, "--", "src", "test"], cwd); | |
| 767 | return diff.ok ? addedProblems(parseAdded(diff.out)) : []; | |
| 766 | const head = judgedHead(cwd); | |
| 767 | const diff = git(["diff", "-U0", "--no-color", "--no-ext-diff", `${base}..${head}`, "--", "src", "test"], cwd); | |
| 768 | // A blank line's neighbours: each file read once, as the pull leaves it. | |
| 769 | const shown = new Map<string, string | undefined>(); | |
| 770 | const fileText = (path: string): string | undefined => { | |
| 771 | if (!shown.has(path)) { | |
| 772 | // Not through `git`, which trims: a leading blank line would shift every line number. | |
| 773 | const r = Bun.spawnSync(["git", "show", `${head}:${path}`], { stderr: "pipe", cwd }); | |
| 774 | shown.set(path, r.exitCode === 0 ? r.stdout.toString() : undefined); | |
| 775 | } | |
| 776 | return shown.get(path); | |
| 777 | }; | |
| 778 | return diff.ok ? addedProblems(parseAdded(diff.out), fileText) : []; | |
| 768 | 779 | } |
| 769 | 780 | |
| 770 | 781 | /** |
src/kernel/review_rules.ts +22 −1
| @@ -40,6 +40,12 @@ | ||
| 40 | 40 | * refused; end the write with `// env-restored: <where>` to say so. Every |
| 41 | 41 | * unrestored write is named, so one push shows them all. |
| 42 | 42 | * |
| 43 | * - "a pull adds no stray blank line" (#16369, after #14923, #15057, #15079, | |
| 44 | * #16363): an added blank line right before a line of only closing brackets | |
| 45 | * (`});`, `}`, `]`), or between two import lines. The neighbours are read | |
| 46 | * from the file as the pull leaves it (`fileText`), since `-U0` holds only | |
| 47 | * the added lines. Without a reader the rule is skipped. | |
| 48 | * | |
| 43 | 49 | * A comment's width is not here: it is formatting, decided and fixed by |
| 44 | 50 | * `src/kernel/comment_wrap.ts` (`bun scripts/gate.ts --fix`, #12968). |
| 45 | 51 | */ |
| @@ -116,6 +122,12 @@ const FINALLY_BLOCK = /(?:^|[^.\w])finally\s*\{/; | ||
| 116 | 122 | const HOOK = /\bafter(?:Each|All)\s*\(/; |
| 117 | 123 | const HOOK_REACH = 6; |
| 118 | 124 | const CODE_FILE = /^(?:src|test)\/(?!.*\/vendor\/).+\.ts$/; |
| 125 | // A line of only closing brackets, with a trailing `;` or `,` at most. | |
| 126 | const CLOSERS = /^\s*[)\]}][)\]}]*[;,]?\s*$/; | |
| 127 | // The end of an import statement: one line whole, a side-effect import, or the | |
| 128 | // `} from "..."` that closes a multi-line one. | |
| 129 | const IMPORT_END = /^import\b.*;\s*$|^\}\s*from\s*["'][^"']+["'];?\s*$/; | |
| 130 | const IMPORT_START = /^import\b/; | |
| 119 | 131 | |
| 120 | 132 | /** A line as code: string literals emptied, a trailing comment dropped. */ |
| 121 | 133 | const code = (text: string): string => text.replace(/(["'`])(?:\\.|(?!\1).)*\1/g, '""').replace(/\/\/.*$/, ""); |
| @@ -143,13 +155,22 @@ function hookRestores(added: readonly Added[], a: Added): boolean { | ||
| 143 | 155 | } |
| 144 | 156 | |
| 145 | 157 | /** One line per problem among the lines a pull adds, each naming its rule. */ |
| 146 | export function addedProblems(added: readonly Added[]): string[] { | |
| 158 | export function addedProblems(added: readonly Added[], fileText?: (path: string) => string | undefined): string[] { | |
| 147 | 159 | const out: string[] = []; |
| 148 | 160 | const restores = (path: string): boolean => |
| 149 | 161 | added.some((a) => a.path === path && (FINALLY_BLOCK.test(code(a.text)) || hookRestores(added, a))); |
| 150 | 162 | for (const { path, line, text } of added) { |
| 151 | 163 | if (!CODE_FILE.test(path)) continue; |
| 152 | 164 | const at = `${path}:${line}`; |
| 165 | if (fileText && text.trim() === "") { | |
| 166 | const file = fileText(path)?.split("\n"); | |
| 167 | const before = file?.[line - 2]; | |
| 168 | const after = file?.[line]; | |
| 169 | if (after !== undefined && CLOSERS.test(after)) | |
| 170 | out.push(`rule "a pull adds no stray blank line": ${at} is blank before \`${after.trim()}\`; remove it`); | |
| 171 | else if (before !== undefined && after !== undefined && IMPORT_END.test(before) && IMPORT_START.test(after)) | |
| 172 | out.push(`rule "a pull adds no stray blank line": ${at} is blank inside the import block; remove it`); | |
| 173 | } | |
| 153 | 174 | if (TEST_FILE.test(path)) { |
| 154 | 175 | if (ENV_WRITE.test(code(text)) && !text.includes("// env-restored:") && !restores(path)) { |
| 155 | 176 | out.push(`rule "a test restores the environment it changes": ${at} sets process.env; put it back in a \`finally {\` block or an afterEach that restores it, or end the line with \`// env-restored: <where>\`; so a failed expect() leaves nothing set for the next test`); |
test/gate-added-refusal.test.ts +16
| @@ -48,3 +48,19 @@ test("the gate passes when no base resolves", async () => { | ||
| 48 | 48 | delete process.env.FORGE_CHECK_BASE; |
| 49 | 49 | expect(await addedRefusal(dir)).toEqual([]); |
| 50 | 50 | }); |
| 51 | ||
| 52 | test("the gate reads a blank line's neighbours from the head commit's file, not from the diff", async () => { | |
| 53 | const dir = mkdtempSync(join(tmpdir(), "forge-added-")); | |
| 54 | dirs.push(dir); | |
| 55 | sh(dir, "init", "-q", "-b", "trunk"); | |
| 56 | mkdirSync(join(dir, "test")); | |
| 57 | const before = 'describe("d", () => {\n test("t", () => {});\n});\n'; | |
| 58 | writeFileSync(join(dir, "test/b.test.ts"), before); | |
| 59 | sh(dir, "add", "-A"); | |
| 60 | sh(dir, "commit", "-qm", "base"); | |
| 61 | process.env.FORGE_CHECK_BASE = sh(dir, "rev-parse", "HEAD"); // env-restored: the afterEach above | |
| 62 | // Only the blank line is added; the `});` after it is main's own line. | |
| 63 | writeFileSync(join(dir, "test/b.test.ts"), before.replace("{});\n", "{});\n\n")); | |
| 64 | sh(dir, "commit", "-qam", "head"); | |
| 65 | expect(await addedRefusal(dir)).toEqual(['rule "a pull adds no stray blank line": test/b.test.ts:3 is blank before `});`; remove it']); | |
| 66 | }); |
test/review-rules.test.ts +31
| @@ -141,3 +141,34 @@ test("a `++ x` line added is read as a line, not as a file header", () => { | ||
| 141 | 141 | const diff = diffOf("test/a.test.ts", "++ " + CAST); |
| 142 | 142 | expect(parseAdded(diff)).toEqual([{ path: "test/a.test.ts", line: 4, text: "++ " + CAST }]); |
| 143 | 143 | }); |
| 144 | ||
| 145 | test("an added blank line before a closing bracket or inside the import block is refused, naming the rule", () => { | |
| 146 | const file = [ | |
| 147 | 'import { a } from "./a.ts";', | |
| 148 | "", | |
| 149 | "import {", | |
| 150 | " b,", | |
| 151 | '} from "./b.ts";', | |
| 152 | "", | |
| 153 | 'import "./c.ts";', | |
| 154 | "", | |
| 155 | "const x = 1;", | |
| 156 | 'describe("d", () => {', | |
| 157 | " f(x);", | |
| 158 | "", | |
| 159 | "});", | |
| 160 | "", | |
| 161 | ].join("\n"); | |
| 162 | const blank = (line: number) => ({ path: "test/a.test.ts", line, text: "" }); | |
| 163 | const read = (path: string) => (path === "test/a.test.ts" ? file : undefined); | |
| 164 | expect(addedProblems([2, 6, 8, 12, 14].map(blank), read)).toEqual([ | |
| 165 | 'rule "a pull adds no stray blank line": test/a.test.ts:2 is blank inside the import block; remove it', | |
| 166 | 'rule "a pull adds no stray blank line": test/a.test.ts:6 is blank inside the import block; remove it', | |
| 167 | 'rule "a pull adds no stray blank line": test/a.test.ts:12 is blank before `});`; remove it', | |
| 168 | ]); | |
| 169 | // The same lines in src, and a blank before a plain `}`, are judged too; outside src and test, or | |
| 170 | // with no reader, they are not. | |
| 171 | expect(addedProblems([{ path: "src/a.ts", line: 2, text: "" }], () => " g();\n\n}\n")).toHaveLength(1); | |
| 172 | expect(addedProblems([{ path: "docs/a.ts", line: 2, text: "" }], () => " g();\n\n}\n")).toEqual([]); | |
| 173 | expect(addedProblems([blank(12)])).toEqual([]); | |
| 174 | }); |