Public: anyone can read this; the crew can change it.
Drift gate: a page may not define a one-off class for what a ui.ts part already does (warn with a count) #16728
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/5881-drift-gate-a-page-may-not-define-a-one-o → main 0 commits fork
4 files
changed +132
−4
docs/design/baseline.json +2 −1
forge.json +3 −3
| @@ -37,9 +37,9 @@ | ||
| 37 | 37 | } |
| 38 | 38 | }, |
| 39 | 39 | "check": { |
| 40 | "fast": "bun scripts/gate.ts && mkdir -p .forge-cache && GOMEMLIMIT=2GiB ./node_modules/typescript/bin/tsc --noEmit --incremental --tsBuildInfoFile .forge-cache/tsc.tsbuildinfo && bun scripts/test-shards.ts --timeout 60000 --tier fast --diff $FORGE_CHECK_BASE..HEAD --learned \"$FORGE_CHECK_LEARNED\"", | |
| 41 | "run": "bun scripts/gate.ts && mkdir -p .forge-cache && GOMEMLIMIT=2GiB ./node_modules/typescript/bin/tsc --noEmit --incremental --tsBuildInfoFile .forge-cache/tsc.tsbuildinfo", | |
| 42 | "full": "bun scripts/gate.ts && mkdir -p .forge-cache && GOMEMLIMIT=2GiB ./node_modules/typescript/bin/tsc --noEmit --incremental --tsBuildInfoFile .forge-cache/tsc.tsbuildinfo && bun test test/client-size.test.ts", | |
| 40 | "fast": "bun scripts/gate.ts && bun scripts/design-drift.ts && mkdir -p .forge-cache && GOMEMLIMIT=2GiB ./node_modules/typescript/bin/tsc --noEmit --incremental --tsBuildInfoFile .forge-cache/tsc.tsbuildinfo && bun scripts/test-shards.ts --timeout 60000 --tier fast --diff $FORGE_CHECK_BASE..HEAD --learned \"$FORGE_CHECK_LEARNED\"", | |
| 41 | "run": "bun scripts/gate.ts && bun scripts/design-drift.ts && mkdir -p .forge-cache && GOMEMLIMIT=2GiB ./node_modules/typescript/bin/tsc --noEmit --incremental --tsBuildInfoFile .forge-cache/tsc.tsbuildinfo", | |
| 42 | "full": "bun scripts/gate.ts && bun scripts/design-drift.ts && mkdir -p .forge-cache && GOMEMLIMIT=2GiB ./node_modules/typescript/bin/tsc --noEmit --incremental --tsBuildInfoFile .forge-cache/tsc.tsbuildinfo && bun test test/client-size.test.ts", | |
| 43 | 43 | "verify": "bun scripts/gate.ts && bun scripts/test-shards.ts --timeout 60000 --gate --impact --keep-going --nice", |
| 44 | 44 | "timeout": 7200, |
| 45 | 45 | "limits": { |
scripts/design-drift.ts added +92
| @@ -0,0 +1,92 @@ | ||
| 1 | /** | |
| 2 | * The drift gate (#5881): a page module may not define a one-off class for | |
| 3 | * what a `ui.ts` part already does. `design-lint.ts` ratchets new classes | |
| 4 | * against DESIGN.md §5; this counts duplication — a `/* css *\/` rule in a | |
| 5 | * page whose class is a home-made list, row, button, empty state or picker. | |
| 6 | * | |
| 7 | * A class is a duplicate when one of its words (`x-list`, `row-x`, | |
| 8 | * `x-btn`) names a part: list/rows → `.list`/`.row`, btn/button → `.btn`, | |
| 9 | * empty → `.empty`, picker → the search picker. Each class counts once per | |
| 10 | * file, at the line of its first rule. The shared stylesheet (`src/web/style/`) | |
| 11 | * and `ui.ts` are where the parts live and are not scanned. | |
| 12 | * | |
| 13 | * It is a warning: it prints `drift: N one-off classes duplicate a part` with | |
| 14 | * file:line for each and exits 0. `--update` writes N to the `drift` key of | |
| 15 | * docs/design/baseline.json, which `design-lint.ts --update` keeps. | |
| 16 | */ | |
| 17 | ||
| 18 | import { existsSync, readFileSync, writeFileSync } from "node:fs"; | |
| 19 | import { join, relative } from "node:path"; | |
| 20 | import { sourceFiles } from "./design-lint.ts"; | |
| 21 | ||
| 22 | export type Duplicate = { file: string; line: number; cls: string; part: string }; | |
| 23 | ||
| 24 | /** The word in a class name → the part it re-makes. */ | |
| 25 | const PARTS: Record<string, string> = { | |
| 26 | list: ".list/.row", lists: ".list/.row", row: ".list/.row", rows: ".list/.row", | |
| 27 | btn: ".btn", button: ".btn", buttons: ".btn", | |
| 28 | empty: ".empty", picker: "the search picker", | |
| 29 | }; | |
| 30 | ||
| 31 | /** The part a class name re-makes, or undefined. */ | |
| 32 | export function partOf(cls: string): string | undefined { | |
| 33 | for (const w of cls.split("-")) if (PARTS[w]) return PARTS[w]; | |
| 34 | return undefined; | |
| 35 | } | |
| 36 | ||
| 37 | const SHARED = /(^|\/)(style\/|ui\.ts$)/; | |
| 38 | ||
| 39 | /** The css templates: tagged `/* css *\/ \`…\``, or the `const CSS = \`…\`` a page keeps beside its markup. */ | |
| 40 | const CSS_BLOCK = /(?:\/\* css \*\/|\b(?:const|let) [A-Za-z_]*CSS\s*=)\s*`((?:[^`\\]|\\.)*)`/g; | |
| 41 | ||
| 42 | /** The one-off classes in one file's css templates, the first rule's line each. */ | |
| 43 | export function duplicatesIn(file: string, text: string): Duplicate[] { | |
| 44 | const seen = new Set<string>(); | |
| 45 | const out: Duplicate[] = []; | |
| 46 | for (const m of text.matchAll(CSS_BLOCK)) { | |
| 47 | const css = m[1]!; | |
| 48 | const from = m.index! + m[0].length - 1 - css.length; | |
| 49 | // Selectors only: what precedes a `{`, comments blanked (newlines kept). | |
| 50 | const bare = css.replace(/\/\*[\s\S]*?\*\//g, (c) => c.replace(/[^\n]/g, " ")); | |
| 51 | for (const sel of bare.matchAll(/([^{};]+)\{/g)) { | |
| 52 | for (const c of sel[1]!.matchAll(/(?:^|[\s,>+~(\]:)])\.([a-z][a-z0-9_-]*)/gi)) { | |
| 53 | const cls = c[1]!, part = partOf(cls); | |
| 54 | if (!part || seen.has(cls)) continue; | |
| 55 | seen.add(cls); | |
| 56 | const at = from + sel.index! + sel[1]!.indexOf("." + cls, c.index!); | |
| 57 | out.push({ file, line: text.slice(0, at).split("\n").length, cls, part }); | |
| 58 | } | |
| 59 | } | |
| 60 | } | |
| 61 | return out.sort((a, b) => a.line - b.line); | |
| 62 | } | |
| 63 | ||
| 64 | /** The page modules: every src/web file but the ones that hold the parts. */ | |
| 65 | export function drift(root: string): Duplicate[] { | |
| 66 | const files = sourceFiles(root); | |
| 67 | const rel = (f: string) => relative(join(root, "src/web"), f); | |
| 68 | // A class the shared stylesheet defines is the part itself, not a copy of it. | |
| 69 | const shared = new Set(files.filter((f) => SHARED.test(rel(f))).flatMap((f) => duplicatesIn(f, readFileSync(f, "utf8")).map((d) => d.cls))); | |
| 70 | return files | |
| 71 | .filter((f) => !SHARED.test(rel(f))) | |
| 72 | .flatMap((f) => duplicatesIn(relative(root, f), readFileSync(f, "utf8"))) | |
| 73 | .filter((d) => !shared.has(d.cls)); | |
| 74 | } | |
| 75 | ||
| 76 | export function report(found: readonly Duplicate[]): string { | |
| 77 | return [`drift: ${found.length} one-off ${found.length === 1 ? "class duplicates" : "classes duplicate"} a part`, ...found.map((d) => ` ${d.file}:${d.line} .${d.cls} → use ${d.part} from ui.ts`)].join("\n"); | |
| 78 | } | |
| 79 | ||
| 80 | if (import.meta.main) { | |
| 81 | const root = process.cwd(); | |
| 82 | const found = drift(root); | |
| 83 | console.log(report(found)); | |
| 84 | const path = join(root, "docs/design/baseline.json"); | |
| 85 | const file = existsSync(path) ? JSON.parse(readFileSync(path, "utf8")) : { files: {} }; // a corrupt file throws | |
| 86 | const base = file.drift; | |
| 87 | if (typeof base === "number" && found.length > base) console.log(`warning: ${found.length - base} more than the baseline of ${base}; use the part, or run --update`); | |
| 88 | if (process.argv.includes("--update")) { | |
| 89 | writeFileSync(path, JSON.stringify({ ...file, drift: found.length }, null, 2) + "\n"); | |
| 90 | console.log("wrote docs/design/baseline.json"); | |
| 91 | } | |
| 92 | } |
test/design-drift.test.ts added +35
| @@ -0,0 +1,35 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { duplicatesIn, partOf, report } from "../scripts/design-drift.ts"; | |
| 3 | ||
| 4 | const PAGE = [ | |
| 5 | "export const page = 1;", | |
| 6 | "const CSS = `", | |
| 7 | ".todo-list { display: grid; gap: 8px; }", | |
| 8 | ".todo-list li { padding: 4px; }", | |
| 9 | ".todo-card { padding: 8px; }", | |
| 10 | "`;", | |
| 11 | "const other = /* css */ `", | |
| 12 | "/* .commented-btn { } */", | |
| 13 | ".save-btn, .x:hover { color: var(--fg); }", | |
| 14 | ".nothing-here-empty { margin: 0 }", | |
| 15 | "`;", | |
| 16 | ].join("\n"); | |
| 17 | ||
| 18 | describe("drift gate: one-off classes that duplicate a part (#5881)", () => { | |
| 19 | test("a fixture page with a home-made list class is counted, once, at its first rule", () => { | |
| 20 | const found = duplicatesIn("src/web/pages/fixture.ts", PAGE); | |
| 21 | expect(found.map((d) => [d.cls, d.line])).toEqual([["todo-list", 3], ["save-btn", 9], ["nothing-here-empty", 10]]); | |
| 22 | expect(report(found)).toContain("drift: 3 one-off classes duplicate a part"); | |
| 23 | expect(report(found)).toContain("src/web/pages/fixture.ts:3 .todo-list"); | |
| 24 | }); | |
| 25 | ||
| 26 | test("a single class reads in the singular", () => { | |
| 27 | expect(report(duplicatesIn("p.ts", "const CSS = `.a-list { gap: 1px }`;"))).toContain("drift: 1 one-off class duplicates a part"); | |
| 28 | }); | |
| 29 | ||
| 30 | test("a page with no home-made part is clean", () => { | |
| 31 | expect(duplicatesIn("p.ts", "const c = /* css */ `.todo-card { gap: 4px }`;")).toEqual([]); | |
| 32 | expect(partOf("card")).toBeUndefined(); | |
| 33 | expect(partOf("rs-list")).toBe(".list/.row"); | |
| 34 | }); | |
| 35 | }); |
new above
offline