@avi2dg/checks 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,9 +5,10 @@ Deterministic checks shared across my TypeScript repos. One package,
5
5
  Effect language-service block, the shared commitlint config, the shared
6
6
  dependency-cruiser base, the test-layout check with its bunfig preset,
7
7
  the commit-identity check with its workflow, the comment gate with its
8
- workflow and backtest, the Stryker mutation-testing preset with its
9
- no-regression comparator, the CI-wiring check, and the Effect
10
- error-channel plugin compiled to JavaScript.
8
+ workflow and backtest, the oxlint suppressions ratchet, the Stryker
9
+ mutation-testing preset with its no-regression comparator, the
10
+ CI-wiring check, and the Effect error-channel plugin compiled to
11
+ JavaScript.
11
12
 
12
13
  Published as `@avi2dg/checks` on the public npm registry.
13
14
 
@@ -347,6 +348,58 @@ jobs:
347
348
  HEAD_SHA: ${{ github.event.pull_request.head.sha }}
348
349
  ```
349
350
 
351
+ ## Suppressions ratchet
352
+
353
+ `scripts/suppressions-ratchet.ts` holds oxlint's bulk-suppression
354
+ baseline, `oxlint-suppressions.json`, to counts that only fall. oxlint
355
+ accepts whatever `oxlint --suppress-all` writes, so raising a count to
356
+ let a new site through passes the lint. The ratchet reads the file at a
357
+ base and a head and fails naming each file and rule whose count rose or
358
+ that appeared:
359
+
360
+ ```sh
361
+ bun run checks-suppressions-ratchet <base-ref> <head-ref>
362
+ bun run checks-suppressions-ratchet <ref>
363
+ ```
364
+
365
+ With one argument it compares that commit with its parent, and exits 2
366
+ when the parent is not in the clone. With two it reads the base where
367
+ the head branched off, at their merge-base, so a count the base branch
368
+ lowered since does not read as a rise at the head. A count that fell and
369
+ an entry that went both pass. A commit without the file counts as empty,
370
+ so the commit that first adds a baseline fails with every entry
371
+ appearing. The file is read from the directory the command runs in,
372
+ which is where oxlint writes it. It exits 2 when a ref does not resolve
373
+ or the file is not oxlint's count per rule per file:
374
+
375
+ ```
376
+ suppressions-ratchet: 2 count(s) in oxlint-suppressions.json rose or appeared; fix the site instead of suppressing it:
377
+ src/added.ts typescript/no-unsafe-type-assertion appeared with 1
378
+ src/dispatch.ts typescript/no-non-null-assertion rose from 12 to 13
379
+ ```
380
+
381
+ Enforcement runs on pull requests, where the range from the base branch's
382
+ current tip to the head is visible:
383
+
384
+ ```yaml
385
+ on:
386
+ pull_request:
387
+ types: [opened, edited, synchronize, reopened]
388
+ jobs:
389
+ suppressions-ratchet:
390
+ runs-on: ubuntu-latest
391
+ steps:
392
+ - uses: actions/checkout@v5
393
+ with:
394
+ fetch-depth: 0
395
+ - uses: oven-sh/setup-bun@v2
396
+ - run: bun install --frozen-lockfile
397
+ - run: bunx checks-suppressions-ratchet "origin/$BASE_REF" "$HEAD_SHA"
398
+ env:
399
+ BASE_REF: ${{ github.event.pull_request.base.ref }}
400
+ HEAD_SHA: ${{ github.event.pull_request.head.sha }}
401
+ ```
402
+
350
403
  ## CI wiring
351
404
 
352
405
  `checks-ci-wiring` fails when a command the repository's CI must run no
@@ -569,7 +622,7 @@ a tag off `main` and reruns the build, `dist/` check, lint, typecheck
569
622
  and tests before it publishes:
570
623
 
571
624
  ```sh
