|
| 1 | +#!/usr/bin/env node |
| 2 | +// Durable guard for the "every first-party source file is format-gated" invariant |
| 3 | +// that #1789 established by a one-shot manual audit (PR #1792). A prettier |
| 4 | +// `format`/`format:check` glob that stops covering a file fails silently — the |
| 5 | +// file is simply skipped, not reported — so a nested dir (`.storybook/`, a |
| 6 | +// client `scripts/`) or a new extension can re-open the gap unnoticed. This is |
| 7 | +// the format-coverage analog of `verify:build-gate`: it enumerates every tracked |
| 8 | +// source file and asserts each is matched by at least one `prettier --check` |
| 9 | +// glob declared across the repo's `package.json`s. Exits non-zero, listing the |
| 10 | +// offenders, on any miss. |
| 11 | +// |
| 12 | +// Source of truth is the `format:check*` scripts themselves — this parser reads |
| 13 | +// the globs out of them, so widening/narrowing a glob is reflected here with no |
| 14 | +// second list to keep in sync. |
| 15 | + |
| 16 | +import { readFileSync } from "node:fs"; |
| 17 | +import { execFileSync } from "node:child_process"; |
| 18 | +import path from "node:path"; |
| 19 | +import { fileURLToPath } from "node:url"; |
| 20 | + |
| 21 | +const repoRoot = path.resolve( |
| 22 | + path.dirname(fileURLToPath(import.meta.url)), |
| 23 | + "..", |
| 24 | +); |
| 25 | + |
| 26 | +// File extensions prettier formats as first-party source here. Kept in sync with |
| 27 | +// the union of the format globs; a file with one of these extensions that no |
| 28 | +// glob matches is the failure this guard exists to catch. |
| 29 | +const SOURCE_EXTENSIONS = [ |
| 30 | + "ts", |
| 31 | + "tsx", |
| 32 | + "mts", |
| 33 | + "cts", |
| 34 | + "js", |
| 35 | + "jsx", |
| 36 | + "mjs", |
| 37 | + "cjs", |
| 38 | +]; |
| 39 | + |
| 40 | +// The `package.json`s whose `format:check*` scripts define the gate. The root |
| 41 | +// carries the split `format:check:core|scripts|shared`; each client carries a |
| 42 | +// single `format:check`. Paths are relative to the repo root; each glob resolves |
| 43 | +// relative to its manifest's directory (prettier's cwd for that script). |
| 44 | +const MANIFESTS = [ |
| 45 | + ".", |
| 46 | + "clients/web", |
| 47 | + "clients/cli", |
| 48 | + "clients/tui", |
| 49 | + "clients/launcher", |
| 50 | +]; |
| 51 | + |
| 52 | +/** Split a shell-ish command string into args, honoring double quotes. */ |
| 53 | +function tokenize(command) { |
| 54 | + const tokens = []; |
| 55 | + const re = /"([^"]*)"|(\S+)/g; |
| 56 | + let m; |
| 57 | + while ((m = re.exec(command)) !== null) { |
| 58 | + tokens.push(m[1] !== undefined ? m[1] : m[2]); |
| 59 | + } |
| 60 | + return tokens; |
| 61 | +} |
| 62 | + |
| 63 | +/** Extract the path/glob args from every `prettier --check …` in a manifest. */ |
| 64 | +function prettierCheckArgs(scripts) { |
| 65 | + const args = []; |
| 66 | + for (const value of Object.values(scripts ?? {})) { |
| 67 | + if (typeof value !== "string" || !value.includes("prettier --check")) |
| 68 | + continue; |
| 69 | + // A manifest may chain `prettier --check …` inside a larger script; take the |
| 70 | + // segment starting at each occurrence up to the next `&&`/`||`/`;`. |
| 71 | + for (const segment of value.split(/&&|\|\||;/)) { |
| 72 | + const trimmed = segment.trim(); |
| 73 | + if (!trimmed.startsWith("prettier --check")) continue; |
| 74 | + const tokens = tokenize(trimmed).slice(2); // drop `prettier` `--check` |
| 75 | + for (const t of tokens) { |
| 76 | + if (t.startsWith("-")) continue; // a flag, not a path |
| 77 | + args.push(t); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return args; |
| 82 | +} |
| 83 | + |
| 84 | +const GLOB_CHARS = /[*?{}[\]]/; |
| 85 | + |
| 86 | +/** Convert a prettier glob to an anchored RegExp over repo-relative POSIX paths. */ |
| 87 | +function globToRegExp(glob) { |
| 88 | + let re = ""; |
| 89 | + for (let i = 0; i < glob.length; i++) { |
| 90 | + const c = glob[i]; |
| 91 | + if (c === "*") { |
| 92 | + if (glob[i + 1] === "*") { |
| 93 | + // `**` (optionally `**/`) crosses path separators. |
| 94 | + if (glob[i + 2] === "/") { |
| 95 | + re += "(?:.*/)?"; |
| 96 | + i += 2; |
| 97 | + } else { |
| 98 | + re += ".*"; |
| 99 | + i += 1; |
| 100 | + } |
| 101 | + } else { |
| 102 | + re += "[^/]*"; // `*` stays within a segment |
| 103 | + } |
| 104 | + } else if (c === "?") { |
| 105 | + re += "[^/]"; |
| 106 | + } else if (c === "{") { |
| 107 | + re += "(?:"; |
| 108 | + } else if (c === "}") { |
| 109 | + re += ")"; |
| 110 | + } else if (c === ",") { |
| 111 | + re += "|"; |
| 112 | + } else if (".+^$()|\\/".includes(c)) { |
| 113 | + re += "\\" + c; |
| 114 | + } else { |
| 115 | + re += c; |
| 116 | + } |
| 117 | + } |
| 118 | + return new RegExp("^" + re + "$"); |
| 119 | +} |
| 120 | + |
| 121 | +/** Build the set of coverage predicates from all manifests' format globs. */ |
| 122 | +function buildMatchers() { |
| 123 | + const matchers = []; |
| 124 | + for (const manifestDir of MANIFESTS) { |
| 125 | + const manifestPath = path.join(repoRoot, manifestDir, "package.json"); |
| 126 | + const pkg = JSON.parse(readFileSync(manifestPath, "utf8")); |
| 127 | + for (const arg of prettierCheckArgs(pkg.scripts)) { |
| 128 | + const rel = manifestDir === "." ? arg : path.posix.join(manifestDir, arg); |
| 129 | + if (GLOB_CHARS.test(arg)) { |
| 130 | + const re = globToRegExp(rel); |
| 131 | + matchers.push((f) => re.test(f)); |
| 132 | + } else { |
| 133 | + // A bare path: a directory prettier recurses into, or an exact file. |
| 134 | + matchers.push((f) => f === rel || f.startsWith(rel + "/")); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + return matchers; |
| 139 | +} |
| 140 | + |
| 141 | +function trackedSourceFiles() { |
| 142 | + const out = execFileSync( |
| 143 | + "git", |
| 144 | + ["ls-files", ...SOURCE_EXTENSIONS.map((e) => `*.${e}`)], |
| 145 | + { cwd: repoRoot, encoding: "utf8" }, |
| 146 | + ); |
| 147 | + return out.split("\n").filter(Boolean); |
| 148 | +} |
| 149 | + |
| 150 | +const matchers = buildMatchers(); |
| 151 | +const files = trackedSourceFiles(); |
| 152 | +const ungated = files.filter((f) => !matchers.some((m) => m(f))); |
| 153 | + |
| 154 | +if (ungated.length > 0) { |
| 155 | + console.error( |
| 156 | + `verify:format-coverage — ${ungated.length} tracked source file(s) are not covered by any prettier format glob:\n`, |
| 157 | + ); |
| 158 | + for (const f of ungated) console.error(" " + f); |
| 159 | + console.error( |
| 160 | + "\nAdd the file's directory (or a matching glob) to the relevant `format`/`format:check` script,", |
| 161 | + ); |
| 162 | + console.error("or widen the extension set. See AGENTS.md."); |
| 163 | + process.exit(1); |
| 164 | +} |
| 165 | + |
| 166 | +console.log( |
| 167 | + `verify:format-coverage — OK: all ${files.length} tracked source files are format-gated.`, |
| 168 | +); |
0 commit comments