@antelopejs/tooling-configs 0.2.16 → 0.2.18
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 +101 -52
- package/dist/bin/api-diff.mjs +430 -107
- package/dist/bin/body-check.d.mts +1 -0
- package/dist/bin/body-check.mjs +344 -0
- package/dist/bin/capture.d.mts +1 -0
- package/dist/bin/capture.mjs +559 -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 +6 -6
- package/dist/bin/ratchet.mjs +316 -10
- package/dist/bin/suppressions.mjs +118 -80
- package/dist/bin/sync.mjs +554 -153
- package/dist/bin/verify.mjs +240 -127
- package/dist/capture/index.d.mts +50 -0
- package/dist/capture/index.mjs +2 -0
- 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-DJ0TeaTU.mjs} +11 -5
- package/dist/formal/index.d.mts +40 -0
- package/dist/formal/index.mjs +205 -0
- package/dist/{json-BqvuCkqr.mjs → json-CG2L7YFl.mjs} +12 -8
- package/dist/oxc/anti-slop/index.mjs +869 -21
- package/dist/oxc/fmt.mjs +1 -1
- package/dist/oxc/guardrails/index.d.mts +1 -1
- package/dist/oxc/guardrails/index.mjs +736 -4
- package/dist/oxc/lint.d.mts +20 -3
- package/dist/oxc/lint.mjs +34 -10
- package/dist/oxlint-DIV-lLSS.mjs +98 -0
- package/dist/{package-Dtjt_lSa.mjs → package-BRTjkzt5.mjs} +29 -3
- package/dist/proof-stamp-DDDC0J6l.mjs +279 -0
- package/dist/ratchet-BAcLRYuM.mjs +448 -0
- package/dist/{renames-BGO4dWJ8.mjs → renames-L2owF5GD.mjs} +2 -2
- package/dist/scenes-DZVXkcxS.mjs +104 -0
- package/dist/shared-igPpEDpS.mjs +622 -0
- package/dist/{size-BVgNbLO6.mjs → size-DBatLT-F.mjs} +4 -4
- package/dist/suppression-base-9L8dlMQK.mjs +120 -0
- package/dist/suppression-budget-C0SkPO5R.mjs +535 -0
- package/dist/{suppressions-BJAngUxo.mjs → suppressions-DM60NkvL.mjs} +180 -18
- package/dist/templates-CypDm-j9.mjs +121 -0
- package/dist/typescript-CM0LrVP7.mjs +222 -0
- package/dist/verify/index.d.mts +6 -1
- package/dist/verify/index.mjs +1 -1
- package/package.json +31 -5
- 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/suppression-budget-Yyf5_J5l.mjs +0 -283
- package/dist/typescript-C54ONh9m.mjs +0 -433
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { l as resolveRevision, o as isAgentFile } from "./files-DJ0TeaTU.mjs";
|
|
2
|
+
import { a as parseBudget, n as SUPPRESSIONS_FILE, o as safetyCeiling, p as coveredLimits } from "./suppression-budget-C0SkPO5R.mjs";
|
|
3
|
+
import { o as revisionSuppressions, r as countSuppressions } from "./suppressions-DM60NkvL.mjs";
|
|
4
|
+
import { readFileSync } from "node:fs";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { spawnSync } from "node:child_process";
|
|
7
|
+
//#region src/guardrails/suppression-base.ts
|
|
8
|
+
function verified(root, ref) {
|
|
9
|
+
const result = spawnSync("git", [
|
|
10
|
+
"rev-parse",
|
|
11
|
+
"--verify",
|
|
12
|
+
"--quiet",
|
|
13
|
+
"--end-of-options",
|
|
14
|
+
`${ref}^{commit}`
|
|
15
|
+
], {
|
|
16
|
+
cwd: root,
|
|
17
|
+
encoding: "utf8"
|
|
18
|
+
});
|
|
19
|
+
return result.status === 0 ? result.stdout.trim() : void 0;
|
|
20
|
+
}
|
|
21
|
+
function fetchBase(root, branch, remote) {
|
|
22
|
+
const result = spawnSync("git", [
|
|
23
|
+
"fetch",
|
|
24
|
+
"--quiet",
|
|
25
|
+
"--no-tags",
|
|
26
|
+
"--depth=1",
|
|
27
|
+
"origin",
|
|
28
|
+
`+refs/heads/${branch}:${remote}`
|
|
29
|
+
], {
|
|
30
|
+
cwd: root,
|
|
31
|
+
encoding: "utf8",
|
|
32
|
+
env: {
|
|
33
|
+
...process.env,
|
|
34
|
+
GIT_TERMINAL_PROMPT: "0"
|
|
35
|
+
},
|
|
36
|
+
stdio: [
|
|
37
|
+
"ignore",
|
|
38
|
+
"ignore",
|
|
39
|
+
"pipe"
|
|
40
|
+
]
|
|
41
|
+
});
|
|
42
|
+
if (result.status === 0) return;
|
|
43
|
+
const detail = result.error?.message ?? result.stderr.trim();
|
|
44
|
+
throw new Error(`Cannot compare with the pull request base: ${remote} is missing and fetching ${branch} from origin failed${detail ? ` (${detail})` : ""}. In CI, fetch the base branch before this check with credentials, or pass --base.`);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The commit a pull request merges into: `base` when given; in CI the
|
|
48
|
+
* `GITHUB_BASE_REF` branch, taken from the local `origin/<branch>` ref when
|
|
49
|
+
* present and otherwise fetched from `origin`, an error naming the missing ref
|
|
50
|
+
* when the fetch fails; otherwise `origin/HEAD`, the remote's default branch.
|
|
51
|
+
* A requested base that cannot be resolved is an error; without one, a
|
|
52
|
+
* repository with no `origin/HEAD` has no base.
|
|
53
|
+
*/
|
|
54
|
+
function suppressionBase(root, base) {
|
|
55
|
+
if (base !== void 0) return resolveRevision(root, base);
|
|
56
|
+
const branch = process.env.GITHUB_BASE_REF;
|
|
57
|
+
if (branch) {
|
|
58
|
+
const remote = `refs/remotes/origin/${branch}`;
|
|
59
|
+
if (verified(root, remote) === void 0) fetchBase(root, branch, remote);
|
|
60
|
+
return resolveRevision(root, remote);
|
|
61
|
+
}
|
|
62
|
+
return verified(root, "refs/remotes/origin/HEAD");
|
|
63
|
+
}
|
|
64
|
+
function agentFilesDropped(base, current) {
|
|
65
|
+
return base.agentFiles && !current.agentFiles ? [`${SUPPRESSIONS_FILE}: drops agentFiles, which the base has. Keep it; the repository's own files under the agent directories stay counted.`] : [];
|
|
66
|
+
}
|
|
67
|
+
/** What the agent directories hold, when `current` begins recording them and `base` does not; nothing otherwise. */
|
|
68
|
+
function adoptableAgentCounts(root, revision, base, current) {
|
|
69
|
+
const none = {
|
|
70
|
+
rules: {},
|
|
71
|
+
config: {},
|
|
72
|
+
tsExpectError: 0,
|
|
73
|
+
tsIgnore: 0,
|
|
74
|
+
safety: 0
|
|
75
|
+
};
|
|
76
|
+
if (base.agentFiles || !current.agentFiles) return none;
|
|
77
|
+
const counts = countSuppressions(revisionSuppressions(root, revision).filter(({ file }) => isAgentFile(file)));
|
|
78
|
+
return {
|
|
79
|
+
...counts,
|
|
80
|
+
config: counts.config ?? {}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Fails each ceiling of the working-tree budget that is higher than on
|
|
85
|
+
* `revision`, and each rule that `revision` lists but the working tree
|
|
86
|
+
* dropped, config rule-offs included once the base has their ceilings; a
|
|
87
|
+
* config ceiling may rise by what a containing scope of its rule falls. The
|
|
88
|
+
* safety ceiling may rise by what the SAFETY rule's own ceiling falls. A
|
|
89
|
+
* base that does not record the agent directories' files lets the working
|
|
90
|
+
* tree record them, each ceiling rising by no more than its count there; a
|
|
91
|
+
* base that does must keep recording them. A base without a budget, a first
|
|
92
|
+
* adoption, passes.
|
|
93
|
+
*/
|
|
94
|
+
function raisedCeilings(root, revision) {
|
|
95
|
+
const committed = spawnSync("git", ["show", `${revision}:${SUPPRESSIONS_FILE}`], {
|
|
96
|
+
cwd: root,
|
|
97
|
+
encoding: "utf8"
|
|
98
|
+
});
|
|
99
|
+
if (committed.status !== 0) return [];
|
|
100
|
+
const label = `${revision.slice(0, 12)}:${SUPPRESSIONS_FILE}`;
|
|
101
|
+
const base = parseBudget(committed.stdout, label);
|
|
102
|
+
const current = parseBudget(readFileSync(join(root, SUPPRESSIONS_FILE), "utf8"), SUPPRESSIONS_FILE);
|
|
103
|
+
const failures = agentFilesDropped(base, current);
|
|
104
|
+
const room = adoptableAgentCounts(root, revision, base, current);
|
|
105
|
+
const compare = (key, ceiling, after, adoptable = 0) => {
|
|
106
|
+
const before = ceiling + adoptable;
|
|
107
|
+
if (after === void 0) failures.push(`${SUPPRESSIONS_FILE}: removes ${key}, which has ceiling ${before} on the base. Keep the key; lower it with suppressions:update.`);
|
|
108
|
+
else if (after > before) failures.push(`${SUPPRESSIONS_FILE}: raises ${key} from ${ceiling} on the base to ${after}${adoptable ? `, above the ${before} that recording the agent files allows` : ""}.`);
|
|
109
|
+
};
|
|
110
|
+
for (const rule of /* @__PURE__ */ new Set([...Object.keys(base.rules), ...Object.keys(current.rules)])) compare(`lint rule \`${rule}\``, base.rules[rule] ?? 0, Object.hasOwn(base.rules, rule) ? current.rules[rule] : current.rules[rule] ?? 0, room.rules[rule]);
|
|
111
|
+
const baseConfig = base.config ?? {};
|
|
112
|
+
const covered = coveredLimits(baseConfig, current.config ?? {});
|
|
113
|
+
for (const rule of /* @__PURE__ */ new Set([...Object.keys(baseConfig), ...Object.keys(current.config ?? {})])) if (base.config !== void 0) compare(`lint config rule-off \`${rule}\``, covered[rule] ?? 0, current.config === void 0 || Object.hasOwn(baseConfig, rule) && !Object.hasOwn(current.config, rule) ? void 0 : current.config[rule] ?? 0, room.config[rule]);
|
|
114
|
+
compare("`@ts-expect-error`", base.tsExpectError, current.tsExpectError, room.tsExpectError);
|
|
115
|
+
compare("`@ts-ignore`", base.tsIgnore, current.tsIgnore, room.tsIgnore);
|
|
116
|
+
compare("`SAFETY:` comments", safetyCeiling(base, current.rules["anti-slop/require-safety-comment-for-type-assertion"] ?? 0), current.safety, room.safety);
|
|
117
|
+
return failures.length ? [...failures, "Ceilings only go down. Fix the code, or remove another suppression of the same kind; raising a ceiling needs the operator's approval through the ruleset bypass."] : [];
|
|
118
|
+
}
|
|
119
|
+
//#endregion
|
|
120
|
+
export { suppressionBase as n, raisedCeilings as t };
|
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
import { n as renamedPaths } from "./renames-L2owF5GD.mjs";
|
|
2
|
+
import { o as isAgentFile, r as changedFiles } from "./files-DJ0TeaTU.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-DM60NkvL.mjs";
|
|
4
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { join, matchesGlob } from "node:path";
|
|
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
|
|
138
|
+
//#region src/guardrails/suppression-policy.ts
|
|
139
|
+
/** Rules a directive may never name, each with what to do instead. */
|
|
140
|
+
const NEVER_SUPPRESSED = /* @__PURE__ */ new Map([
|
|
141
|
+
["anti-slop/require-safety-comment-for-type-assertion", "remove the assertion (a type guard, a schema parse at the boundary, an inferred or owner-derived type), or, where it really holds, write `SAFETY: <why it holds>` above it"],
|
|
142
|
+
["antelopejs/no-comments", "say it in code, move a public contract into TSDoc, or delete the comment"],
|
|
143
|
+
["antelopejs/no-constant-value-assertion", "assert on a value the code under test computes, or delete the test"],
|
|
144
|
+
["antelopejs/no-fs-read-in-tests", "drive the behaviour through the public interface, or read from fixtures/"],
|
|
145
|
+
["antelopejs/no-self-import-in-tests", "import the module under test by relative path, or publish the subpath in exports and link the package to itself"],
|
|
146
|
+
["antelopejs/no-typeof-function-only-test", "assert on what the function returns or does, or delete the test"],
|
|
147
|
+
["antelopejs/require-assertion", "assert on the behaviour, or delete the test"],
|
|
148
|
+
["typescript/ban-ts-comment", "use `@ts-expect-error <reason>`"]
|
|
149
|
+
]);
|
|
150
|
+
const MIN_REASON_WORDS = 3;
|
|
151
|
+
/** Words that say a suppression is wanted without saying why the code needs it. */
|
|
152
|
+
const VAGUE_WORDS = new Set(`a an the this that it its is are be was here there we for of to as and or not no
|
|
153
|
+
ok okay fine safe safely correct valid works working intended intentional intentionally
|
|
154
|
+
needed necessary required expected known false positive legacy existing old behavior behaviour
|
|
155
|
+
preserve preserved preserves preserving keep kept compat compatibility workaround hack
|
|
156
|
+
temporary todo fixme later ignore ignored disable disabled suppress suppressed lint rule
|
|
157
|
+
type types typing typescript cast casting assert assertion by design trust trusted me`.split(/\s+/u));
|
|
158
|
+
function words(text) {
|
|
159
|
+
return text.toLowerCase().split(/[^\p{L}\p{N}]+/u).filter(Boolean);
|
|
160
|
+
}
|
|
161
|
+
/** A reason too short or too vague to tell a reader why this code needs its suppression. */
|
|
162
|
+
function genericReason(suppression) {
|
|
163
|
+
const said = words(suppression.reason);
|
|
164
|
+
if (said.length < MIN_REASON_WORDS) return true;
|
|
165
|
+
const named = new Set(suppression.kind === "rule" ? words(suppression.rule) : []);
|
|
166
|
+
return said.every((word) => VAGUE_WORDS.has(word) || named.has(word));
|
|
167
|
+
}
|
|
168
|
+
function identity(suppression) {
|
|
169
|
+
if (suppression.kind === "config") return [
|
|
170
|
+
suppression.kind,
|
|
171
|
+
suppression.rule,
|
|
172
|
+
suppression.scope
|
|
173
|
+
].join("\0");
|
|
174
|
+
return [
|
|
175
|
+
suppression.kind,
|
|
176
|
+
suppression.kind === "rule" ? suppression.rule : "",
|
|
177
|
+
suppression.reason
|
|
178
|
+
].join("\0");
|
|
179
|
+
}
|
|
180
|
+
/** Takes one occurrence of `key` from `counts`, reporting whether one was left. */
|
|
181
|
+
function take(counts, key) {
|
|
182
|
+
const left = counts.get(key) ?? 0;
|
|
183
|
+
if (left > 0) counts.set(key, left - 1);
|
|
184
|
+
return left > 0;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* The items of `after` that `before` does not account for, as multisets of
|
|
188
|
+
* `key`: an item is matched first in its own file, then in any file, so a
|
|
189
|
+
* moved item is not added and an added one is reported where it was added.
|
|
190
|
+
*/
|
|
191
|
+
function unmatched(before, after, key) {
|
|
192
|
+
const sameFile = /* @__PURE__ */ new Map();
|
|
193
|
+
for (const item of before) {
|
|
194
|
+
const local = `${item.file}\0${key(item)}`;
|
|
195
|
+
sameFile.set(local, (sameFile.get(local) ?? 0) + 1);
|
|
196
|
+
}
|
|
197
|
+
const elsewhere = after.filter((item) => !take(sameFile, `${item.file}\0${key(item)}`));
|
|
198
|
+
const anyFile = /* @__PURE__ */ new Map();
|
|
199
|
+
for (const [local, count] of sameFile) {
|
|
200
|
+
const global = local.slice(local.indexOf("\0") + 1);
|
|
201
|
+
anyFile.set(global, (anyFile.get(global) ?? 0) + count);
|
|
202
|
+
}
|
|
203
|
+
return elsewhere.filter((item) => !take(anyFile, key(item)));
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* The suppressions of `after` that `before` does not account for, by kind,
|
|
207
|
+
* rule and reason, so a suppression moved to another file with its reason is
|
|
208
|
+
* not added, and one whose reason changed is.
|
|
209
|
+
*/
|
|
210
|
+
function addedSuppressions(before, after) {
|
|
211
|
+
return unmatched(before, after, identity);
|
|
212
|
+
}
|
|
213
|
+
function label(suppression) {
|
|
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}\``;
|
|
216
|
+
if (suppression.kind === "safety") return "`SAFETY:`";
|
|
217
|
+
return suppression.kind === "tsIgnore" ? "`@ts-ignore`" : "`@ts-expect-error`";
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Fails each added suppression that names a rule no directive may name, and
|
|
221
|
+
* each whose reason is empty or generic. Suppressions already on the base
|
|
222
|
+
* are left to the burn-down the budget tracks.
|
|
223
|
+
*/
|
|
224
|
+
function policyFailures(added) {
|
|
225
|
+
return added.flatMap((suppression) => {
|
|
226
|
+
const at = `${suppression.file}:${suppression.line}`;
|
|
227
|
+
const instead = suppression.kind === "rule" || suppression.kind === "config" ? NEVER_SUPPRESSED.get(suppression.rule) : void 0;
|
|
228
|
+
if (instead !== void 0) return [`${at}: ${label(suppression)}: this rule is never suppressed. Fix: ${instead}.`];
|
|
229
|
+
if (suppression.kind === "config" || suppression.kind === "rule" && suppression.rule === "*") return [];
|
|
230
|
+
if (!genericReason(suppression)) return [];
|
|
231
|
+
const quoted = suppression.reason ? `"${suppression.reason}"` : "none";
|
|
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.`];
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
/** Fails each block disable left open at the end of its file. */
|
|
236
|
+
function openDisableFailures(open) {
|
|
237
|
+
return open.map(({ directive, rule, file, line }) => `${file}:${line}: \`${directive}${rule === "*" ? "" : ` ${rule}`}\` is never closed, so it also silences every line added below it. Fix: add \`${directive.replace("disable", "enable")}${rule === "*" ? "" : ` ${rule}`}\` right after the last line that needs it, or use \`${directive}-next-line\`.`);
|
|
238
|
+
}
|
|
239
|
+
/** The open blocks of `after` beyond those of `before`, by directive and rule. */
|
|
240
|
+
function addedOpenDisables(before, after) {
|
|
241
|
+
return unmatched(before, after, ({ directive, rule }) => [directive, rule].join("\0"));
|
|
242
|
+
}
|
|
243
|
+
function hasCommit(root, revision) {
|
|
244
|
+
return spawnSync("git", [
|
|
245
|
+
"rev-parse",
|
|
246
|
+
"--verify",
|
|
247
|
+
"--quiet",
|
|
248
|
+
`${revision}^{commit}`
|
|
249
|
+
], { cwd: root }).status === 0;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Fails what the working tree adds against `revision` (HEAD when none is
|
|
253
|
+
* given): a suppression that names a never-suppressed rule or gives no
|
|
254
|
+
* specific reason, and a block disable left open.
|
|
255
|
+
*/
|
|
256
|
+
function checkPolicy(root, revision = "HEAD") {
|
|
257
|
+
if (!hasCommit(root, revision)) return [];
|
|
258
|
+
return [...policyFailures(addedSuppressions(revisionSuppressions(root, revision), worktreeSuppressions(root))), ...openDisableFailures(addedOpenDisables(atRevision(root, revision, openDisablesIn), inWorktree(root, openDisablesIn)))];
|
|
259
|
+
}
|
|
260
|
+
/** The same policy for a commit: the staged files against their HEAD versions, rename sources and deleted files included. */
|
|
261
|
+
function checkStagedPolicy(root, staged, previous) {
|
|
262
|
+
if (!hasCommit(root, "HEAD")) return [];
|
|
263
|
+
return [...policyFailures(addedSuppressions(revisionSuppressions(root, "HEAD", previous), revisionSuppressions(root, "", staged))), ...openDisableFailures(addedOpenDisables(atRevision(root, "HEAD", openDisablesIn, previous), atRevision(root, "", openDisablesIn, staged)))];
|
|
264
|
+
}
|
|
265
|
+
//#endregion
|
|
266
|
+
//#region src/guardrails/suppression-budget.ts
|
|
267
|
+
/** The per-repository suppression budget, at the repository root. It is permanent: nothing deletes it, even at zero. */
|
|
268
|
+
const SUPPRESSIONS_FILE = "guardrails.suppressions.json";
|
|
269
|
+
/** What a failure tells the author to do. */
|
|
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
|
+
}
|
|
276
|
+
const LABELS = {
|
|
277
|
+
tsExpectError: "`@ts-expect-error`",
|
|
278
|
+
tsIgnore: "`@ts-ignore`",
|
|
279
|
+
safety: "`SAFETY:` comments"
|
|
280
|
+
};
|
|
281
|
+
function labelOf(suppression) {
|
|
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];
|
|
285
|
+
}
|
|
286
|
+
/** The rule a SAFETY comment satisfies, so a directive disabling it may become a SAFETY comment without a raise. */
|
|
287
|
+
const SAFETY_RULE = "anti-slop/require-safety-comment-for-type-assertion";
|
|
288
|
+
/** The safety ceiling plus the room the directives disabling the SAFETY rule leave below theirs, so the two only ever shrink together. */
|
|
289
|
+
function safetyCeiling(budget, disables) {
|
|
290
|
+
return budget.safety + Math.max(0, (budget.rules["anti-slop/require-safety-comment-for-type-assertion"] ?? 0) - disables);
|
|
291
|
+
}
|
|
292
|
+
function limitOf(budget, suppression) {
|
|
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}"`)]));
|
|
304
|
+
}
|
|
305
|
+
function count(value, source) {
|
|
306
|
+
if (value === void 0) throw new Error(`${source} is missing`);
|
|
307
|
+
if (!Number.isInteger(value) || value < 0) throw new Error(`${source} must be a non-negative integer`);
|
|
308
|
+
return value;
|
|
309
|
+
}
|
|
310
|
+
/** Parses a budget, rejecting a missing field and anything but non-negative integer ceilings. */
|
|
311
|
+
function parseBudget(text, source) {
|
|
312
|
+
const parsed = JSON.parse(text);
|
|
313
|
+
if (parsed === null || Array.isArray(parsed)) throw new Error(`${source}: expected an object with "rules"`);
|
|
314
|
+
const budget = {
|
|
315
|
+
rules: counted(parsed.rules ?? {}, `${source}: rules`),
|
|
316
|
+
tsExpectError: count(parsed.tsExpectError, `${source}: tsExpectError`),
|
|
317
|
+
tsIgnore: count(parsed.tsIgnore, `${source}: tsIgnore`),
|
|
318
|
+
safety: count(parsed.safety, `${source}: safety`)
|
|
319
|
+
};
|
|
320
|
+
if (parsed.config !== void 0) budget.config = counted(parsed.config, `${source}: config`);
|
|
321
|
+
if (parsed.agentFiles !== void 0) {
|
|
322
|
+
if (parsed.agentFiles !== true) throw new Error(`${source}: agentFiles must be true`);
|
|
323
|
+
budget.agentFiles = true;
|
|
324
|
+
}
|
|
325
|
+
return budget;
|
|
326
|
+
}
|
|
327
|
+
/** Serializes a budget with sorted rules and config rule-offs, zero ceilings kept, so rewriting an unchanged budget is a no-op. */
|
|
328
|
+
function formatBudget(budget) {
|
|
329
|
+
const sorted = {
|
|
330
|
+
...budget,
|
|
331
|
+
rules: sortedKeys(budget.rules)
|
|
332
|
+
};
|
|
333
|
+
if (budget.config !== void 0) sorted.config = sortedKeys(budget.config);
|
|
334
|
+
return `${JSON.stringify(sorted, null, 2)}\n`;
|
|
335
|
+
}
|
|
336
|
+
/** `suppressions` without those in the agent directories, unless `budget` records them. */
|
|
337
|
+
function budgeted(budget, suppressions) {
|
|
338
|
+
return budget.agentFiles ? suppressions : suppressions.filter(({ file }) => !isAgentFile(file));
|
|
339
|
+
}
|
|
340
|
+
/** The totals of the working tree's suppressions in the agent directories. */
|
|
341
|
+
function agentCounts(root) {
|
|
342
|
+
return countSuppressions(worktreeSuppressions(root).filter(({ file }) => isAgentFile(file)));
|
|
343
|
+
}
|
|
344
|
+
function summed(base, extra) {
|
|
345
|
+
const count = (map, key) => Object.hasOwn(map, key) ? map[key] : 0;
|
|
346
|
+
return Object.fromEntries([.../* @__PURE__ */ new Set([...Object.keys(base), ...Object.keys(extra)])].map((key) => [key, count(base, key) + count(extra, key)]));
|
|
347
|
+
}
|
|
348
|
+
/** `budget` with each ceiling raised by the count of its key in the agent directories, and the repository marked as recording them. */
|
|
349
|
+
function withAgentCeilings(budget, agent) {
|
|
350
|
+
return {
|
|
351
|
+
rules: summed(budget.rules, agent.rules),
|
|
352
|
+
tsExpectError: budget.tsExpectError + agent.tsExpectError,
|
|
353
|
+
tsIgnore: budget.tsIgnore + agent.tsIgnore,
|
|
354
|
+
safety: budget.safety + agent.safety,
|
|
355
|
+
config: summed(budget.config ?? {}, agent.config ?? {}),
|
|
356
|
+
agentFiles: true
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* A budget with config rule-off ceilings and the agent directories' ceilings:
|
|
361
|
+
* `text` itself when it has both or does not parse, else `text` plus today's
|
|
362
|
+
* config rule-offs outside the agent directories, and the agent directories'
|
|
363
|
+
* counts added on top of the ceilings.
|
|
364
|
+
*/
|
|
365
|
+
function withAdoptedCeilings(root, text) {
|
|
366
|
+
let budget;
|
|
367
|
+
try {
|
|
368
|
+
budget = parseBudget(text, SUPPRESSIONS_FILE);
|
|
369
|
+
} catch {
|
|
370
|
+
return text;
|
|
371
|
+
}
|
|
372
|
+
if (budget.config !== void 0 && budget.agentFiles) return text;
|
|
373
|
+
const counted = countSuppressions(budgeted(budget, worktreeSuppressions(root)));
|
|
374
|
+
const adopted = {
|
|
375
|
+
...budget,
|
|
376
|
+
config: budget.config ?? counted.config
|
|
377
|
+
};
|
|
378
|
+
return formatBudget(budget.agentFiles ? adopted : withAgentCeilings(adopted, agentCounts(root)));
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* The budget sync writes: `current` with today's config rule-offs and the
|
|
382
|
+
* agent directories' suppressions adopted when it predates them; when the file
|
|
383
|
+
* is missing, the committed one when it was only deleted, so recreating it
|
|
384
|
+
* never raises a ceiling, else the current counts.
|
|
385
|
+
*/
|
|
386
|
+
function adoptedBudget(root, current) {
|
|
387
|
+
if (current !== void 0) return withAdoptedCeilings(root, current);
|
|
388
|
+
const committed = spawnSync("git", ["show", `HEAD:${SUPPRESSIONS_FILE}`], {
|
|
389
|
+
cwd: root,
|
|
390
|
+
encoding: "utf8"
|
|
391
|
+
});
|
|
392
|
+
if (committed.status === 0) return withAdoptedCeilings(root, committed.stdout);
|
|
393
|
+
return formatBudget({
|
|
394
|
+
...countSuppressions(worktreeSuppressions(root)),
|
|
395
|
+
agentFiles: true
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
/** Each `file:line` once, indented, in the order found. */
|
|
399
|
+
function locations(found) {
|
|
400
|
+
return [...new Set(found.map(({ file, line }) => ` ${file}:${line}`))];
|
|
401
|
+
}
|
|
402
|
+
function groupByKey(suppressions) {
|
|
403
|
+
const groups = /* @__PURE__ */ new Map();
|
|
404
|
+
for (const suppression of suppressions) {
|
|
405
|
+
const key = labelOf(suppression);
|
|
406
|
+
groups.set(key, [...groups.get(key) ?? [], suppression]);
|
|
407
|
+
}
|
|
408
|
+
return groups;
|
|
409
|
+
}
|
|
410
|
+
/** Each ceiling of `limits` lowered to its count in `current`, rules at zero kept. */
|
|
411
|
+
function lowerEach(limits, current = {}) {
|
|
412
|
+
return Object.fromEntries(Object.entries(limits).map(([rule, limit]) => [rule, Math.min(limit, Object.hasOwn(current, rule) ? current[rule] : 0)]));
|
|
413
|
+
}
|
|
414
|
+
function lowered(budget, current) {
|
|
415
|
+
const result = {
|
|
416
|
+
rules: lowerEach(budget.rules, current.rules),
|
|
417
|
+
tsExpectError: Math.min(budget.tsExpectError, current.tsExpectError),
|
|
418
|
+
tsIgnore: Math.min(budget.tsIgnore, current.tsIgnore),
|
|
419
|
+
safety: Math.min(safetyCeiling(budget, current.rules["anti-slop/require-safety-comment-for-type-assertion"] ?? 0), current.safety)
|
|
420
|
+
};
|
|
421
|
+
if (budget.config !== void 0) result.config = lowerEach(budget.config, current.config);
|
|
422
|
+
if (budget.agentFiles) result.agentFiles = true;
|
|
423
|
+
return result;
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Fails each budget key whose count in the working tree is above its ceiling,
|
|
427
|
+
* listing the suppressions in the files this change touches (all of them when
|
|
428
|
+
* it touches none). SAFETY comments may also use the room the
|
|
429
|
+
* directives disabling the SAFETY rule leave below their ceiling. `update`
|
|
430
|
+
* lowers every ceiling to its current count, moving that room to `safety`;
|
|
431
|
+
* no command ever raises the two together. A config rule-off may use the
|
|
432
|
+
* room a containing scope of its rule gives up, and `update` moves that room
|
|
433
|
+
* to the narrower key.
|
|
434
|
+
*/
|
|
435
|
+
function checkSuppressions(root, update = false) {
|
|
436
|
+
const path = join(root, SUPPRESSIONS_FILE);
|
|
437
|
+
if (!existsSync(path)) throw new Error(`${SUPPRESSIONS_FILE} is missing. Run antelopejs-sync.`);
|
|
438
|
+
const committed = parseBudget(readFileSync(path, "utf8"), SUPPRESSIONS_FILE);
|
|
439
|
+
const suppressions = budgeted(committed, worktreeSuppressions(root));
|
|
440
|
+
const counts = countSuppressions(suppressions);
|
|
441
|
+
const budget = withCoveredScopes(committed, counts);
|
|
442
|
+
const changed = changedFiles(root);
|
|
443
|
+
const failures = [];
|
|
444
|
+
const failed = [];
|
|
445
|
+
for (const [key, found] of groupByKey(suppressions)) {
|
|
446
|
+
if (!capped(budget, found[0])) continue;
|
|
447
|
+
const limit = found[0].kind === "safety" ? safetyCeiling(budget, counts.rules["anti-slop/require-safety-comment-for-type-assertion"] ?? 0) : limitOf(budget, found[0]);
|
|
448
|
+
if (found.length <= limit) continue;
|
|
449
|
+
failed.push(found[0]);
|
|
450
|
+
const inChanged = found.filter(({ file }) => changed.has(file));
|
|
451
|
+
const shown = inChanged.length ? inChanged : found;
|
|
452
|
+
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.`] : []);
|
|
453
|
+
}
|
|
454
|
+
if (update) writeFileSync(path, formatBudget(lowered(budget, counts)));
|
|
455
|
+
return [...failures, ...fixesFor(failed)];
|
|
456
|
+
}
|
|
457
|
+
/** `budget` with each config ceiling widened by the room containing scopes of its rule give up at `counts`. */
|
|
458
|
+
function withCoveredScopes(budget, counts) {
|
|
459
|
+
if (budget.config === void 0) return budget;
|
|
460
|
+
return {
|
|
461
|
+
...budget,
|
|
462
|
+
config: coveredLimits(budget.config, counts.config ?? {})
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
function budgetAt(root, revision) {
|
|
466
|
+
const result = spawnSync("git", ["show", `${revision}:${SUPPRESSIONS_FILE}`], {
|
|
467
|
+
cwd: root,
|
|
468
|
+
encoding: "utf8"
|
|
469
|
+
});
|
|
470
|
+
return result.status === 0 ? parseBudget(result.stdout, `${revision || "index"}:${SUPPRESSIONS_FILE}`) : void 0;
|
|
471
|
+
}
|
|
472
|
+
function stagedNames(root, filter) {
|
|
473
|
+
return execFileSync("git", [
|
|
474
|
+
"diff",
|
|
475
|
+
"--cached",
|
|
476
|
+
"--name-only",
|
|
477
|
+
"--find-renames",
|
|
478
|
+
`--diff-filter=${filter}`,
|
|
479
|
+
"-z"
|
|
480
|
+
], {
|
|
481
|
+
cwd: root,
|
|
482
|
+
encoding: "utf8"
|
|
483
|
+
}).split("\0").filter(Boolean);
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Compares the staged files' suppressions with those files at HEAD, rename
|
|
487
|
+
* sources and deleted files included, and fails each key that rises by more
|
|
488
|
+
* than the staged budget raises its ceiling, then applies the suppression
|
|
489
|
+
* policy to what the commit adds. A commit that first adds the budget is
|
|
490
|
+
* not compared.
|
|
491
|
+
*/
|
|
492
|
+
function stagedSuppressions(root) {
|
|
493
|
+
const renames = renamedPaths(root, true);
|
|
494
|
+
const staged = stagedNames(root, "ACMR");
|
|
495
|
+
const previous = [...staged.map((file) => renames.get(file) ?? file), ...stagedNames(root, "D")];
|
|
496
|
+
return [...stagedBudget(root, staged, previous), ...checkStagedPolicy(root, staged, previous)];
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* How much a staged config key may rise: what the staged budget raises its
|
|
500
|
+
* ceiling, plus what a containing scope of its rule falls in the staged files.
|
|
501
|
+
*/
|
|
502
|
+
function stagedScopes(head, index, before, after) {
|
|
503
|
+
const limits = { ...before };
|
|
504
|
+
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));
|
|
505
|
+
const covered = coveredLimits(limits, after);
|
|
506
|
+
return (rule, scope, previous) => (covered[configKey(rule, scope)] ?? 0) - previous;
|
|
507
|
+
}
|
|
508
|
+
function stagedBudget(root, staged, previous) {
|
|
509
|
+
const head = budgetAt(root, "HEAD");
|
|
510
|
+
if (head === void 0) return [];
|
|
511
|
+
const index = budgetAt(root, "") ?? head;
|
|
512
|
+
const headSuppressions = budgeted(index, revisionSuppressions(root, "HEAD", previous));
|
|
513
|
+
const stagedSuppressions = budgeted(index, revisionSuppressions(root, "", staged));
|
|
514
|
+
const before = groupByKey(headSuppressions);
|
|
515
|
+
const after = groupByKey(stagedSuppressions);
|
|
516
|
+
const scopes = stagedScopes(head, index, countSuppressions(headSuppressions).config ?? {}, countSuppressions(stagedSuppressions).config ?? {});
|
|
517
|
+
const additions = stagedAdditions(root, [...new Set([...after.values()].flat().map(({ file }) => file))]);
|
|
518
|
+
const failures = [];
|
|
519
|
+
const failed = [];
|
|
520
|
+
const safetyRule = `lint rule \`${SAFETY_RULE}\``;
|
|
521
|
+
const converted = Math.max(0, (before.get(safetyRule)?.length ?? 0) - (after.get(safetyRule)?.length ?? 0));
|
|
522
|
+
const freed = (head.rules["anti-slop/require-safety-comment-for-type-assertion"] ?? 0) - (index.rules["anti-slop/require-safety-comment-for-type-assertion"] ?? 0);
|
|
523
|
+
for (const [key, found] of after) {
|
|
524
|
+
if (!capped(index, found[0])) continue;
|
|
525
|
+
const rise = found.length - (before.get(key)?.length ?? 0);
|
|
526
|
+
const raised = limitOf(index, found[0]) - limitOf(head, found[0]);
|
|
527
|
+
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;
|
|
528
|
+
failed.push(found[0]);
|
|
529
|
+
const added = found.filter(({ file, line }) => additions.get(file)?.some(({ start, end }) => line >= start && line <= end));
|
|
530
|
+
failures.push(`${key}: this commit adds ${rise} to the staged files.`, ...locations(added.length ? added : found));
|
|
531
|
+
}
|
|
532
|
+
return [...failures, ...fixesFor(failed)];
|
|
533
|
+
}
|
|
534
|
+
//#endregion
|
|
535
|
+
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 };
|