572
- git tag v0.5.0 && git push origin v0.5.0
625
+ git tag v0.6.0 && git push origin v0.6.0
573
626
  ```
574
627
 
575
628
  The `release` workflow publishes the tagged version through npm
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@avi2dg/checks",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Deterministic checks shared across the captain's TypeScript repos",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -22,6 +22,7 @@
22
22
  "scripts/ci-wiring.ts",
23
23
  "scripts/comments.ts",
24
24
  "scripts/comment-gate.ts",
25
+ "scripts/suppressions-ratchet.ts",
25
26
  "scripts/backtest.ts",
26
27
  "oxlintrc.json",
27
28
  "stryker.preset.js",
@@ -39,6 +40,7 @@
39
40
  "./scripts/ci-wiring.ts": "./scripts/ci-wiring.ts",
40
41
  "./scripts/comments.ts": "./scripts/comments.ts",
41
42
  "./scripts/comment-gate.ts": "./scripts/comment-gate.ts",
43
+ "./scripts/suppressions-ratchet.ts": "./scripts/suppressions-ratchet.ts",
42
44
  "./scripts/backtest.ts": "./scripts/backtest.ts",
43
45
  "./oxlintrc.json": "./oxlintrc.json",
44
46
  "./stryker.preset.js": "./stryker.preset.js",
@@ -52,6 +54,7 @@
52
54
  "checks-mutation-compare": "scripts/mutation-compare.ts",
53
55
  "checks-ci-wiring": "scripts/ci-wiring.ts",
54
56
  "checks-comment-gate": "scripts/comment-gate.ts",
57
+ "checks-suppressions-ratchet": "scripts/suppressions-ratchet.ts",
55
58
  "checks-backtest": "scripts/backtest.ts"
56
59
  },
57
60
  "scripts": {
@@ -0,0 +1,142 @@
1
+ #!/usr/bin/env bun
2
+ export const SUPPRESSIONS = "oxlint-suppressions.json";
3
+
4
+ export type Suppressions = ReadonlyMap<string, ReadonlyMap<string, number>>;
5
+
6
+ export type Rise =
7
+ | { readonly kind: "rose"; readonly file: string; readonly rule: string; readonly base: number; readonly head: number }
8
+ | { readonly kind: "appeared"; readonly file: string; readonly rule: string; readonly head: number };
9
+
10
+ export type Ratchet = {
11
+ readonly counted: number;
12
+ readonly lowered: number;
13
+ readonly rises: readonly Rise[];
14
+ };
15
+
16
+ export class SuppressionsError extends Error {}
17
+
18
+ const USAGE = "usage: suppressions-ratchet.ts <ref> | <base-ref> <head-ref>";
19
+
20
+ function isRecord(value: unknown): value is Record<string, unknown> {
21
+ return typeof value === "object" && value !== null && !Array.isArray(value);
22
+ }
23
+
24
+ function isCount(value: unknown): value is number {
25
+ return typeof value === "number" && Number.isInteger(value) && value >= 0;
26
+ }
27
+
28
+ export function parseSuppressions(text: string, where: string): Suppressions {
29
+ let parsed: unknown;
30
+ try {
31
+ parsed = JSON.parse(text);
32
+ } catch {
33
+ throw new SuppressionsError(`suppressions-ratchet: ${where} is not valid JSON`);
34
+ }
35
+ if (!isRecord(parsed)) {
36
+ throw new SuppressionsError(`suppressions-ratchet: ${where} is not an object of files`);
37
+ }
38
+ const files = new Map<string, ReadonlyMap<string, number>>();
39
+ for (const [file, rules] of Object.entries(parsed)) {
40
+ if (!isRecord(rules)) {
41
+ throw new SuppressionsError(`suppressions-ratchet: ${where} holds ${file} without an object of rules`);
42
+ }
43
+ const counts = new Map<string, number>();
44
+ for (const [rule, entry] of Object.entries(rules)) {
45
+ if (!isRecord(entry) || !isCount(entry["count"])) {
46
+ throw new SuppressionsError(`suppressions-ratchet: ${where} holds ${file} ${rule} without a whole count`);
47
+ }
48
+ counts.set(rule, entry["count"]);
49
+ }
50
+ files.set(file, counts);
51
+ }
52
+ return files;
53
+ }
54
+
55
+ export function compareSuppressions(base: Suppressions, head: Suppressions): Ratchet {
56
+ const rises: Rise[] = [];
57
+ let counted = 0;
58
+ for (const [file, rules] of head) {
59
+ for (const [rule, count] of rules) {
60
+ counted += 1;
61
+ const before = base.get(file)?.get(rule);
62
+ if (before === undefined) {
63
+ if (count > 0) rises.push({ kind: "appeared", file, rule, head: count });
64
+ } else if (count > before) {
65
+ rises.push({ kind: "rose", file, rule, base: before, head: count });
66
+ }
67
+ }
68
+ }
69
+ let lowered = 0;
70
+ for (const [file, rules] of base) {
71
+ for (const [rule, count] of rules) {
72
+ if ((head.get(file)?.get(rule) ?? 0) < count) lowered += 1;
73
+ }
74
+ }
75
+ rises.sort((a, b) => a.file.localeCompare(b.file) || a.rule.localeCompare(b.rule));
76
+ return { counted, lowered, rises };
77
+ }
78
+
79
+ function describeRise(rise: Rise): string {
80
+ const change = rise.kind === "rose" ? `rose from ${rise.base} to ${rise.head}` : `appeared with ${rise.head}`;
81
+ return `${rise.file} ${rise.rule} ${change}`;
82
+ }
83
+
84
+ export function report({ counted, lowered, rises }: Ratchet): string {
85
+ if (rises.length === 0) {
86
+ return `suppressions-ratchet: no count in ${SUPPRESSIONS} rose or appeared (${counted} at the head, ${lowered} lowered)`;
87
+ }
88
+ return [
89
+ `suppressions-ratchet: ${rises.length} count(s) in ${SUPPRESSIONS} rose or appeared; fix the site instead of suppressing it:`,
90
+ ...rises.map((rise) => ` ${describeRise(rise)}`),
91
+ ].join("\n");
92
+ }
93
+
94
+ function die(message: string): never {
95
+ console.error(message);
96
+ process.exit(2);
97
+ }
98
+
99
+ function git(...args: readonly string[]): string {
100
+ const result = Bun.spawnSync(["git", ...args], { stdout: "pipe", stderr: "pipe" });
101
+ if (!result.success) {
102
+ die(`suppressions-ratchet: git ${args.join(" ")}: ${result.stderr.toString().trim()}`);
103
+ }
104
+ return result.stdout.toString();
105
+ }
106
+
107
+ function commitOf(rev: string): string {
108
+ return git("rev-parse", "--verify", `${rev}^{commit}`).trim();
109
+ }
110
+
111
+ function mergeBase(base: string, head: string): string {
112
+ const result = Bun.spawnSync(["git", "merge-base", base, head], { stdout: "pipe", stderr: "pipe" });
113
+ if (!result.success) die(`suppressions-ratchet: ${base} and ${head} share no commit`);
114
+ return result.stdout.toString().trim();
115
+ }
116
+
117
+ function suppressionsAt(commit: string): Suppressions {
118
+ const blob = git("ls-tree", "--object-only", commit, "--", SUPPRESSIONS).trim();
119
+ if (blob === "") return new Map();
120
+ try {
121
+ return parseSuppressions(git("cat-file", "blob", blob), `${SUPPRESSIONS} at ${commit}`);
122
+ } catch (error) {
123
+ return die(error instanceof SuppressionsError ? error.message : `suppressions-ratchet: ${String(error)}`);
124
+ }
125
+ }
126
+
127
+ export function runRange(base: string, head: string): Ratchet {
128
+ const headCommit = commitOf(head);
129
+ // At the base tip, a count the base branch lowered after the head branched off would read as a rise at the head.
130
+ const branchPoint = mergeBase(commitOf(base), headCommit);
131
+ return compareSuppressions(suppressionsAt(branchPoint), suppressionsAt(headCommit));
132
+ }
133
+
134
+ if (import.meta.main) {
135
+ const [first, second, ...extra] = process.argv.slice(2);
136
+ if (first === undefined || extra.length > 0) die(USAGE);
137
+
138
+ const ratchet = second !== undefined ? runRange(first, second) : runRange(`${first}^`, first);
139
+
140
+ console.log(report(ratchet));
141
+ process.exit(ratchet.rises.length === 0 ? 0 : 1);
142
+ }