@antelopejs/tooling-configs 0.2.16 → 0.2.17
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 +88 -48
- package/dist/bin/api-diff.mjs +409 -97
- package/dist/bin/body-check.d.mts +1 -0
- package/dist/bin/body-check.mjs +344 -0
- package/dist/bin/check-size.mjs +3 -3
- package/dist/bin/checks.mjs +3 -3
- package/dist/bin/comment-proof.mjs +4 -3
- package/dist/bin/garden.mjs +5 -5
- package/dist/bin/ratchet.mjs +267 -7
- package/dist/bin/suppressions.mjs +118 -80
- package/dist/bin/sync.mjs +490 -35
- package/dist/bin/verify.mjs +5 -5
- package/dist/{ceilings-BSw9p4YB.mjs → ceilings-B7KayYIG.mjs} +11 -3
- package/dist/{checks-BlWDFYio.mjs → checks-CxkSu2E6.mjs} +1 -1
- package/dist/{ci-workflow-DIsoEgMd.mjs → ci-workflow-ChW4He0r.mjs} +84 -3
- package/dist/{files-2FikHZ9F.mjs → files-Bc036sJW.mjs} +2 -2
- package/dist/{json-BqvuCkqr.mjs → json-CG2L7YFl.mjs} +12 -8
- package/dist/oxc/anti-slop/index.mjs +160 -13
- package/dist/oxc/fmt.mjs +1 -1
- package/dist/oxc/guardrails/index.d.mts +1 -1
- package/dist/oxc/guardrails/index.mjs +645 -1
- package/dist/oxc/lint.d.mts +11 -3
- package/dist/oxc/lint.mjs +21 -8
- package/dist/oxlint-Bltmk8-6.mjs +94 -0
- package/dist/{package-Dtjt_lSa.mjs → package-DTxLPNDu.mjs} +3 -2
- package/dist/proof-stamp-w-3j47A-.mjs +279 -0
- package/dist/ratchet-DwRj9h4L.mjs +275 -0
- package/dist/{renames-BGO4dWJ8.mjs → renames-L2owF5GD.mjs} +2 -2
- package/dist/shared-CoUt4GUm.mjs +73 -0
- package/dist/{size-BVgNbLO6.mjs → size-DNtWruCO.mjs} +3 -3
- package/dist/suppression-base-DAfCXwjL.mjs +95 -0
- package/dist/{suppression-budget-Yyf5_J5l.mjs → suppression-budget-Bd2GqMYb.mjs} +248 -33
- package/dist/{suppressions-BJAngUxo.mjs → suppressions-BD_f8f7L.mjs} +180 -18
- package/dist/typescript-BmS5BmrS.mjs +222 -0
- package/dist/verify/index.mjs +1 -1
- package/package.json +4 -2
- package/dist/kinds-Bzyyi9yk.mjs +0 -25
- package/dist/ratchet-DRdIYwmv.mjs +0 -80
- package/dist/shared-CsXIMK3I.mjs +0 -34
- package/dist/staged-ratchet-CjoRfZaM.mjs +0 -236
- package/dist/typescript-C54ONh9m.mjs +0 -433
|
@@ -1,10 +1,140 @@
|
|
|
1
|
-
import { n as renamedPaths } from "./renames-
|
|
2
|
-
import { n as changedFiles } from "./files-
|
|
3
|
-
import { t as
|
|
4
|
-
import { a as revisionSuppressions, i as openDisablesIn, n as countSuppressions, o as worktreeSuppressions, r as inWorktree, t as atRevision } from "./suppressions-BJAngUxo.mjs";
|
|
1
|
+
import { n as renamedPaths } from "./renames-L2owF5GD.mjs";
|
|
2
|
+
import { n as changedFiles } from "./files-Bc036sJW.mjs";
|
|
3
|
+
import { a as openDisablesIn, i as inWorktree, l as worktreeSuppressions, n as configKey, o as revisionSuppressions, r as countSuppressions, s as sortedKeys, t as atRevision } from "./suppressions-BD_f8f7L.mjs";
|
|
5
4
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
6
|
-
import { join } from "node:path";
|
|
5
|
+
import { join, matchesGlob } from "node:path";
|
|
7
6
|
import { execFileSync, spawnSync } from "node:child_process";
|
|
7
|
+
//#region src/guardrails/config-scopes.ts
|
|
8
|
+
/** Scopes that name every file: a block without `files`, and the globstar forms. */
|
|
9
|
+
const EVERY_FILE = /* @__PURE__ */ new Set([
|
|
10
|
+
"*",
|
|
11
|
+
"**",
|
|
12
|
+
"**/*"
|
|
13
|
+
]);
|
|
14
|
+
/** Glob syntax beyond `*` and `**`, compared only for equality. */
|
|
15
|
+
const OTHER_SYNTAX = /[?[\]{}()!+@]/u;
|
|
16
|
+
/** A glob with syntax beyond `*` and `**`, or a `.` or `..` segment, which ESLint resolves and oxlint does not. */
|
|
17
|
+
function unsupported(glob) {
|
|
18
|
+
return OTHER_SYNTAX.test(glob) || segmentsOf(glob).some((segment) => segment === "." || segment === "..");
|
|
19
|
+
}
|
|
20
|
+
/** The rule and glob of a config budget key, `<rule> for <glob>`. */
|
|
21
|
+
function keyParts(key) {
|
|
22
|
+
const at = key.indexOf(" for ");
|
|
23
|
+
return at < 0 ? void 0 : {
|
|
24
|
+
rule: key.slice(0, at),
|
|
25
|
+
glob: key.slice(at + 5)
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function segmentsOf(glob) {
|
|
29
|
+
return glob.replace(/^\.\//u, "").replace(/\/+$/u, "").split("/").filter(Boolean);
|
|
30
|
+
}
|
|
31
|
+
/** Whether one path segment pattern names every name `narrow` names: `narrow` read literally must match `wide`, and a wildcard-free `wide` must equal it. */
|
|
32
|
+
function segmentContains(wide, narrow) {
|
|
33
|
+
if (wide === narrow || wide === "*") return true;
|
|
34
|
+
if (!wide.includes("*")) return false;
|
|
35
|
+
return matchesGlob(narrow, wide);
|
|
36
|
+
}
|
|
37
|
+
function segmentsContain(wide, narrow, from = 0, at = 0) {
|
|
38
|
+
if (from === wide.length) return at === narrow.length;
|
|
39
|
+
if (wide[from] === "**") {
|
|
40
|
+
for (let next = at; next <= narrow.length; next++) if (segmentsContain(wide, narrow, from + 1, next)) return true;
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
if (at === narrow.length || narrow[at] === "**") return false;
|
|
44
|
+
return segmentContains(wide[from], narrow[at]) && segmentsContain(wide, narrow, from + 1, at + 1);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Whether glob `wide` names every file `narrow` names, decided
|
|
48
|
+
* conservatively: `*`, `**` and `**\/*` name every file; otherwise segment by
|
|
49
|
+
* segment, where only `**` in `wide` covers `**` in `narrow`, a single-segment
|
|
50
|
+
* `*` never does, and other glob syntax or a `.` or `..` segment must match
|
|
51
|
+
* exactly.
|
|
52
|
+
*/
|
|
53
|
+
function globContains(wide, narrow) {
|
|
54
|
+
if (EVERY_FILE.has(wide) || wide === narrow) return true;
|
|
55
|
+
if (EVERY_FILE.has(narrow)) return false;
|
|
56
|
+
if ([wide, narrow].some(unsupported)) return false;
|
|
57
|
+
return segmentsContain(segmentsOf(wide), segmentsOf(narrow));
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Config ceilings with the room a scope gives up lent to the narrower scopes
|
|
61
|
+
* of the same rule: a key over its limit may also use the largest amount any
|
|
62
|
+
* same-rule key whose glob contains its glob sits below its own limit. A
|
|
63
|
+
* narrowed or split scope therefore passes, and a widened or moved one does
|
|
64
|
+
* not.
|
|
65
|
+
*/
|
|
66
|
+
function coveredLimits(limits, counts) {
|
|
67
|
+
const covered = { ...limits };
|
|
68
|
+
for (const [key, count] of Object.entries(counts)) {
|
|
69
|
+
const limit = Object.hasOwn(limits, key) ? limits[key] : 0;
|
|
70
|
+
const narrow = keyParts(key);
|
|
71
|
+
if (count <= limit || narrow === void 0) continue;
|
|
72
|
+
let room = 0;
|
|
73
|
+
for (const [wideKey, wideLimit] of Object.entries(limits)) {
|
|
74
|
+
const wide = keyParts(wideKey);
|
|
75
|
+
if (wideKey !== key && wide?.rule === narrow.rule && globContains(wide.glob, narrow.glob)) room = Math.max(room, wideLimit - (Object.hasOwn(counts, wideKey) ? counts[wideKey] : 0));
|
|
76
|
+
}
|
|
77
|
+
covered[key] = limit + room;
|
|
78
|
+
}
|
|
79
|
+
return covered;
|
|
80
|
+
}
|
|
81
|
+
//#endregion
|
|
82
|
+
//#region src/guardrails/staged-additions.ts
|
|
83
|
+
/**
|
|
84
|
+
* Each staged file's path and hunks against `revision`, HEAD when omitted,
|
|
85
|
+
* following renames.
|
|
86
|
+
*/
|
|
87
|
+
function stagedChanges(root, targets, revision) {
|
|
88
|
+
const revisions = revision === void 0 ? [] : [revision];
|
|
89
|
+
const renames = renamedPaths(root, true, revisions);
|
|
90
|
+
const changes = /* @__PURE__ */ new Map();
|
|
91
|
+
for (const file of targets) {
|
|
92
|
+
const previous = renames.get(file) ?? file;
|
|
93
|
+
const diff = execFileSync("git", [
|
|
94
|
+
"diff",
|
|
95
|
+
"--cached",
|
|
96
|
+
...revisions,
|
|
97
|
+
"--no-ext-diff",
|
|
98
|
+
"--no-color",
|
|
99
|
+
"--unified=0",
|
|
100
|
+
"--find-renames",
|
|
101
|
+
"--",
|
|
102
|
+
.../* @__PURE__ */ new Set([previous, file])
|
|
103
|
+
], {
|
|
104
|
+
cwd: root,
|
|
105
|
+
encoding: "utf8"
|
|
106
|
+
});
|
|
107
|
+
changes.set(file, {
|
|
108
|
+
previous,
|
|
109
|
+
hunks: Array.from(diff.matchAll(/^@@ -\d+(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/gm), ([, removed, start, count]) => ({
|
|
110
|
+
removed: Number(removed ?? 1),
|
|
111
|
+
start: Number(start),
|
|
112
|
+
count: Number(count ?? 1)
|
|
113
|
+
}))
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return changes;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* The line ranges each staged file adds against `revision`, HEAD when
|
|
120
|
+
* omitted, following renames.
|
|
121
|
+
*/
|
|
122
|
+
function stagedAdditions(root, targets, revision) {
|
|
123
|
+
return new Map(Array.from(stagedChanges(root, targets, revision), ([file, { hunks }]) => [file, hunks.map(({ start, count }) => ({
|
|
124
|
+
start,
|
|
125
|
+
end: start + count - 1
|
|
126
|
+
}))]));
|
|
127
|
+
}
|
|
128
|
+
/** The line of the compared revision a staged line is unchanged from, or `undefined` when the staged change adds it. */
|
|
129
|
+
function previousLine(hunks, line) {
|
|
130
|
+
let shift = 0;
|
|
131
|
+
for (const { removed, start, count } of hunks) {
|
|
132
|
+
if (count > 0 && line >= start && line < start + count) return void 0;
|
|
133
|
+
if (count > 0 ? start + count <= line : start < line) shift += removed - count;
|
|
134
|
+
}
|
|
135
|
+
return line + shift;
|
|
136
|
+
}
|
|
137
|
+
//#endregion
|
|
8
138
|
//#region src/guardrails/suppression-policy.ts
|
|
9
139
|
/** Rules a directive may never name, each with what to do instead. */
|
|
10
140
|
const NEVER_SUPPRESSED = /* @__PURE__ */ new Map([
|
|
@@ -36,6 +166,11 @@ function genericReason(suppression) {
|
|
|
36
166
|
return said.every((word) => VAGUE_WORDS.has(word) || named.has(word));
|
|
37
167
|
}
|
|
38
168
|
function identity(suppression) {
|
|
169
|
+
if (suppression.kind === "config") return [
|
|
170
|
+
suppression.kind,
|
|
171
|
+
suppression.rule,
|
|
172
|
+
suppression.scope
|
|
173
|
+
].join("\0");
|
|
39
174
|
return [
|
|
40
175
|
suppression.kind,
|
|
41
176
|
suppression.kind === "rule" ? suppression.rule : "",
|
|
@@ -77,6 +212,7 @@ function addedSuppressions(before, after) {
|
|
|
77
212
|
}
|
|
78
213
|
function label(suppression) {
|
|
79
214
|
if (suppression.kind === "rule") return `\`${suppression.directive} ${suppression.rule}\``;
|
|
215
|
+
if (suppression.kind === "config") return `lint config rule-off \`${suppression.rule}\` for \`${suppression.scope}\``;
|
|
80
216
|
if (suppression.kind === "safety") return "`SAFETY:`";
|
|
81
217
|
return suppression.kind === "tsIgnore" ? "`@ts-ignore`" : "`@ts-expect-error`";
|
|
82
218
|
}
|
|
@@ -88,9 +224,9 @@ function label(suppression) {
|
|
|
88
224
|
function policyFailures(added) {
|
|
89
225
|
return added.flatMap((suppression) => {
|
|
90
226
|
const at = `${suppression.file}:${suppression.line}`;
|
|
91
|
-
const instead = suppression.kind === "rule" ? NEVER_SUPPRESSED.get(suppression.rule) : void 0;
|
|
227
|
+
const instead = suppression.kind === "rule" || suppression.kind === "config" ? NEVER_SUPPRESSED.get(suppression.rule) : void 0;
|
|
92
228
|
if (instead !== void 0) return [`${at}: ${label(suppression)}: this rule is never suppressed. Fix: ${instead}.`];
|
|
93
|
-
if (suppression.kind === "rule" && suppression.rule === "*") return [];
|
|
229
|
+
if (suppression.kind === "config" || suppression.kind === "rule" && suppression.rule === "*") return [];
|
|
94
230
|
if (!genericReason(suppression)) return [];
|
|
95
231
|
const quoted = suppression.reason ? `"${suppression.reason}"` : "none";
|
|
96
232
|
return [`${at}: ${label(suppression)} reason ${quoted} does not say why this code needs it. Fix: state the fact about this line that makes the suppression hold (at least ${MIN_REASON_WORDS} words${suppression.kind === "rule" ? ", after ` -- `" : ""}), or fix the code.`];
|
|
@@ -132,13 +268,20 @@ function checkStagedPolicy(root, staged, previous) {
|
|
|
132
268
|
const SUPPRESSIONS_FILE = "guardrails.suppressions.json";
|
|
133
269
|
/** What a failure tells the author to do. */
|
|
134
270
|
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.";
|
|
271
|
+
/** What a failure caused by a lint config rule-off tells the author to do. */
|
|
272
|
+
const CONFIG_FIX = "Fix: delete the config entry and fix the code its rule reports, or move the exception onto the line that needs it as a directive with a reason. A rule wrong for every repository should be changed in the preset.";
|
|
273
|
+
function fixesFor(failed) {
|
|
274
|
+
return [...failed.some(({ kind }) => kind !== "config") ? [SUPPRESSION_FIX] : [], ...failed.some(({ kind }) => kind === "config") ? [CONFIG_FIX] : []];
|
|
275
|
+
}
|
|
135
276
|
const LABELS = {
|
|
136
277
|
tsExpectError: "`@ts-expect-error`",
|
|
137
278
|
tsIgnore: "`@ts-ignore`",
|
|
138
279
|
safety: "`SAFETY:` comments"
|
|
139
280
|
};
|
|
140
281
|
function labelOf(suppression) {
|
|
141
|
-
|
|
282
|
+
if (suppression.kind === "rule") return `lint rule \`${suppression.rule}\``;
|
|
283
|
+
if (suppression.kind === "config") return `lint config rule-off \`${suppression.rule}\` for \`${suppression.scope}\``;
|
|
284
|
+
return LABELS[suppression.kind];
|
|
142
285
|
}
|
|
143
286
|
/** The rule a SAFETY comment satisfies, so a directive disabling it may become a SAFETY comment without a raise. */
|
|
144
287
|
const SAFETY_RULE = "anti-slop/require-safety-comment-for-type-assertion";
|
|
@@ -147,7 +290,17 @@ function safetyCeiling(budget, disables) {
|
|
|
147
290
|
return budget.safety + Math.max(0, (budget.rules["anti-slop/require-safety-comment-for-type-assertion"] ?? 0) - disables);
|
|
148
291
|
}
|
|
149
292
|
function limitOf(budget, suppression) {
|
|
150
|
-
|
|
293
|
+
if (suppression.kind === "rule") return budget.rules[suppression.rule] ?? 0;
|
|
294
|
+
if (suppression.kind === "config") return budget.config?.[configKey(suppression.rule, suppression.scope)] ?? 0;
|
|
295
|
+
return budget[suppression.kind];
|
|
296
|
+
}
|
|
297
|
+
/** Whether `budget` caps this kind: config rule-offs go uncapped until sync adopts their ceilings. */
|
|
298
|
+
function capped(budget, suppression) {
|
|
299
|
+
return suppression.kind !== "config" || budget.config !== void 0;
|
|
300
|
+
}
|
|
301
|
+
function counted(map, source) {
|
|
302
|
+
if (map === null || Array.isArray(map)) throw new Error(`${source} must be an object of counts`);
|
|
303
|
+
return Object.fromEntries(Object.entries(map).map(([rule, limit]) => [rule, count(limit, `${source} "${rule}"`)]));
|
|
151
304
|
}
|
|
152
305
|
function count(value, source) {
|
|
153
306
|
if (value === void 0) throw new Error(`${source} is missing`);
|
|
@@ -158,32 +311,58 @@ function count(value, source) {
|
|
|
158
311
|
function parseBudget(text, source) {
|
|
159
312
|
const parsed = JSON.parse(text);
|
|
160
313
|
if (parsed === null || Array.isArray(parsed)) throw new Error(`${source}: expected an object with "rules"`);
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
return {
|
|
164
|
-
rules: Object.fromEntries(Object.entries(rules).map(([rule, limit]) => [rule, count(limit, `${source}: rules "${rule}"`)])),
|
|
314
|
+
const budget = {
|
|
315
|
+
rules: counted(parsed.rules ?? {}, `${source}: rules`),
|
|
165
316
|
tsExpectError: count(parsed.tsExpectError, `${source}: tsExpectError`),
|
|
166
317
|
tsIgnore: count(parsed.tsIgnore, `${source}: tsIgnore`),
|
|
167
318
|
safety: count(parsed.safety, `${source}: safety`)
|
|
168
319
|
};
|
|
320
|
+
if (parsed.config !== void 0) budget.config = counted(parsed.config, `${source}: config`);
|
|
321
|
+
return budget;
|
|
169
322
|
}
|
|
170
|
-
/** Serializes a budget with sorted rules, zero ceilings kept, so rewriting an unchanged budget is a no-op. */
|
|
323
|
+
/** Serializes a budget with sorted rules and config rule-offs, zero ceilings kept, so rewriting an unchanged budget is a no-op. */
|
|
171
324
|
function formatBudget(budget) {
|
|
172
|
-
const
|
|
173
|
-
|
|
325
|
+
const sorted = {
|
|
326
|
+
...budget,
|
|
327
|
+
rules: sortedKeys(budget.rules)
|
|
328
|
+
};
|
|
329
|
+
if (budget.config !== void 0) sorted.config = sortedKeys(budget.config);
|
|
330
|
+
return `${JSON.stringify(sorted, null, 2)}\n`;
|
|
331
|
+
}
|
|
332
|
+
/** A budget with config rule-off ceilings: `text` itself when it has them or does not parse, else `text` plus today's config rule-offs. */
|
|
333
|
+
function withConfigCeilings(root, text) {
|
|
334
|
+
let budget;
|
|
335
|
+
try {
|
|
336
|
+
budget = parseBudget(text, SUPPRESSIONS_FILE);
|
|
337
|
+
} catch {
|
|
338
|
+
return text;
|
|
339
|
+
}
|
|
340
|
+
if (budget.config !== void 0) return text;
|
|
341
|
+
const { config } = countSuppressions(worktreeSuppressions(root));
|
|
342
|
+
return formatBudget({
|
|
174
343
|
...budget,
|
|
175
|
-
|
|
176
|
-
}
|
|
344
|
+
config
|
|
345
|
+
});
|
|
177
346
|
}
|
|
178
|
-
/**
|
|
179
|
-
|
|
347
|
+
/**
|
|
348
|
+
* The budget sync writes: `current` with today's config rule-offs adopted
|
|
349
|
+
* when it predates them; when the file is missing, the committed one when it
|
|
350
|
+
* was only deleted, so recreating it never raises a ceiling, else the
|
|
351
|
+
* current counts.
|
|
352
|
+
*/
|
|
353
|
+
function adoptedBudget(root, current) {
|
|
354
|
+
if (current !== void 0) return withConfigCeilings(root, current);
|
|
180
355
|
const committed = spawnSync("git", ["show", `HEAD:${SUPPRESSIONS_FILE}`], {
|
|
181
356
|
cwd: root,
|
|
182
357
|
encoding: "utf8"
|
|
183
358
|
});
|
|
184
|
-
if (committed.status === 0) return committed.stdout;
|
|
359
|
+
if (committed.status === 0) return withConfigCeilings(root, committed.stdout);
|
|
185
360
|
return formatBudget(countSuppressions(worktreeSuppressions(root)));
|
|
186
361
|
}
|
|
362
|
+
/** Each `file:line` once, indented, in the order found. */
|
|
363
|
+
function locations(found) {
|
|
364
|
+
return [...new Set(found.map(({ file, line }) => ` ${file}:${line}`))];
|
|
365
|
+
}
|
|
187
366
|
function groupByKey(suppressions) {
|
|
188
367
|
const groups = /* @__PURE__ */ new Map();
|
|
189
368
|
for (const suppression of suppressions) {
|
|
@@ -192,13 +371,19 @@ function groupByKey(suppressions) {
|
|
|
192
371
|
}
|
|
193
372
|
return groups;
|
|
194
373
|
}
|
|
374
|
+
/** Each ceiling of `limits` lowered to its count in `current`, rules at zero kept. */
|
|
375
|
+
function lowerEach(limits, current = {}) {
|
|
376
|
+
return Object.fromEntries(Object.entries(limits).map(([rule, limit]) => [rule, Math.min(limit, Object.hasOwn(current, rule) ? current[rule] : 0)]));
|
|
377
|
+
}
|
|
195
378
|
function lowered(budget, current) {
|
|
196
|
-
|
|
197
|
-
rules:
|
|
379
|
+
const result = {
|
|
380
|
+
rules: lowerEach(budget.rules, current.rules),
|
|
198
381
|
tsExpectError: Math.min(budget.tsExpectError, current.tsExpectError),
|
|
199
382
|
tsIgnore: Math.min(budget.tsIgnore, current.tsIgnore),
|
|
200
383
|
safety: Math.min(safetyCeiling(budget, current.rules["anti-slop/require-safety-comment-for-type-assertion"] ?? 0), current.safety)
|
|
201
384
|
};
|
|
385
|
+
if (budget.config !== void 0) result.config = lowerEach(budget.config, current.config);
|
|
386
|
+
return result;
|
|
202
387
|
}
|
|
203
388
|
/**
|
|
204
389
|
* Fails each budget key whose count in the working tree is above its ceiling,
|
|
@@ -206,25 +391,39 @@ function lowered(budget, current) {
|
|
|
206
391
|
* it touches none). SAFETY comments may also use the room the
|
|
207
392
|
* directives disabling the SAFETY rule leave below their ceiling. `update`
|
|
208
393
|
* lowers every ceiling to its current count, moving that room to `safety`;
|
|
209
|
-
* no command ever raises the two together.
|
|
394
|
+
* no command ever raises the two together. A config rule-off may use the
|
|
395
|
+
* room a containing scope of its rule gives up, and `update` moves that room
|
|
396
|
+
* to the narrower key.
|
|
210
397
|
*/
|
|
211
398
|
function checkSuppressions(root, update = false) {
|
|
212
399
|
const path = join(root, SUPPRESSIONS_FILE);
|
|
213
400
|
if (!existsSync(path)) throw new Error(`${SUPPRESSIONS_FILE} is missing. Run antelopejs-sync.`);
|
|
214
|
-
const
|
|
401
|
+
const committed = parseBudget(readFileSync(path, "utf8"), SUPPRESSIONS_FILE);
|
|
215
402
|
const suppressions = worktreeSuppressions(root);
|
|
216
403
|
const counts = countSuppressions(suppressions);
|
|
404
|
+
const budget = withCoveredScopes(committed, counts);
|
|
217
405
|
const changed = changedFiles(root);
|
|
218
406
|
const failures = [];
|
|
407
|
+
const failed = [];
|
|
219
408
|
for (const [key, found] of groupByKey(suppressions)) {
|
|
409
|
+
if (!capped(budget, found[0])) continue;
|
|
220
410
|
const limit = found[0].kind === "safety" ? safetyCeiling(budget, counts.rules["anti-slop/require-safety-comment-for-type-assertion"] ?? 0) : limitOf(budget, found[0]);
|
|
221
411
|
if (found.length <= limit) continue;
|
|
412
|
+
failed.push(found[0]);
|
|
222
413
|
const inChanged = found.filter(({ file }) => changed.has(file));
|
|
223
414
|
const shown = inChanged.length ? inChanged : found;
|
|
224
|
-
failures.push(`${SUPPRESSIONS_FILE}: ${key}: ${found.length} exceeds ceiling ${limit}.`, ...shown
|
|
415
|
+
failures.push(`${SUPPRESSIONS_FILE}: ${key}: ${found.length} exceeds ceiling ${limit}.`, ...locations(shown), ...shown.length < found.length ? [` ...and ${found.length - shown.length} in files this change does not touch.`] : []);
|
|
225
416
|
}
|
|
226
417
|
if (update) writeFileSync(path, formatBudget(lowered(budget, counts)));
|
|
227
|
-
return
|
|
418
|
+
return [...failures, ...fixesFor(failed)];
|
|
419
|
+
}
|
|
420
|
+
/** `budget` with each config ceiling widened by the room containing scopes of its rule give up at `counts`. */
|
|
421
|
+
function withCoveredScopes(budget, counts) {
|
|
422
|
+
if (budget.config === void 0) return budget;
|
|
423
|
+
return {
|
|
424
|
+
...budget,
|
|
425
|
+
config: coveredLimits(budget.config, counts.config ?? {})
|
|
426
|
+
};
|
|
228
427
|
}
|
|
229
428
|
function budgetAt(root, revision) {
|
|
230
429
|
const result = spawnSync("git", ["show", `${revision}:${SUPPRESSIONS_FILE}`], {
|
|
@@ -259,25 +458,41 @@ function stagedSuppressions(root) {
|
|
|
259
458
|
const previous = [...staged.map((file) => renames.get(file) ?? file), ...stagedNames(root, "D")];
|
|
260
459
|
return [...stagedBudget(root, staged, previous), ...checkStagedPolicy(root, staged, previous)];
|
|
261
460
|
}
|
|
461
|
+
/**
|
|
462
|
+
* How much a staged config key may rise: what the staged budget raises its
|
|
463
|
+
* ceiling, plus what a containing scope of its rule falls in the staged files.
|
|
464
|
+
*/
|
|
465
|
+
function stagedScopes(head, index, before, after) {
|
|
466
|
+
const limits = { ...before };
|
|
467
|
+
for (const key of /* @__PURE__ */ new Set([...Object.keys(index.config ?? {}), ...Object.keys(head.config ?? {})])) limits[key] = (limits[key] ?? 0) + Math.max(0, (index.config?.[key] ?? 0) - (head.config?.[key] ?? 0));
|
|
468
|
+
const covered = coveredLimits(limits, after);
|
|
469
|
+
return (rule, scope, previous) => (covered[configKey(rule, scope)] ?? 0) - previous;
|
|
470
|
+
}
|
|
262
471
|
function stagedBudget(root, staged, previous) {
|
|
263
472
|
const head = budgetAt(root, "HEAD");
|
|
264
473
|
if (head === void 0) return [];
|
|
265
474
|
const index = budgetAt(root, "") ?? head;
|
|
266
|
-
const
|
|
267
|
-
const
|
|
475
|
+
const headSuppressions = revisionSuppressions(root, "HEAD", previous);
|
|
476
|
+
const stagedSuppressions = revisionSuppressions(root, "", staged);
|
|
477
|
+
const before = groupByKey(headSuppressions);
|
|
478
|
+
const after = groupByKey(stagedSuppressions);
|
|
479
|
+
const scopes = stagedScopes(head, index, countSuppressions(headSuppressions).config ?? {}, countSuppressions(stagedSuppressions).config ?? {});
|
|
268
480
|
const additions = stagedAdditions(root, [...new Set([...after.values()].flat().map(({ file }) => file))]);
|
|
269
481
|
const failures = [];
|
|
482
|
+
const failed = [];
|
|
270
483
|
const safetyRule = `lint rule \`${SAFETY_RULE}\``;
|
|
271
484
|
const converted = Math.max(0, (before.get(safetyRule)?.length ?? 0) - (after.get(safetyRule)?.length ?? 0));
|
|
272
485
|
const freed = (head.rules["anti-slop/require-safety-comment-for-type-assertion"] ?? 0) - (index.rules["anti-slop/require-safety-comment-for-type-assertion"] ?? 0);
|
|
273
486
|
for (const [key, found] of after) {
|
|
487
|
+
if (!capped(index, found[0])) continue;
|
|
274
488
|
const rise = found.length - (before.get(key)?.length ?? 0);
|
|
275
489
|
const raised = limitOf(index, found[0]) - limitOf(head, found[0]);
|
|
276
|
-
if (rise <= (found[0].kind === "safety" ? converted + Math.max(0, raised - Math.max(0, freed)) : Math.max(0, raised))) continue;
|
|
490
|
+
if (rise <= (found[0].kind === "safety" ? converted + Math.max(0, raised - Math.max(0, freed)) : found[0].kind === "config" ? scopes(found[0].rule, found[0].scope, found.length - rise) : Math.max(0, raised))) continue;
|
|
491
|
+
failed.push(found[0]);
|
|
277
492
|
const added = found.filter(({ file, line }) => additions.get(file)?.some(({ start, end }) => line >= start && line <= end));
|
|
278
|
-
failures.push(`${key}: this commit adds ${rise} to the staged files.`, ...(added.length ? added : found)
|
|
493
|
+
failures.push(`${key}: this commit adds ${rise} to the staged files.`, ...locations(added.length ? added : found));
|
|
279
494
|
}
|
|
280
|
-
return
|
|
495
|
+
return [...failures, ...fixesFor(failed)];
|
|
281
496
|
}
|
|
282
497
|
//#endregion
|
|
283
|
-
export { parseBudget as a,
|
|
498
|
+
export { parseBudget as a, stagedSuppressions as c, stagedAdditions as d, stagedChanges as f, checkSuppressions as i, checkPolicy as l, SUPPRESSIONS_FILE as n, safetyCeiling as o, coveredLimits as p, adoptedBudget as r, stagedNames as s, SAFETY_RULE as t, previousLine as u };
|
|
@@ -1,9 +1,155 @@
|
|
|
1
1
|
import { n as markerPattern, t as DEFAULT_SAFETY_MARKERS } from "./require-safety-comment-for-type-assertion-BBQ669kj.mjs";
|
|
2
|
-
import { i as gitFiles, r as generatedOutput } from "./files-
|
|
2
|
+
import { i as gitFiles, r as generatedOutput } from "./files-Bc036sJW.mjs";
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
4
|
import { existsSync, lstatSync, readFileSync } from "node:fs";
|
|
5
5
|
import { join } from "node:path";
|
|
6
6
|
import { execFileSync } from "node:child_process";
|
|
7
|
+
//#region src/guardrails/parser.ts
|
|
8
|
+
let compiler;
|
|
9
|
+
/** This package's own TypeScript 5, loaded on first use, so the count never depends on the compiler a repository installs (TypeScript 7 has no parser API). */
|
|
10
|
+
function typescript() {
|
|
11
|
+
compiler ??= createRequire(import.meta.url)("typescript");
|
|
12
|
+
return compiler;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/guardrails/config-suppressions.ts
|
|
16
|
+
/** The lint configs a repository writes, at any depth: `oxlint.config.*`, `.oxlintrc.json`, `.oxlintrc.jsonc` and `eslint.config.*`. */
|
|
17
|
+
const LINT_CONFIG_FILE = /(?:^|\/)(?:(?:oxlint|eslint)\.config\.[cm]?[jt]s|\.oxlintrc\.jsonc?)$/u;
|
|
18
|
+
const WEAK_SEVERITIES = /* @__PURE__ */ new Set([
|
|
19
|
+
"off",
|
|
20
|
+
"allow",
|
|
21
|
+
"warn"
|
|
22
|
+
]);
|
|
23
|
+
const WEAK_LEVELS = /* @__PURE__ */ new Set(["0", "1"]);
|
|
24
|
+
function unwrapped(node) {
|
|
25
|
+
const compiler = typescript();
|
|
26
|
+
let current = node;
|
|
27
|
+
while (compiler.isParenthesizedExpression(current) || compiler.isAsExpression(current) || compiler.isSatisfiesExpression(current) || compiler.isTypeAssertionExpression(current) || compiler.isNonNullExpression(current)) current = current.expression;
|
|
28
|
+
return current;
|
|
29
|
+
}
|
|
30
|
+
/** The expression `node` stands for: itself, or the initializer of the same-file variable it names. */
|
|
31
|
+
function resolved(node, declarations, seen = /* @__PURE__ */ new Set()) {
|
|
32
|
+
const compiler = typescript();
|
|
33
|
+
const value = unwrapped(node);
|
|
34
|
+
if (!compiler.isIdentifier(value) || seen.has(value.text)) return value;
|
|
35
|
+
const initializer = declarations.get(value.text);
|
|
36
|
+
if (initializer === void 0) return value;
|
|
37
|
+
seen.add(value.text);
|
|
38
|
+
return resolved(initializer, declarations, seen);
|
|
39
|
+
}
|
|
40
|
+
function propertyName(name) {
|
|
41
|
+
const compiler = typescript();
|
|
42
|
+
return compiler.isIdentifier(name) || compiler.isStringLiteralLike(name) || compiler.isNumericLiteral(name) ? name.text : void 0;
|
|
43
|
+
}
|
|
44
|
+
function weakens(node, declarations) {
|
|
45
|
+
const compiler = typescript();
|
|
46
|
+
const value = resolved(node, declarations);
|
|
47
|
+
if (compiler.isStringLiteralLike(value)) return WEAK_SEVERITIES.has(value.text);
|
|
48
|
+
if (compiler.isNumericLiteral(value)) return WEAK_LEVELS.has(value.text);
|
|
49
|
+
if (compiler.isArrayLiteralExpression(value)) {
|
|
50
|
+
const [severity] = value.elements;
|
|
51
|
+
return severity !== void 0 && weakens(severity, declarations);
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
/** The entries of a rule map that weaken their rule, spreads of same-file maps included. */
|
|
56
|
+
function weakEntries(node, declarations, source, depth = 0) {
|
|
57
|
+
const compiler = typescript();
|
|
58
|
+
const value = resolved(node, declarations);
|
|
59
|
+
if (!compiler.isObjectLiteralExpression(value) || depth > 8) return [];
|
|
60
|
+
return value.properties.flatMap((property) => {
|
|
61
|
+
if (compiler.isSpreadAssignment(property)) return weakEntries(property.expression, declarations, source, depth + 1);
|
|
62
|
+
if (!compiler.isPropertyAssignment(property)) return [];
|
|
63
|
+
const name = propertyName(property.name);
|
|
64
|
+
if (name === void 0 || !weakens(property.initializer, declarations)) return [];
|
|
65
|
+
const start = property.getStart(source);
|
|
66
|
+
return [{
|
|
67
|
+
name,
|
|
68
|
+
line: source.getLineAndCharacterOfPosition(start).line + 1
|
|
69
|
+
}];
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/** The globs of a `files` value, each spread of a same-file array expanded; anything else counts as one scope. */
|
|
73
|
+
function globs(node, declarations, source) {
|
|
74
|
+
const compiler = typescript();
|
|
75
|
+
const value = resolved(node, declarations);
|
|
76
|
+
if (compiler.isStringLiteralLike(value)) return [value.text];
|
|
77
|
+
if (!compiler.isArrayLiteralExpression(value)) return [value.getText(source)];
|
|
78
|
+
return value.elements.flatMap((element) => globs(compiler.isSpreadElement(element) ? element.expression : element, declarations, source));
|
|
79
|
+
}
|
|
80
|
+
function member(object, key) {
|
|
81
|
+
const compiler = typescript();
|
|
82
|
+
for (const property of object.properties) {
|
|
83
|
+
if (compiler.isPropertyAssignment(property) && propertyName(property.name) === key) return property.initializer;
|
|
84
|
+
if (compiler.isShorthandPropertyAssignment(property) && property.name.text === key) return property.name;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function sourceOf(file, text) {
|
|
88
|
+
const compiler = typescript();
|
|
89
|
+
if (/\.jsonc?$/u.test(file)) return compiler.parseJsonText(file, text);
|
|
90
|
+
return compiler.createSourceFile(file, text, compiler.ScriptTarget.Latest, false, /\.[cm]?ts$/u.test(file) ? compiler.ScriptKind.TS : compiler.ScriptKind.JS);
|
|
91
|
+
}
|
|
92
|
+
function declarationsIn(source) {
|
|
93
|
+
const compiler = typescript();
|
|
94
|
+
const declarations = /* @__PURE__ */ new Map();
|
|
95
|
+
const visit = (node) => {
|
|
96
|
+
if (compiler.isVariableDeclaration(node) && compiler.isIdentifier(node.name) && node.initializer !== void 0) declarations.set(node.name.text, node.initializer);
|
|
97
|
+
compiler.forEachChild(node, visit);
|
|
98
|
+
};
|
|
99
|
+
visit(source);
|
|
100
|
+
return declarations;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* A glob of the config in `directory` as a path from the repository root;
|
|
104
|
+
* the whole directory when `glob` is undefined. Root globs stay as written,
|
|
105
|
+
* and a root block without `files` is `*`.
|
|
106
|
+
*/
|
|
107
|
+
function anchored(directory, glob) {
|
|
108
|
+
if (glob === void 0) return directory === "" ? "*" : `${directory}/**`;
|
|
109
|
+
if (directory === "") return glob;
|
|
110
|
+
const negated = glob.startsWith("!");
|
|
111
|
+
const path = (negated ? glob.slice(1) : glob).replace(/^(?:\.\/|\/)+/u, "");
|
|
112
|
+
return `${negated ? "!" : ""}${directory}/${path}`;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Each rule and category a lint config file switches off, to `allow` or to
|
|
116
|
+
* a warning: in every object's `rules` and `categories`, and in the argument
|
|
117
|
+
* of an ESLint composer's `overrideRules`, once per glob of the object's
|
|
118
|
+
* `files`, each glob anchored at the config's directory so a scope never
|
|
119
|
+
* reaches past it. Rule maps and globs held in same-file variables or spread from
|
|
120
|
+
* them count where they are used; configs the pack's presets return are
|
|
121
|
+
* not in the file, so they never count.
|
|
122
|
+
*/
|
|
123
|
+
function configRuleOffs(file, text) {
|
|
124
|
+
if (!LINT_CONFIG_FILE.test(file)) return [];
|
|
125
|
+
const compiler = typescript();
|
|
126
|
+
const source = sourceOf(file, text);
|
|
127
|
+
const declarations = declarationsIn(source);
|
|
128
|
+
const directory = file.includes("/") ? file.slice(0, file.lastIndexOf("/")) : "";
|
|
129
|
+
const whole = [anchored(directory)];
|
|
130
|
+
const found = [];
|
|
131
|
+
const add = (map, scopes, prefix) => {
|
|
132
|
+
if (map === void 0) return;
|
|
133
|
+
for (const { name, line } of weakEntries(map, declarations, source)) for (const scope of scopes) found.push({
|
|
134
|
+
rule: `${prefix}${name}`,
|
|
135
|
+
scope,
|
|
136
|
+
line
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
const visit = (node) => {
|
|
140
|
+
if (compiler.isObjectLiteralExpression(node)) {
|
|
141
|
+
const files = member(node, "files");
|
|
142
|
+
const scopes = files === void 0 ? whole : globs(files, declarations, source).map((glob) => anchored(directory, glob));
|
|
143
|
+
add(member(node, "rules"), scopes, "");
|
|
144
|
+
add(member(node, "categories"), scopes, "category:");
|
|
145
|
+
}
|
|
146
|
+
if (compiler.isCallExpression(node) && compiler.isPropertyAccessExpression(node.expression) && node.expression.name.text === "overrideRules") add(node.arguments[0], whole, "");
|
|
147
|
+
compiler.forEachChild(node, visit);
|
|
148
|
+
};
|
|
149
|
+
visit(source);
|
|
150
|
+
return found;
|
|
151
|
+
}
|
|
152
|
+
//#endregion
|
|
7
153
|
//#region src/guardrails/suppressions.ts
|
|
8
154
|
const CODE_FILE = /\.(?:[cm]?[jt]sx?|vue)$/u;
|
|
9
155
|
const CANDIDATE = /(?:oxlint|eslint)-disable|@ts-(?:expect-error|ignore)/u;
|
|
@@ -14,12 +160,6 @@ const SAFETY_COMMENT = markerPattern(DEFAULT_SAFETY_MARKERS);
|
|
|
14
160
|
const TS_DIRECTIVE = /^[/*\s]*@ts-(expect-error|ignore)(?![\w-])/u;
|
|
15
161
|
const SAFETY_REASON = /SAFETY\s*:([\s\S]*)$/u;
|
|
16
162
|
const ENABLE_DIRECTIVE = /^((?:oxlint|eslint)-enable)(?=\s|$)([\s\S]*)$/u;
|
|
17
|
-
let compiler;
|
|
18
|
-
/** This package's own TypeScript 5, loaded on first use, so the count never depends on the compiler a repository installs (TypeScript 7 has no parser API). */
|
|
19
|
-
function typescript() {
|
|
20
|
-
compiler ??= createRequire(import.meta.url)("typescript");
|
|
21
|
-
return compiler;
|
|
22
|
-
}
|
|
23
163
|
function scriptKind(lang) {
|
|
24
164
|
const kinds = typescript().ScriptKind;
|
|
25
165
|
if (/^[cm]?tsx$/u.test(lang)) return kinds.TSX;
|
|
@@ -114,10 +254,16 @@ function fileComments(file, text) {
|
|
|
114
254
|
function lineOf(text, offset) {
|
|
115
255
|
return text.slice(0, offset).split("\n").length;
|
|
116
256
|
}
|
|
117
|
-
/** The suppressions in one code file's text; `file` decides the language and labels each entry. */
|
|
257
|
+
/** The suppressions in one code file's or lint config's text; `file` decides the language and labels each entry. */
|
|
118
258
|
function suppressionsIn(file, text) {
|
|
119
|
-
|
|
120
|
-
|
|
259
|
+
const found = configRuleOffs(file, text).map(({ rule, scope, line }) => ({
|
|
260
|
+
kind: "config",
|
|
261
|
+
rule,
|
|
262
|
+
scope,
|
|
263
|
+
file,
|
|
264
|
+
line
|
|
265
|
+
}));
|
|
266
|
+
if (!CODE_FILE.test(file) || !(CANDIDATE.test(text) || SAFETY_COMMENT.test(text))) return found;
|
|
121
267
|
for (const comment of fileComments(file, text)) {
|
|
122
268
|
const line = lineOf(text, comment.start);
|
|
123
269
|
for (const { directive, rule, reason } of lintDirective(comment.value)) found.push({
|
|
@@ -189,9 +335,9 @@ function generatedIn(root) {
|
|
|
189
335
|
}
|
|
190
336
|
function authoredCode(root, files) {
|
|
191
337
|
const generated = generatedIn(root);
|
|
192
|
-
return files.filter((file) => CODE_FILE.test(file) && !generated(file));
|
|
338
|
+
return files.filter((file) => (CODE_FILE.test(file) || LINT_CONFIG_FILE.test(file)) && !generated(file));
|
|
193
339
|
}
|
|
194
|
-
/** What `read` finds in the working tree's authored code files: tracked and untracked, ignored files and generated output left out. */
|
|
340
|
+
/** What `read` finds in the working tree's authored code files and lint configs: tracked and untracked, ignored files and generated output left out. */
|
|
195
341
|
function inWorktree(root, read, files = gitFiles(root)) {
|
|
196
342
|
return authoredCode(root, files).flatMap((file) => {
|
|
197
343
|
const path = join(root, file);
|
|
@@ -253,7 +399,15 @@ function atRevision(root, revision, read, files) {
|
|
|
253
399
|
function revisionSuppressions(root, revision, files) {
|
|
254
400
|
return atRevision(root, revision, suppressionsIn, files);
|
|
255
401
|
}
|
|
256
|
-
/**
|
|
402
|
+
/** The budget key of a config rule-off: its rule and the glob it is switched off for, so a wider or moved scope is a new key at ceiling 0. */
|
|
403
|
+
function configKey(rule, scope) {
|
|
404
|
+
return `${rule} for ${scope}`;
|
|
405
|
+
}
|
|
406
|
+
/** A count map with its keys sorted, so serialising it is stable. */
|
|
407
|
+
function sortedKeys(counts) {
|
|
408
|
+
return Object.fromEntries(Object.entries(counts).sort(([a], [b]) => a.localeCompare(b)));
|
|
409
|
+
}
|
|
410
|
+
/** Totals by budget key, rules and config rule-offs sorted by id. */
|
|
257
411
|
function countSuppressions(suppressions) {
|
|
258
412
|
const counts = {
|
|
259
413
|
rules: {},
|
|
@@ -261,10 +415,18 @@ function countSuppressions(suppressions) {
|
|
|
261
415
|
tsIgnore: 0,
|
|
262
416
|
safety: 0
|
|
263
417
|
};
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
418
|
+
const rules = /* @__PURE__ */ new Map();
|
|
419
|
+
const config = /* @__PURE__ */ new Map();
|
|
420
|
+
for (const suppression of suppressions) if (suppression.kind === "rule") rules.set(suppression.rule, (rules.get(suppression.rule) ?? 0) + 1);
|
|
421
|
+
else if (suppression.kind === "config") {
|
|
422
|
+
const key = configKey(suppression.rule, suppression.scope);
|
|
423
|
+
config.set(key, (config.get(key) ?? 0) + 1);
|
|
424
|
+
} else counts[suppression.kind]++;
|
|
425
|
+
return {
|
|
426
|
+
...counts,
|
|
427
|
+
rules: sortedKeys(Object.fromEntries(rules)),
|
|
428
|
+
config: sortedKeys(Object.fromEntries(config))
|
|
429
|
+
};
|
|
268
430
|
}
|
|
269
431
|
//#endregion
|
|
270
|
-
export {
|
|
432
|
+
export { openDisablesIn as a, suppressionsIn as c, inWorktree as i, worktreeSuppressions as l, configKey as n, revisionSuppressions as o, countSuppressions as r, sortedKeys as s, atRevision as t };
|