Public: anyone can read this; the crew can change it.

scripts/design-lint.ts runs the empty-words and width-read ratchets, so a builder sees them before the push #17531

opened this

Merged 17h 41m ago, live in production

torque/forge:torque/17521-scripts-design-lint-ts-runs-the-empty-wo → main 0 commits fork

  • Merged. torque/17521-scripts-design-lint-ts-runs-the-empty-wo was merged into main as 57d706a.

Review

Sign in to review this change.
5 files changed +188 −94
scripts/design-lint.ts +20 −1
@@ -45,6 +45,10 @@
4545 * `stranger` (#11458) names each word of `STRANGER_NEVER_SEES` that a stranger-path page says; see
4646 * `strangerWords()`.
4747 *
48 * The empty-words and width-read ratchets (#17521) are counted in `src/design/ratchets.ts`, the
49 * module test/empty-words-all.test.ts and test/behaviors-rule.test.ts hold them with; see
50 * `ratchetFindings()`.
51 *
4852 * `banner` (#10699) refuses a standing alert in the home and board builders; see `banner()`.
4953 *
5054 * A red names the file:line, the literal and what to use instead, so one pass
@@ -77,6 +81,7 @@ import { scanFeel, staleAllows } from "../src/design/feel.ts";
7781 import { motionFindings } from "../src/design/motion.ts";
7882 import { PHONE_PX, widthFindings } from "../src/design/width.ts";
7983 import { cssBlocksAt, scanOverlay, sourceFiles } from "../src/design/overlay.ts";
84 import { ownWordsOff, type Source, tsSources, WIDTH_SKIP, widthReadsOver } from "../src/design/ratchets.ts";
8085
8186 // The covered-page rule lives in src/design/ so the release preflight runs it too (#11916).
8287 export { motionFindings, type MotionFinding } from "../src/design/motion.ts";
@@ -668,6 +673,20 @@ export function scanRing(root: string): string[] {
668673 return out;
669674 }
670675
676 /**
677 * The two ratchets the check most often refused for (#17521): a page's own bare "No …" empty state
678 * beyond OWN_WORDS (or fewer than it names), and a width read in script beyond WIDTH_READS. The
679 * same counts and words as test/empty-words-all.test.ts and test/behaviors-rule.test.ts.
680 */
681 export function ratchetFindings(web: readonly Source[], src: readonly Source[]): string[] {
682 const own = ownWordsOff(web);
683 return [...own.over, ...own.moved, ...widthReadsOver(src)];
684 }
685 /** Each over the files its test reads: every file under src/web, and src/ but for WIDTH_SKIP. */
686 export function scanRatchets(root: string): string[] {
687 return ratchetFindings(tsSources(root, "src/web"), tsSources(root, "src", WIDTH_SKIP));
688 }
689
671690 const BASELINE = "docs/design/baseline.json";
672691 const DESIGN = "docs/DESIGN.md";
673692 const SECTIONS = "docs/design/sections";
@@ -684,7 +703,7 @@ if (import.meta.main) {
684703 } else {
685704 const base: Baseline = JSON.parse(readFileSync(join(root, BASELINE), "utf8"));
686705 const debt: QuietDebt = JSON.parse(readFileSync(join(root, QUIET_DEBT), "utf8"));
687 const problems = [...compare(base, now, designText(root), areaFiles(root)), ...compareQuiet(debt, scanQuiet(root)), ...scanBanner(root), ...compareLayout(base.layout ?? {}, scanLayout(root)), ...scanOverlay(root), ...scanCards(root), ...scanStranger(root), ...scanMotion(root), ...scanFeel(root), ...staleAllows(root).map((a) => `${a.file}: ${a.rule} "${a.text}" is allowed in docs/design/feel-allow.json but no longer found: take it out`), ...scanWidth(root), ...scanDictate(root), ...scanRing(root), ...contrastFindings()];
706 const problems = [...compare(base, now, designText(root), areaFiles(root)), ...compareQuiet(debt, scanQuiet(root)), ...scanBanner(root), ...compareLayout(base.layout ?? {}, scanLayout(root)), ...scanOverlay(root), ...scanCards(root), ...scanStranger(root), ...scanMotion(root), ...scanFeel(root), ...staleAllows(root).map((a) => `${a.file}: ${a.rule} "${a.text}" is allowed in docs/design/feel-allow.json but no longer found: take it out`), ...scanWidth(root), ...scanDictate(root), ...scanRing(root), ...scanRatchets(root), ...contrastFindings()];
688707 for (const p of problems) console.error(p);
689708 if (problems.length) process.exit(1);
690709 console.log("design: no colour, size or component added outside the system");
src/design/ratchets.ts added +114
@@ -0,0 +1,114 @@
1 /**
2 * Two per-file ratchets, counted once (#17521): a page's own bare "No …" empty state (#15851) and a
3 * width read in script (#13783). test/empty-words-all.test.ts and test/behaviors-rule.test.ts hold
4 * them; scripts/design-lint.ts prints the same findings, so a builder sees them before the push.
5 * Each table names a file's count today; a file may hold no more, and an empty-words count must be
6 * exact, so moving one off shrinks it and a new one fails.
7 *
8 * This module lives in src/design/, not src/web/, because the empty-words scan reads every file
9 * under src/web. The width pattern spells its words with a character class so this file reads no
10 * width itself; the pattern is the same.
11 */
12 import { readdirSync, readFileSync, statSync } from "node:fs";
13 import { join } from "node:path";
14
15 /** A source file: its path from the repository root, and its text. */
16 export type Source = { path: string; text: string };
17
18 /** Every `.ts` file under `dir` (relative to `root`), skipping paths `skip` matches. */
19 export function tsSources(root: string, dir: string, skip?: RegExp): Source[] {
20 const walk = (d: string): string[] =>
21 readdirSync(join(root, d)).flatMap((n) => {
22 const p = `${d}/${n}`;
23 if (skip?.test(p)) return [];
24 return statSync(join(root, p)).isDirectory() ? walk(p) : p.endsWith(".ts") ? [p] : [];
25 });
26 return walk(dir).map((path) => ({ path, text: readFileSync(join(root, path), "utf8") }));
27 }
28
29 // A page's own "No …" empty state, in either form: empty({ title: "No …" }) (one line or several)
30 // or an inline <p class="… muted …">No …</p>, whatever classes sit beside muted and whatever
31 // follows. A new empty state goes in src/core/empty_words.ts and is drawn with emptyList(kind).
32 const OWN_FORM = /empty\(\{\s*(?:icon: [^,]+,\s*)?title: [`"]No |empty\("No |<p class="(?:[^"]* )?muted(?: [^"]*)?">\s*No /g;
33 // What is left is split into #15899 (admin, system), #15900 (boards, bets, flows), #15901
34 // (repository pages), #15902 (apps, forges) and #15903 (studio, renderers); #15851 waits for them.
35 export const OWN_WORDS: Readonly<Record<string, number>> = {
36 "src/web/diff-view.ts": 1,
37 "src/web/pages/boards.ts": 1,
38 "src/web/pages/browse.ts": 1,
39 "src/web/pages/build_shots.ts": 1,
40 "src/web/pages/commits.ts": 1,
41 "src/web/pages/contribute.ts": 1,
42 "src/web/pages/display.ts": 1,
43 "src/web/pages/family.ts": 1,
44 "src/web/pages/inventory.ts": 1,
45 "src/web/pages/issues.ts": 1,
46 "src/web/pages/looks.ts": 1,
47 "src/web/pages/notes.ts": 1,
48 "src/web/pages/origin-flow.ts": 1,
49 "src/web/pages/origins.ts": 1,
50 "src/web/pages/packs.ts": 2,
51 "src/web/pages/pull.ts": 1,
52 "src/web/pages/reports.ts": 1,
53 "src/web/pages/sources.ts": 1,
54 "src/web/studio/explore.ts": 1,
55 "src/web/studio/hold.ts": 1,
56 };
57
58 /** How many bare "No …" empty states the text words itself. */
59 export const ownWords = (src: string): number => [...src.matchAll(OWN_FORM)].length;
60
61 /**
62 * Files under src/web off their OWN_WORDS count, each with its fix: `over` words more bare "No …"
63 * empty states than named (the table moves them), `moved` fewer (the count is lowered).
64 */
65 export function ownWordsOff(files: readonly Source[]): { over: string[]; moved: string[] } {
66 const over: string[] = [];
67 const moved: string[] = [];
68 for (const f of files) {
69 const n = ownWords(f.text), named = OWN_WORDS[f.path] ?? 0;
70 if (n > named) over.push(`${f.path}: ${n} > ${named} bare 'No …' empty states; add the words to the EMPTY_WORDS table (src/core/empty_words.ts) and draw them with emptyList(kind)`);
71 else if (n < named) moved.push(`${f.path}: ${n} < ${named} bare 'No …' empty states; lower its count in OWN_WORDS (src/design/ratchets.ts)`);
72 }
73 return { over, moved };
74 }
75
76 /** Paths under src/ the width ratchet does not read. */
77 export const WIDTH_SKIP = /\/(vendor|render|og)(\/|$)/;
78
79 /**
80 * Files that read a width in script today, with how many reads: layout must answer to CSS instead.
81 */
82 export const WIDTH_READS: Readonly<Record<string, number>> = {
83 "src/looks/capture.ts": 6,
84 "src/looks/readable.ts": 4,
85 "src/actions/runs.ts": 1,
86 "src/checks/looks.ts": 3,
87 "src/checks/summary.ts": 1,
88 // +1: the probe for the page's own bar at the pill's corner (#16310).
89 "src/apps/toolbar.ts": 6,
90 // The toolbar lab prints the viewport it measured in (#16310).
91 "src/apps/toolbar_lab.ts": 1,
92 "src/web/space_client.ts": 2,
93 "src/web/forge_made.ts": 1,
94 "src/web/client.ts": 2,
95 "src/web/pages/iterations.ts": 7,
96 "src/web/pages/map.ts": 2,
97 "src/web/studio/cards.ts": 1,
98 "src/keeper/covered.ts": 1,
99 "src/core/looks.ts": 3,
100 };
101
102 const WIDTH = /inner[W]idth|client[W]idth|offset[W]idth|matchMedia\(['"]\((min|max)-/g;
103
104 /** How many times the text reads a width in script. */
105 export const widthReads = (text: string): number => (text.match(WIDTH) ?? []).length;
106
107 /** Files that read a width in script beyond WIDTH_READS, and the fix. The behaviors read none. */
108 export function widthReadsOver(files: readonly Source[]): string[] {
109 return files
110 .filter((f) => f.path !== "src/web/behaviors.ts")
111 .map((f) => ({ path: f.path, n: widthReads(f.text) }))
112 .filter((f) => f.n > (WIDTH_READS[f.path] ?? 0))
113 .map((f) => `${f.path}: ${f.n} reads of a width in script; use a media or container query`);
114 }
test/behaviors-rule.test.ts +11 −47
@@ -1,25 +1,19 @@
11 /**
22 * The rule (#13783): a page uses a behavior (`src/web/behaviors.ts`) or plain HTML for an
33 * interaction, and layout answers to CSS alone. Both are ratchets: the pages that already carry
4 * their own script, and the files that already read a width in script, are named here and may only
5 * leave this list. A page that is new, or a read that is new, fails until it uses a behavior or
6 * CSS.
4 * their own script (named here), and the files that already read a width in script (named in
5 * `WIDTH_READS`, src/design/ratchets.ts), may only leave their list. A page that is new, or a read
6 * that is new, fails until it uses a behavior or CSS.
7 *
8 * @tree-scan: walks all of src/ through tsSources (src/design/ratchets.ts).
79 */
810 import { expect, test } from "bun:test";
9 import { readFileSync, readdirSync, statSync } from "node:fs";
1011 import { join } from "node:path";
12 import { tsSources, WIDTH_SKIP, widthReads, widthReadsOver } from "../src/design/ratchets.ts";
1113
12 const SRC = join(import.meta.dir, "..", "src");
13 const SKIP = /\/(vendor|render|og)(\/|$)/;
14
15 function walk(dir: string): string[] {
16 return readdirSync(dir).flatMap((n) => {
17 const p = join(dir, n);
18 if (SKIP.test(p)) return [];
19 return statSync(p).isDirectory() ? walk(p) : p.endsWith(".ts") ? [p] : [];
20 });
21 }
22 const files = walk(SRC).map((p) => ({ path: p.slice(SRC.length - 3), text: readFileSync(p, "utf8") }));
14 // The width-read table and its pattern live in src/design/ratchets.ts, so `bun
15 // scripts/design-lint.ts` names the same files before a push (#17521).
16 const files = tsSources(join(import.meta.dir, ".."), "src", WIDTH_SKIP);
2317
2418 /**
2519 * Pages that ship their own script today (each is replaced by a behavior, page by page, then
@@ -90,31 +84,6 @@ const OWN_SCRIPT = new Set([
9084 "src/web/studio/toolbar.ts"
9185 ]);
9286
93 /**
94 * Files that read a width in script today, with how many reads: layout must answer to CSS instead.
95 */
96 const WIDTH_READS: Record<string, number> = {
97 "src/looks/capture.ts": 6,
98 "src/looks/readable.ts": 4,
99 "src/actions/runs.ts": 1,
100 "src/checks/looks.ts": 3,
101 "src/checks/summary.ts": 1,
102 // +1: the probe for the page's own bar at the pill's corner (#16310).
103 "src/apps/toolbar.ts": 6,
104 // The toolbar lab prints the viewport it measured in (#16310).
105 "src/apps/toolbar_lab.ts": 1,
106 "src/web/space_client.ts": 2,
107 "src/web/forge_made.ts": 1,
108 "src/web/client.ts": 2,
109 "src/web/pages/iterations.ts": 7,
110 "src/web/pages/map.ts": 2,
111 "src/web/studio/cards.ts": 1,
112 "src/keeper/covered.ts": 1,
113 "src/core/looks.ts": 3
114 };
115
116 const WIDTH = /innerWidth|clientWidth|offsetWidth|matchMedia\(['"]\((min|max)-/g;
117
11887 /**
11988 * Whether the file sets a script of its own. Handing one on to `documentHeaders` (the 304 a held
12089 * page is owed carries the page's CSP hash) forwards a script; it does not own one.
@@ -162,17 +131,12 @@ test("the list only names pages that still set a script", () => {
162131 });
163132
164133 test("no file reads a width in script beyond what the list allows", () => {
165 const over = files
166 .filter((f) => f.path !== "src/web/behaviors.ts")
167 .map((f) => ({ path: f.path, n: (f.text.match(WIDTH) ?? []).length }))
168 .filter((f) => f.n > (WIDTH_READS[f.path] ?? 0))
169 .map((f) => `${f.path}: ${f.n} reads of a width in script; use a media or container query`);
170 expect(over).toEqual([]);
134 expect(widthReadsOver(files)).toEqual([]);
171135 });
172136
173137 test("the behaviors themselves read no width and the home lanes use them", () => {
174138 const behaviors = files.find((f) => f.path === "src/web/behaviors.ts")?.text ?? "";
175 expect(behaviors.match(WIDTH) ?? []).toEqual([]);
139 expect(widthReads(behaviors)).toBe(0);
176140 const home = files.find((f) => f.path === "src/web/pages/home_flow.ts")?.text ?? "";
177141 expect(home).toContain("data-disclose");
178142 expect(home).toContain("behaviorsJs");
test/design-lint-ratchets.test.ts added +34
@@ -0,0 +1,34 @@
1 /**
2 * The design lint names the empty-words and width-read ratchets (#17521), so a builder sees them
3 * before the check does: the same counts, the same words and the fix.
4 */
5 import { expect, test } from "bun:test";
6 import { ratchetFindings } from "../scripts/design-lint.ts";
7 import { OWN_WORDS, WIDTH_READS } from "../src/design/ratchets.ts";
8
9 const page = "src/web/pages/packs.ts";
10 const reader = "src/web/pages/map.ts";
11 const bareNo = (n: number) => Array.from({ length: n }, () => `<p class="small muted">No packs yet.</p>`).join("\n");
12 const reads = (n: number) => Array.from({ length: n }, () => "const w = window.inner" + "Width;").join("\n");
13
14 test("one extra bare 'No …' and one extra width read are both named, with the fix", () => {
15 const web = [{ path: page, text: bareNo(OWN_WORDS[page]! + 1) }];
16 const src = [{ path: reader, text: reads(WIDTH_READS[reader]! + 1) }];
17 const found = ratchetFindings(web, src);
18 expect(found).toEqual([
19 `${page}: 3 > 2 bare 'No …' empty states; add the words to the EMPTY_WORDS table (src/core/empty_words.ts) and draw them with emptyList(kind)`,
20 `${reader}: 3 reads of a width in script; use a media or container query`,
21 ]);
22 });
23
24 test("a file at its count is quiet; one below is told to lower it", () => {
25 expect(ratchetFindings([{ path: page, text: bareNo(2) }], [{ path: reader, text: reads(2) }])).toEqual([]);
26 expect(ratchetFindings([{ path: page, text: bareNo(1) }], [])).toEqual([
27 `${page}: 1 < 2 bare 'No …' empty states; lower its count in OWN_WORDS (src/design/ratchets.ts)`,
28 ]);
29 });
30
31 test("a file nobody named may hold none", () => {
32 const fresh = "src/web/pages/brand-new.ts";
33 expect(ratchetFindings([{ path: fresh, text: `empty({ title: "No rows" })` }], [{ path: fresh, text: "el.client" + "Width" }])).toHaveLength(2);
34 });
test/empty-words-all.test.ts +9 −46
@@ -3,55 +3,20 @@
33 * reason and one link only where they add something and the place exists), and a page that words
44 * its own "No …" empty state is named below until it moves.
55 *
6 * @tree-scan: walks all of src/web/ through readdirSync on a parameter.
6 * @tree-scan: walks all of src/web/ through tsSources (src/design/ratchets.ts).
77 */
88 import { expect, test } from "bun:test";
9 import { readdirSync, readFileSync, statSync } from "node:fs";
10 import { join } from "node:path";
119 import { EMPTY_WORDS } from "../src/core/empty_words.ts";
10 import { ownWordsOff, tsSources } from "../src/design/ratchets.ts";
1211
13 const walk = (dir: string): string[] =>
14 readdirSync(dir).flatMap((f) => {
15 const p = join(dir, f);
16 return statSync(p).isDirectory() ? walk(p) : p.endsWith(".ts") ? [p] : [];
17 });
18 const files = walk("src/web").map((p) => [p, readFileSync(p, "utf8")] as const);
19
20 // A page's own "No …" empty state, in either form: empty({ title: "No …" }) (one line or several)
21 // or an inline <p class="… muted …">No …</p>, whatever classes sit beside muted and whatever
22 // follows. The table below is a ratchet: a file may hold no more than its count, and must name the
23 // count exactly, so moving one to emptyList(kind) shrinks it and a new one fails. A new empty state
24 // goes in src/core/empty_words.ts and is drawn with emptyList(kind).
25 const OWN_FORM = /empty\(\{\s*(?:icon: [^,]+,\s*)?title: [`"]No |empty\("No |<p class="(?:[^"]* )?muted(?: [^"]*)?">\s*No /g;
26 // What is left is split into #15899 (admin, system), #15900 (boards, bets, flows), #15901
27 // (repository pages), #15902 (apps, forges) and #15903 (studio, renderers); #15851 waits for them.
28 const OWN_WORDS: Readonly<Record<string, number>> = {
29 "src/web/diff-view.ts": 1,
30 "src/web/pages/boards.ts": 1,
31 "src/web/pages/browse.ts": 1,
32 "src/web/pages/build_shots.ts": 1,
33 "src/web/pages/commits.ts": 1,
34 "src/web/pages/contribute.ts": 1,
35 "src/web/pages/display.ts": 1,
36 "src/web/pages/family.ts": 1,
37 "src/web/pages/inventory.ts": 1,
38 "src/web/pages/issues.ts": 1,
39 "src/web/pages/looks.ts": 1,
40 "src/web/pages/notes.ts": 1,
41 "src/web/pages/origin-flow.ts": 1,
42 "src/web/pages/origins.ts": 1,
43 "src/web/pages/packs.ts": 2,
44 "src/web/pages/pull.ts": 1,
45 "src/web/pages/reports.ts": 1,
46 "src/web/pages/sources.ts": 1,
47 "src/web/studio/explore.ts": 1,
48 "src/web/studio/hold.ts": 1,
49 };
50 const own = (src: string): number => [...src.matchAll(OWN_FORM)].length;
12 // The form a page's own "No …" empty state takes, and the table of pages that still word one (a
13 // ratchet: no more than its count, and the count exact), live in src/design/ratchets.ts, so
14 // `bun scripts/design-lint.ts` names the same files before a push (#17521).
15 const files = tsSources(".", "src/web");
5116
5217 test("every emptyList(kind) a page registers has an entry in the table", () => {
5318 const missing: string[] = [];
54 for (const [p, src] of files) {
19 for (const { path: p, text: src } of files) {
5520 for (const m of src.matchAll(/emptyList\(\s*"([^"]+)"/g)) {
5621 if (!(m[1]! in EMPTY_WORDS)) missing.push(`${p}: emptyList("${m[1]}") has no entry in src/core/empty_words.ts`);
5722 }
@@ -80,11 +45,9 @@ test("a link is a label and a place that exists; no default href points nowhere"
8045 });
8146
8247 test("a page does not word more bare 'No …' empty states than it is named for", () => {
83 const over = files.filter(([p, src]) => own(src) > (OWN_WORDS[p] ?? 0)).map(([p, src]) => `${p}: ${own(src)} > ${OWN_WORDS[p] ?? 0}`);
84 expect(over).toEqual([]);
48 expect(ownWordsOff(files).over).toEqual([]);
8549 });
8650
8751 test("a page that moved to the table is off the list of own words", () => {
88 const moved = files.filter(([p, src]) => own(src) < (OWN_WORDS[p] ?? 0)).map(([p, src]) => `${p}: ${own(src)} < ${OWN_WORDS[p]}`);
89 expect(moved).toEqual([]);
52 expect(ownWordsOff(files).moved).toEqual([]);
9053 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs