@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
package/dist/bin/verify.mjs
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { a as
|
|
3
|
-
import { a as
|
|
2
|
+
import { a as VERIFY_KINDS, c as isVerifyKind, l as skillFolder } from "../shared-CoUt4GUm.mjs";
|
|
3
|
+
import { a as isString, f as parseJson, i as isObject, u as optStr } from "../json-CG2L7YFl.mjs";
|
|
4
|
+
import { a as exitIs, c as interrupted, i as exists, r as equal, u as refused } from "../checks-CxkSu2E6.mjs";
|
|
4
5
|
import { n as runCliAsync } from "../cli-4Tv71vYk.mjs";
|
|
5
|
-
import { o as repositoryRoot } from "../files-
|
|
6
|
-
import { a as skillFolder, i as isVerifyKind, t as VERIFY_KINDS } from "../kinds-Bzyyi9yk.mjs";
|
|
6
|
+
import { o as repositoryRoot } from "../files-Bc036sJW.mjs";
|
|
7
7
|
import { appendFileSync, closeSync, copyFileSync, cpSync, existsSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
|
|
8
8
|
import { basename, dirname, extname, join, matchesGlob, posix, relative, resolve, sep } from "node:path";
|
|
9
9
|
import { execFileSync, spawn, spawnSync } from "node:child_process";
|
|
10
10
|
import { parseArgs } from "node:util";
|
|
11
11
|
import { pathToFileURL } from "node:url";
|
|
12
|
-
import { homedir, tmpdir } from "node:os";
|
|
13
12
|
import ts from "typescript";
|
|
13
|
+
import { homedir, tmpdir } from "node:os";
|
|
14
14
|
import { randomBytes } from "node:crypto";
|
|
15
15
|
import { createServer } from "node:net";
|
|
16
16
|
//#region src/verify/doors.ts
|
|
@@ -34,14 +34,21 @@ function readCeilings(root) {
|
|
|
34
34
|
if (!existsSync(path)) return EMPTY_CEILINGS;
|
|
35
35
|
return parseCeilings(readFileSync(path, "utf8"), path);
|
|
36
36
|
}
|
|
37
|
-
/**
|
|
37
|
+
/** Whether `value` is a `major.minor.patch` release. */
|
|
38
|
+
function isPackRelease(value) {
|
|
39
|
+
return /^\d+\.\d+\.\d+$/u.test(value);
|
|
40
|
+
}
|
|
41
|
+
/** Parses the ceilings file, rejecting anything but non-negative integer counts and a release for `pack`. */
|
|
38
42
|
function parseCeilings(text, source) {
|
|
39
43
|
const parsed = JSON.parse(text);
|
|
40
44
|
if (parsed === null || Array.isArray(parsed)) throw new Error(`${source}: expected an object with "rules" and "oversizeFiles"`);
|
|
41
|
-
|
|
45
|
+
if (parsed.pack !== void 0 && !isPackRelease(parsed.pack)) throw new Error(`${source}: "pack" must be a release such as "0.2.17"`);
|
|
46
|
+
const ceilings = {
|
|
42
47
|
rules: parseCounts(parsed.rules, `${source}: rules`),
|
|
43
48
|
oversizeFiles: parseCounts(parsed.oversizeFiles, `${source}: oversizeFiles`)
|
|
44
49
|
};
|
|
50
|
+
if (parsed.pack !== void 0) ceilings.pack = parsed.pack;
|
|
51
|
+
return ceilings;
|
|
45
52
|
}
|
|
46
53
|
function parseCounts(value, source) {
|
|
47
54
|
if (value === void 0) return {};
|
|
@@ -59,10 +66,11 @@ function sortedCounts(counts) {
|
|
|
59
66
|
/** Serializes ceilings with sorted keys, so rewriting an unchanged file is a no-op. */
|
|
60
67
|
function formatCeilings(ceilings) {
|
|
61
68
|
const sorted = {
|
|
69
|
+
pack: ceilings.pack,
|
|
62
70
|
rules: sortedCounts(ceilings.rules),
|
|
63
71
|
oversizeFiles: sortedCounts(ceilings.oversizeFiles)
|
|
64
72
|
};
|
|
65
73
|
return `${JSON.stringify(sorted, null, 2)}\n`;
|
|
66
74
|
}
|
|
67
75
|
//#endregion
|
|
68
|
-
export { formatCeilings as a, findGitRoot as i, MAX_FILE_BYTES as n,
|
|
76
|
+
export { formatCeilings as a, parseCeilings as c, findGitRoot as i, readCeilings as l, MAX_FILE_BYTES as n, isPackRelease as o, MEASURE_ENV as r, isRetired as s, CEILINGS_FILE as t };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
-
import { isMap, isPair, isScalar, isSeq, parseDocument, visit } from "yaml";
|
|
3
|
+
import { Scalar, isMap, isPair, isScalar, isSeq, parseDocument, visit } from "yaml";
|
|
4
4
|
//#region src/guardrails/workflow.ts
|
|
5
5
|
const CI_NODE_PATTERN = /^AntelopeJS\/\.github\/\.github\/workflows\/ci-node\.yml@\S+$/;
|
|
6
6
|
const CI_RENOVATE_SYNC_PATTERN = /^AntelopeJS\/\.github\/\.github\/workflows\/ci-renovate-sync\.yml@\S+$/;
|
|
@@ -53,6 +53,51 @@ function ciRenovateSyncCallers(text) {
|
|
|
53
53
|
return callers(text, CI_RENOVATE_SYNC_PATTERN);
|
|
54
54
|
}
|
|
55
55
|
//#endregion
|
|
56
|
+
//#region src/guardrails/workflow-needs.ts
|
|
57
|
+
const NEEDS_REFERENCE = /(?<![\w.'"-])needs(?:\.([A-Za-z_][\w-]*)|\[\s*(['"])([^'"]+)\2\s*\])/g;
|
|
58
|
+
function neededIds(job) {
|
|
59
|
+
const needs = job.get("needs", true);
|
|
60
|
+
return (isSeq(needs) ? needs.items : [needs]).flatMap((entry) => isScalar(entry) ? [String(entry.value)] : []);
|
|
61
|
+
}
|
|
62
|
+
function isText$1(value) {
|
|
63
|
+
return String(value) === value;
|
|
64
|
+
}
|
|
65
|
+
function expressionsOf(job) {
|
|
66
|
+
const expressions = [];
|
|
67
|
+
visit(job, { Scalar(key, node, path) {
|
|
68
|
+
if (key === "key") return;
|
|
69
|
+
const text = node.value;
|
|
70
|
+
if (!isText$1(text)) return;
|
|
71
|
+
const parent = path.at(-1);
|
|
72
|
+
if (isPair(parent) && isScalar(parent.key) && parent.key.value === "if") expressions.push(text);
|
|
73
|
+
else expressions.push(...text.match(/\$\{\{[\s\S]*?\}\}/g) ?? []);
|
|
74
|
+
} });
|
|
75
|
+
return expressions;
|
|
76
|
+
}
|
|
77
|
+
function referencedIds(job) {
|
|
78
|
+
return expressionsOf(job).flatMap((expression) => [...expression.matchAll(NEEDS_REFERENCE)].map(([, dotted, , quoted]) => dotted ?? quoted));
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Lists every `needs:` entry naming no job and every `needs.<id>` or `needs['<id>']` read in a job's `if:` or `${{ }}`
|
|
82
|
+
* expressions whose id is not in that job's `needs:`. GitHub evaluates such a read to null, so a gate condition like
|
|
83
|
+
* `needs.checks.result == 'skipped'` silently stops failing once the job it named was renamed.
|
|
84
|
+
*/
|
|
85
|
+
function staleNeeds(document, path) {
|
|
86
|
+
const jobs = document.get("jobs", true);
|
|
87
|
+
if (!isMap(jobs)) return [];
|
|
88
|
+
const ids = new Set(jobs.items.flatMap(({ key }) => isScalar(key) ? [String(key.value)] : []));
|
|
89
|
+
const problems = [];
|
|
90
|
+
for (const { key, value: job } of jobs.items) {
|
|
91
|
+
if (!isScalar(key) || !isMap(job)) continue;
|
|
92
|
+
const name = String(key.value);
|
|
93
|
+
const needed = neededIds(job);
|
|
94
|
+
for (const id of needed) if (!ids.has(id)) problems.push(`${path}: job ${name} needs ${id}, but no job ${id} exists. Replace it with the job id it was renamed to (the ci-node.yml caller is ci), then rerun antelopejs-sync --check.`);
|
|
95
|
+
const listed = `[${needed.join(", ")}]`;
|
|
96
|
+
for (const id of new Set(referencedIds(job))) if (!needed.includes(id)) problems.push(`${path}: job ${name} reads needs.${id}, but ${id} is not in its needs: ${listed}, so the read is always null. Read needs.<id> of a job in that list (the ci-node.yml caller is needs.ci), or add ${id} to needs, then rerun antelopejs-sync --check.`);
|
|
97
|
+
}
|
|
98
|
+
return problems;
|
|
99
|
+
}
|
|
100
|
+
//#endregion
|
|
56
101
|
//#region src/guardrails/ci-workflow.ts
|
|
57
102
|
const CI_PATH = ".github/workflows/ci.yml";
|
|
58
103
|
const MUTATION_PATH = ".github/workflows/mutation.yml";
|
|
@@ -206,6 +251,38 @@ function skipRenovateSync(document, job) {
|
|
|
206
251
|
else job.set("with", document.createNode({ "renovate-sync": false }));
|
|
207
252
|
return true;
|
|
208
253
|
}
|
|
254
|
+
function lines(text) {
|
|
255
|
+
return text.split("\n").map((line) => line.trim()).filter(Boolean);
|
|
256
|
+
}
|
|
257
|
+
function normalizePath(path) {
|
|
258
|
+
return path.replace(/^(?:\.\/)+/, "").replace(/\/+$/, "");
|
|
259
|
+
}
|
|
260
|
+
function mergedDocsFiles(current, plan) {
|
|
261
|
+
const computed = new Set(plan.docsFiles);
|
|
262
|
+
const kept = current.map(normalizePath).filter((path) => path && !computed.has(path) && plan.exists(path));
|
|
263
|
+
return [.../* @__PURE__ */ new Set([...plan.docsFiles, ...kept])];
|
|
264
|
+
}
|
|
265
|
+
function setDocsFiles(document, job, plan) {
|
|
266
|
+
const inputs = job.get("with", true);
|
|
267
|
+
const current = isMap(inputs) ? inputs.get("docs-files", true) : void 0;
|
|
268
|
+
const existing = isScalar(current) ? lines(String(current.value)) : [];
|
|
269
|
+
const files = mergedDocsFiles(existing, plan);
|
|
270
|
+
if (isScalar(current) && existing.join("\n") === files.join("\n")) return false;
|
|
271
|
+
const value = new Scalar(`${files.join("\n")}\n`);
|
|
272
|
+
value.type = Scalar.BLOCK_LITERAL;
|
|
273
|
+
if (isMap(inputs)) inputs.set("docs-files", value);
|
|
274
|
+
else job.set("with", document.createNode({ "docs-files": value }));
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
function setCallersDocsFiles(document, jobs, callers, plan) {
|
|
278
|
+
let changed = false;
|
|
279
|
+
if (!plan?.docsFiles.length) return false;
|
|
280
|
+
for (const { id } of callers) {
|
|
281
|
+
const job = jobs.get(id, true);
|
|
282
|
+
if (isMap(job)) changed = setDocsFiles(document, job, plan) || changed;
|
|
283
|
+
}
|
|
284
|
+
return changed;
|
|
285
|
+
}
|
|
209
286
|
function canonicalSyncJob() {
|
|
210
287
|
const job = parseDocument(CI_CALLER).getIn(["jobs", RENOVATE_SYNC_JOB_ID], true);
|
|
211
288
|
if (!isMap(job)) throw new Error("CI_CALLER has no renovate-sync job");
|
|
@@ -236,8 +313,10 @@ function assertNoClash(jobs, callerId, hasSyncCaller) {
|
|
|
236
313
|
* dispatchable and read-only, with the Renovate sync it would otherwise run turned off. The write permissions the sync
|
|
237
314
|
* needs to push and dispatch the caller live on a separate job calling ci-renovate-sync.yml, which is added when missing.
|
|
238
315
|
* No `uses:` ref is ever rewritten. A workflow with no caller job is repository-specific CI and is left as it is.
|
|
316
|
+
* When `docsFiles` computes paths, every caller's `docs-files` input lists them first, then keeps its hand-listed paths
|
|
317
|
+
* that still exist, so sync never drops coverage.
|
|
239
318
|
*/
|
|
240
|
-
function ciWorkflow(current, callers, syncCallers) {
|
|
319
|
+
function ciWorkflow(current, callers, syncCallers, docsFiles) {
|
|
241
320
|
if (current === void 0) return CI_CALLER;
|
|
242
321
|
if (!callers.length) return current;
|
|
243
322
|
const document = parseDocument(current);
|
|
@@ -248,7 +327,7 @@ function ciWorkflow(current, callers, syncCallers) {
|
|
|
248
327
|
assertNoClash(jobs, caller.id, syncCallers.length > 0);
|
|
249
328
|
const pair = jobs.items.find(({ key }) => isScalar(key) && key.value === caller.id);
|
|
250
329
|
if (!pair || !isMap(pair.value)) return current;
|
|
251
|
-
let changed =
|
|
330
|
+
let changed = setCallersDocsFiles(document, jobs, callers, docsFiles);
|
|
252
331
|
if (caller.named) {
|
|
253
332
|
pair.value.delete("name");
|
|
254
333
|
changed = true;
|
|
@@ -266,6 +345,8 @@ function ciWorkflow(current, callers, syncCallers) {
|
|
|
266
345
|
addDispatch(document);
|
|
267
346
|
changed = true;
|
|
268
347
|
}
|
|
348
|
+
const stale = staleNeeds(document, CI_PATH);
|
|
349
|
+
if (stale.length) throw new Error(stale.join("\n"));
|
|
269
350
|
if (!changed) return current;
|
|
270
351
|
blockOverflowingFlow(document);
|
|
271
352
|
return document.toString({
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import "./ceilings-
|
|
2
|
-
import { n as IGNORE_PATTERNS, t as AGENT_IGNORE_PATTERNS } from "./shared-
|
|
1
|
+
import "./ceilings-B7KayYIG.mjs";
|
|
2
|
+
import { n as IGNORE_PATTERNS, t as AGENT_IGNORE_PATTERNS } from "./shared-CoUt4GUm.mjs";
|
|
3
3
|
import { closeSync, existsSync, lstatSync, openSync, readSync } from "node:fs";
|
|
4
4
|
import { dirname, join, matchesGlob, relative, resolve } from "node:path";
|
|
5
5
|
import { execFileSync, spawnSync } from "node:child_process";
|
|
@@ -21,37 +21,41 @@ function list(value, path) {
|
|
|
21
21
|
if (!Array.isArray(value)) throw new DecodeError(`${path}: expected a list`);
|
|
22
22
|
return value;
|
|
23
23
|
}
|
|
24
|
+
/** The field's value when the payload has it as an own key; never one inherited from `Object.prototype`. */
|
|
25
|
+
function field(source, key) {
|
|
26
|
+
return Object.hasOwn(source, key) ? source[key] : void 0;
|
|
27
|
+
}
|
|
24
28
|
function str(source, key) {
|
|
25
|
-
const value = source
|
|
29
|
+
const value = field(source, key);
|
|
26
30
|
if (value === void 0 || !isString(value)) throw new DecodeError(`${key}: expected a string`);
|
|
27
31
|
return value;
|
|
28
32
|
}
|
|
29
33
|
function optStr(source, key) {
|
|
30
|
-
const value = source
|
|
34
|
+
const value = field(source, key);
|
|
31
35
|
return value !== void 0 && isString(value) ? value : void 0;
|
|
32
36
|
}
|
|
33
37
|
function num(source, key) {
|
|
34
|
-
const value = source
|
|
38
|
+
const value = field(source, key);
|
|
35
39
|
if (value === void 0 || !isNumber(value)) throw new DecodeError(`${key}: expected a number`);
|
|
36
40
|
return value;
|
|
37
41
|
}
|
|
38
42
|
function optNum(source, key) {
|
|
39
|
-
const value = source
|
|
43
|
+
const value = field(source, key);
|
|
40
44
|
return value !== void 0 && isNumber(value) ? value : void 0;
|
|
41
45
|
}
|
|
42
46
|
function flag(source, key) {
|
|
43
|
-
return source
|
|
47
|
+
return field(source, key) === true;
|
|
44
48
|
}
|
|
45
49
|
function child(source, key) {
|
|
46
|
-
const value = source
|
|
50
|
+
const value = field(source, key);
|
|
47
51
|
return value === void 0 ? {} : object(value, key);
|
|
48
52
|
}
|
|
49
53
|
function children(source, key) {
|
|
50
|
-
const value = source
|
|
54
|
+
const value = field(source, key);
|
|
51
55
|
return value === void 0 ? [] : list(value, key).map((entry) => object(entry, key));
|
|
52
56
|
}
|
|
53
57
|
function strings(source, key) {
|
|
54
|
-
const value = source
|
|
58
|
+
const value = field(source, key);
|
|
55
59
|
return value === void 0 ? [] : list(value, key).filter((entry) => isString(entry));
|
|
56
60
|
}
|
|
57
61
|
function time(source, key) {
|
|
@@ -908,18 +908,57 @@ const noReflectGetRule = defineRule({
|
|
|
908
908
|
}
|
|
909
909
|
});
|
|
910
910
|
//#endregion
|
|
911
|
-
//#region src/oxc/anti-slop/
|
|
911
|
+
//#region src/oxc/anti-slop/shared/type-guards.ts
|
|
912
|
+
/** Files whose functions may declare a type predicate in JSDoc instead of a TypeScript return type. */
|
|
913
|
+
const JSDOC_PREDICATE_FILE = /\.m?js$/;
|
|
914
|
+
/** A `@returns {x is T}` type, `x` captured: `asserts x is T` does not match. */
|
|
915
|
+
const JSDOC_PREDICATE = /^\s*([A-Za-z_$][\w$]*)\s+is\s+\S/;
|
|
912
916
|
function isRuntimeFunction(node) {
|
|
913
917
|
return node.type === "ArrowFunctionExpression" || node.type === "FunctionDeclaration" || node.type === "FunctionExpression";
|
|
914
918
|
}
|
|
915
|
-
function
|
|
919
|
+
/** Return the statement or member a JSDoc block documenting this function is attached to. */
|
|
920
|
+
function documentedNode(fn) {
|
|
921
|
+
const parent = fn.parent;
|
|
922
|
+
if ((parent.type === "Property" || parent.type === "MethodDefinition") && parent.value === fn) return parent;
|
|
923
|
+
let node = fn;
|
|
924
|
+
if (parent.type === "VariableDeclarator" && parent.init === fn && parent.parent.type === "VariableDeclaration" && parent.parent.declarations.length === 1) node = parent.parent;
|
|
925
|
+
const container = node.parent;
|
|
926
|
+
return container !== null && (container.type === "ExportNamedDeclaration" || container.type === "ExportDefaultDeclaration") ? container : node;
|
|
927
|
+
}
|
|
928
|
+
/** Return the type inside the braces of the first `@returns {...}` tag of a JSDoc block. */
|
|
929
|
+
function jsDocReturnType(text) {
|
|
930
|
+
const tag = /@returns\s*\{/.exec(text);
|
|
931
|
+
if (tag === null) return null;
|
|
932
|
+
const start = tag.index + tag[0].length;
|
|
933
|
+
let depth = 1;
|
|
934
|
+
for (let index = start; index < text.length; index++) if (text[index] === "{") depth++;
|
|
935
|
+
else if (text[index] === "}" && --depth === 0) return text.slice(start, index);
|
|
936
|
+
return null;
|
|
937
|
+
}
|
|
938
|
+
/** Return whether the JSDoc block right before the function declares `@returns {param is T}` for one of its parameters. */
|
|
939
|
+
function hasJsDocTypePredicate(fn, sourceCode) {
|
|
940
|
+
const comment = sourceCode.getCommentsBefore(documentedNode(fn)).at(-1);
|
|
941
|
+
if (comment === void 0 || comment.type !== "Block" || !comment.value.startsWith("*")) return false;
|
|
942
|
+
const returnType = jsDocReturnType(comment.value);
|
|
943
|
+
const subject = returnType === null ? void 0 : JSDOC_PREDICATE.exec(returnType)?.[1];
|
|
944
|
+
return subject !== void 0 && fn.params.some((parameter) => parameter.type === "Identifier" && parameter.name === subject);
|
|
945
|
+
}
|
|
946
|
+
/** Return whether a function declares a type predicate or assertion, or a JSDoc `@returns {x is T}` in a `.js`/`.mjs` file. */
|
|
947
|
+
function isTypeGuard(fn, sourceCode, filename) {
|
|
948
|
+
if (fn.returnType?.typeAnnotation.type === "TSTypePredicate") return true;
|
|
949
|
+
return JSDOC_PREDICATE_FILE.test(filename) && hasJsDocTypePredicate(fn, sourceCode);
|
|
950
|
+
}
|
|
951
|
+
/** Return the nearest runtime function around a node when that function is a type guard. */
|
|
952
|
+
function enclosingTypeGuard(node, sourceCode, filename) {
|
|
916
953
|
let current = node.parent;
|
|
917
954
|
while (current !== null && current.type !== "Program") {
|
|
918
|
-
if (isRuntimeFunction(current)) return current
|
|
955
|
+
if (isRuntimeFunction(current)) return isTypeGuard(current, sourceCode, filename) ? current : null;
|
|
919
956
|
current = current.parent;
|
|
920
957
|
}
|
|
921
|
-
return
|
|
958
|
+
return null;
|
|
922
959
|
}
|
|
960
|
+
//#endregion
|
|
961
|
+
//#region src/oxc/anti-slop/rules/no-runtime-typeof.ts
|
|
923
962
|
/** Return whether typeof safely probes for the existence of a possibly absent binding. */
|
|
924
963
|
function isExistenceProbe(node) {
|
|
925
964
|
const parent = node.parent;
|
|
@@ -933,12 +972,37 @@ function isExistenceProbe(node) {
|
|
|
933
972
|
const other = parent.left === node ? parent.right : parent.left;
|
|
934
973
|
return other.type === "Literal" && other.value === "undefined";
|
|
935
974
|
}
|
|
975
|
+
/** Built-ins whose `.constructor` comparison re-implements a `typeof` result. */
|
|
976
|
+
const TYPEOF_CONSTRUCTORS = /* @__PURE__ */ new Set([
|
|
977
|
+
"String",
|
|
978
|
+
"Number",
|
|
979
|
+
"Boolean",
|
|
980
|
+
"BigInt",
|
|
981
|
+
"Symbol",
|
|
982
|
+
"Function"
|
|
983
|
+
]);
|
|
984
|
+
/** Return the built-in name when the comparison is `x.constructor === String` or its mirror. */
|
|
985
|
+
function constructorCheckTarget(node) {
|
|
986
|
+
if (![
|
|
987
|
+
"===",
|
|
988
|
+
"!==",
|
|
989
|
+
"==",
|
|
990
|
+
"!="
|
|
991
|
+
].includes(node.operator)) return void 0;
|
|
992
|
+
for (const [operand, other] of [[node.left, node.right], [node.right, node.left]]) {
|
|
993
|
+
const side = operand.type === "ChainExpression" ? operand.expression : operand;
|
|
994
|
+
if (side.type === "MemberExpression" && !side.computed && side.property.type === "Identifier" && side.property.name === "constructor" && other.type === "Identifier" && TYPEOF_CONSTRUCTORS.has(other.name)) return other.name;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
936
997
|
/** Disallow runtime typeof checks that narrow unparsed values instead of decoding them. */
|
|
937
998
|
const noRuntimeTypeofRule = defineRule({
|
|
938
999
|
meta: {
|
|
939
1000
|
type: "problem",
|
|
940
1001
|
docs: { description: "Disallow runtime typeof checks; external values must be decoded into meaningful types at their I/O boundary." },
|
|
941
|
-
messages: {
|
|
1002
|
+
messages: {
|
|
1003
|
+
runtimeTypeof: "A `typeof` check narrows a representation without establishing its contract. Parse input at its I/O boundary, then branch on the domain value.",
|
|
1004
|
+
constructorCheck: "`.constructor === {{name}}` is a `typeof` check in another form. Parse input at its I/O boundary, or move the check into a type guard such as `(value: unknown): value is string`."
|
|
1005
|
+
},
|
|
942
1006
|
schema: [{
|
|
943
1007
|
type: "object",
|
|
944
1008
|
properties: { allowInTypeGuards: { type: "boolean" } },
|
|
@@ -947,14 +1011,26 @@ const noRuntimeTypeofRule = defineRule({
|
|
|
947
1011
|
defaultOptions: [{ allowInTypeGuards: false }]
|
|
948
1012
|
},
|
|
949
1013
|
createOnce(context) {
|
|
950
|
-
|
|
1014
|
+
function allowedInTypeGuard(node) {
|
|
951
1015
|
const option = context.options?.[0];
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
1016
|
+
return typeof option === "object" && option !== null && !Array.isArray(option) && option.allowInTypeGuards === true && enclosingTypeGuard(node, context.sourceCode, context.filename) !== null;
|
|
1017
|
+
}
|
|
1018
|
+
return {
|
|
1019
|
+
UnaryExpression(node) {
|
|
1020
|
+
if (node.operator === "typeof" && !isExistenceProbe(node) && !allowedInTypeGuard(node)) context.report({
|
|
1021
|
+
node,
|
|
1022
|
+
messageId: "runtimeTypeof"
|
|
1023
|
+
});
|
|
1024
|
+
},
|
|
1025
|
+
BinaryExpression(node) {
|
|
1026
|
+
const name = constructorCheckTarget(node);
|
|
1027
|
+
if (name !== void 0 && !allowedInTypeGuard(node)) context.report({
|
|
1028
|
+
node,
|
|
1029
|
+
messageId: "constructorCheck",
|
|
1030
|
+
data: { name }
|
|
1031
|
+
});
|
|
1032
|
+
}
|
|
1033
|
+
};
|
|
958
1034
|
}
|
|
959
1035
|
});
|
|
960
1036
|
//#endregion
|
|
@@ -998,11 +1074,59 @@ function isTypePredicateSubject(owner, parameterName) {
|
|
|
998
1074
|
const predicate = owner.returnType?.typeAnnotation;
|
|
999
1075
|
return predicate?.type === "TSTypePredicate" && predicate.parameterName.type === "Identifier" && predicate.parameterName.name === parameterName;
|
|
1000
1076
|
}
|
|
1077
|
+
/** Parameter decorators that inject a request value: interface-api's `JSONBody` and `Parameter`, and dms-lang's `JsonBody`. The decorated parameter is the I/O boundary the value is decoded at. */
|
|
1078
|
+
const BOUNDARY_PARAMETER_DECORATORS = /* @__PURE__ */ new Set([
|
|
1079
|
+
"JSONBody",
|
|
1080
|
+
"JsonBody",
|
|
1081
|
+
"Parameter"
|
|
1082
|
+
]);
|
|
1083
|
+
function decoratorName(decorator) {
|
|
1084
|
+
const target = decorator.expression.type === "CallExpression" ? decorator.expression.callee : decorator.expression;
|
|
1085
|
+
return target.type === "Identifier" ? target.name : null;
|
|
1086
|
+
}
|
|
1087
|
+
function hasBoundaryDecorator(parameter) {
|
|
1088
|
+
return (parameter.decorators ?? []).some((decorator) => {
|
|
1089
|
+
const name = decoratorName(decorator);
|
|
1090
|
+
return name !== null && BOUNDARY_PARAMETER_DECORATORS.has(name);
|
|
1091
|
+
});
|
|
1092
|
+
}
|
|
1093
|
+
function isThisParameter(parameter) {
|
|
1094
|
+
return parameter.type === "Identifier" && parameter.name === "this";
|
|
1095
|
+
}
|
|
1096
|
+
/** Return whether a function is passed directly as argument `position` of a `.method(...)` call. */
|
|
1097
|
+
function isMethodArgument(owner, method, position) {
|
|
1098
|
+
const call = owner.parent;
|
|
1099
|
+
return call.type === "CallExpression" && call.arguments[position] === owner && call.callee.type === "MemberExpression" && !call.callee.computed && call.callee.property.type === "Identifier" && call.callee.property.name === method;
|
|
1100
|
+
}
|
|
1101
|
+
/** Return whether a callback is written directly as `.catch(onRejected)` or `.then(_, onRejected)`. */
|
|
1102
|
+
function isRejectionCallback(owner) {
|
|
1103
|
+
return isRuntimeFunction(owner) && (isMethodArgument(owner, "catch", 0) || isMethodArgument(owner, "then", 1));
|
|
1104
|
+
}
|
|
1105
|
+
/** Return whether a binding is the second parameter of a `new Promise(...)` executor. */
|
|
1106
|
+
function isPromiseExecutorReject(binding) {
|
|
1107
|
+
const executor = binding.parent;
|
|
1108
|
+
if (executor === null || !isRuntimeFunction(executor) || executor.params[1] !== binding) return false;
|
|
1109
|
+
const construction = executor.parent;
|
|
1110
|
+
return construction.type === "NewExpression" && construction.arguments[0] === executor && construction.callee.type === "Identifier" && construction.callee.name === "Promise";
|
|
1111
|
+
}
|
|
1112
|
+
/** Return whether a `(reason: unknown) => void` type is declared as a promise's `reject` or typed on a `new Promise` executor's reject parameter. */
|
|
1113
|
+
function isRejectFunctionType(owner) {
|
|
1114
|
+
if (owner.type !== "TSFunctionType" || owner.params.length !== 1 || owner.returnType.typeAnnotation.type !== "TSVoidKeyword" || owner.parent.type !== "TSTypeAnnotation") return false;
|
|
1115
|
+
const holder = owner.parent.parent;
|
|
1116
|
+
if (holder.type === "TSPropertySignature" || holder.type === "PropertyDefinition") return !holder.computed && holder.key.type === "Identifier" && holder.key.name === "reject";
|
|
1117
|
+
return holder.type === "Identifier" && (holder.name === "reject" || isPromiseExecutorReject(holder));
|
|
1118
|
+
}
|
|
1119
|
+
/** Return whether a parameter is exempt whatever its name: `this`, a boundary-decorated request value, or a promise rejection reason. */
|
|
1120
|
+
function isExemptPosition(owner, parameter) {
|
|
1121
|
+
if (isThisParameter(parameter) || hasBoundaryDecorator(parameter)) return true;
|
|
1122
|
+
if (owner.params[0] === parameter && isRejectionCallback(owner)) return true;
|
|
1123
|
+
return isRejectFunctionType(owner);
|
|
1124
|
+
}
|
|
1001
1125
|
/** Disallow unknown inputs except explicitly named error-cause enrichment. */
|
|
1002
1126
|
const noUnknownParametersRule = defineRule({
|
|
1003
1127
|
meta: {
|
|
1004
1128
|
type: "problem",
|
|
1005
|
-
docs: { description: "Disallow explicitly unknown function parameters except `cause
|
|
1129
|
+
docs: { description: "Disallow explicitly unknown function parameters except `cause`, type-predicate subjects, `this`, request values injected by `@JSONBody`, `@JsonBody` or `@Parameter`, and promise rejection reasons; decode unknown input at its I/O boundary instead." },
|
|
1006
1130
|
messages: { unknownParameter: "Parameter `{{parameter}}` leaves input unparsed. Accept a named domain type; run the expected schema or parser at the I/O boundary before calling this function." }
|
|
1007
1131
|
},
|
|
1008
1132
|
createOnce(context) {
|
|
@@ -1011,6 +1135,7 @@ const noUnknownParametersRule = defineRule({
|
|
|
1011
1135
|
const annotation = functionParameterTypeAnnotation(parameter);
|
|
1012
1136
|
if (annotation === null || annotation === void 0) continue;
|
|
1013
1137
|
if (!containsUnknownType(annotation.typeAnnotation)) continue;
|
|
1138
|
+
if (isExemptPosition(node, parameter)) continue;
|
|
1014
1139
|
const name = functionParameterBindingName(parameter, context.sourceCode);
|
|
1015
1140
|
if (name === "cause" || isTypePredicateSubject(node, name)) continue;
|
|
1016
1141
|
context.report({
|
|
@@ -1157,6 +1282,27 @@ function isTypeNode(node) {
|
|
|
1157
1282
|
function typeReferenceName$1(type) {
|
|
1158
1283
|
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
1159
1284
|
}
|
|
1285
|
+
/** Return whether a type is literally `Record<string, unknown>`. */
|
|
1286
|
+
function isStringUnknownRecord(node) {
|
|
1287
|
+
if (node.type !== "TSTypeReference" || typeReferenceName$1(node) !== "Record") return false;
|
|
1288
|
+
const params = node.typeArguments?.params ?? [];
|
|
1289
|
+
const [key, value] = params;
|
|
1290
|
+
return params.length === 2 && key?.type === "TSStringKeyword" && value?.type === "TSUnknownKeyword";
|
|
1291
|
+
}
|
|
1292
|
+
/** Return whether a type is the whole target of a `value is T` predicate. */
|
|
1293
|
+
function isTypePredicateTarget(node) {
|
|
1294
|
+
const annotation = node.parent;
|
|
1295
|
+
return annotation.type === "TSTypeAnnotation" && annotation.parent.type === "TSTypePredicate" && !annotation.parent.asserts;
|
|
1296
|
+
}
|
|
1297
|
+
/** Return whether a type is written in the body of its nearest function, and that function is a type guard. */
|
|
1298
|
+
function isInsideTypeGuardBody(node, sourceCode, filename) {
|
|
1299
|
+
const body = enclosingTypeGuard(node, sourceCode, filename)?.body;
|
|
1300
|
+
return body !== void 0 && body !== null && node.start >= body.start && node.end <= body.end;
|
|
1301
|
+
}
|
|
1302
|
+
/** `Record<string, unknown>` is the narrowing a type guard establishes before it reads keys, so it passes as a predicate target or inside a guard body. */
|
|
1303
|
+
function isTypeGuardRecord(node, sourceCode, filename) {
|
|
1304
|
+
return isStringUnknownRecord(node) && (isTypePredicateTarget(node) || isInsideTypeGuardBody(node, sourceCode, filename));
|
|
1305
|
+
}
|
|
1160
1306
|
function isInsideTypeAliasDeclaration(node) {
|
|
1161
1307
|
let current = node.parent;
|
|
1162
1308
|
while (current !== null && current.type !== "Program") {
|
|
@@ -1209,6 +1355,7 @@ const noUnsafeDictionaryTypeRule = defineRule({
|
|
|
1209
1355
|
};
|
|
1210
1356
|
const reportIfUnsafe = (node) => {
|
|
1211
1357
|
if (environment === null || !shouldReportType(node, environment)) return;
|
|
1358
|
+
if (isTypeGuardRecord(node, context.sourceCode, context.filename)) return;
|
|
1212
1359
|
const unsafe = classifyUnsafeDictionary(node, environment);
|
|
1213
1360
|
if (unsafe === null) return;
|
|
1214
1361
|
report(node, unsafe.unsafeValue);
|
package/dist/oxc/fmt.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { n as TEST_FILE_GLOBS, t as ASSERTION_FUNCTION_NAMES } from "../../shared-BG0Nkzh0.mjs";
|
|
2
2
|
//#region src/oxc/guardrails/index.d.ts
|
|
3
|
-
/** AntelopeJS rules that reject comments and low-evidence tests. */
|
|
3
|
+
/** AntelopeJS rules that reject comments, inherited-key lookups, presence-only type guards and low-evidence tests. */
|
|
4
4
|
declare const guardrailsPlugin: import("@oxlint/plugins").Plugin;
|
|
5
5
|
//#endregion
|
|
6
6
|
export { ASSERTION_FUNCTION_NAMES, TEST_FILE_GLOBS, guardrailsPlugin as default };
|