@antelopejs/tooling-configs 0.2.7 → 0.2.9

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.
@@ -1,144 +0,0 @@
1
- import { n as renamedPaths } from "./renames-BGO4dWJ8.mjs";
2
- import { n as changedFiles } from "./files-BMxKg5IR.mjs";
3
- import { t as stagedAdditions } from "./staged-ratchet-CJHjp2kx.mjs";
4
- import { n as revisionSuppressions, r as worktreeSuppressions, t as countSuppressions } from "./suppressions-CgGa-kh7.mjs";
5
- import { existsSync, readFileSync, writeFileSync } from "node:fs";
6
- import { join } from "node:path";
7
- import { execFileSync, spawnSync } from "node:child_process";
8
- //#region src/guardrails/suppression-budget.ts
9
- /** The per-repository suppression budget, at the repository root. It is permanent: nothing deletes it, even at zero. */
10
- const SUPPRESSIONS_FILE = "guardrails.suppressions.json";
11
- /** What a failure tells the author to do. */
12
- const SUPPRESSION_FIX = "Fix: fix the code, or remove another suppression of the same kind; a rule that keeps needing suppressions should be changed in the preset.";
13
- const LABELS = {
14
- tsExpectError: "`@ts-expect-error`",
15
- tsIgnore: "`@ts-ignore`",
16
- safety: "`SAFETY:` comments"
17
- };
18
- function labelOf(suppression) {
19
- return suppression.kind === "rule" ? `lint rule \`${suppression.rule}\`` : LABELS[suppression.kind];
20
- }
21
- function limitOf(budget, suppression) {
22
- return suppression.kind === "rule" ? budget.rules[suppression.rule] ?? 0 : budget[suppression.kind];
23
- }
24
- function count(value, source) {
25
- if (value === void 0) throw new Error(`${source} is missing`);
26
- if (!Number.isInteger(value) || value < 0) throw new Error(`${source} must be a non-negative integer`);
27
- return value;
28
- }
29
- /** Parses a budget, rejecting a missing field and anything but non-negative integer ceilings. */
30
- function parseBudget(text, source) {
31
- const parsed = JSON.parse(text);
32
- if (parsed === null || Array.isArray(parsed)) throw new Error(`${source}: expected an object with "rules"`);
33
- const rules = parsed.rules ?? {};
34
- if (rules === null || Array.isArray(rules)) throw new Error(`${source}: rules must be an object of counts`);
35
- return {
36
- rules: Object.fromEntries(Object.entries(rules).map(([rule, limit]) => [rule, count(limit, `${source}: rules "${rule}"`)])),
37
- tsExpectError: count(parsed.tsExpectError, `${source}: tsExpectError`),
38
- tsIgnore: count(parsed.tsIgnore, `${source}: tsIgnore`),
39
- safety: count(parsed.safety, `${source}: safety`)
40
- };
41
- }
42
- /** Serializes a budget with sorted rules, zero ceilings kept, so rewriting an unchanged budget is a no-op. */
43
- function formatBudget(budget) {
44
- const rules = Object.fromEntries(Object.entries(budget.rules).sort(([a], [b]) => a.localeCompare(b)));
45
- return `${JSON.stringify({
46
- ...budget,
47
- rules
48
- }, null, 2)}\n`;
49
- }
50
- /** The budget sync writes at adoption: the committed one when the file was only deleted, so recreating it never raises a ceiling, else the current counts. */
51
- function adoptedBudget(root) {
52
- const committed = spawnSync("git", ["show", `HEAD:${SUPPRESSIONS_FILE}`], {
53
- cwd: root,
54
- encoding: "utf8"
55
- });
56
- if (committed.status === 0) return committed.stdout;
57
- return formatBudget(countSuppressions(worktreeSuppressions(root)));
58
- }
59
- function groupByKey(suppressions) {
60
- const groups = /* @__PURE__ */ new Map();
61
- for (const suppression of suppressions) {
62
- const key = labelOf(suppression);
63
- groups.set(key, [...groups.get(key) ?? [], suppression]);
64
- }
65
- return groups;
66
- }
67
- function lowered(budget, current) {
68
- return {
69
- rules: Object.fromEntries(Object.entries(budget.rules).map(([rule, limit]) => [rule, Math.min(limit, current.rules[rule] ?? 0)])),
70
- tsExpectError: Math.min(budget.tsExpectError, current.tsExpectError),
71
- tsIgnore: Math.min(budget.tsIgnore, current.tsIgnore),
72
- safety: Math.min(budget.safety, current.safety)
73
- };
74
- }
75
- /**
76
- * Fails each budget key whose count in the working tree is above its ceiling,
77
- * listing the suppressions in the files this change touches (all of them when
78
- * it touches none). `update` lowers every ceiling to its current count; no
79
- * command ever raises one.
80
- */
81
- function checkSuppressions(root, update = false) {
82
- const path = join(root, SUPPRESSIONS_FILE);
83
- if (!existsSync(path)) throw new Error(`${SUPPRESSIONS_FILE} is missing. Run antelopejs-sync.`);
84
- const budget = parseBudget(readFileSync(path, "utf8"), SUPPRESSIONS_FILE);
85
- const suppressions = worktreeSuppressions(root);
86
- const changed = changedFiles(root);
87
- const failures = [];
88
- for (const [key, found] of groupByKey(suppressions)) {
89
- const limit = limitOf(budget, found[0]);
90
- if (found.length <= limit) continue;
91
- const inChanged = found.filter(({ file }) => changed.has(file));
92
- const shown = inChanged.length ? inChanged : found;
93
- failures.push(`${SUPPRESSIONS_FILE}: ${key}: ${found.length} exceeds ceiling ${limit}.`, ...shown.map(({ file, line }) => ` ${file}:${line}`), ...shown.length < found.length ? [` ...and ${found.length - shown.length} in files this change does not touch.`] : []);
94
- }
95
- if (update) writeFileSync(path, formatBudget(lowered(budget, countSuppressions(suppressions))));
96
- return failures.length ? [...failures, SUPPRESSION_FIX] : [];
97
- }
98
- function budgetAt(root, revision) {
99
- const result = spawnSync("git", ["show", `${revision}:${SUPPRESSIONS_FILE}`], {
100
- cwd: root,
101
- encoding: "utf8"
102
- });
103
- return result.status === 0 ? parseBudget(result.stdout, `${revision || "index"}:${SUPPRESSIONS_FILE}`) : void 0;
104
- }
105
- function stagedNames(root, filter) {
106
- return execFileSync("git", [
107
- "diff",
108
- "--cached",
109
- "--name-only",
110
- "--find-renames",
111
- `--diff-filter=${filter}`,
112
- "-z"
113
- ], {
114
- cwd: root,
115
- encoding: "utf8"
116
- }).split("\0").filter(Boolean);
117
- }
118
- /**
119
- * Compares the staged files' suppressions with those files at HEAD, rename
120
- * sources and deleted files included, and fails each key that rises by more
121
- * than the staged budget raises its ceiling. A commit that first adds the
122
- * budget is not compared.
123
- */
124
- function stagedSuppressions(root) {
125
- const head = budgetAt(root, "HEAD");
126
- if (head === void 0) return [];
127
- const index = budgetAt(root, "") ?? head;
128
- const renames = renamedPaths(root, true);
129
- const staged = stagedNames(root, "ACMR");
130
- const previous = [...staged.map((file) => renames.get(file) ?? file), ...stagedNames(root, "D")];
131
- const before = groupByKey(revisionSuppressions(root, "HEAD", previous));
132
- const after = groupByKey(revisionSuppressions(root, "", staged));
133
- const additions = stagedAdditions(root, [...new Set([...after.values()].flat().map(({ file }) => file))]);
134
- const failures = [];
135
- for (const [key, found] of after) {
136
- const rise = found.length - (before.get(key)?.length ?? 0);
137
- if (rise <= Math.max(0, limitOf(index, found[0]) - limitOf(head, found[0]))) continue;
138
- const added = found.filter(({ file, line }) => additions.get(file)?.some(({ start, end }) => line >= start && line <= end));
139
- failures.push(`${key}: this commit adds ${rise} to the staged files.`, ...(added.length ? added : found).map(({ file, line }) => ` ${file}:${line}`));
140
- }
141
- return failures.length ? [...failures, SUPPRESSION_FIX] : [];
142
- }
143
- //#endregion
144
- export { stagedSuppressions as a, parseBudget as i, adoptedBudget as n, checkSuppressions as r, SUPPRESSIONS_FILE as t };