From 4b819b123cb4eb8d1d5a20674601ddbad1cbef90 Mon Sep 17 00:00:00 2001 From: Andrew Cole Date: Sat, 28 Nov 2026 07:01:26 -0800 Subject: [PATCH] Commit hook: format check and lint of the staged files only Co-Authored-By: Claude --- .husky/pre-commit | 14 +++++++------- CLAUDE.md | 2 +- package.json | 1 + scripts/hook-lint.mjs | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 scripts/hook-lint.mjs diff --git a/.husky/pre-commit b/.husky/pre-commit index 93ef3988c..6f6e5839e 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,12 +1,13 @@ # Pre-commit gate, a quick check that the change broke nothing large: -# 1. format:check — Prettier, verify only (run `npm run format` to fix) -# 2. lint — eslint, one rule: multi-line comments are starred blocks -# 3. test — golden suite, values only: engine/tests/golden/runAll.ts -# 4. test:unit:hook — the unit tests of canon, cade and web, and the engine's +# 1. lint:hook — Prettier (verify only; `npm run format` fixes) and eslint, +# on the staged files only (scripts/hook-lint.mjs) +# 2. test — golden suite, values only: engine/tests/golden/runAll.ts +# 3. test:unit:hook — the unit tests of canon, cade and web, and the engine's # without the whole-return tests (each form's form.test.ts), which run only # for the forms this commit changes (scripts/hook-unit-tests.mjs) # -# The slow tests do not run here. Every unit test, the integration tests, the full +# The slow checks do not run here. Format and lint of the whole repository, +# every unit test, the integration tests, the full # required-inputs check and the PDF fill run on GitHub for each pull request # (.github/workflows/gates.yml), side by side, and a pull request merges only # when its "all gates" check is green. Run one locally when you need it: @@ -17,7 +18,6 @@ # in two stages (expected.ts before fact-fix, or vice versa). Don't make # that a habit. -npm run format:check -npm run lint +npm run lint:hook npm test npm run test:unit:hook diff --git a/CLAUDE.md b/CLAUDE.md index 090e2a60e..00a365b5b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -493,7 +493,7 @@ Most readers open a PR on a phone and do not open the diff. The description must - `Risk: routine` or `Risk: high — `; add `Complex: ` when the reader needs more than a glance, and `Pattern change: ` when the PR changes how we do something. A pattern change also updates this file. 3. **What changed**: one bullet per idea, in the order a reader would ask. Name a file only when the reader has to go there. 4. **Example outcome** when a user or operator sees something different: the real screen text, log line or command output. -5. **Verified**: what ran and what did not. Say "not run" out loud. A pull request merges only when its `all gates` check on GitHub is green. The check (`.github/workflows/gates.yml`) runs every test on GitHub's runners as parallel jobs: typecheck, lint and format, the scenario values, the PDF fill, the unit tests, the integration tests and the full required-inputs check. No job calls a model or needs a secret. GitHub tests the head merged with `main` as of the last push; when `main` moved since, `gh pr update-branch ` runs the check again. The commit hook is only a quick check (format, lint, scenario values, and the unit tests without the whole-return tests of forms the commit does not change: `scripts/hook-unit-tests.mjs`), so the pull request's check is the one place where the filled PDFs, the integration tests and every removed answer are checked. `gh pr checks --watch` waits for the result. +5. **Verified**: what ran and what did not. Say "not run" out loud. A pull request merges only when its `all gates` check on GitHub is green. The check (`.github/workflows/gates.yml`) runs every test on GitHub's runners as parallel jobs: typecheck, lint and format, the scenario values, the PDF fill, the unit tests, the integration tests and the full required-inputs check. No job calls a model or needs a secret. GitHub tests the head merged with `main` as of the last push; when `main` moved since, `gh pr update-branch ` runs the check again. The commit hook is only a quick check (format and lint of the staged files, scenario values, and the unit tests without the whole-return tests of forms the commit does not change: `scripts/hook-unit-tests.mjs`), so the pull request's check is the one place where the filled PDFs, the integration tests and every removed answer are checked. `gh pr checks --watch` waits for the result. 6. **PDFs to review** when the PR adds or changes a PDF: one link for each file to its page on the PR's branch, `https://github.com/captaincole/OpenReturn/blob//`. GitHub's diff does not show a PDF; the file's page does. Put the files a person must read first (a made input document, the filled return of a new form), then the other filled returns, then blank forms. `git diff --name-only --diff-filter=AM origin/main...HEAD -- '*.pdf'` lists the files. After a push that changes the list, update it. 7. **Refs**: the issue, the PRs it builds on, the doc that recommends the change. diff --git a/package.json b/package.json index 087ce6cb3..1692ddd0f 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "test:pdf": "npm -w @openreturn/engine run test:pdf", "test:inputs": "npm -w @openreturn/engine run test:inputs", "test:unit": "npm -w @openreturn/engine run test:unit && npm -w canon run test:unit && npm -w cade run test:unit && npm --prefix web run test:unit", + "lint:hook": "node scripts/hook-lint.mjs", "test:unit:hook": "node scripts/hook-unit-tests.mjs && npm -w canon run test:unit && npm -w cade run test:unit && npm --prefix web run test:unit", "test:integration": "npm -w @openreturn/engine run test:integration && npm -w cade run test:integration", "coverage": "npm -w @openreturn/engine run coverage", diff --git a/scripts/hook-lint.mjs b/scripts/hook-lint.mjs new file mode 100644 index 000000000..56cfbe874 --- /dev/null +++ b/scripts/hook-lint.mjs @@ -0,0 +1,38 @@ +#!/usr/bin/env node +/* + * Format check and lint for the commit hook, on the staged files only. + * + * `npm run format:check` and `npm run lint` read every file of the repository + * (about 80 seconds and 220 seconds on a busy machine). A commit changes a few + * files, and those take a few seconds. GitHub runs both over the whole + * repository for each pull request (.github/workflows/gates.yml). + */ +import { execFileSync, spawnSync } from "node:child_process"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); + +/* The extensions that `npm run format:check` and `npm run lint` cover. */ +const CHECKED = [".ts", ".tsx", ".mjs", ".js"]; + +const staged = execFileSync("git", ["diff", "--cached", "--name-only", "--diff-filter=ACMR"], { + cwd: root, + encoding: "utf8", +}) + .split("\n") + .filter((file) => CHECKED.some((extension) => file.endsWith(extension))); + +if (staged.length === 0) { + console.log("No staged code file: nothing to format-check or lint."); + process.exit(0); +} + +const run = (tool, args) => + spawnSync("npx", [tool, ...args, ...staged], { cwd: root, stdio: "inherit" }).status ?? 1; + +console.log(`Format check and lint of ${staged.length} staged file(s).`); +const format = run("prettier", ["--check", "--ignore-unknown"]); +if (format !== 0) process.exit(format); +/* A staged file that the lint config ignores is not a warning here. */ +process.exit(run("eslint", ["--max-warnings", "0", "--no-warn-ignored"]));