@antelopejs/tooling-configs 0.5.8 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -49
- package/dist/{api-CCOiLN3w.mjs → api-BQLrI_S1.mjs} +19 -2
- package/dist/bin/api-diff.mjs +155 -37
- package/dist/bin/body-check.mjs +6 -3
- package/dist/bin/check-size.mjs +1 -1
- package/dist/bin/checks.mjs +1 -1
- package/dist/bin/comment-proof.mjs +3 -5
- package/dist/bin/garden.mjs +86 -34
- package/dist/bin/lease.mjs +314 -26
- package/dist/bin/ratchet.mjs +4 -4
- package/dist/bin/readme-strip.mjs +1 -1
- package/dist/bin/suppressions.mjs +10 -6
- package/dist/bin/sync.mjs +23 -98
- package/dist/bin/triage.mjs +1 -1
- package/dist/bin/verify.mjs +1 -1
- package/dist/{checks-BC-F8bsv.mjs → checks-BefrkjiT.mjs} +729 -25
- package/dist/{docs-compat-Ft1r2sf0.mjs → docs-compat-CSljVi3O.mjs} +13 -8
- package/dist/eslint.d.mts +13 -4
- package/dist/eslint.mjs +48 -6
- package/dist/guardrails-BfGwehVj.mjs +2414 -0
- package/dist/installed-typescript-6MtIWlE4.mjs +84 -0
- package/dist/oxc/anti-slop/index.mjs +541 -34
- package/dist/oxc/guardrails/index.d.mts +1 -1
- package/dist/oxc/guardrails/index.mjs +2 -1854
- package/dist/oxc/lint.d.mts +10 -1
- package/dist/oxc/lint.mjs +13 -5
- package/dist/{oxlint-CCOJqj3d.mjs → oxlint-CkHhK5K_.mjs} +33 -11
- package/dist/{package-DnxZ2CXZ.mjs → package-DDG3YH7W.mjs} +10 -5
- package/dist/{ports-DGP6bPJy.mjs → ports-DAeEJsYh.mjs} +22 -0
- package/dist/{proof-stamp-lyD84zmj.mjs → proof-stamp-BFV7JTZT.mjs} +5 -4
- package/dist/{ratchet-SQ7vTR0i.mjs → ratchet-2FeMs1kh.mjs} +4 -3
- package/dist/{suppression-base-whhvw-IJ.mjs → suppression-base-CYaLZAq7.mjs} +2 -2
- package/dist/{suppression-budget-CtIYSoDO.mjs → suppression-budget-CPDSFg4X.mjs} +1 -1
- package/dist/{suppressions-C79kRvYI.mjs → suppressions-CxPnh4sC.mjs} +2 -2
- package/dist/{templates-Dg188oRC.mjs → templates-HG0fel2S.mjs} +1 -1
- package/dist/{type-aware-rules-DMuN6sHb.mjs → type-aware-rules-Da3769fI.mjs} +9 -1
- package/dist/{typescript--_8og8AM.mjs → typescript-CxfNxyr2.mjs} +59 -98
- package/dist/{workflow-CPcNc8KL.mjs → workflow-C-oBwL2H.mjs} +25 -1
- package/package.json +9 -5
- package/dist/bin/check-interface-ranges.d.mts +0 -1
- package/dist/bin/check-interface-ranges.mjs +0 -44
- package/dist/interface-ranges-CcVSr59f.mjs +0 -116
|
@@ -0,0 +1,2414 @@
|
|
|
1
|
+
import { a as matcherChain, i as isAssertion, o as memberName, r as callName, s as testCallback } from "./shared-WC0z3y9F.mjs";
|
|
2
|
+
import { r as installedTypeScriptMajor, t as asInstalledCompilerReads } from "./installed-typescript-6MtIWlE4.mjs";
|
|
3
|
+
import { isBuiltin } from "node:module";
|
|
4
|
+
import { existsSync, lstatSync, readFileSync, readdirSync, realpathSync } from "node:fs";
|
|
5
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
6
|
+
import { defineRule, eslintCompatPlugin } from "@oxlint/plugins";
|
|
7
|
+
import ts from "@typescript/typescript6";
|
|
8
|
+
//#region src/oxc/guardrails/rules/no-comments.ts
|
|
9
|
+
const allowedCommentForms = [
|
|
10
|
+
{
|
|
11
|
+
type: "Shebang",
|
|
12
|
+
pattern: /^/u
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
type: "Block",
|
|
16
|
+
pattern: /^[*!]/u,
|
|
17
|
+
raw: true
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
type: "Line",
|
|
21
|
+
pattern: /^\/\s*<reference\s[^>]*\/\s*>\s*$/u
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
type: null,
|
|
25
|
+
pattern: /^(?:oxlint|eslint)-enable(?:\s|$)/u
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
type: null,
|
|
29
|
+
pattern: /^(?:oxlint|eslint)-disable(?:-line|-next-line)?\s+(?!-)[\w@/]/u
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
type: null,
|
|
33
|
+
pattern: /^@ts-expect-error(?:\s*:\s*|\s+)(?!:)\S/u
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
type: null,
|
|
37
|
+
pattern: /^SAFETY:/u,
|
|
38
|
+
safety: true
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
type: null,
|
|
42
|
+
pattern: /^(?:#|@)__PURE__\s*$/u
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
type: null,
|
|
46
|
+
pattern: /^@vite-ignore\b/u
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
type: null,
|
|
50
|
+
pattern: /^webpack(?:ChunkName|Prefetch|Preload|Mode|Ignore|Exports|Include|Exclude)\s*:/u
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
type: null,
|
|
54
|
+
pattern: /^(?:prettier|oxfmt)-ignore\s*$/u
|
|
55
|
+
}
|
|
56
|
+
];
|
|
57
|
+
const disableDirective = /^(?:oxlint|eslint)-disable(?:-line|-next-line)?(?:\s|$)/u;
|
|
58
|
+
/** Ordinary comments must become executable constraints or documentation on a public item. */
|
|
59
|
+
const noCommentsRule = defineRule({
|
|
60
|
+
meta: {
|
|
61
|
+
type: "problem",
|
|
62
|
+
docs: { description: "Disallow comments except documentation and explicit compiler or safety directives." },
|
|
63
|
+
messages: {
|
|
64
|
+
comment: "Comments rot. Encode this constraint in a type, a lint rule, or a test, or write TSDoc (`/** */`) on the public item it documents.",
|
|
65
|
+
unnamedDisable: "Name the rule(s) to disable and give a reason, for example: oxlint-disable-next-line no-debugger -- debugging this operation."
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
createOnce(context) {
|
|
69
|
+
return { Program() {
|
|
70
|
+
let safetyLine;
|
|
71
|
+
for (const comment of context.sourceCode.getAllComments()) {
|
|
72
|
+
const text = comment.value.replace(/^\s*\*?\s*/u, "").trimEnd();
|
|
73
|
+
const form = allowedCommentForms.find((allowed) => (allowed.type === null || allowed.type === comment.type) && allowed.pattern.test("raw" in allowed ? comment.value : text));
|
|
74
|
+
const continuesSafety = comment.type === "Line" && safetyLine !== void 0 && comment.loc.start.line === safetyLine.loc.end.line + 1 && /^\s*$/u.test(context.sourceCode.text.slice(safetyLine.end, comment.start));
|
|
75
|
+
safetyLine = comment.type === "Line" && (continuesSafety || form !== void 0 && "safety" in form) ? comment : void 0;
|
|
76
|
+
const unnamedDisable = disableDirective.test(text) && form === void 0;
|
|
77
|
+
if (!unnamedDisable && (form !== void 0 || continuesSafety)) continue;
|
|
78
|
+
context.report({
|
|
79
|
+
loc: comment.loc,
|
|
80
|
+
messageId: unnamedDisable ? "unnamedDisable" : "comment"
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
} };
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/oxc/guardrails/rules/no-constant-value-assertion.ts
|
|
88
|
+
const equalityMatchers = /* @__PURE__ */ new Set([
|
|
89
|
+
"toBe",
|
|
90
|
+
"toEqual",
|
|
91
|
+
"toStrictEqual",
|
|
92
|
+
"to.equal",
|
|
93
|
+
"to.eq",
|
|
94
|
+
"eql",
|
|
95
|
+
"to.eql",
|
|
96
|
+
"to.deep.eq",
|
|
97
|
+
"to.deep.equal",
|
|
98
|
+
"to.be.equal"
|
|
99
|
+
]);
|
|
100
|
+
const constantName = /^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)*$/u;
|
|
101
|
+
function isConstant(node) {
|
|
102
|
+
if (node?.type === "Identifier") return constantName.test(node.name);
|
|
103
|
+
if (node?.type !== "MemberExpression") return false;
|
|
104
|
+
return isConstant(node.object);
|
|
105
|
+
}
|
|
106
|
+
function isLiteralValue(node) {
|
|
107
|
+
if (node.type === "Literal") return !("regex" in node) && !("bigint" in node);
|
|
108
|
+
if (node.type === "TemplateLiteral") return node.expressions.length === 0;
|
|
109
|
+
if (node.type === "UnaryExpression") return node.operator === "-" && node.argument.type === "Literal" && Number.isFinite(node.argument.value);
|
|
110
|
+
if (node.type === "ArrayExpression") return node.elements.every((element) => element !== null && isLiteralValue(element));
|
|
111
|
+
if (node.type === "ObjectExpression") return node.properties.every((property) => property.type === "Property" && !property.computed && !property.method && !property.shorthand && property.kind === "init" && isLiteralValue(property.value));
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
/** Constant declarations already specify their values; tests must assert the behavior they drive. */
|
|
115
|
+
const noConstantValueAssertionRule = defineRule({
|
|
116
|
+
meta: {
|
|
117
|
+
type: "problem",
|
|
118
|
+
docs: { description: "Disallow assertions that only restate a constant's literal value." },
|
|
119
|
+
messages: { constant: "A test that restates a constant fails only when someone edits the constant on purpose. Assert the behaviour the constant drives." }
|
|
120
|
+
},
|
|
121
|
+
createOnce(context) {
|
|
122
|
+
return { CallExpression(node) {
|
|
123
|
+
const chain = matcherChain(node);
|
|
124
|
+
const expected = node.arguments[0];
|
|
125
|
+
if (chain !== null && equalityMatchers.has(chain.matcher) && isConstant(chain.expect.arguments[0]) && expected !== void 0 && isLiteralValue(expected)) context.report({
|
|
126
|
+
node,
|
|
127
|
+
messageId: "constant"
|
|
128
|
+
});
|
|
129
|
+
} };
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/oxc/guardrails/rules/no-fs-read-in-tests.ts
|
|
134
|
+
const fsModules = /* @__PURE__ */ new Set(["fs", "node:fs"]);
|
|
135
|
+
const readMethods = /* @__PURE__ */ new Set(["readFileSync", "readdirSync"]);
|
|
136
|
+
function isFsRequire(node) {
|
|
137
|
+
return node.type === "CallExpression" && node.callee.type === "Identifier" && node.callee.name === "require" && node.arguments[0]?.type === "Literal" && fsModules.has(String(node.arguments[0].value));
|
|
138
|
+
}
|
|
139
|
+
function isFsNamespace(source, node) {
|
|
140
|
+
if (isFsRequire(node)) return true;
|
|
141
|
+
if (node.type !== "Identifier") return false;
|
|
142
|
+
let scope = source.getScope(node);
|
|
143
|
+
while (scope !== null) {
|
|
144
|
+
const binding = scope.set.get(node.name);
|
|
145
|
+
if (binding !== void 0) return binding.defs.some((definition) => {
|
|
146
|
+
if (definition.node.type === "TSImportEqualsDeclaration") {
|
|
147
|
+
const reference = definition.node.moduleReference;
|
|
148
|
+
return reference.type === "TSExternalModuleReference" && fsModules.has(reference.expression.value);
|
|
149
|
+
}
|
|
150
|
+
if (definition.type === "ImportBinding") return (definition.node.type !== "ImportSpecifier" || definition.node.imported.type === "Identifier" && definition.node.imported.name === "default") && definition.parent?.type === "ImportDeclaration" && fsModules.has(definition.parent.source.value);
|
|
151
|
+
return definition.node.type === "VariableDeclarator" && definition.node.init !== null && isFsRequire(definition.node.init);
|
|
152
|
+
});
|
|
153
|
+
scope = scope.upper;
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
/** Tests exercise behavior through public interfaces rather than reading implementation text. */
|
|
158
|
+
const noFsReadInTestsRule = defineRule({
|
|
159
|
+
meta: {
|
|
160
|
+
type: "problem",
|
|
161
|
+
docs: { description: "Move synchronous filesystem reads out of tests into fixture helpers." },
|
|
162
|
+
messages: { fsRead: "Tests exercise behaviour through the public interface. A test that reads source files asserts text, not behaviour. Move fixture reads into a helper under fixtures/, helpers/ or locales/." }
|
|
163
|
+
},
|
|
164
|
+
createOnce(context) {
|
|
165
|
+
return {
|
|
166
|
+
ImportDeclaration(node) {
|
|
167
|
+
if (!fsModules.has(node.source.value) || node.importKind === "type") return;
|
|
168
|
+
for (const specifier of node.specifiers) {
|
|
169
|
+
if (specifier.type !== "ImportSpecifier" || specifier.importKind === "type") continue;
|
|
170
|
+
const name = specifier.imported.type === "Identifier" ? specifier.imported.name : specifier.imported.value;
|
|
171
|
+
if (readMethods.has(name)) context.report({
|
|
172
|
+
node: specifier,
|
|
173
|
+
messageId: "fsRead"
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
MemberExpression(node) {
|
|
178
|
+
const name = memberName(node);
|
|
179
|
+
if (name !== null && readMethods.has(name) && isFsNamespace(context.sourceCode, node.object)) context.report({
|
|
180
|
+
node,
|
|
181
|
+
messageId: "fsRead"
|
|
182
|
+
});
|
|
183
|
+
},
|
|
184
|
+
VariableDeclarator(node) {
|
|
185
|
+
if (node.id.type !== "ObjectPattern" || node.init === null || !isFsNamespace(context.sourceCode, node.init)) return;
|
|
186
|
+
for (const property of node.id.properties) {
|
|
187
|
+
if (property.type !== "Property") continue;
|
|
188
|
+
const name = property.key.type === "Identifier" && !property.computed ? property.key.name : property.key.type === "Literal" ? String(property.key.value) : null;
|
|
189
|
+
if (name !== null && readMethods.has(name)) context.report({
|
|
190
|
+
node: property,
|
|
191
|
+
messageId: "fsRead"
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
//#endregion
|
|
199
|
+
//#region src/oxc/guardrails/local-types.ts
|
|
200
|
+
/** Collects top-level and exported type declarations; local ones inside functions are not resolved. */
|
|
201
|
+
function collectLocalTypes(program) {
|
|
202
|
+
const types = {
|
|
203
|
+
aliases: /* @__PURE__ */ new Map(),
|
|
204
|
+
interfaces: /* @__PURE__ */ new Map()
|
|
205
|
+
};
|
|
206
|
+
for (const statement of program.body) {
|
|
207
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
208
|
+
if (declaration?.type === "TSTypeAliasDeclaration") types.aliases.set(declaration.id.name, declaration.typeAnnotation);
|
|
209
|
+
if (declaration?.type === "TSInterfaceDeclaration") {
|
|
210
|
+
const merged = types.interfaces.get(declaration.id.name) ?? [];
|
|
211
|
+
merged.push(declaration);
|
|
212
|
+
types.interfaces.set(declaration.id.name, merged);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return types;
|
|
216
|
+
}
|
|
217
|
+
/** The type reference's name when it is a plain identifier, such as `Record` in `Record<string, T>`. */
|
|
218
|
+
function referenceName(type) {
|
|
219
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
220
|
+
}
|
|
221
|
+
/** Strips parentheses and non-null assertions, which change neither the value nor its prototype. */
|
|
222
|
+
function unwrapExpression(node) {
|
|
223
|
+
let current = node;
|
|
224
|
+
while (current.type === "ParenthesizedExpression" || current.type === "TSNonNullExpression") current = current.expression;
|
|
225
|
+
return current;
|
|
226
|
+
}
|
|
227
|
+
/** The variable an identifier refers to, found by walking out from its scope. */
|
|
228
|
+
function resolveVariable(source, identifier) {
|
|
229
|
+
let scope = source.getScope(identifier);
|
|
230
|
+
while (scope !== null) {
|
|
231
|
+
const variable = scope.set.get(identifier.name);
|
|
232
|
+
if (variable !== void 0) return variable;
|
|
233
|
+
scope = scope.upper;
|
|
234
|
+
}
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
/** The nearest function around a node, or null at module level. */
|
|
238
|
+
function enclosingFunction$1(node) {
|
|
239
|
+
let current = node.parent;
|
|
240
|
+
while (current !== null) {
|
|
241
|
+
if (current.type === "ArrowFunctionExpression" || current.type === "FunctionDeclaration" || current.type === "FunctionExpression") return current;
|
|
242
|
+
current = current.parent;
|
|
243
|
+
}
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
/** The predicate a function's return type declares, when it names a parameter (`x is T`, `asserts x is T`). */
|
|
247
|
+
function parameterPredicate(fn) {
|
|
248
|
+
const predicate = fn.returnType?.typeAnnotation;
|
|
249
|
+
return predicate?.type === "TSTypePredicate" && predicate.parameterName.type === "Identifier" ? predicate : null;
|
|
250
|
+
}
|
|
251
|
+
/** The parameter name a predicate narrows, or null for `this` predicates. */
|
|
252
|
+
function predicateName(predicate) {
|
|
253
|
+
return predicate.parameterName.type === "Identifier" ? predicate.parameterName.name : null;
|
|
254
|
+
}
|
|
255
|
+
/** The field `this.<name>` refers to: a property or constructor parameter property of the class around `node`. */
|
|
256
|
+
function classField(node, name) {
|
|
257
|
+
let current = node.parent;
|
|
258
|
+
while (current !== null && current.type !== "ClassBody") {
|
|
259
|
+
if (current.type === "FunctionDeclaration" || current.type === "FunctionExpression" && current.parent.type !== "MethodDefinition") return null;
|
|
260
|
+
current = current.parent;
|
|
261
|
+
}
|
|
262
|
+
if (current === null) return null;
|
|
263
|
+
for (const member of current.body) {
|
|
264
|
+
if (member.type === "PropertyDefinition" && !member.computed && member.key.type === "Identifier" && member.key.name === name) return {
|
|
265
|
+
typeAnnotation: member.typeAnnotation ?? null,
|
|
266
|
+
value: member.value
|
|
267
|
+
};
|
|
268
|
+
if (member.type === "MethodDefinition" && member.kind === "constructor") {
|
|
269
|
+
for (const parameter of member.value.params) if (parameter.type === "TSParameterProperty" && parameter.parameter.type === "Identifier" && parameter.parameter.name === name) return {
|
|
270
|
+
typeAnnotation: parameter.parameter.typeAnnotation ?? null,
|
|
271
|
+
value: null
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
//#endregion
|
|
278
|
+
//#region src/oxc/guardrails/key-provenance.ts
|
|
279
|
+
const OWN_KEY_LISTS = /* @__PURE__ */ new Set(["Object.keys", "Object.getOwnPropertyNames"]);
|
|
280
|
+
const KEY_PRESERVING_METHODS = /* @__PURE__ */ new Set([
|
|
281
|
+
"filter",
|
|
282
|
+
"reverse",
|
|
283
|
+
"slice",
|
|
284
|
+
"sort",
|
|
285
|
+
"toReversed",
|
|
286
|
+
"toSorted"
|
|
287
|
+
]);
|
|
288
|
+
const ITERATING_METHODS = /* @__PURE__ */ new Set([
|
|
289
|
+
"every",
|
|
290
|
+
"filter",
|
|
291
|
+
"find",
|
|
292
|
+
"findIndex",
|
|
293
|
+
"flatMap",
|
|
294
|
+
"forEach",
|
|
295
|
+
"map",
|
|
296
|
+
"some"
|
|
297
|
+
]);
|
|
298
|
+
/** A literal, a template without substitutions, or a `typeof` result: never an `Object.prototype` key. */
|
|
299
|
+
function isStaticKey(key) {
|
|
300
|
+
if (key.type === "Literal") return true;
|
|
301
|
+
if (key.type === "UnaryExpression") return key.operator === "typeof";
|
|
302
|
+
return key.type === "TemplateLiteral" && key.expressions.length === 0;
|
|
303
|
+
}
|
|
304
|
+
function constInitializer(identifier, source) {
|
|
305
|
+
const [definition] = resolveVariable(source, identifier)?.defs ?? [];
|
|
306
|
+
if (definition?.type !== "Variable" || definition.parent?.type !== "VariableDeclaration" || definition.parent.kind !== "const" || definition.node.type !== "VariableDeclarator" || definition.node.init === null) return null;
|
|
307
|
+
let init = definition.node.init;
|
|
308
|
+
while (init.type === "TSAsExpression" || init.type === "TSSatisfiesExpression") init = init.expression;
|
|
309
|
+
return unwrapExpression(init);
|
|
310
|
+
}
|
|
311
|
+
function propertyValue(object, name) {
|
|
312
|
+
for (const property of object.properties) if (property.type === "Property" && !property.computed && property.key.type === "Identifier" && property.key.name === name) return property.value;
|
|
313
|
+
return null;
|
|
314
|
+
}
|
|
315
|
+
/** `const KEY = "a"` or `const LABELS = { a: "x" }` read as `LABELS.a`, declared in this file. */
|
|
316
|
+
function isConstantKey(key, source) {
|
|
317
|
+
if (key.type === "Identifier") {
|
|
318
|
+
const init = constInitializer(key, source);
|
|
319
|
+
return init !== null && isStaticKey(init);
|
|
320
|
+
}
|
|
321
|
+
if (key.type !== "MemberExpression" || key.computed || key.object.type !== "Identifier" || key.property.type !== "Identifier") return false;
|
|
322
|
+
const init = constInitializer(key.object, source);
|
|
323
|
+
const value = init?.type === "ObjectExpression" ? propertyValue(init, key.property.name) : null;
|
|
324
|
+
return value !== null && isStaticKey(value);
|
|
325
|
+
}
|
|
326
|
+
function keyList(node, source, depth = 0) {
|
|
327
|
+
if (depth > 8) return null;
|
|
328
|
+
if (node.type === "Identifier") {
|
|
329
|
+
const init = constInitializer(node, source);
|
|
330
|
+
return init === null ? null : keyList(init, source, depth + 1);
|
|
331
|
+
}
|
|
332
|
+
if (node.type === "ArrayExpression") return node.elements.every((element) => element !== null && isStaticKey(element)) ? { kind: "literal" } : null;
|
|
333
|
+
if (node.type !== "CallExpression") return null;
|
|
334
|
+
const name = callName(node.callee);
|
|
335
|
+
const [first] = node.arguments;
|
|
336
|
+
if (first !== void 0 && OWN_KEY_LISTS.has(name ?? "")) return {
|
|
337
|
+
kind: "keys",
|
|
338
|
+
table: source.getText(first)
|
|
339
|
+
};
|
|
340
|
+
if (first !== void 0 && name === "Object.entries") return {
|
|
341
|
+
kind: "entries",
|
|
342
|
+
table: source.getText(first)
|
|
343
|
+
};
|
|
344
|
+
const callee = node.callee;
|
|
345
|
+
return callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && KEY_PRESERVING_METHODS.has(callee.property.name) ? keyList(callee.object, source, depth + 1) : null;
|
|
346
|
+
}
|
|
347
|
+
/** The pattern that binds a key: the whole binding, or the first element of `[key, value]`. */
|
|
348
|
+
function keyBinding(pattern, name) {
|
|
349
|
+
if (pattern === name) return "plain";
|
|
350
|
+
return pattern.type === "ArrayPattern" && pattern.elements[0] === name ? "entry" : null;
|
|
351
|
+
}
|
|
352
|
+
/** The list a key is iterated from, with how it is bound, or a `for...in` loop. */
|
|
353
|
+
function keySource(definition) {
|
|
354
|
+
if (definition.type === "Parameter") {
|
|
355
|
+
const fn = definition.node;
|
|
356
|
+
const call = fn.parent;
|
|
357
|
+
if (fn.type !== "ArrowFunctionExpression" && fn.type !== "FunctionExpression" || call?.type !== "CallExpression" || call.arguments[0] !== fn || call.callee.type !== "MemberExpression" || call.callee.computed || call.callee.property.type !== "Identifier" || !ITERATING_METHODS.has(call.callee.property.name) || fn.params[0] === void 0) return null;
|
|
358
|
+
return {
|
|
359
|
+
list: call.callee.object,
|
|
360
|
+
binding: keyBinding(fn.params[0], definition.name)
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
const loop = definition.parent?.parent;
|
|
364
|
+
if (definition.node.type !== "VariableDeclarator") return null;
|
|
365
|
+
if (loop?.type === "ForInStatement") return {
|
|
366
|
+
list: loop,
|
|
367
|
+
binding: "plain"
|
|
368
|
+
};
|
|
369
|
+
return loop?.type === "ForOfStatement" ? {
|
|
370
|
+
list: loop.right,
|
|
371
|
+
binding: keyBinding(definition.node.id, definition.name)
|
|
372
|
+
} : null;
|
|
373
|
+
}
|
|
374
|
+
function isOwnKeyBinding(key, table, source) {
|
|
375
|
+
if (key.type !== "Identifier") return false;
|
|
376
|
+
const [definition] = resolveVariable(source, key)?.defs ?? [];
|
|
377
|
+
const found = definition === void 0 ? null : keySource(definition);
|
|
378
|
+
if (found === null || found.binding === null) return false;
|
|
379
|
+
if (found.list.type === "ForInStatement") return source.getText(found.list.right) === table;
|
|
380
|
+
const list = keyList(found.list, source);
|
|
381
|
+
if (list === null) return false;
|
|
382
|
+
if (list.kind === "literal") return found.binding === "plain";
|
|
383
|
+
const binding = list.kind === "keys" ? "plain" : "entry";
|
|
384
|
+
return list.table === table && found.binding === binding;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Keys the rule can prove are not inherited: static keys, same-file constants, and keys iterated
|
|
388
|
+
* from string literals or from `Object.keys`, `Object.entries` or `for...in` of the same table,
|
|
389
|
+
* through `filter`, `sort` and similar key-preserving calls. Imported constants are not resolved.
|
|
390
|
+
*/
|
|
391
|
+
function isKnownKey(key, table, source) {
|
|
392
|
+
return isStaticKey(key) || isConstantKey(key, source) || isOwnKeyBinding(key, table, source);
|
|
393
|
+
}
|
|
394
|
+
/** `Object.prototype.hasOwnProperty.call`, `{}.hasOwnProperty.call` and other `<x>.hasOwnProperty.call` forms. */
|
|
395
|
+
function isHasOwnPropertyCall(callee) {
|
|
396
|
+
return callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && callee.property.name === "call" && callee.object.type === "MemberExpression" && !callee.object.computed && callee.object.property.type === "Identifier" && callee.object.property.name === "hasOwnProperty";
|
|
397
|
+
}
|
|
398
|
+
//#endregion
|
|
399
|
+
//#region src/oxc/guardrails/own-key-guard.ts
|
|
400
|
+
const HAS_OWN_CALLS = /* @__PURE__ */ new Set(["Object.hasOwn", "hasOwnProperty.call"]);
|
|
401
|
+
const EXITS = /* @__PURE__ */ new Set([
|
|
402
|
+
"BreakStatement",
|
|
403
|
+
"ContinueStatement",
|
|
404
|
+
"ReturnStatement",
|
|
405
|
+
"ThrowStatement"
|
|
406
|
+
]);
|
|
407
|
+
function checksOwnKey(node, read) {
|
|
408
|
+
const [first, second] = node.arguments;
|
|
409
|
+
if (first === void 0) return false;
|
|
410
|
+
const text = (argument) => read.source.getText(argument);
|
|
411
|
+
if (HAS_OWN_CALLS.has(callName(node.callee) ?? "") || isHasOwnPropertyCall(node.callee)) return second !== void 0 && text(first) === read.table && text(second) === read.key;
|
|
412
|
+
const callee = node.callee;
|
|
413
|
+
return callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && callee.property.name === "hasOwnProperty" && text(callee.object) === read.table && text(first) === read.key;
|
|
414
|
+
}
|
|
415
|
+
const EQUALITY = /* @__PURE__ */ new Set(["==", "==="]);
|
|
416
|
+
const INEQUALITY = /* @__PURE__ */ new Set(["!=", "!=="]);
|
|
417
|
+
const MAX_DEPTH$2 = 8;
|
|
418
|
+
const ASSERTS = /* @__PURE__ */ new Set([
|
|
419
|
+
"assert",
|
|
420
|
+
"assert.ok",
|
|
421
|
+
"assert.strict"
|
|
422
|
+
]);
|
|
423
|
+
/** The initializer of a `const` the identifier names, such as `has` in `const has = Object.hasOwn(t, k)`. */
|
|
424
|
+
function constValue(node, read) {
|
|
425
|
+
const [definition] = resolveVariable(read.source, node)?.defs ?? [];
|
|
426
|
+
if (definition?.type !== "Variable" || definition.parent?.type !== "VariableDeclaration" || definition.parent.kind !== "const" || definition.node.type !== "VariableDeclarator") return null;
|
|
427
|
+
return definition.node.init;
|
|
428
|
+
}
|
|
429
|
+
/** `x === true`, `x !== false` and their mirrors: the outcome `x` has when the comparison has `outcome`. */
|
|
430
|
+
function comparedOutcome(node, outcome) {
|
|
431
|
+
const equal = EQUALITY.has(node.operator);
|
|
432
|
+
if (!equal && !INEQUALITY.has(node.operator)) return null;
|
|
433
|
+
const [literal, operand] = node.right.type === "Literal" ? [node.right, node.left] : [node.left, node.right];
|
|
434
|
+
if (literal.type !== "Literal") return null;
|
|
435
|
+
const value = literal.value;
|
|
436
|
+
if (value !== true && value !== false) return null;
|
|
437
|
+
return {
|
|
438
|
+
operand,
|
|
439
|
+
outcome: outcome === equal === value
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
/** A value that is `true` or `false`, so comparing it with a boolean literal tests its truth: a call, a `!`, or a `const` of one. */
|
|
443
|
+
function isBooleanValued(node, read, depth) {
|
|
444
|
+
if (depth > MAX_DEPTH$2) return false;
|
|
445
|
+
if (node.type === "ParenthesizedExpression") return isBooleanValued(node.expression, read, depth);
|
|
446
|
+
if (node.type === "Identifier") {
|
|
447
|
+
const value = constValue(node, read);
|
|
448
|
+
return value !== null && isBooleanValued(value, read, depth + 1);
|
|
449
|
+
}
|
|
450
|
+
return node.type === "CallExpression" || node.type === "UnaryExpression" && node.operator === "!";
|
|
451
|
+
}
|
|
452
|
+
/** `Object.getOwnPropertyDescriptor(Object.prototype, key)?.value` or `Object.prototype[key]`: the value `Object.prototype` itself holds under the key. */
|
|
453
|
+
function isRootValue(node, read) {
|
|
454
|
+
const value = node.type === "ChainExpression" ? node.expression : node;
|
|
455
|
+
if (value.type !== "MemberExpression") return false;
|
|
456
|
+
const text = (part) => read.source.getText(part);
|
|
457
|
+
if (value.computed) return text(value.object) === "Object.prototype" && text(value.property) === read.key;
|
|
458
|
+
const call = value.object;
|
|
459
|
+
return value.property.type === "Identifier" && value.property.name === "value" && call.type === "CallExpression" && callName(call.callee) === "Object.getOwnPropertyDescriptor" && call.arguments.length === 2 && text(call.arguments[0]) === "Object.prototype" && text(call.arguments[1]) === read.key;
|
|
460
|
+
}
|
|
461
|
+
/** The outcome of `table[key] === <root value>` (or `!==`, either order) under which the read differs from `Object.prototype`'s own value, or `null` for another comparison. */
|
|
462
|
+
function differsFromRootWhen(node, read) {
|
|
463
|
+
const equal = EQUALITY.has(node.operator);
|
|
464
|
+
if (!equal && !INEQUALITY.has(node.operator)) return null;
|
|
465
|
+
const reads = (side) => side.type === "MemberExpression" && side.computed && read.source.getText(side.object) === read.table && read.source.getText(side.property) === read.key;
|
|
466
|
+
return reads(node.left) && isRootValue(node.right, read) || reads(node.right) && isRootValue(node.left, read) ? !equal : null;
|
|
467
|
+
}
|
|
468
|
+
/** `a && b` that is true, or `a || b` that is false, needs one operand to hold; `a || b` that is true, or `a && b` that is false, needs both. */
|
|
469
|
+
function combinedHolds(node, outcome, read, depth) {
|
|
470
|
+
const left = holds$1(node.left, outcome, read, depth);
|
|
471
|
+
const right = holds$1(node.right, outcome, read, depth);
|
|
472
|
+
if (node.operator === (outcome ? "&&" : "||")) return left || right;
|
|
473
|
+
return node.operator === (outcome ? "||" : "&&") && left && right;
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Whether the key is safe to read whenever `node` evaluates to `outcome`: the table owns the key,
|
|
477
|
+
* the key is no `Object.prototype` name, or the table answers it with something other than
|
|
478
|
+
* `Object.prototype`'s own value.
|
|
479
|
+
*/
|
|
480
|
+
function holds$1(node, outcome, read, depth = 0) {
|
|
481
|
+
if (depth > MAX_DEPTH$2) return false;
|
|
482
|
+
switch (node.type) {
|
|
483
|
+
case "ParenthesizedExpression": return holds$1(node.expression, outcome, read, depth);
|
|
484
|
+
case "CallExpression": return outcome ? checksOwnKey(node, read) : checksOwnKey(node, {
|
|
485
|
+
...read,
|
|
486
|
+
table: "Object.prototype"
|
|
487
|
+
});
|
|
488
|
+
case "Identifier": {
|
|
489
|
+
const value = constValue(node, read);
|
|
490
|
+
return value !== null && holds$1(value, outcome, read, depth + 1);
|
|
491
|
+
}
|
|
492
|
+
case "UnaryExpression": return node.operator === "!" && holds$1(node.argument, !outcome, read, depth);
|
|
493
|
+
case "BinaryExpression": {
|
|
494
|
+
if (differsFromRootWhen(node, read) === outcome) return true;
|
|
495
|
+
const compared = comparedOutcome(node, outcome);
|
|
496
|
+
return compared !== null && isBooleanValued(compared.operand, read, depth) && holds$1(compared.operand, compared.outcome, read, depth);
|
|
497
|
+
}
|
|
498
|
+
case "LogicalExpression": return combinedHolds(node, outcome, read, depth);
|
|
499
|
+
default: return false;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
function exits(statement) {
|
|
503
|
+
if (statement.type === "BlockStatement") {
|
|
504
|
+
const last = statement.body.at(-1);
|
|
505
|
+
return last !== void 0 && exits(last);
|
|
506
|
+
}
|
|
507
|
+
if (statement.type === "IfStatement") return statement.alternate !== null && exits(statement.consequent) && exits(statement.alternate);
|
|
508
|
+
return EXITS.has(statement.type);
|
|
509
|
+
}
|
|
510
|
+
/** Whether a statement ends by storing `table[key]`, which makes the key own. */
|
|
511
|
+
function storesKey(statement, read) {
|
|
512
|
+
if (statement.type === "BlockStatement") {
|
|
513
|
+
const last = statement.body.at(-1);
|
|
514
|
+
return last !== void 0 && storesKey(last, read);
|
|
515
|
+
}
|
|
516
|
+
if (statement.type !== "ExpressionStatement") return false;
|
|
517
|
+
const assignment = statement.expression;
|
|
518
|
+
return assignment.type === "AssignmentExpression" && assignment.operator === "=" && assignment.left.type === "MemberExpression" && assignment.left.computed && read.source.getText(assignment.left.object) === read.table && read.source.getText(assignment.left.property) === read.key;
|
|
519
|
+
}
|
|
520
|
+
function settles(statement, read) {
|
|
521
|
+
return exits(statement) || storesKey(statement, read);
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* A statement after which the key is own: an `assert` of the check, or an `if` whose branch where
|
|
525
|
+
* the check fails leaves the block or stores the key.
|
|
526
|
+
*/
|
|
527
|
+
function establishes(statement, read) {
|
|
528
|
+
if (statement.type === "ExpressionStatement" && statement.expression.type === "CallExpression") {
|
|
529
|
+
const [condition] = statement.expression.arguments;
|
|
530
|
+
return ASSERTS.has(callName(statement.expression.callee) ?? "") && condition !== void 0 && holds$1(condition, true, read);
|
|
531
|
+
}
|
|
532
|
+
if (statement.type !== "IfStatement") return false;
|
|
533
|
+
if (settles(statement.consequent, read) && holds$1(statement.test, false, read)) return true;
|
|
534
|
+
return statement.alternate !== null && settles(statement.alternate, read) && holds$1(statement.test, true, read);
|
|
535
|
+
}
|
|
536
|
+
function statementsOf(node) {
|
|
537
|
+
if (node.type === "BlockStatement" || node.type === "Program" || node.type === "StaticBlock") return node.body;
|
|
538
|
+
return node.type === "SwitchCase" ? node.consequent : null;
|
|
539
|
+
}
|
|
540
|
+
function guardsChild(parent, child, read) {
|
|
541
|
+
if (parent.type === "ConditionalExpression" || parent.type === "IfStatement") {
|
|
542
|
+
if (child === parent.consequent) return holds$1(parent.test, true, read);
|
|
543
|
+
return child === parent.alternate && holds$1(parent.test, false, read);
|
|
544
|
+
}
|
|
545
|
+
if (parent.type === "LogicalExpression" && child === parent.right) return (parent.operator === "&&" || parent.operator === "||") && holds$1(parent.left, parent.operator === "&&", read);
|
|
546
|
+
if (parent.type === "WhileStatement") return child === parent.body && holds$1(parent.test, true, read);
|
|
547
|
+
if (parent.type === "ForStatement") return (child === parent.body || child === parent.update) && parent.test !== null && holds$1(parent.test, true, read);
|
|
548
|
+
const statements = statementsOf(parent);
|
|
549
|
+
if (statements === null) return false;
|
|
550
|
+
const index = statements.indexOf(child);
|
|
551
|
+
return statements.slice(0, Math.max(index, 0)).some((statement) => establishes(statement, read));
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Whether `node`, a read of `table[key]`, cannot answer a key only `Object.prototype` holds. It is
|
|
555
|
+
* compared with `Object.prototype`'s own value of the key, or a check guards it: an own-key check of
|
|
556
|
+
* `table` and `key` (`Object.hasOwn`, `hasOwnProperty.call`, `table.hasOwnProperty`), a failed
|
|
557
|
+
* `Object.prototype` membership check of `key`, or the read differing from `Object.prototype`'s
|
|
558
|
+
* value, directly, negated, combined with `&&`/`||`, compared with a boolean or held in a `const`.
|
|
559
|
+
* The check guards it when it holds in the branch of a conditional, `if`, `&&`/`||`, `while` or
|
|
560
|
+
* `for` that holds `node`, or an earlier statement asserts it, or leaves the block or stores the
|
|
561
|
+
* key where it fails.
|
|
562
|
+
*/
|
|
563
|
+
function isOwnKeyGuarded(node, table, key, source) {
|
|
564
|
+
const read = {
|
|
565
|
+
table,
|
|
566
|
+
key,
|
|
567
|
+
source
|
|
568
|
+
};
|
|
569
|
+
if (node.parent?.type === "BinaryExpression" && differsFromRootWhen(node.parent, read) !== null) return true;
|
|
570
|
+
let child = node;
|
|
571
|
+
let parent = node.parent;
|
|
572
|
+
while (parent !== null) {
|
|
573
|
+
if (guardsChild(parent, child, read)) return true;
|
|
574
|
+
child = parent;
|
|
575
|
+
parent = parent.parent;
|
|
576
|
+
}
|
|
577
|
+
return false;
|
|
578
|
+
}
|
|
579
|
+
//#endregion
|
|
580
|
+
//#region src/oxc/guardrails/rules/no-inherited-key-lookup.ts
|
|
581
|
+
const WRAPPER_TYPES = /* @__PURE__ */ new Set([
|
|
582
|
+
"Readonly",
|
|
583
|
+
"Partial",
|
|
584
|
+
"Required"
|
|
585
|
+
]);
|
|
586
|
+
const MAX_ALIAS_DEPTH$1 = 8;
|
|
587
|
+
function isStringKey(type) {
|
|
588
|
+
if (type.type === "TSStringKeyword") return true;
|
|
589
|
+
if (type.type === "TSUnionType") return type.types.some(isStringKey);
|
|
590
|
+
return type.type === "TSTypeReference" && referenceName(type) === "PropertyKey";
|
|
591
|
+
}
|
|
592
|
+
function hasStringIndex(members) {
|
|
593
|
+
return members.some((member) => member.type === "TSIndexSignature" && member.parameters.some((parameter) => isStringKey(parameter.typeAnnotation.typeAnnotation)));
|
|
594
|
+
}
|
|
595
|
+
function isStringKeyedReference(type, environment, depth) {
|
|
596
|
+
const name = referenceName(type);
|
|
597
|
+
if (name === null) return false;
|
|
598
|
+
const alias = environment.types.aliases.get(name);
|
|
599
|
+
if (alias !== void 0) return isStringKeyedType(alias, environment, depth + 1);
|
|
600
|
+
const declarations = environment.types.interfaces.get(name);
|
|
601
|
+
if (declarations !== void 0) return declarations.some((declaration) => hasStringIndex(declaration.body.body));
|
|
602
|
+
const [first] = type.typeArguments?.params ?? [];
|
|
603
|
+
if (first === void 0) return false;
|
|
604
|
+
if (name === "Record") return isStringKey(first);
|
|
605
|
+
return WRAPPER_TYPES.has(name) && isStringKeyedType(first, environment, depth + 1);
|
|
606
|
+
}
|
|
607
|
+
function isStringKeyedType(type, environment, depth = 0) {
|
|
608
|
+
if (depth > MAX_ALIAS_DEPTH$1) return false;
|
|
609
|
+
switch (type.type) {
|
|
610
|
+
case "TSParenthesizedType": return isStringKeyedType(type.typeAnnotation, environment, depth);
|
|
611
|
+
case "TSTypeLiteral": return hasStringIndex(type.members);
|
|
612
|
+
case "TSMappedType": return isStringKey(type.constraint);
|
|
613
|
+
case "TSIntersectionType":
|
|
614
|
+
case "TSUnionType": return type.types.some((part) => isStringKeyedType(part, environment, depth));
|
|
615
|
+
case "TSTypeReference": return isStringKeyedReference(type, environment, depth);
|
|
616
|
+
case "TSTypeQuery": return type.exprName.type === "Identifier" && tableKind(type.exprName, environment, depth + 1) === "dictionary";
|
|
617
|
+
default: return false;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
function isProtoKey(key) {
|
|
621
|
+
return key.type === "Identifier" && key.name === "__proto__" || key.type === "Literal" && key.value === "__proto__";
|
|
622
|
+
}
|
|
623
|
+
function isNullPrototype(init) {
|
|
624
|
+
const value = unwrapExpression(init.type === "TSAsExpression" || init.type === "TSSatisfiesExpression" ? init.expression : init);
|
|
625
|
+
if (value.type === "CallExpression") return callName(value.callee) === "Object.create" && value.arguments[0]?.type === "Literal" && value.arguments[0].value === null;
|
|
626
|
+
return value.type === "ObjectExpression" && value.properties.some((property) => property.type === "Property" && !property.computed && isProtoKey(property.key) && property.value.type === "Literal" && property.value.value === null);
|
|
627
|
+
}
|
|
628
|
+
function initializerKind(init, environment, depth) {
|
|
629
|
+
if (init.type === "TSSatisfiesExpression") return isStringKeyedType(init.typeAnnotation, environment, depth) ? "dictionary" : initializerKind(init.expression, environment, depth);
|
|
630
|
+
if (init.type === "TSAsExpression") {
|
|
631
|
+
if (init.typeAnnotation.type === "TSTypeReference" && referenceName(init.typeAnnotation) === "const") return initializerKind(init.expression, environment, depth);
|
|
632
|
+
return isStringKeyedType(init.typeAnnotation, environment, depth) ? "dictionary" : null;
|
|
633
|
+
}
|
|
634
|
+
return unwrapExpression(init).type === "ObjectExpression" ? "literal" : null;
|
|
635
|
+
}
|
|
636
|
+
function annotationOf(definition) {
|
|
637
|
+
const name = definition.name;
|
|
638
|
+
return name.type === "Identifier" && "typeAnnotation" in name ? name.typeAnnotation?.typeAnnotation ?? void 0 : void 0;
|
|
639
|
+
}
|
|
640
|
+
function variableKind(variable, environment, depth) {
|
|
641
|
+
const [definition] = variable.defs;
|
|
642
|
+
if (definition === void 0) return null;
|
|
643
|
+
const annotation = annotationOf(definition);
|
|
644
|
+
if (definition.type === "Parameter") return annotation !== void 0 && isStringKeyedType(annotation, environment, depth) ? "dictionary" : null;
|
|
645
|
+
if (definition.type !== "Variable" || definition.node.type !== "VariableDeclarator") return null;
|
|
646
|
+
const declared = declaredKind(annotation, definition.node.init, environment, depth);
|
|
647
|
+
if (declared !== null || depth >= MAX_ALIAS_DEPTH$1 || definition.parent?.type !== "VariableDeclaration" || definition.parent.kind !== "const" || definition.node.init === null || !isOpaqueAnnotation(annotation)) return declared;
|
|
648
|
+
return tableKind(widened(definition.node.init), environment, depth + 1);
|
|
649
|
+
}
|
|
650
|
+
/** The value under `as unknown` and `as any` casts, which widen the type without changing the value. */
|
|
651
|
+
function widened(init) {
|
|
652
|
+
let value = init;
|
|
653
|
+
while (value.type === "TSAsExpression" && isOpaqueAnnotation(value.typeAnnotation)) value = value.expression;
|
|
654
|
+
return value;
|
|
655
|
+
}
|
|
656
|
+
/** No annotation, `unknown` or `any`: the alias holds whatever its initializer holds. */
|
|
657
|
+
function isOpaqueAnnotation(annotation) {
|
|
658
|
+
return annotation === void 0 || annotation.type === "TSUnknownKeyword" || annotation.type === "TSAnyKeyword";
|
|
659
|
+
}
|
|
660
|
+
/** The table a field declares, given its type annotation and initializer. */
|
|
661
|
+
function declaredKind(annotation, init, environment, depth) {
|
|
662
|
+
if (init !== null && isNullPrototype(init)) return null;
|
|
663
|
+
if (annotation !== void 0) return isStringKeyedType(annotation, environment, depth) ? "dictionary" : null;
|
|
664
|
+
return init === null ? null : initializerKind(init, environment, depth);
|
|
665
|
+
}
|
|
666
|
+
function fieldKind(node, environment, depth) {
|
|
667
|
+
if (node.computed || node.object.type !== "ThisExpression" || node.property.type !== "Identifier") return null;
|
|
668
|
+
const field = classField(node, node.property.name);
|
|
669
|
+
if (field === null) return null;
|
|
670
|
+
return declaredKind(field.typeAnnotation?.typeAnnotation ?? void 0, field.value, environment, depth);
|
|
671
|
+
}
|
|
672
|
+
function tableKind(node, environment, depth = 0) {
|
|
673
|
+
const value = node.type === "Identifier" ? node : unwrapExpression(node);
|
|
674
|
+
if (value.type === "ConditionalExpression") {
|
|
675
|
+
const kinds = [value.consequent, value.alternate].map((branch) => tableKind(branch, environment, depth));
|
|
676
|
+
if (kinds.includes("dictionary")) return "dictionary";
|
|
677
|
+
return kinds.includes("literal") ? "literal" : null;
|
|
678
|
+
}
|
|
679
|
+
if (value.type === "MemberExpression") return fieldKind(value, environment, depth);
|
|
680
|
+
if (value.type !== "Identifier") return null;
|
|
681
|
+
const variable = resolveVariable(environment.source, value);
|
|
682
|
+
return variable === null ? null : variableKind(variable, environment, depth);
|
|
683
|
+
}
|
|
684
|
+
function isWrite(node) {
|
|
685
|
+
const parent = node.parent;
|
|
686
|
+
if (parent.type === "AssignmentExpression") return parent.left === node && parent.operator === "=";
|
|
687
|
+
if (parent.type === "UnaryExpression") return parent.operator === "delete";
|
|
688
|
+
return (parent.type === "ForInStatement" || parent.type === "ForOfStatement") && parent.left === node;
|
|
689
|
+
}
|
|
690
|
+
function isPredicateKey(node) {
|
|
691
|
+
const fn = enclosingFunction$1(node);
|
|
692
|
+
const predicate = fn === null ? null : parameterPredicate(fn);
|
|
693
|
+
return predicate !== null && node.left.type === "Identifier" && predicateName(predicate) === node.left.name;
|
|
694
|
+
}
|
|
695
|
+
/** The value a `const { [key]: x } = value` declaration or `({ [key]: x } = value)` assignment reads. */
|
|
696
|
+
function destructuredValue(pattern) {
|
|
697
|
+
const parent = pattern.parent;
|
|
698
|
+
if (parent.type === "VariableDeclarator") return parent.id === pattern ? parent.init : null;
|
|
699
|
+
return parent.type === "AssignmentExpression" && parent.left === pattern ? parent.right : null;
|
|
700
|
+
}
|
|
701
|
+
/** A destructured binding nothing reads or exports, such as `_removed` in `const { [key]: _removed, ...rest } = table`. */
|
|
702
|
+
function isDiscarded(value, source) {
|
|
703
|
+
if (value.type !== "Identifier") return false;
|
|
704
|
+
const variable = resolveVariable(source, value);
|
|
705
|
+
return variable !== null && variable.defs.every((definition) => definition.parent?.parent?.type !== "ExportNamedDeclaration") && !variable.references.some((reference) => reference.isRead());
|
|
706
|
+
}
|
|
707
|
+
/** Lookups into string-keyed object tables must not answer inherited `Object.prototype` keys. */
|
|
708
|
+
const noInheritedKeyLookupRule = defineRule({
|
|
709
|
+
meta: {
|
|
710
|
+
type: "problem",
|
|
711
|
+
docs: { description: "Disallow reading a string-keyed object table with a computed key, or testing it with `in`, without an own-key check." },
|
|
712
|
+
messages: {
|
|
713
|
+
lookup: "`{{ table }}` is a string-keyed object table, so `{{ table }}[{{ key }}]` also answers keys inherited from `Object.prototype` such as `constructor` and `toString`. Store the table in a `Map` and call `.get({{ key }})`, check `Object.hasOwn({{ table }}, {{ key }})` before the read, or build the table with a null prototype (`Object.create(null)` or `{ __proto__: null, ... }`).",
|
|
714
|
+
inOperator: "`{{ key }} in {{ table }}` is also true for keys inherited from `Object.prototype` such as `constructor` and `toString`. Use `Object.hasOwn({{ table }}, {{ key }})`, or store the table in a `Map` and call `.has({{ key }})`."
|
|
715
|
+
}
|
|
716
|
+
},
|
|
717
|
+
createOnce(context) {
|
|
718
|
+
let environment = null;
|
|
719
|
+
const report = (candidate) => {
|
|
720
|
+
if (!isOwnKeyGuarded(candidate.node, candidate.table, candidate.key, context.sourceCode)) context.report({
|
|
721
|
+
node: candidate.node,
|
|
722
|
+
messageId: candidate.messageId,
|
|
723
|
+
data: {
|
|
724
|
+
table: candidate.table,
|
|
725
|
+
key: candidate.key
|
|
726
|
+
}
|
|
727
|
+
});
|
|
728
|
+
};
|
|
729
|
+
const lookup = (node, object, key) => {
|
|
730
|
+
if (environment === null) return;
|
|
731
|
+
const kind = tableKind(object, environment);
|
|
732
|
+
if (kind === null || kind === "literal" && key.type !== "TSAsExpression") return;
|
|
733
|
+
const table = context.sourceCode.getText(object);
|
|
734
|
+
if (isKnownKey(key, table, context.sourceCode)) return;
|
|
735
|
+
report({
|
|
736
|
+
node,
|
|
737
|
+
messageId: "lookup",
|
|
738
|
+
table,
|
|
739
|
+
key: context.sourceCode.getText(key)
|
|
740
|
+
});
|
|
741
|
+
};
|
|
742
|
+
return {
|
|
743
|
+
Program(node) {
|
|
744
|
+
environment = {
|
|
745
|
+
source: context.sourceCode,
|
|
746
|
+
types: collectLocalTypes(node)
|
|
747
|
+
};
|
|
748
|
+
},
|
|
749
|
+
MemberExpression(node) {
|
|
750
|
+
if (node.computed && !isWrite(node)) lookup(node, node.object, node.property);
|
|
751
|
+
},
|
|
752
|
+
ObjectPattern(node) {
|
|
753
|
+
const object = destructuredValue(node);
|
|
754
|
+
if (object === null) return;
|
|
755
|
+
for (const property of node.properties) if (property.type === "Property" && property.computed && !isDiscarded(property.value, context.sourceCode)) lookup(property, object, property.key);
|
|
756
|
+
},
|
|
757
|
+
BinaryExpression(node) {
|
|
758
|
+
if (environment === null || node.operator !== "in" || isStaticKey(node.left) || isPredicateKey(node) || tableKind(node.right, environment) === null) return;
|
|
759
|
+
report({
|
|
760
|
+
node,
|
|
761
|
+
messageId: "inOperator",
|
|
762
|
+
table: context.sourceCode.getText(node.right),
|
|
763
|
+
key: context.sourceCode.getText(node.left)
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
});
|
|
769
|
+
//#endregion
|
|
770
|
+
//#region src/oxc/guardrails/rules/no-presence-only-type-guard.ts
|
|
771
|
+
const MAX_ALIAS_DEPTH = 8;
|
|
772
|
+
const OBJECT_PROTOTYPE_KEYS = Object.getOwnPropertyNames(Object.prototype);
|
|
773
|
+
const OPEN_TYPES = /* @__PURE__ */ new Set(["TSUnknownKeyword", "TSAnyKeyword"]);
|
|
774
|
+
const WIDE_TYPES = /* @__PURE__ */ new Set([...OPEN_TYPES, "TSObjectKeyword"]);
|
|
775
|
+
function signatureName(member) {
|
|
776
|
+
if (member.type !== "TSPropertySignature" && member.type !== "TSMethodSignature" || member.computed) return null;
|
|
777
|
+
if (member.key.type === "Identifier") return member.key.name;
|
|
778
|
+
return member.key.type === "Literal" ? String(member.key.value) : null;
|
|
779
|
+
}
|
|
780
|
+
function addSignatures(members, body) {
|
|
781
|
+
for (const member of body) {
|
|
782
|
+
const name = signatureName(member);
|
|
783
|
+
if (name === null) continue;
|
|
784
|
+
members.set(name, member.type === "TSPropertySignature" ? member.typeAnnotation?.typeAnnotation ?? null : null);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
function referenceMembers(type, types, depth) {
|
|
788
|
+
const name = referenceName(type);
|
|
789
|
+
if (name === null) return null;
|
|
790
|
+
const alias = types.aliases.get(name);
|
|
791
|
+
if (alias !== void 0) return declaredMembers(alias, types, depth + 1);
|
|
792
|
+
const declarations = types.interfaces.get(name);
|
|
793
|
+
if (declarations === void 0) return null;
|
|
794
|
+
const members = /* @__PURE__ */ new Map();
|
|
795
|
+
for (const declaration of declarations) addSignatures(members, declaration.body.body);
|
|
796
|
+
return members;
|
|
797
|
+
}
|
|
798
|
+
/** The members a narrowed type declares in this file, or null when the file does not declare them. */
|
|
799
|
+
function declaredMembers(type, types, depth = 0) {
|
|
800
|
+
if (depth > MAX_ALIAS_DEPTH) return null;
|
|
801
|
+
switch (type.type) {
|
|
802
|
+
case "TSParenthesizedType": return declaredMembers(type.typeAnnotation, types, depth);
|
|
803
|
+
case "TSTypeLiteral": {
|
|
804
|
+
const members = /* @__PURE__ */ new Map();
|
|
805
|
+
addSignatures(members, type.members);
|
|
806
|
+
return members;
|
|
807
|
+
}
|
|
808
|
+
case "TSTypeReference": return referenceMembers(type, types, depth);
|
|
809
|
+
case "TSIntersectionType": {
|
|
810
|
+
const parts = type.types.map((part) => declaredMembers(part, types, depth)).filter((part) => part !== null);
|
|
811
|
+
return parts.length === 0 ? null : new Map(parts.flatMap((part) => [...part]));
|
|
812
|
+
}
|
|
813
|
+
default: return null;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
function parameterType(fn, name) {
|
|
817
|
+
for (const parameter of fn.params) {
|
|
818
|
+
const binding = parameter.type === "AssignmentPattern" ? parameter.left : parameter;
|
|
819
|
+
if (binding.type === "Identifier" && binding.name === name) return binding.typeAnnotation?.typeAnnotation ?? void 0;
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
/**
|
|
823
|
+
* A key typed `prefix${string}` cannot name an inherited member when no `Object.prototype` member starts with that prefix.
|
|
824
|
+
* The prefix is the cooked text TypeScript reads, so `\x63onstructor` is `constructor`. A prefix with no cooked text is open.
|
|
825
|
+
*/
|
|
826
|
+
function excludesPrototypeKeys(type) {
|
|
827
|
+
if (type?.type !== "TSTemplateLiteralType") return false;
|
|
828
|
+
const prefix = type.quasis[0]?.value.cooked;
|
|
829
|
+
return prefix !== void 0 && prefix !== null && !OBJECT_PROTOTYPE_KEYS.some((key) => key.startsWith(prefix));
|
|
830
|
+
}
|
|
831
|
+
function isOpenRecord(type) {
|
|
832
|
+
const value = type.typeArguments?.params[1];
|
|
833
|
+
return referenceName(type) === "Record" && value !== void 0 && OPEN_TYPES.has(value.type);
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* A parameter typed `unknown`, `object`, `{}` or an open record carries no member types, so the guard
|
|
837
|
+
* alone vouches for them. A named or inferred parameter type already declares what a discriminant leaves out.
|
|
838
|
+
*/
|
|
839
|
+
function isWide(type) {
|
|
840
|
+
if (type === void 0) return false;
|
|
841
|
+
if (WIDE_TYPES.has(type.type)) return true;
|
|
842
|
+
if (type.type === "TSTypeLiteral") return type.members.length === 0;
|
|
843
|
+
if (type.type === "TSTypeReference") return isOpenRecord(type);
|
|
844
|
+
return type.type === "TSUnionType" && type.types.some(isWide);
|
|
845
|
+
}
|
|
846
|
+
function frameFor(fn) {
|
|
847
|
+
const predicate = parameterPredicate(fn);
|
|
848
|
+
const name = predicate === null ? null : predicateName(predicate);
|
|
849
|
+
const narrowed = predicate?.typeAnnotation?.typeAnnotation;
|
|
850
|
+
if (predicate === null || name === null || narrowed === void 0) return null;
|
|
851
|
+
return {
|
|
852
|
+
name,
|
|
853
|
+
predicate,
|
|
854
|
+
narrowed,
|
|
855
|
+
parameter: parameterType(fn, name),
|
|
856
|
+
aliases: /* @__PURE__ */ new Set([name]),
|
|
857
|
+
reads: [],
|
|
858
|
+
dynamicReads: [],
|
|
859
|
+
presence: []
|
|
860
|
+
};
|
|
861
|
+
}
|
|
862
|
+
function guardDeclarations(program) {
|
|
863
|
+
const guards = /* @__PURE__ */ new Map();
|
|
864
|
+
const record = (name, fn) => {
|
|
865
|
+
const narrowed = parameterPredicate(fn)?.typeAnnotation?.typeAnnotation;
|
|
866
|
+
if (narrowed !== void 0) guards.set(name, narrowed);
|
|
867
|
+
};
|
|
868
|
+
for (const statement of program.body) {
|
|
869
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
870
|
+
if (declaration?.type === "FunctionDeclaration" && declaration.id !== null) record(declaration.id.name, declaration);
|
|
871
|
+
if (declaration?.type !== "VariableDeclaration") continue;
|
|
872
|
+
for (const declarator of declaration.declarations) {
|
|
873
|
+
const init = declarator.init;
|
|
874
|
+
if (declarator.id.type === "Identifier" && (init?.type === "ArrowFunctionExpression" || init?.type === "FunctionExpression")) record(declarator.id.name, init);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
return guards;
|
|
878
|
+
}
|
|
879
|
+
function unwrapAlias(node) {
|
|
880
|
+
let current = unwrapExpression(node);
|
|
881
|
+
while (current.type === "TSAsExpression" || current.type === "TSSatisfiesExpression" || current.type === "TSTypeAssertion") current = unwrapExpression(current.expression);
|
|
882
|
+
return current;
|
|
883
|
+
}
|
|
884
|
+
const PRESENCE_CALLS = /* @__PURE__ */ new Set([
|
|
885
|
+
"Object.hasOwn",
|
|
886
|
+
"hasOwnProperty.call",
|
|
887
|
+
"Reflect.has"
|
|
888
|
+
]);
|
|
889
|
+
/** `Object.hasOwn(x, "k")`, `hasOwnProperty.call(x, "k")` and `Reflect.has(x, "k")` test presence, as `"k" in x` does. */
|
|
890
|
+
function presenceCall(node) {
|
|
891
|
+
const [first, second] = node.arguments;
|
|
892
|
+
if (!PRESENCE_CALLS.has(callName(node.callee) ?? "") && !isHasOwnPropertyCall(node.callee) || first === void 0 || first.type === "SpreadElement" || second?.type !== "Literal") return null;
|
|
893
|
+
const object = unwrapAlias(first);
|
|
894
|
+
return object.type === "Identifier" ? {
|
|
895
|
+
object: object.name,
|
|
896
|
+
key: String(second.value)
|
|
897
|
+
} : null;
|
|
898
|
+
}
|
|
899
|
+
function callReads(node, environment) {
|
|
900
|
+
const objects = node.arguments.flatMap((argument) => argument.type === "Identifier" ? [argument.name] : []);
|
|
901
|
+
const keys = node.arguments.flatMap((argument) => argument.type === "Literal" ? [String(argument.value)] : []);
|
|
902
|
+
const guard = node.callee.type === "Identifier" ? environment.guards.get(node.callee.name) : void 0;
|
|
903
|
+
if (guard !== void 0) keys.push(...declaredMembers(guard, environment.types)?.keys() ?? []);
|
|
904
|
+
return objects.flatMap((object) => keys.map((key) => [object, key]));
|
|
905
|
+
}
|
|
906
|
+
/** Reports the presence-only checks and unread members of one finished guard. */
|
|
907
|
+
function evaluate(frame, env, context) {
|
|
908
|
+
const wide = isWide(frame.parameter);
|
|
909
|
+
if (!wide && frame.narrowed.type !== "TSIntersectionType") return;
|
|
910
|
+
const members = declaredMembers(frame.narrowed, env.types);
|
|
911
|
+
const isRead = (key) => frame.reads.some(([object, read]) => read === key && frame.aliases.has(object));
|
|
912
|
+
const isOpen = (key) => {
|
|
913
|
+
const type = members?.get(key);
|
|
914
|
+
return type !== void 0 && type !== null && OPEN_TYPES.has(type.type);
|
|
915
|
+
};
|
|
916
|
+
const present = /* @__PURE__ */ new Set();
|
|
917
|
+
for (const presence of frame.presence) {
|
|
918
|
+
if (!frame.aliases.has(presence.object)) continue;
|
|
919
|
+
present.add(presence.key);
|
|
920
|
+
if (isRead(presence.key) || isOpen(presence.key)) continue;
|
|
921
|
+
context.report({
|
|
922
|
+
node: presence.node,
|
|
923
|
+
messageId: "presenceOnly",
|
|
924
|
+
data: {
|
|
925
|
+
key: presence.key,
|
|
926
|
+
param: presence.object
|
|
927
|
+
}
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
const dynamic = frame.dynamicReads.some((object) => frame.aliases.has(object));
|
|
931
|
+
if (!wide || dynamic) return;
|
|
932
|
+
for (const member of members?.keys() ?? []) {
|
|
933
|
+
if (isOpen(member) || present.has(member) || isRead(member)) continue;
|
|
934
|
+
context.report({
|
|
935
|
+
node: frame.predicate,
|
|
936
|
+
messageId: "uncheckedMember",
|
|
937
|
+
data: {
|
|
938
|
+
member,
|
|
939
|
+
param: frame.name,
|
|
940
|
+
type: context.sourceCode.getText(frame.narrowed)
|
|
941
|
+
}
|
|
942
|
+
});
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
/** A type predicate must check what it narrows to, not only that a key is present. */
|
|
946
|
+
const noPresenceOnlyTypeGuardRule = defineRule({
|
|
947
|
+
meta: {
|
|
948
|
+
type: "problem",
|
|
949
|
+
docs: { description: "Disallow type predicates that narrow on `in` alone: a key answered by the prototype, or a member whose value is never checked." },
|
|
950
|
+
messages: {
|
|
951
|
+
inheritedKey: "`{{ key }} in ...` is also true for keys inherited from `Object.prototype` such as `constructor` and `toString`, so this guard narrows `{{ key }}` to keys the table does not have. Use `Object.hasOwn(table, {{ key }})`, or keep the keys in a `Map` or `Set` and call `.has({{ key }})`.",
|
|
952
|
+
presenceOnly: "`\"{{ key }}\" in {{ param }}` checks only that `{{ key }}` is present, yet the guard narrows `{{ param }}` to a type that declares its value. Check the value too (`typeof {{ param }}.{{ key }} === \"function\"`, `Array.isArray({{ param }}.{{ key }})`, a nested guard), or parse `{{ param }}` with a schema.",
|
|
953
|
+
uncheckedMember: "This guard narrows `{{ param }}` to `{{ type }}` but never reads `{{ param }}.{{ member }}`, so a value with any `{{ member }}` passes. Check it (`typeof`, `Array.isArray`, a nested guard), narrow to a type without it (`Pick<...>`), or parse `{{ param }}` with a schema."
|
|
954
|
+
}
|
|
955
|
+
},
|
|
956
|
+
createOnce(context) {
|
|
957
|
+
let environment = null;
|
|
958
|
+
const stack = [];
|
|
959
|
+
const active = () => stack.filter((frame) => frame !== null);
|
|
960
|
+
const enter = (fn) => {
|
|
961
|
+
stack.push(frameFor(fn));
|
|
962
|
+
};
|
|
963
|
+
const leave = () => {
|
|
964
|
+
const frame = stack.pop();
|
|
965
|
+
if (frame !== null && frame !== void 0 && environment !== null) evaluate(frame, environment, context);
|
|
966
|
+
};
|
|
967
|
+
return {
|
|
968
|
+
Program(node) {
|
|
969
|
+
environment = {
|
|
970
|
+
types: collectLocalTypes(node),
|
|
971
|
+
guards: guardDeclarations(node)
|
|
972
|
+
};
|
|
973
|
+
stack.length = 0;
|
|
974
|
+
},
|
|
975
|
+
FunctionDeclaration: enter,
|
|
976
|
+
FunctionExpression: enter,
|
|
977
|
+
ArrowFunctionExpression: enter,
|
|
978
|
+
"FunctionDeclaration:exit": leave,
|
|
979
|
+
"FunctionExpression:exit": leave,
|
|
980
|
+
"ArrowFunctionExpression:exit": leave,
|
|
981
|
+
MemberExpression(node) {
|
|
982
|
+
const target = unwrapAlias(node.object);
|
|
983
|
+
if (target.type !== "Identifier") return;
|
|
984
|
+
const object = target.name;
|
|
985
|
+
const key = memberName(node);
|
|
986
|
+
for (const frame of active()) if (key === null) frame.dynamicReads.push(object);
|
|
987
|
+
else frame.reads.push([object, key]);
|
|
988
|
+
},
|
|
989
|
+
CallExpression(node) {
|
|
990
|
+
if (environment === null) return;
|
|
991
|
+
const presence = presenceCall(node);
|
|
992
|
+
if (presence !== null) {
|
|
993
|
+
active().at(-1)?.presence.push({
|
|
994
|
+
node,
|
|
995
|
+
...presence
|
|
996
|
+
});
|
|
997
|
+
return;
|
|
998
|
+
}
|
|
999
|
+
const reads = callReads(node, environment);
|
|
1000
|
+
for (const frame of active()) frame.reads.push(...reads);
|
|
1001
|
+
},
|
|
1002
|
+
VariableDeclarator(node) {
|
|
1003
|
+
if (node.init === null) return;
|
|
1004
|
+
const init = unwrapAlias(node.init);
|
|
1005
|
+
if (init.type !== "Identifier") return;
|
|
1006
|
+
for (const frame of active()) {
|
|
1007
|
+
if (!frame.aliases.has(init.name)) continue;
|
|
1008
|
+
if (node.id.type === "Identifier") frame.aliases.add(node.id.name);
|
|
1009
|
+
if (node.id.type !== "ObjectPattern") continue;
|
|
1010
|
+
for (const property of node.id.properties) if (property.type === "Property" && property.key.type === "Identifier") frame.reads.push([init.name, property.key.name]);
|
|
1011
|
+
}
|
|
1012
|
+
},
|
|
1013
|
+
BinaryExpression(node) {
|
|
1014
|
+
const frame = active().at(-1);
|
|
1015
|
+
if (node.operator !== "in" || frame === void 0) return;
|
|
1016
|
+
if (node.left.type === "Identifier" && node.left.name === frame.name) {
|
|
1017
|
+
if (!excludesPrototypeKeys(frame.parameter)) context.report({
|
|
1018
|
+
node,
|
|
1019
|
+
messageId: "inheritedKey",
|
|
1020
|
+
data: { key: frame.name }
|
|
1021
|
+
});
|
|
1022
|
+
return;
|
|
1023
|
+
}
|
|
1024
|
+
if (node.left.type === "Literal" && node.right.type === "Identifier") frame.presence.push({
|
|
1025
|
+
node,
|
|
1026
|
+
object: node.right.name,
|
|
1027
|
+
key: String(node.left.value)
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
};
|
|
1031
|
+
}
|
|
1032
|
+
});
|
|
1033
|
+
//#endregion
|
|
1034
|
+
//#region src/oxc/guardrails/read-decision.ts
|
|
1035
|
+
const MIRRORED = {
|
|
1036
|
+
"==": "==",
|
|
1037
|
+
"===": "===",
|
|
1038
|
+
"!=": "!=",
|
|
1039
|
+
"!==": "!==",
|
|
1040
|
+
"<": ">",
|
|
1041
|
+
"<=": ">=",
|
|
1042
|
+
">": "<",
|
|
1043
|
+
">=": "<="
|
|
1044
|
+
};
|
|
1045
|
+
const UNKNOWN = {
|
|
1046
|
+
whenTrue: false,
|
|
1047
|
+
whenFalse: false
|
|
1048
|
+
};
|
|
1049
|
+
const FOUND_WHEN_TRUE = {
|
|
1050
|
+
whenTrue: false,
|
|
1051
|
+
whenFalse: true
|
|
1052
|
+
};
|
|
1053
|
+
const EMPTY_WHEN_TRUE = {
|
|
1054
|
+
whenTrue: true,
|
|
1055
|
+
whenFalse: false
|
|
1056
|
+
};
|
|
1057
|
+
/** Whether `node` is `call`, or a call or member chained from it such as `table.get(id).run()`. */
|
|
1058
|
+
function chainsFrom(node, call) {
|
|
1059
|
+
let current = node;
|
|
1060
|
+
while (current !== call) if (current.type === "CallExpression") current = current.callee;
|
|
1061
|
+
else if (current.type === "MemberExpression") current = current.object;
|
|
1062
|
+
else return false;
|
|
1063
|
+
return true;
|
|
1064
|
+
}
|
|
1065
|
+
function unwrap(node) {
|
|
1066
|
+
if (node.type === "AwaitExpression") return unwrap(node.argument);
|
|
1067
|
+
if (node.type === "ChainExpression" || node.type === "ParenthesizedExpression" || node.type === "TSAsExpression" || node.type === "TSNonNullExpression") return unwrap(node.expression);
|
|
1068
|
+
return node;
|
|
1069
|
+
}
|
|
1070
|
+
function isMethodCall(member) {
|
|
1071
|
+
return member.parent?.type === "CallExpression" && member.parent.callee === member;
|
|
1072
|
+
}
|
|
1073
|
+
/** Whether `.length` or an index such as `[0]` reads the settled result: on rows, they still tell whether the read found any. */
|
|
1074
|
+
function isCountOrIndex(member) {
|
|
1075
|
+
return memberName(member) === "length" || member.computed && member.property.type === "Literal" && Number.isInteger(member.property.value);
|
|
1076
|
+
}
|
|
1077
|
+
/** Whether `parent` passes on the read's value. Before the read settles a method call builds the query (`.limit(1).run()`, `.then(...)`); once awaited, only `.length` or an index keeps it, since a field or method of the row is other data. */
|
|
1078
|
+
function carriesValue(node, parent, settled) {
|
|
1079
|
+
if (parent.type === "MemberExpression") return parent.object === node && (settled ? isCountOrIndex(parent) : isMethodCall(parent));
|
|
1080
|
+
return parent.type === "AwaitExpression" || parent.type === "ChainExpression" || parent.type === "ParenthesizedExpression" || parent.type === "TSAsExpression" || parent.type === "TSNonNullExpression" || parent.type === "CallExpression" && parent.callee === node;
|
|
1081
|
+
}
|
|
1082
|
+
/** The outermost expression that still holds a read's value, the node around it, and whether the read settled on the way through `await` or `.then(...)`. */
|
|
1083
|
+
function readValue(call) {
|
|
1084
|
+
let node = call;
|
|
1085
|
+
let parent = node.parent;
|
|
1086
|
+
let settled = false;
|
|
1087
|
+
while (parent !== null && carriesValue(node, parent, settled)) {
|
|
1088
|
+
if (parent.type === "AwaitExpression" || parent.type === "MemberExpression" && memberName(parent) === "then") settled = true;
|
|
1089
|
+
node = parent;
|
|
1090
|
+
parent = node.parent;
|
|
1091
|
+
}
|
|
1092
|
+
return {
|
|
1093
|
+
node,
|
|
1094
|
+
parent,
|
|
1095
|
+
settled
|
|
1096
|
+
};
|
|
1097
|
+
}
|
|
1098
|
+
/** Whether a read's value is awaited, directly or through a chain such as `table.get(id).run()` or `.then(...)`: a table read is asynchronous, unlike `Map.get` or `headers.get`. */
|
|
1099
|
+
function isAwaitedRead(call) {
|
|
1100
|
+
return readValue(call).settled;
|
|
1101
|
+
}
|
|
1102
|
+
/** The variable a read's result is stored in, through `await`, `.run()` and the like: `rows` in `const rows = await table.getAll().run()`, or the first row in `const [row] = await table.getAll()` or `(await table.getAll())[0]`. */
|
|
1103
|
+
function readBinding(source, call) {
|
|
1104
|
+
const { node, parent } = readValue(call);
|
|
1105
|
+
if (parent?.type !== "VariableDeclarator" || parent.init !== node) return null;
|
|
1106
|
+
const target = parent.id.type === "ArrayPattern" ? parent.id.elements[0] : parent.id;
|
|
1107
|
+
if (target?.type !== "Identifier") return null;
|
|
1108
|
+
return source.getDeclaredVariables(parent).find((variable) => variable.name === target.name) ?? null;
|
|
1109
|
+
}
|
|
1110
|
+
function isSubject(source, node, subject) {
|
|
1111
|
+
const value = unwrap(node);
|
|
1112
|
+
if (value.type === "Identifier") return subject.binding !== null && resolveVariable(source, value) === subject.binding;
|
|
1113
|
+
return chainsFrom(value, subject.call);
|
|
1114
|
+
}
|
|
1115
|
+
function isCounted(source, node, subject) {
|
|
1116
|
+
const value = unwrap(node);
|
|
1117
|
+
return isSubject(source, value, subject) || value.type === "MemberExpression" && memberName(value) === "length" && isSubject(source, value.object, subject);
|
|
1118
|
+
}
|
|
1119
|
+
function holds(operator, left, right) {
|
|
1120
|
+
switch (operator) {
|
|
1121
|
+
case "==":
|
|
1122
|
+
case "===": return left === right;
|
|
1123
|
+
case "!=":
|
|
1124
|
+
case "!==": return left !== right;
|
|
1125
|
+
case "<": return left < right;
|
|
1126
|
+
case "<=": return left <= right;
|
|
1127
|
+
case ">": return left > right;
|
|
1128
|
+
case ">=": return left >= right;
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
function isComparison(operator) {
|
|
1132
|
+
return Object.hasOwn(MIRRORED, operator);
|
|
1133
|
+
}
|
|
1134
|
+
/** What `subject <operator> constant` proves: `record === undefined`, `count === 0` and `rows.length < 1` pass only when the read found nothing. */
|
|
1135
|
+
function compared(operator, constant) {
|
|
1136
|
+
if (constant.type === "Literal" && constant.value === null && !("regex" in constant) || constant.type === "Identifier" && constant.name === "undefined") return operator === "==" || operator === "===" ? EMPTY_WHEN_TRUE : FOUND_WHEN_TRUE;
|
|
1137
|
+
if (constant.type !== "Literal" || !Number.isFinite(constant.value)) return UNKNOWN;
|
|
1138
|
+
const bound = Number(constant.value);
|
|
1139
|
+
const empty = holds(operator, 0, bound);
|
|
1140
|
+
if (empty === holds(operator, Number.MAX_SAFE_INTEGER, bound)) return UNKNOWN;
|
|
1141
|
+
return empty ? EMPTY_WHEN_TRUE : FOUND_WHEN_TRUE;
|
|
1142
|
+
}
|
|
1143
|
+
function implication(source, test, subject) {
|
|
1144
|
+
const value = unwrap(test);
|
|
1145
|
+
if (isCounted(source, value, subject)) return FOUND_WHEN_TRUE;
|
|
1146
|
+
if (value.type === "UnaryExpression" && value.operator === "!") {
|
|
1147
|
+
const inner = implication(source, value.argument, subject);
|
|
1148
|
+
return {
|
|
1149
|
+
whenTrue: inner.whenFalse,
|
|
1150
|
+
whenFalse: inner.whenTrue
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
if (value.type === "LogicalExpression" && value.operator !== "??") {
|
|
1154
|
+
const left = implication(source, value.left, subject);
|
|
1155
|
+
const right = implication(source, value.right, subject);
|
|
1156
|
+
return value.operator === "&&" ? {
|
|
1157
|
+
whenTrue: left.whenTrue || right.whenTrue,
|
|
1158
|
+
whenFalse: false
|
|
1159
|
+
} : {
|
|
1160
|
+
whenTrue: false,
|
|
1161
|
+
whenFalse: left.whenFalse || right.whenFalse
|
|
1162
|
+
};
|
|
1163
|
+
}
|
|
1164
|
+
if (value.type !== "BinaryExpression" || !isComparison(value.operator)) return UNKNOWN;
|
|
1165
|
+
if (isCounted(source, value.left, subject)) return compared(value.operator, unwrap(value.right));
|
|
1166
|
+
if (isCounted(source, value.right, subject)) return compared(MIRRORED[value.operator], unwrap(value.left));
|
|
1167
|
+
return UNKNOWN;
|
|
1168
|
+
}
|
|
1169
|
+
function within(outer, inner) {
|
|
1170
|
+
return outer !== null && outer.range[0] <= inner.range[0] && inner.range[1] <= outer.range[1];
|
|
1171
|
+
}
|
|
1172
|
+
function lastStatement(statement) {
|
|
1173
|
+
if (statement?.type === "BlockStatement") return lastStatement(statement.body.at(-1) ?? null);
|
|
1174
|
+
return statement;
|
|
1175
|
+
}
|
|
1176
|
+
function isFunction$1(node) {
|
|
1177
|
+
return node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
|
|
1178
|
+
}
|
|
1179
|
+
function isLoop(node) {
|
|
1180
|
+
return node.type === "ForStatement" || node.type === "ForInStatement" || node.type === "ForOfStatement" || node.type === "WhileStatement" || node.type === "DoWhileStatement";
|
|
1181
|
+
}
|
|
1182
|
+
/** The node an exit leaves: its function for `return` and `throw`, its loop for `continue`, its loop or switch for `break`. */
|
|
1183
|
+
function exited(exit) {
|
|
1184
|
+
const leavesFunction = exit.type === "ReturnStatement" || exit.type === "ThrowStatement";
|
|
1185
|
+
if (!leavesFunction && exit.type !== "ContinueStatement" && exit.type !== "BreakStatement") return null;
|
|
1186
|
+
let current = exit.parent;
|
|
1187
|
+
while (current !== null) {
|
|
1188
|
+
if (isFunction$1(current)) return current;
|
|
1189
|
+
if (!leavesFunction && isLoop(current)) return current;
|
|
1190
|
+
if (exit.type === "BreakStatement" && current.type === "SwitchStatement") return current;
|
|
1191
|
+
current = current.parent;
|
|
1192
|
+
}
|
|
1193
|
+
return null;
|
|
1194
|
+
}
|
|
1195
|
+
function leaves(branch, site) {
|
|
1196
|
+
const exit = lastStatement(branch);
|
|
1197
|
+
return exit !== null && within(exited(exit), site);
|
|
1198
|
+
}
|
|
1199
|
+
/** Whether a branch ends in an exit that skips `site`: the exit leaves a function or loop that holds the site, and the site follows the `if` in the block that holds it. An `else if` reaches that block only when every earlier arm exits past the site too. */
|
|
1200
|
+
function skips(conditional, branch, site) {
|
|
1201
|
+
let arm = conditional;
|
|
1202
|
+
let block = conditional.parent;
|
|
1203
|
+
while (block?.type === "IfStatement") {
|
|
1204
|
+
if (block.alternate !== arm || !leaves(block.consequent, site)) return false;
|
|
1205
|
+
arm = block;
|
|
1206
|
+
block = block.parent;
|
|
1207
|
+
}
|
|
1208
|
+
return conditional.range[1] <= site.range[0] && within(block, site) && leaves(branch, site);
|
|
1209
|
+
}
|
|
1210
|
+
/** Whether a conditional runs `site` only when the read found nothing: the site sits in the branch taken only then, or it follows an `if` whose branch for a found row skips it, as in `if (record) return;`. */
|
|
1211
|
+
function decides(source, subject, conditional, site) {
|
|
1212
|
+
if (conditional.type === "LogicalExpression") {
|
|
1213
|
+
const left = implication(source, conditional.left, subject);
|
|
1214
|
+
return (conditional.operator === "&&" ? left.whenTrue : left.whenFalse) && within(conditional.right, site);
|
|
1215
|
+
}
|
|
1216
|
+
const { whenTrue, whenFalse } = implication(source, conditional.test, subject);
|
|
1217
|
+
if (whenTrue && within(conditional.consequent, site) || whenFalse && within(conditional.alternate, site)) return true;
|
|
1218
|
+
return conditional.type === "IfStatement" && (whenFalse && skips(conditional, conditional.consequent, site) || whenTrue && skips(conditional, conditional.alternate, site));
|
|
1219
|
+
}
|
|
1220
|
+
/** The function a plain call runs, found through the scope its name resolves in: a function declaration (the implementation of an overloaded one, whose signatures declare no function), or a variable that holds a function. A name the code reassigns is not followed. */
|
|
1221
|
+
function calledFunction(source, callee) {
|
|
1222
|
+
const variable = resolveVariable(source, callee);
|
|
1223
|
+
if (variable === null || variable.references.some((reference) => reference.isWrite() && !reference.init)) return null;
|
|
1224
|
+
for (const { node } of variable.defs) {
|
|
1225
|
+
if (node.type === "FunctionDeclaration") return node;
|
|
1226
|
+
if (node.type === "VariableDeclarator" && (node.init?.type === "ArrowFunctionExpression" || node.init?.type === "FunctionExpression")) return node.init;
|
|
1227
|
+
}
|
|
1228
|
+
return null;
|
|
1229
|
+
}
|
|
1230
|
+
//#endregion
|
|
1231
|
+
//#region src/oxc/guardrails/rules/no-read-then-insert.ts
|
|
1232
|
+
const READ_METHOD = /^(?:get|getAll|count|find|findOne|(?:get|find)By(?:[A-Z]\w*)?)$/u;
|
|
1233
|
+
const INSERT_METHOD = /^(?:insert|insertOne|insertMany)$/u;
|
|
1234
|
+
const INLINE_CALLBACK_METHODS = /* @__PURE__ */ new Set([
|
|
1235
|
+
"map",
|
|
1236
|
+
"flatMap",
|
|
1237
|
+
"forEach",
|
|
1238
|
+
"filter",
|
|
1239
|
+
"reduce",
|
|
1240
|
+
"some",
|
|
1241
|
+
"every",
|
|
1242
|
+
"then",
|
|
1243
|
+
"catch",
|
|
1244
|
+
"finally"
|
|
1245
|
+
]);
|
|
1246
|
+
const DUPLICATE_CHECK = /duplicate|unique|conflict|E11000|^11000$|23505|ER_DUP_ENTRY/iu;
|
|
1247
|
+
function contains(outer, inner) {
|
|
1248
|
+
return outer[0] <= inner[0] && inner[1] <= outer[1];
|
|
1249
|
+
}
|
|
1250
|
+
function isScope(node) {
|
|
1251
|
+
return node.type === "Program" || node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
|
|
1252
|
+
}
|
|
1253
|
+
function enclosingScope(node) {
|
|
1254
|
+
let current = node.parent;
|
|
1255
|
+
while (current !== null) {
|
|
1256
|
+
if (isScope(current)) return current;
|
|
1257
|
+
current = current.parent;
|
|
1258
|
+
}
|
|
1259
|
+
return null;
|
|
1260
|
+
}
|
|
1261
|
+
/** A callback that runs while its caller waits: handed to `map`, `then` or the like, or to a call whose value the caller awaits or returns. A handler registered by `app.post(...)` or wrapped by `export const POST = withAuth(...)` runs later, on its own. */
|
|
1262
|
+
function isInlineCallback(scope) {
|
|
1263
|
+
const call = scope.parent;
|
|
1264
|
+
if (scope.type === "Program" || scope.type === "FunctionDeclaration" || call?.type !== "CallExpression" || !call.arguments.some((argument) => argument === scope)) return false;
|
|
1265
|
+
const method = call.callee.type === "MemberExpression" ? memberName(call.callee) : null;
|
|
1266
|
+
return method !== null && INLINE_CALLBACK_METHODS.has(method) || isAwaited(call);
|
|
1267
|
+
}
|
|
1268
|
+
/** Whether the call's value reaches an `await` or a `return`, through array elements, call arguments, `?.` or a `.then` chain, or is the body of an inline arrow. */
|
|
1269
|
+
function isAwaited(call) {
|
|
1270
|
+
let node = call;
|
|
1271
|
+
let parent = node.parent;
|
|
1272
|
+
while (parent !== null) {
|
|
1273
|
+
if (parent.type === "AwaitExpression" || parent.type === "ReturnStatement") return true;
|
|
1274
|
+
if (parent.type === "ArrowFunctionExpression" && parent.body === node) return isInlineCallback(parent);
|
|
1275
|
+
if (!(parent.type === "ArrayExpression" || parent.type === "ChainExpression" || parent.type === "SpreadElement" || parent.type === "CallExpression" || parent.type === "MemberExpression" && parent.object === node)) return false;
|
|
1276
|
+
node = parent;
|
|
1277
|
+
parent = node.parent;
|
|
1278
|
+
}
|
|
1279
|
+
return false;
|
|
1280
|
+
}
|
|
1281
|
+
/** The function a read or an insert runs in, seen through inline callbacks such as `withRetry(() => model.getAll())` or `rows.map((row) => model.insert(row))`. */
|
|
1282
|
+
function runningScope(node) {
|
|
1283
|
+
let scope = enclosingScope(node);
|
|
1284
|
+
while (scope !== null && isInlineCallback(scope)) scope = enclosingScope(scope);
|
|
1285
|
+
return scope;
|
|
1286
|
+
}
|
|
1287
|
+
function rootName(node) {
|
|
1288
|
+
let current = node;
|
|
1289
|
+
while (current.type === "MemberExpression" || current.type === "CallExpression") current = current.type === "MemberExpression" ? current.object : current.callee;
|
|
1290
|
+
return current.type === "Identifier" ? current.name : null;
|
|
1291
|
+
}
|
|
1292
|
+
function declares(scope, name) {
|
|
1293
|
+
return scope.type !== "Program" && scope.params.some((param) => param.type === "Identifier" && param.name === name);
|
|
1294
|
+
}
|
|
1295
|
+
function calledMember(node, methods) {
|
|
1296
|
+
if (node.callee.type !== "MemberExpression") return null;
|
|
1297
|
+
const name = memberName(node.callee);
|
|
1298
|
+
return name !== null && methods.test(name) ? node.callee : null;
|
|
1299
|
+
}
|
|
1300
|
+
function isUpsert(node) {
|
|
1301
|
+
return node.arguments.slice(1).some((argument) => argument.type === "ObjectExpression" && argument.properties.some((property) => property.type === "Property" && !property.computed && (property.key.type === "Identifier" && property.key.name === "conflict" || property.key.type === "Literal" && property.key.value === "conflict") && !(property.value.type === "Literal" && property.value.value === "error")));
|
|
1302
|
+
}
|
|
1303
|
+
/** `model.get` and `model.insert` touch one table, and so do `model.get` and the model's raw `model.table.insert`, but not `db.table("a")` and `db.table("b")`, nor `this.get` and `this.audit.insert`. */
|
|
1304
|
+
function sameTable(read, insert) {
|
|
1305
|
+
return read.receivers.has(insert.receiver) || insert.receiver === `${read.receiver}.table`;
|
|
1306
|
+
}
|
|
1307
|
+
/** A catch that returns, continues or breaks in its own function without a rejection, or keeps every `throw` inside a branch, can finish without rethrowing. */
|
|
1308
|
+
function canFinish(facts, handler) {
|
|
1309
|
+
const own = handler.type === "BlockStatement" ? enclosingScope(handler) : handler;
|
|
1310
|
+
const block = handler.type === "BlockStatement" ? handler : (handler.type === "ArrowFunctionExpression" || handler.type === "FunctionExpression") && handler.body?.type === "BlockStatement" ? handler.body : null;
|
|
1311
|
+
return facts.exits.some((exit) => contains(handler.range, exit.range) && enclosingScope(exit) === own && !facts.throwRanges.some((thrown) => contains(exit.range, thrown))) || !facts.throwBlocks.some((thrownIn) => thrownIn === block);
|
|
1312
|
+
}
|
|
1313
|
+
/** A catch tolerates the lost race when it rethrows nothing, or when it can finish without rethrowing and either reads the table back or checks for a duplicate outside a `throw`. */
|
|
1314
|
+
function recovers(facts, handler, insert) {
|
|
1315
|
+
if (handler.type !== "BlockStatement" && handler.type !== "ArrowFunctionExpression" && handler.type !== "FunctionExpression") return DUPLICATE_CHECK.test(facts.text(handler));
|
|
1316
|
+
const range = handler.range;
|
|
1317
|
+
if (!facts.throwRanges.some((thrown) => contains(range, thrown))) return true;
|
|
1318
|
+
if (!canFinish(facts, handler)) return false;
|
|
1319
|
+
return facts.reads.some((read) => contains(range, read.range) && sameTable(read, insert)) || facts.duplicateChecks.some((check) => contains(range, check) && !facts.throwRanges.some((thrown) => contains(thrown, check)));
|
|
1320
|
+
}
|
|
1321
|
+
function isHandedToHelper(facts, insert, child, parent) {
|
|
1322
|
+
return (child.type === "ArrowFunctionExpression" || child.type === "FunctionExpression") && parent.type === "CallExpression" && parent.arguments.some((argument) => argument !== child && facts.text(argument) === insert.receiver);
|
|
1323
|
+
}
|
|
1324
|
+
function recoveryOf(site, child, parent) {
|
|
1325
|
+
if (parent.type === "TryStatement" && parent.block === child) return parent.handler?.body ?? null;
|
|
1326
|
+
if (parent.type === "MemberExpression" && parent.object === child && chainsFrom(child, site) && memberName(parent) === "catch" && parent.parent?.type === "CallExpression" && parent.parent.callee === parent) return parent.parent.arguments[0] ?? null;
|
|
1327
|
+
return null;
|
|
1328
|
+
}
|
|
1329
|
+
/** Whether the insert is guarded on the way from `site` up to `stop`: `site` is the insert itself, or a call that reaches it. Only the insert's own function can shadow the read's receiver or hand the insert to a helper; any call on the way can recover from it. */
|
|
1330
|
+
function isGuarded(facts, insert, site, stop, read) {
|
|
1331
|
+
let child = site;
|
|
1332
|
+
let parent = child.parent;
|
|
1333
|
+
while (parent !== null && child !== stop) {
|
|
1334
|
+
const between = site === insert.call && (read === null || !contains(child.range, read.range));
|
|
1335
|
+
if (between && read !== null && insert.root !== null && isScope(child) && declares(child, insert.root)) return true;
|
|
1336
|
+
if (between && isHandedToHelper(facts, insert, child, parent)) return true;
|
|
1337
|
+
const recovery = recoveryOf(site, child, parent);
|
|
1338
|
+
if (recovery !== null && recovers(facts, recovery, insert)) return true;
|
|
1339
|
+
child = parent;
|
|
1340
|
+
parent = child.parent;
|
|
1341
|
+
}
|
|
1342
|
+
return false;
|
|
1343
|
+
}
|
|
1344
|
+
function isRaced(facts, insert) {
|
|
1345
|
+
const start = insert.call.range[0];
|
|
1346
|
+
return facts.reads.some((read) => sameTable(read, insert) && read.range[1] <= start && read.scope === insert.scope && !isGuarded(facts, insert, insert.call, insert.scope, read));
|
|
1347
|
+
}
|
|
1348
|
+
/** The unguarded inserts each function makes, in its own body or through the functions of this file it calls, grown until no call adds one so the order of a cycle does not matter. */
|
|
1349
|
+
function reachedInserts(facts) {
|
|
1350
|
+
const reached = /* @__PURE__ */ new Map();
|
|
1351
|
+
const of = (scope) => {
|
|
1352
|
+
const known = reached.get(scope);
|
|
1353
|
+
if (known !== void 0) return known;
|
|
1354
|
+
const created = /* @__PURE__ */ new Set();
|
|
1355
|
+
reached.set(scope, created);
|
|
1356
|
+
return created;
|
|
1357
|
+
};
|
|
1358
|
+
for (const insert of facts.inserts) if (!isGuarded(facts, insert, insert.call, insert.scope, null)) of(insert.scope).add(insert);
|
|
1359
|
+
let changed = true;
|
|
1360
|
+
while (changed) {
|
|
1361
|
+
changed = false;
|
|
1362
|
+
for (const site of facts.sites) {
|
|
1363
|
+
const into = of(site.scope);
|
|
1364
|
+
for (const insert of of(site.target)) {
|
|
1365
|
+
if (into.has(insert) || isGuarded(facts, insert, site.call, site.scope, null)) continue;
|
|
1366
|
+
into.add(insert);
|
|
1367
|
+
changed = true;
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
return reached;
|
|
1372
|
+
}
|
|
1373
|
+
function racedThroughCalls(facts) {
|
|
1374
|
+
const reached = reachedInserts(facts);
|
|
1375
|
+
return facts.reads.filter(({ call }) => isAwaitedRead(call)).flatMap((read) => {
|
|
1376
|
+
const conditionals = facts.conditionals.filter((conditional) => conditional.range[1] > read.range[0] && runningScope(conditional) === read.scope);
|
|
1377
|
+
return facts.sites.filter((site) => site.scope === read.scope && read.range[1] <= site.call.range[0] && conditionals.some((conditional) => decides(facts.source, read, conditional, site.call))).flatMap((site) => [...reached.get(site.target) ?? []].filter((insert) => !isGuarded(facts, insert, site.call, read.scope, null)).map((insert) => ({
|
|
1378
|
+
insert,
|
|
1379
|
+
read,
|
|
1380
|
+
site
|
|
1381
|
+
})));
|
|
1382
|
+
});
|
|
1383
|
+
}
|
|
1384
|
+
function receiverChain(facts, start) {
|
|
1385
|
+
const texts = /* @__PURE__ */ new Set();
|
|
1386
|
+
let current = start;
|
|
1387
|
+
while (current !== null) {
|
|
1388
|
+
texts.add(facts.text(current));
|
|
1389
|
+
if (current.type === "CallExpression") current = current.callee;
|
|
1390
|
+
else if (current.type === "MemberExpression") current = current.object;
|
|
1391
|
+
else current = null;
|
|
1392
|
+
}
|
|
1393
|
+
return texts;
|
|
1394
|
+
}
|
|
1395
|
+
function collectCall(facts, node) {
|
|
1396
|
+
if (facts.text(node.callee) === "Promise.reject") {
|
|
1397
|
+
facts.throwRanges.push(node.range);
|
|
1398
|
+
const statement = node.parent;
|
|
1399
|
+
if (statement?.type === "ReturnStatement" && statement.parent !== null) facts.throwBlocks.push(statement.parent);
|
|
1400
|
+
}
|
|
1401
|
+
const scope = runningScope(node);
|
|
1402
|
+
if (scope === null) return;
|
|
1403
|
+
const target = node.callee.type === "Identifier" ? calledFunction(facts.source, node.callee) : null;
|
|
1404
|
+
if (target !== null && node.callee.type === "Identifier") facts.sites.push({
|
|
1405
|
+
call: node,
|
|
1406
|
+
scope,
|
|
1407
|
+
name: node.callee.name,
|
|
1408
|
+
target
|
|
1409
|
+
});
|
|
1410
|
+
const read = calledMember(node, READ_METHOD);
|
|
1411
|
+
if (read !== null) facts.reads.push({
|
|
1412
|
+
call: node,
|
|
1413
|
+
binding: readBinding(facts.source, node),
|
|
1414
|
+
range: node.range,
|
|
1415
|
+
scope,
|
|
1416
|
+
receiver: facts.text(read.object),
|
|
1417
|
+
receivers: receiverChain(facts, read.object)
|
|
1418
|
+
});
|
|
1419
|
+
const insert = calledMember(node, INSERT_METHOD);
|
|
1420
|
+
if (insert !== null && !isUpsert(node)) facts.inserts.push({
|
|
1421
|
+
call: node,
|
|
1422
|
+
scope,
|
|
1423
|
+
callee: insert,
|
|
1424
|
+
receiver: facts.text(insert.object),
|
|
1425
|
+
root: rootName(insert.object)
|
|
1426
|
+
});
|
|
1427
|
+
}
|
|
1428
|
+
const RACE = "Two replicas that start on an empty table both read nothing and both insert, so one writes a duplicate row or fails on the key. Key the row by a stable id, wrap the insert in try/catch, read the row back in the catch, and rethrow only when it is still missing, or upsert with `insert(row, { conflict: \"update\" })`.";
|
|
1429
|
+
/** Writing a row that a startup read found missing races every other replica that read the same, whether the insert sits in the reading function or in a function of the file it calls only when the read found nothing. A catch that rethrows nothing, recognises a duplicate in a condition, or reads the row back and branches, tolerates the lost race, and so does an upsert or a helper handed the table. */
|
|
1430
|
+
const noReadThenInsertRule = defineRule({
|
|
1431
|
+
meta: {
|
|
1432
|
+
type: "problem",
|
|
1433
|
+
docs: { description: "Reject an insert that follows a read of the same table, or that a called function makes when a read found nothing, unless a catch tolerates losing it." },
|
|
1434
|
+
messages: {
|
|
1435
|
+
readThenInsert: `\`{{receiver}}\` is read and then inserted into. ${RACE}`,
|
|
1436
|
+
readThenCalledInsert: `\`{{read}}\` found nothing, so \`{{call}}\` runs and inserts into \`{{receiver}}\`. ${RACE}`
|
|
1437
|
+
}
|
|
1438
|
+
},
|
|
1439
|
+
createOnce(context) {
|
|
1440
|
+
const facts = {
|
|
1441
|
+
reads: [],
|
|
1442
|
+
inserts: [],
|
|
1443
|
+
duplicateChecks: [],
|
|
1444
|
+
throwRanges: [],
|
|
1445
|
+
throwBlocks: [],
|
|
1446
|
+
exits: [],
|
|
1447
|
+
conditionals: [],
|
|
1448
|
+
sites: [],
|
|
1449
|
+
get source() {
|
|
1450
|
+
return context.sourceCode;
|
|
1451
|
+
},
|
|
1452
|
+
text: (node) => context.sourceCode.getText(node)
|
|
1453
|
+
};
|
|
1454
|
+
const exit = (node) => facts.exits.push(node);
|
|
1455
|
+
const conditional = (node) => facts.conditionals.push(node);
|
|
1456
|
+
return {
|
|
1457
|
+
before() {
|
|
1458
|
+
for (const list of [
|
|
1459
|
+
facts.reads,
|
|
1460
|
+
facts.inserts,
|
|
1461
|
+
facts.duplicateChecks,
|
|
1462
|
+
facts.throwRanges,
|
|
1463
|
+
facts.throwBlocks,
|
|
1464
|
+
facts.exits,
|
|
1465
|
+
facts.conditionals,
|
|
1466
|
+
facts.sites
|
|
1467
|
+
]) list.length = 0;
|
|
1468
|
+
},
|
|
1469
|
+
ThrowStatement(node) {
|
|
1470
|
+
facts.throwRanges.push(node.range);
|
|
1471
|
+
if (node.parent !== null) facts.throwBlocks.push(node.parent);
|
|
1472
|
+
},
|
|
1473
|
+
ReturnStatement: exit,
|
|
1474
|
+
ContinueStatement: exit,
|
|
1475
|
+
BreakStatement: exit,
|
|
1476
|
+
IfStatement: conditional,
|
|
1477
|
+
ConditionalExpression: conditional,
|
|
1478
|
+
LogicalExpression: conditional,
|
|
1479
|
+
Identifier(node) {
|
|
1480
|
+
if (DUPLICATE_CHECK.test(node.name)) facts.duplicateChecks.push(node.range);
|
|
1481
|
+
},
|
|
1482
|
+
TemplateLiteral(node) {
|
|
1483
|
+
if (node.quasis.some((quasi) => DUPLICATE_CHECK.test(quasi.value.raw))) facts.duplicateChecks.push(node.range);
|
|
1484
|
+
},
|
|
1485
|
+
Literal(node) {
|
|
1486
|
+
if (DUPLICATE_CHECK.test(String(node.value))) facts.duplicateChecks.push(node.range);
|
|
1487
|
+
},
|
|
1488
|
+
CallExpression(node) {
|
|
1489
|
+
collectCall(facts, node);
|
|
1490
|
+
},
|
|
1491
|
+
"Program:exit"() {
|
|
1492
|
+
const reported = /* @__PURE__ */ new Set();
|
|
1493
|
+
for (const insert of facts.inserts) {
|
|
1494
|
+
if (!isRaced(facts, insert)) continue;
|
|
1495
|
+
reported.add(insert);
|
|
1496
|
+
context.report({
|
|
1497
|
+
node: insert.callee,
|
|
1498
|
+
messageId: "readThenInsert",
|
|
1499
|
+
data: { receiver: insert.receiver }
|
|
1500
|
+
});
|
|
1501
|
+
}
|
|
1502
|
+
for (const { insert, read, site } of racedThroughCalls(facts)) {
|
|
1503
|
+
if (reported.has(insert)) continue;
|
|
1504
|
+
reported.add(insert);
|
|
1505
|
+
context.report({
|
|
1506
|
+
node: insert.callee,
|
|
1507
|
+
messageId: "readThenCalledInsert",
|
|
1508
|
+
data: {
|
|
1509
|
+
read: facts.text(read.call.callee),
|
|
1510
|
+
call: site.name,
|
|
1511
|
+
receiver: insert.receiver
|
|
1512
|
+
}
|
|
1513
|
+
});
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
};
|
|
1517
|
+
}
|
|
1518
|
+
});
|
|
1519
|
+
//#endregion
|
|
1520
|
+
//#region src/oxc/guardrails/self-package.ts
|
|
1521
|
+
function isObject(value) {
|
|
1522
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1523
|
+
}
|
|
1524
|
+
function isText(value) {
|
|
1525
|
+
return typeof value === "string";
|
|
1526
|
+
}
|
|
1527
|
+
function readManifest(directory) {
|
|
1528
|
+
let parsed;
|
|
1529
|
+
try {
|
|
1530
|
+
parsed = JSON.parse(readFileSync(join(directory, "package.json"), "utf8"));
|
|
1531
|
+
} catch {
|
|
1532
|
+
return;
|
|
1533
|
+
}
|
|
1534
|
+
if (!isObject(parsed) || !isText(parsed.name)) return void 0;
|
|
1535
|
+
const { antelopeJs } = parsed;
|
|
1536
|
+
return {
|
|
1537
|
+
name: parsed.name,
|
|
1538
|
+
exports: parsed.exports,
|
|
1539
|
+
testedByAjs: isObject(antelopeJs) && isText(antelopeJs.test)
|
|
1540
|
+
};
|
|
1541
|
+
}
|
|
1542
|
+
/** The nearest enclosing package that has a name, with the directory holding its manifest. */
|
|
1543
|
+
function enclosingPackage(file) {
|
|
1544
|
+
let directory = dirname(file);
|
|
1545
|
+
for (;;) {
|
|
1546
|
+
const manifest = readManifest(directory);
|
|
1547
|
+
if (manifest !== void 0) return {
|
|
1548
|
+
directory,
|
|
1549
|
+
manifest
|
|
1550
|
+
};
|
|
1551
|
+
const parent = dirname(directory);
|
|
1552
|
+
if (parent === directory) return void 0;
|
|
1553
|
+
directory = parent;
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
/** The subpath of `specifier` inside package `name` (`.` for the root), or undefined when it names another package. */
|
|
1557
|
+
function selfSubpath(name, specifier) {
|
|
1558
|
+
if (specifier === name) return ".";
|
|
1559
|
+
return specifier.startsWith(`${name}/`) ? `./${specifier.slice(name.length + 1)}` : void 0;
|
|
1560
|
+
}
|
|
1561
|
+
function exportKeys(exportsField) {
|
|
1562
|
+
if (!isObject(exportsField)) return ["."];
|
|
1563
|
+
const keys = Object.keys(exportsField);
|
|
1564
|
+
return keys.some((key) => key.startsWith(".")) ? keys : ["."];
|
|
1565
|
+
}
|
|
1566
|
+
function patternMatches(key, subpath) {
|
|
1567
|
+
const star = key.indexOf("*");
|
|
1568
|
+
if (star === -1) return key === subpath;
|
|
1569
|
+
const prefix = key.slice(0, star);
|
|
1570
|
+
const suffix = key.slice(star + 1);
|
|
1571
|
+
return subpath.length >= key.length - 1 && subpath.startsWith(prefix) && subpath.endsWith(suffix);
|
|
1572
|
+
}
|
|
1573
|
+
/** Whether the package `exports` map publishes `subpath`, so Node can resolve a self-import to it. */
|
|
1574
|
+
function exportsCover(exportsField, subpath) {
|
|
1575
|
+
if (exportsField === void 0 || exportsField === null) return false;
|
|
1576
|
+
return exportKeys(exportsField).some((key) => patternMatches(key, subpath));
|
|
1577
|
+
}
|
|
1578
|
+
/** Whether `node_modules/<name>` is a link back to the package itself, as a workspace self-link makes it. */
|
|
1579
|
+
function isSelfLinked(directory, name) {
|
|
1580
|
+
const installed = join(directory, "node_modules", ...name.split("/"));
|
|
1581
|
+
try {
|
|
1582
|
+
return lstatSync(installed).isSymbolicLink() && realpathSync.native(installed) === realpathSync.native(directory);
|
|
1583
|
+
} catch {
|
|
1584
|
+
return false;
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
//#endregion
|
|
1588
|
+
//#region src/oxc/guardrails/rules/no-self-import-in-tests.ts
|
|
1589
|
+
/** A test that imports its own package name must reach local code: the `exports` map publishes the subpath and `node_modules` links the package to itself. Under `ajs module test` no self-link reaches local code, so every self-import is reported. */
|
|
1590
|
+
const noSelfImportInTestsRule = defineRule({
|
|
1591
|
+
meta: {
|
|
1592
|
+
type: "problem",
|
|
1593
|
+
docs: { description: "Tests import the module under test from local source, never their own published package name." },
|
|
1594
|
+
messages: {
|
|
1595
|
+
selfImport: "This test imports its own package `{{ name }}`, which resolves to the installed published copy, so it never exercises the local code. Import the module under test by relative path, or make the self-import local: publish `{{ subpath }}` in package.json `exports` and link the package to itself (`link:.` in devDependencies).",
|
|
1596
|
+
selfImportUnderAjsTest: "This test imports its own package `{{ name }}`, and `ajs module test` resolves that name through its own module resolver, which can bind it to a copy installed with another module instead of this package, even with a `link:.` self-link. Import the module under test by relative path."
|
|
1597
|
+
}
|
|
1598
|
+
},
|
|
1599
|
+
createOnce(context) {
|
|
1600
|
+
function check(node, specifier) {
|
|
1601
|
+
const owner = enclosingPackage(context.filename);
|
|
1602
|
+
if (owner === void 0) return;
|
|
1603
|
+
const { directory, manifest } = owner;
|
|
1604
|
+
const subpath = selfSubpath(manifest.name, specifier);
|
|
1605
|
+
if (subpath === void 0) return;
|
|
1606
|
+
if (manifest.testedByAjs) {
|
|
1607
|
+
context.report({
|
|
1608
|
+
node,
|
|
1609
|
+
messageId: "selfImportUnderAjsTest",
|
|
1610
|
+
data: { name: manifest.name }
|
|
1611
|
+
});
|
|
1612
|
+
return;
|
|
1613
|
+
}
|
|
1614
|
+
if (exportsCover(manifest.exports, subpath) && isSelfLinked(directory, manifest.name)) return;
|
|
1615
|
+
context.report({
|
|
1616
|
+
node,
|
|
1617
|
+
messageId: "selfImport",
|
|
1618
|
+
data: {
|
|
1619
|
+
name: manifest.name,
|
|
1620
|
+
subpath
|
|
1621
|
+
}
|
|
1622
|
+
});
|
|
1623
|
+
}
|
|
1624
|
+
return {
|
|
1625
|
+
ImportDeclaration(node) {
|
|
1626
|
+
if (node.importKind === "type" || node.specifiers.length > 0 && node.specifiers.every((specifier) => specifier.type === "ImportSpecifier" && specifier.importKind === "type")) return;
|
|
1627
|
+
check(node, node.source.value);
|
|
1628
|
+
},
|
|
1629
|
+
ExportNamedDeclaration(node) {
|
|
1630
|
+
if (node.source && node.exportKind !== "type") check(node, node.source.value);
|
|
1631
|
+
},
|
|
1632
|
+
ExportAllDeclaration(node) {
|
|
1633
|
+
if (node.exportKind !== "type") check(node, node.source.value);
|
|
1634
|
+
},
|
|
1635
|
+
ImportExpression(node) {
|
|
1636
|
+
if (node.source.type === "Literal" && !("regex" in node.source)) check(node, String(node.source.value));
|
|
1637
|
+
},
|
|
1638
|
+
TSImportEqualsDeclaration(node) {
|
|
1639
|
+
const reference = node.moduleReference;
|
|
1640
|
+
if (node.importKind !== "type" && reference.type === "TSExternalModuleReference") check(node, reference.expression.value);
|
|
1641
|
+
},
|
|
1642
|
+
CallExpression(node) {
|
|
1643
|
+
const [argument] = node.arguments;
|
|
1644
|
+
if (node.callee.type === "Identifier" && node.callee.name === "require" && argument?.type === "Literal" && !("regex" in argument)) check(node, String(argument.value));
|
|
1645
|
+
}
|
|
1646
|
+
};
|
|
1647
|
+
}
|
|
1648
|
+
});
|
|
1649
|
+
//#endregion
|
|
1650
|
+
//#region src/oxc/guardrails/rules/no-typeof-function-only-test.ts
|
|
1651
|
+
const functionMatchers = /* @__PURE__ */ new Set([
|
|
1652
|
+
"toBeTypeOf",
|
|
1653
|
+
"to.be.a",
|
|
1654
|
+
"to.be.an"
|
|
1655
|
+
]);
|
|
1656
|
+
const typeofMatchers = /* @__PURE__ */ new Set(["toBe", "to.equal"]);
|
|
1657
|
+
const assertTypeofMatchers = /* @__PURE__ */ new Set(["assert.equal", "assert.strictEqual"]);
|
|
1658
|
+
function isTypeof(node) {
|
|
1659
|
+
return node?.type === "UnaryExpression" && node.operator === "typeof";
|
|
1660
|
+
}
|
|
1661
|
+
function isFunctionString(node) {
|
|
1662
|
+
return node?.type === "Literal" && node.value === "function";
|
|
1663
|
+
}
|
|
1664
|
+
function isFunctionCheck(node) {
|
|
1665
|
+
const name = callName(node.callee);
|
|
1666
|
+
if (name === "assert.isFunction") return true;
|
|
1667
|
+
if (name !== null && assertTypeofMatchers.has(name)) return isTypeof(node.arguments[0]) && isFunctionString(node.arguments[1]);
|
|
1668
|
+
let outer = node;
|
|
1669
|
+
while (outer.parent.type === "MemberExpression" && outer.parent.object === outer) {
|
|
1670
|
+
let member = outer.parent;
|
|
1671
|
+
while (member.parent.type === "MemberExpression" && member.parent.object === member) member = member.parent;
|
|
1672
|
+
if (member.parent.type !== "CallExpression" || member.parent.callee !== member) break;
|
|
1673
|
+
outer = member.parent;
|
|
1674
|
+
}
|
|
1675
|
+
const chain = matcherChain(outer);
|
|
1676
|
+
if (chain === null || !isFunctionString(outer.arguments[0])) return false;
|
|
1677
|
+
return functionMatchers.has(chain.matcher) || typeofMatchers.has(chain.matcher) && isTypeof(chain.expect.arguments[0]);
|
|
1678
|
+
}
|
|
1679
|
+
/** Function existence alone is already a type-checker guarantee, not a behavioral test. */
|
|
1680
|
+
const noTypeofFunctionOnlyTestRule = defineRule({
|
|
1681
|
+
meta: {
|
|
1682
|
+
type: "problem",
|
|
1683
|
+
docs: { description: "Require tests to do more than assert that functions exist." },
|
|
1684
|
+
messages: { functionOnly: "The type checker already proves this. Call the function and assert what it returns or does." }
|
|
1685
|
+
},
|
|
1686
|
+
createOnce(context) {
|
|
1687
|
+
const frames = [];
|
|
1688
|
+
function recordAssertion(node) {
|
|
1689
|
+
const frame = frames.at(-1);
|
|
1690
|
+
if (frame === void 0 || node.start < frame.callback.start || node.end > frame.callback.end || !isAssertion(node)) return;
|
|
1691
|
+
frame.assertions.push(node.type === "CallExpression" && isFunctionCheck(node));
|
|
1692
|
+
}
|
|
1693
|
+
return {
|
|
1694
|
+
before() {
|
|
1695
|
+
frames.length = 0;
|
|
1696
|
+
},
|
|
1697
|
+
CallExpression(node) {
|
|
1698
|
+
const callback = testCallback(node);
|
|
1699
|
+
if (callback !== void 0) frames.push({
|
|
1700
|
+
node,
|
|
1701
|
+
callback,
|
|
1702
|
+
assertions: []
|
|
1703
|
+
});
|
|
1704
|
+
else recordAssertion(node);
|
|
1705
|
+
},
|
|
1706
|
+
MemberExpression: recordAssertion,
|
|
1707
|
+
"CallExpression:exit"(node) {
|
|
1708
|
+
const frame = frames.at(-1);
|
|
1709
|
+
if (frame?.node !== node) return;
|
|
1710
|
+
frames.pop();
|
|
1711
|
+
if (frame.assertions.length > 0 && frame.assertions.every(Boolean)) context.report({
|
|
1712
|
+
node,
|
|
1713
|
+
messageId: "functionOnly"
|
|
1714
|
+
});
|
|
1715
|
+
}
|
|
1716
|
+
};
|
|
1717
|
+
}
|
|
1718
|
+
});
|
|
1719
|
+
//#endregion
|
|
1720
|
+
//#region src/oxc/guardrails/tsconfig-program.ts
|
|
1721
|
+
const CONFIG_NAMES = ["tsconfig.json", "jsconfig.json"];
|
|
1722
|
+
const HOST = {
|
|
1723
|
+
...ts.sys,
|
|
1724
|
+
onUnRecoverableConfigFileDiagnostic: () => void 0
|
|
1725
|
+
};
|
|
1726
|
+
/** The extension a config's `include` globs match besides TypeScript's own, so a Vue component is a root file of its program. */
|
|
1727
|
+
const VUE_EXTENSION = [{
|
|
1728
|
+
extension: ".vue",
|
|
1729
|
+
isMixedContent: true,
|
|
1730
|
+
scriptKind: ts.ScriptKind.Deferred
|
|
1731
|
+
}];
|
|
1732
|
+
const projects = /* @__PURE__ */ new Map();
|
|
1733
|
+
const resolutionCaches = /* @__PURE__ */ new Map();
|
|
1734
|
+
function configAbove(directory) {
|
|
1735
|
+
for (let current = directory;; current = dirname(current)) {
|
|
1736
|
+
const config = CONFIG_NAMES.map((name) => join(current, name)).find((path) => existsSync(path));
|
|
1737
|
+
if (config !== void 0) return config;
|
|
1738
|
+
if (dirname(current) === current) return void 0;
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
/** The files `config` lists, its compiler options as the TypeScript its directory installs reads them, and the configs it references, parsed once per lint run. */
|
|
1742
|
+
function project(config) {
|
|
1743
|
+
if (projects.has(config)) return projects.get(config);
|
|
1744
|
+
const parsed = ts.getParsedCommandLineOfConfigFile(config, void 0, HOST, void 0, void 0, VUE_EXTENSION);
|
|
1745
|
+
const result = parsed && read(parsed, dirname(config));
|
|
1746
|
+
projects.set(config, result);
|
|
1747
|
+
return result;
|
|
1748
|
+
}
|
|
1749
|
+
/** A parsed config as the project of the TypeScript `directory` installs. */
|
|
1750
|
+
function read(parsed, directory) {
|
|
1751
|
+
const options = asInstalledCompilerReads(parsed.options, installedTypeScriptMajor(directory));
|
|
1752
|
+
return {
|
|
1753
|
+
files: new Set(parsed.fileNames),
|
|
1754
|
+
options,
|
|
1755
|
+
references: (parsed.projectReferences ?? []).map((reference) => ts.resolveProjectReferencePath(reference)),
|
|
1756
|
+
automaticTypes: parsed.options.types === void 0 ? ts.getAutomaticTypeDirectiveNames(options, ts.sys) : [],
|
|
1757
|
+
nodeTypes: ts.getAutomaticTypeDirectiveNames(options, ts.sys).includes("node")
|
|
1758
|
+
};
|
|
1759
|
+
}
|
|
1760
|
+
/** The first config the nearest one or its references reach that lists `file`, skipping configs already visited. */
|
|
1761
|
+
function listing(nearest, file, visited) {
|
|
1762
|
+
const queue = [nearest];
|
|
1763
|
+
for (let config = queue.shift(); config; config = queue.shift()) {
|
|
1764
|
+
if (visited.has(config)) continue;
|
|
1765
|
+
visited.add(config);
|
|
1766
|
+
const parsed = project(config);
|
|
1767
|
+
if (parsed?.files.has(file)) return config;
|
|
1768
|
+
queue.push(...parsed?.references ?? []);
|
|
1769
|
+
}
|
|
1770
|
+
}
|
|
1771
|
+
/**
|
|
1772
|
+
* The tsconfig.json or jsconfig.json whose program type-aware lint gives `file`,
|
|
1773
|
+
* found as oxlint-tsgolint finds it: the nearest config, the configs it references,
|
|
1774
|
+
* then the same search from each config above. Undefined when none lists the file,
|
|
1775
|
+
* where tsgolint reads it in its inferred program.
|
|
1776
|
+
*/
|
|
1777
|
+
function programConfig(file) {
|
|
1778
|
+
const path = resolve(file).replaceAll("\\", "/");
|
|
1779
|
+
const visited = /* @__PURE__ */ new Set();
|
|
1780
|
+
let directory = dirname(path);
|
|
1781
|
+
for (;;) {
|
|
1782
|
+
const nearest = configAbove(directory);
|
|
1783
|
+
if (nearest === void 0) return void 0;
|
|
1784
|
+
const config = listing(nearest, path, visited);
|
|
1785
|
+
if (config !== void 0) return config;
|
|
1786
|
+
const parent = dirname(dirname(nearest));
|
|
1787
|
+
if (parent === dirname(nearest)) return void 0;
|
|
1788
|
+
directory = parent;
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
/**
|
|
1792
|
+
* Whether `specifier` imported from `file` names a module under `config`'s compiler
|
|
1793
|
+
* options: a file, a package, an untyped package's JavaScript, or a Node built-in
|
|
1794
|
+
* when the program loads `@types/node`. Any other ambient `declare module` is not a
|
|
1795
|
+
* resolution; only the program sees it.
|
|
1796
|
+
*/
|
|
1797
|
+
function resolves(config, specifier, file) {
|
|
1798
|
+
const parsed = project(config);
|
|
1799
|
+
if (parsed === void 0 || parsed.nodeTypes && isBuiltin(specifier)) return true;
|
|
1800
|
+
let cache = resolutionCaches.get(config);
|
|
1801
|
+
if (cache === void 0) {
|
|
1802
|
+
cache = ts.createModuleResolutionCache(dirname(config), (name) => ts.sys.useCaseSensitiveFileNames ? name : name.toLowerCase(), parsed.options);
|
|
1803
|
+
resolutionCaches.set(config, cache);
|
|
1804
|
+
}
|
|
1805
|
+
return ts.resolveModuleName(specifier, file, parsed.options, ts.sys, cache).resolvedModule !== void 0;
|
|
1806
|
+
}
|
|
1807
|
+
/**
|
|
1808
|
+
* The `@types` packages tsc loads into `config`'s program only because it lists no
|
|
1809
|
+
* `compilerOptions.types`. oxlint-tsgolint runs TypeScript 7, where `types` defaults
|
|
1810
|
+
* to none, so its program loads none of them.
|
|
1811
|
+
*/
|
|
1812
|
+
function automaticTypes(config) {
|
|
1813
|
+
return project(config)?.automaticTypes ?? [];
|
|
1814
|
+
}
|
|
1815
|
+
//#endregion
|
|
1816
|
+
//#region src/oxc/guardrails/vue-script.ts
|
|
1817
|
+
const BLOCK = /<!--[\s\S]*?-->|(<script\b(?:"[^"]*"|'[^']*'|[^>"'])*>)([\s\S]*?)(?=<\/script\s*>)/giu;
|
|
1818
|
+
const GENERIC = /\sgeneric\s*=/iu;
|
|
1819
|
+
const MODULE = "\nexport {};\n";
|
|
1820
|
+
function blank(text) {
|
|
1821
|
+
return Array.from(text, (character) => character === "\n" || character === "\r" ? character : " ".repeat(character.length)).join("");
|
|
1822
|
+
}
|
|
1823
|
+
/**
|
|
1824
|
+
* A Vue single-file component as TypeScript reads it: the contents of its
|
|
1825
|
+
* `<script>` and `<script setup>` blocks, every other character a space and
|
|
1826
|
+
* every line break kept, so an offset or a line is the same in the component,
|
|
1827
|
+
* then an `export {}`, as Vue compiles every component to a module. Undefined
|
|
1828
|
+
* when the text holds no script block, as oxlint passes a component's script
|
|
1829
|
+
* alone.
|
|
1830
|
+
*/
|
|
1831
|
+
function scriptView(sfc) {
|
|
1832
|
+
let view = "";
|
|
1833
|
+
let end = 0;
|
|
1834
|
+
for (const { 1: open, 2: content, index } of sfc.matchAll(BLOCK)) {
|
|
1835
|
+
if (open === void 0 || content === void 0) continue;
|
|
1836
|
+
const start = index + open.length;
|
|
1837
|
+
view += blank(sfc.slice(end, start)) + content;
|
|
1838
|
+
end = start + content.length;
|
|
1839
|
+
}
|
|
1840
|
+
return end === 0 ? void 0 : `${view}${blank(sfc.slice(end))}${MODULE}`;
|
|
1841
|
+
}
|
|
1842
|
+
/** Whether a script block of the component declares type parameters with `generic`, which only Vue's compiler brings into scope, so the view types them as error types. */
|
|
1843
|
+
function declaresTypeParameters(sfc) {
|
|
1844
|
+
return Array.from(sfc.matchAll(BLOCK)).some(({ 1: open }) => open !== void 0 && GENERIC.test(open));
|
|
1845
|
+
}
|
|
1846
|
+
//#endregion
|
|
1847
|
+
//#region src/oxc/guardrails/type-checker.ts
|
|
1848
|
+
const programs = /* @__PURE__ */ new Map();
|
|
1849
|
+
/** The source files every program of the lint run shares, parsed once per text and per set of compiler options that change a parse. */
|
|
1850
|
+
const documents = ts.createDocumentRegistry(ts.sys.useCaseSensitiveFileNames);
|
|
1851
|
+
function normalize(file) {
|
|
1852
|
+
return resolve(file).replaceAll("\\", "/");
|
|
1853
|
+
}
|
|
1854
|
+
function isVue(path) {
|
|
1855
|
+
return path.endsWith(".vue");
|
|
1856
|
+
}
|
|
1857
|
+
/** A file as the program reads it: a component as its script view, undefined when it has none, and any other file as written. */
|
|
1858
|
+
function viewOf(path, text) {
|
|
1859
|
+
return isVue(path) ? scriptView(text) : text;
|
|
1860
|
+
}
|
|
1861
|
+
function readView(host, name) {
|
|
1862
|
+
const text = host.readFile(name);
|
|
1863
|
+
return text === void 0 ? void 0 : viewOf(name, text);
|
|
1864
|
+
}
|
|
1865
|
+
function build(config, path, view) {
|
|
1866
|
+
const parsed = project(config);
|
|
1867
|
+
if (parsed === void 0) return void 0;
|
|
1868
|
+
const rootNames = [...parsed.files];
|
|
1869
|
+
const options = rootNames.some(isVue) ? {
|
|
1870
|
+
...parsed.options,
|
|
1871
|
+
allowNonTsExtensions: true
|
|
1872
|
+
} : parsed.options;
|
|
1873
|
+
const host = ts.createCompilerHost(options, true);
|
|
1874
|
+
host.getSourceFile = (name, language) => {
|
|
1875
|
+
const content = normalize(name) === path ? view : readView(host, name);
|
|
1876
|
+
return content === void 0 ? void 0 : documents.acquireDocument(name, options, ts.ScriptSnapshot.fromString(content), content, void 0, language);
|
|
1877
|
+
};
|
|
1878
|
+
const program = ts.createProgram({
|
|
1879
|
+
rootNames,
|
|
1880
|
+
options,
|
|
1881
|
+
host,
|
|
1882
|
+
oldProgram: programs.get(config)
|
|
1883
|
+
});
|
|
1884
|
+
programs.set(config, program);
|
|
1885
|
+
return program;
|
|
1886
|
+
}
|
|
1887
|
+
/**
|
|
1888
|
+
* The type checker of the tsconfig program that lists `file`, built once per
|
|
1889
|
+
* lint run and rebuilt from the previous one when `text` differs from what it
|
|
1890
|
+
* read. Undefined when no tsconfig lists the file, or when it is a component
|
|
1891
|
+
* with no script view.
|
|
1892
|
+
*/
|
|
1893
|
+
function typedFile(file, text) {
|
|
1894
|
+
const config = programConfig(file);
|
|
1895
|
+
if (config === void 0) return void 0;
|
|
1896
|
+
const path = normalize(file);
|
|
1897
|
+
const view = viewOf(path, text);
|
|
1898
|
+
if (view === void 0) return void 0;
|
|
1899
|
+
const cached = programs.get(config);
|
|
1900
|
+
const program = cached?.getSourceFile(path)?.text === view ? cached : build(config, path, view);
|
|
1901
|
+
const sourceFile = program?.getSourceFile(path);
|
|
1902
|
+
return program && sourceFile ? {
|
|
1903
|
+
checker: program.getTypeChecker(),
|
|
1904
|
+
options: program.getCompilerOptions(),
|
|
1905
|
+
sourceFile
|
|
1906
|
+
} : void 0;
|
|
1907
|
+
}
|
|
1908
|
+
/** The node `test` accepts that spans exactly the `range` of an ESTree node, as oxlint and ESLint give it. */
|
|
1909
|
+
function nodeAt(sourceFile, { range: [start, end] }, test) {
|
|
1910
|
+
let found;
|
|
1911
|
+
function visit(node) {
|
|
1912
|
+
if (found !== void 0 || node.pos > start || node.end < end) return;
|
|
1913
|
+
if (test(node) && node.getStart(sourceFile) === start && node.end === end) found = node;
|
|
1914
|
+
else ts.forEachChild(node, visit);
|
|
1915
|
+
}
|
|
1916
|
+
visit(sourceFile);
|
|
1917
|
+
return found;
|
|
1918
|
+
}
|
|
1919
|
+
//#endregion
|
|
1920
|
+
//#region src/oxc/guardrails/rules/no-unrun-query.ts
|
|
1921
|
+
const PACKAGE = "node_modules/@antelopejs/interface-database";
|
|
1922
|
+
const INTERFACE_DATABASE = `/${PACKAGE}/`;
|
|
1923
|
+
const PROMISE_CALLBACK_METHODS = /* @__PURE__ */ new Set([
|
|
1924
|
+
"then",
|
|
1925
|
+
"catch",
|
|
1926
|
+
"finally"
|
|
1927
|
+
]);
|
|
1928
|
+
const PNPM_ENTRY = "@antelopejs+interface-database@";
|
|
1929
|
+
const installs = /* @__PURE__ */ new Map();
|
|
1930
|
+
/** Whether `directory` installs interface-database: linked at `node_modules/@antelopejs`, or only in pnpm's store as a transitive dependency. A store it cannot read counts as installing it, so the rule falls back to the type check. */
|
|
1931
|
+
function installedIn(directory) {
|
|
1932
|
+
if (existsSync(join(directory, PACKAGE))) return true;
|
|
1933
|
+
const store = join(directory, "node_modules/.pnpm");
|
|
1934
|
+
try {
|
|
1935
|
+
return readdirSync(store).some((entry) => entry.startsWith(PNPM_ENTRY));
|
|
1936
|
+
} catch {
|
|
1937
|
+
return existsSync(store);
|
|
1938
|
+
}
|
|
1939
|
+
}
|
|
1940
|
+
/** Whether a `node_modules` at or above `directory` installs interface-database, so a repository without it never builds a type-checked program for this rule. */
|
|
1941
|
+
function installsInterfaceDatabase(directory) {
|
|
1942
|
+
const known = installs.get(directory);
|
|
1943
|
+
if (known !== void 0) return known;
|
|
1944
|
+
const parent = dirname(directory);
|
|
1945
|
+
const found = installedIn(directory) || parent !== directory && installsInterfaceDatabase(parent);
|
|
1946
|
+
installs.set(directory, found);
|
|
1947
|
+
return found;
|
|
1948
|
+
}
|
|
1949
|
+
function isFunction(node) {
|
|
1950
|
+
return node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
|
|
1951
|
+
}
|
|
1952
|
+
function enclosingFunction(node) {
|
|
1953
|
+
for (let current = node.parent; current !== null; current = current.parent) if (isFunction(current)) return current;
|
|
1954
|
+
return null;
|
|
1955
|
+
}
|
|
1956
|
+
/** Whether `call` hands its arguments to a promise that awaits what they return: `then`, `catch` or `finally`. */
|
|
1957
|
+
function isPromiseCallback(call) {
|
|
1958
|
+
return call.callee.type === "MemberExpression" && PROMISE_CALLBACK_METHODS.has(memberName(call.callee) ?? "");
|
|
1959
|
+
}
|
|
1960
|
+
/** Whether the caller awaits what the function returns: an async function, or a callback handed to `then`, `catch` or `finally`. */
|
|
1961
|
+
function resultIsAwaited(fn) {
|
|
1962
|
+
if (fn.async) return true;
|
|
1963
|
+
const call = fn.parent;
|
|
1964
|
+
return call?.type === "CallExpression" && call.arguments.some((argument) => argument === fn) && isPromiseCallback(call);
|
|
1965
|
+
}
|
|
1966
|
+
/** Whether `call` calls the first parameter of a function it sits in, as a `new Promise` executor calls its `resolve`. */
|
|
1967
|
+
function callsFirstParameter(call) {
|
|
1968
|
+
const { callee } = call;
|
|
1969
|
+
if (callee.type !== "Identifier") return false;
|
|
1970
|
+
for (let fn = enclosingFunction(call); fn !== null; fn = enclosingFunction(fn)) {
|
|
1971
|
+
const [first] = fn.params;
|
|
1972
|
+
const name = first?.type === "AssignmentPattern" ? first.left : first;
|
|
1973
|
+
if (name?.type === "Identifier" && name.name === callee.name) return true;
|
|
1974
|
+
}
|
|
1975
|
+
return false;
|
|
1976
|
+
}
|
|
1977
|
+
const HOLDERS = ts.SymbolFlags.ObjectLiteral | ts.SymbolFlags.Interface | ts.SymbolFlags.TypeLiteral;
|
|
1978
|
+
function declaredIn(symbol, path) {
|
|
1979
|
+
return symbol?.declarations?.some((declaration) => declaration.getSourceFile().fileName.includes(path)) === true;
|
|
1980
|
+
}
|
|
1981
|
+
/** Whether `type` is an interface-database query: its `run()` comes from that package. */
|
|
1982
|
+
function isQuery(checker, type) {
|
|
1983
|
+
return declaredIn(checker.getPropertyOfType(type, "run"), INTERFACE_DATABASE);
|
|
1984
|
+
}
|
|
1985
|
+
/** Whether `type` is an interface-database `Table`, alone, extended or intersected, which a value holds as a handle to query later rather than as a query to run: only a table has that package's `insert()`. */
|
|
1986
|
+
function isTable(checker, type) {
|
|
1987
|
+
return declaredIn(checker.getPropertyOfType(type, "insert"), INTERFACE_DATABASE);
|
|
1988
|
+
}
|
|
1989
|
+
/** Whether `type` is an object literal, or an interface or object type the project declares, as opposed to a class instance or a library type. */
|
|
1990
|
+
function isHolder(type) {
|
|
1991
|
+
const symbol = type.getSymbol();
|
|
1992
|
+
return symbol !== void 0 && (symbol.flags & HOLDERS) !== 0 && (symbol.flags & ts.SymbolFlags.Class) === 0 && !declaredIn(symbol, "/node_modules/");
|
|
1993
|
+
}
|
|
1994
|
+
function members$1(type) {
|
|
1995
|
+
return type.isUnion() ? type.types : [type];
|
|
1996
|
+
}
|
|
1997
|
+
/** The values an array, a tuple or a holder keeps, which awaiting it leaves as they are. */
|
|
1998
|
+
function heldValues(checker, type) {
|
|
1999
|
+
if (checker.isArrayType(type) || checker.isTupleType(type)) {
|
|
2000
|
+
const element = checker.getIndexTypeOfType(type, ts.IndexKind.Number);
|
|
2001
|
+
return element === void 0 ? [] : [element];
|
|
2002
|
+
}
|
|
2003
|
+
return isHolder(type) ? checker.getPropertiesOfType(type).map((property) => checker.getTypeOfSymbol(property)) : [];
|
|
2004
|
+
}
|
|
2005
|
+
/** Whether an array, a tuple or a holder of `type` keeps a query other than a table at any depth. */
|
|
2006
|
+
function holdsHeldQuery(checker, type, visited) {
|
|
2007
|
+
return heldValues(checker, type).some((value) => members$1(value).some((member) => {
|
|
2008
|
+
if (isQuery(checker, member)) return !isTable(checker, member);
|
|
2009
|
+
if (visited.has(member)) return false;
|
|
2010
|
+
visited.add(member);
|
|
2011
|
+
return holdsHeldQuery(checker, member, visited);
|
|
2012
|
+
}));
|
|
2013
|
+
}
|
|
2014
|
+
/** Whether an awaiting caller receives an unrun query from a value of `type`: the query itself, possibly behind a promise, or a query other than a table that arrays, tuples, object literals and project-declared object types keep. */
|
|
2015
|
+
function holdsQuery(checker, type) {
|
|
2016
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2017
|
+
return members$1(checker.getAwaitedType(type) ?? type).some((member) => isQuery(checker, member) || holdsHeldQuery(checker, member, visited));
|
|
2018
|
+
}
|
|
2019
|
+
/** What a function value handed to `then`, `catch` or `finally` resolves the promise to: the return types of its non-async call signatures, since an async one reports its own returns. */
|
|
2020
|
+
function callbackResult(checker, callback) {
|
|
2021
|
+
return checker.getTypeAtLocation(callback).getCallSignatures().filter(({ declaration }) => declaration === void 0 || (ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Async) === 0).map((signature) => checker.getReturnTypeOfSignature(signature));
|
|
2022
|
+
}
|
|
2023
|
+
const executorsByFile = /* @__PURE__ */ new WeakMap();
|
|
2024
|
+
/** The function a `new Promise` argument names: the function written there, or the function declaration or initialised variable its identifier resolves to. */
|
|
2025
|
+
function namedFunction(checker, argument) {
|
|
2026
|
+
if (ts.isFunctionExpression(argument) || ts.isArrowFunction(argument)) return argument;
|
|
2027
|
+
if (!ts.isIdentifier(argument)) return void 0;
|
|
2028
|
+
const declaration = checker.getSymbolAtLocation(argument)?.valueDeclaration;
|
|
2029
|
+
return declaration !== void 0 && ts.isVariableDeclaration(declaration) && declaration.initializer !== void 0 ? declaration.initializer : declaration;
|
|
2030
|
+
}
|
|
2031
|
+
/** The functions `sourceFile` hands to `new Promise` as its executor, inline or by name. */
|
|
2032
|
+
function promiseExecutors(checker, sourceFile) {
|
|
2033
|
+
const known = executorsByFile.get(sourceFile);
|
|
2034
|
+
if (known !== void 0) return known;
|
|
2035
|
+
const executors = /* @__PURE__ */ new Set();
|
|
2036
|
+
const visit = (node) => {
|
|
2037
|
+
const argument = ts.isNewExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === "Promise" ? node.arguments?.[0] : void 0;
|
|
2038
|
+
const executor = argument && namedFunction(checker, argument);
|
|
2039
|
+
if (executor !== void 0) executors.add(executor);
|
|
2040
|
+
ts.forEachChild(node, visit);
|
|
2041
|
+
};
|
|
2042
|
+
visit(sourceFile);
|
|
2043
|
+
executorsByFile.set(sourceFile, executors);
|
|
2044
|
+
return executors;
|
|
2045
|
+
}
|
|
2046
|
+
/** The value `call` resolves its promise with, when its callee is the first parameter of a function the file hands to `new Promise` as its executor. */
|
|
2047
|
+
function resolvedValue(checker, call) {
|
|
2048
|
+
const parameter = checker.getSymbolAtLocation(call.expression)?.valueDeclaration;
|
|
2049
|
+
const executor = parameter?.parent;
|
|
2050
|
+
return parameter !== void 0 && ts.isParameter(parameter) && executor !== void 0 && ts.isFunctionLike(executor) && executor.parameters[0] === parameter && promiseExecutors(checker, call.getSourceFile()).has(executor) ? call.arguments[0] : void 0;
|
|
2051
|
+
}
|
|
2052
|
+
/** Since interface-database 0.4.0 a query is no longer thenable, so an awaited function that returns one, alone or wrapped, resolves to the unrun query and sends nothing. */
|
|
2053
|
+
const noUnrunQueryRule = defineRule({
|
|
2054
|
+
meta: {
|
|
2055
|
+
type: "problem",
|
|
2056
|
+
docs: { description: "An async function, promise callback or promise executor resolves to no unrun interface-database query." },
|
|
2057
|
+
messages: { unrun: "This resolves to an interface-database query without running it, so the caller awaits the query object and nothing reaches the database. Call `.run()` on it." }
|
|
2058
|
+
},
|
|
2059
|
+
createOnce(context) {
|
|
2060
|
+
function check(value, resolved) {
|
|
2061
|
+
if (!installsInterfaceDatabase(dirname(resolve(context.filename)))) return;
|
|
2062
|
+
const typed = typedFile(context.filename, context.sourceCode.text);
|
|
2063
|
+
if (typed !== void 0 && resolved(typed).some((type) => holdsQuery(typed.checker, type))) context.report({
|
|
2064
|
+
node: value,
|
|
2065
|
+
messageId: "unrun"
|
|
2066
|
+
});
|
|
2067
|
+
}
|
|
2068
|
+
function typeOf(node, { checker }) {
|
|
2069
|
+
return node === void 0 ? [] : [checker.getTypeAtLocation(node)];
|
|
2070
|
+
}
|
|
2071
|
+
return {
|
|
2072
|
+
ReturnStatement(node) {
|
|
2073
|
+
const fn = enclosingFunction(node);
|
|
2074
|
+
if (fn === null || node.argument === null || !resultIsAwaited(fn)) return;
|
|
2075
|
+
check(node.argument, (typed) => typeOf(nodeAt(typed.sourceFile, node, ts.isReturnStatement)?.expression, typed));
|
|
2076
|
+
},
|
|
2077
|
+
ArrowFunctionExpression(node) {
|
|
2078
|
+
if (node.body.type === "BlockStatement" || !resultIsAwaited(node)) return;
|
|
2079
|
+
check(node.body, (typed) => typeOf(nodeAt(typed.sourceFile, node, ts.isArrowFunction)?.body, typed));
|
|
2080
|
+
},
|
|
2081
|
+
CallExpression(node) {
|
|
2082
|
+
if (isPromiseCallback(node)) {
|
|
2083
|
+
for (const argument of node.arguments) if (!isFunction(argument)) check(argument, (typed) => {
|
|
2084
|
+
const callback = nodeAt(typed.sourceFile, argument, ts.isExpression);
|
|
2085
|
+
return callback === void 0 ? [] : callbackResult(typed.checker, callback);
|
|
2086
|
+
});
|
|
2087
|
+
}
|
|
2088
|
+
const [value] = node.arguments;
|
|
2089
|
+
if (value !== void 0 && callsFirstParameter(node)) check(value, (typed) => {
|
|
2090
|
+
const call = nodeAt(typed.sourceFile, node, ts.isCallExpression);
|
|
2091
|
+
return typeOf(call && resolvedValue(typed.checker, call), typed);
|
|
2092
|
+
});
|
|
2093
|
+
}
|
|
2094
|
+
};
|
|
2095
|
+
}
|
|
2096
|
+
});
|
|
2097
|
+
//#endregion
|
|
2098
|
+
//#region src/oxc/guardrails/literal-fit.ts
|
|
2099
|
+
const MAX_DEPTH$1 = 8;
|
|
2100
|
+
const INDEX = /^\d+$/u;
|
|
2101
|
+
/** The type a written value has: a shorthand property reads its variable, as a direct assignment would. */
|
|
2102
|
+
function valueType(checker, value) {
|
|
2103
|
+
const shorthand = ts.isShorthandPropertyAssignment(value.parent) && value.parent.name === value ? checker.getShorthandAssignmentValueSymbol(value.parent) : void 0;
|
|
2104
|
+
return shorthand === void 0 ? checker.getTypeAtLocation(value) : checker.getTypeOfSymbolAtLocation(shorthand, value);
|
|
2105
|
+
}
|
|
2106
|
+
function propertyName(property) {
|
|
2107
|
+
if (!(ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)) || !(ts.isIdentifier(property.name) || ts.isStringLiteral(property.name) || ts.isNumericLiteral(property.name))) return void 0;
|
|
2108
|
+
return {
|
|
2109
|
+
name: property.name.text,
|
|
2110
|
+
value: ts.isPropertyAssignment(property) ? property.initializer : property.name
|
|
2111
|
+
};
|
|
2112
|
+
}
|
|
2113
|
+
function objectFits(checker, literal, type, depth) {
|
|
2114
|
+
const names = /* @__PURE__ */ new Set();
|
|
2115
|
+
for (const property of literal.properties) {
|
|
2116
|
+
const named = propertyName(property);
|
|
2117
|
+
if (named === void 0) return false;
|
|
2118
|
+
names.add(named.name);
|
|
2119
|
+
const field = checker.getPropertyOfType(type, named.name);
|
|
2120
|
+
const fieldType = field === void 0 ? checker.getIndexTypeOfType(type, ts.IndexKind.String) : checker.getTypeOfSymbolAtLocation(field, literal);
|
|
2121
|
+
if (fieldType === void 0 || !literalFits(checker, named.value, fieldType, depth + 1)) return false;
|
|
2122
|
+
}
|
|
2123
|
+
return checker.getPropertiesOfType(type).every((field) => (field.flags & ts.SymbolFlags.Optional) !== 0 || names.has(field.name));
|
|
2124
|
+
}
|
|
2125
|
+
function arrayFits(checker, literal, type, depth) {
|
|
2126
|
+
const { elements } = literal;
|
|
2127
|
+
if (elements.some((element) => ts.isSpreadElement(element) || ts.isOmittedExpression(element))) return false;
|
|
2128
|
+
if (checker.isTupleType(type)) {
|
|
2129
|
+
const slots = checker.getPropertiesOfType(type).filter((slot) => INDEX.test(slot.name));
|
|
2130
|
+
const required = slots.filter((slot) => (slot.flags & ts.SymbolFlags.Optional) === 0);
|
|
2131
|
+
return elements.length >= required.length && elements.length <= slots.length && elements.every((element, index) => literalFits(checker, element, checker.getTypeOfSymbolAtLocation(slots[index], literal), depth + 1));
|
|
2132
|
+
}
|
|
2133
|
+
const element = checker.getIndexTypeOfType(type, ts.IndexKind.Number);
|
|
2134
|
+
return element !== void 0 && elements.every((value) => literalFits(checker, value, element, depth + 1));
|
|
2135
|
+
}
|
|
2136
|
+
/**
|
|
2137
|
+
* Whether `expression` fits `type` the way a direct assignment types it:
|
|
2138
|
+
* literals inside object and array literals keep their literal types, which
|
|
2139
|
+
* `Object.assign`'s inferred source type widens. Object literals need every
|
|
2140
|
+
* required property and no undeclared one, tuples their length. Past the
|
|
2141
|
+
* depth cap a literal fits, so the rule stays silent rather than wrong.
|
|
2142
|
+
*/
|
|
2143
|
+
function literalFits(checker, expression, type, depth = 0) {
|
|
2144
|
+
let value = expression;
|
|
2145
|
+
while (ts.isParenthesizedExpression(value)) value = value.expression;
|
|
2146
|
+
if (checker.isTypeAssignableTo(valueType(checker, value), type)) return true;
|
|
2147
|
+
if (!ts.isObjectLiteralExpression(value) && !ts.isArrayLiteralExpression(value)) return false;
|
|
2148
|
+
if (depth === MAX_DEPTH$1) return true;
|
|
2149
|
+
if (type.isUnion()) return type.types.some((member) => literalFits(checker, value, member, depth));
|
|
2150
|
+
return ts.isObjectLiteralExpression(value) ? objectFits(checker, value, type, depth) : arrayFits(checker, value, type, depth);
|
|
2151
|
+
}
|
|
2152
|
+
//#endregion
|
|
2153
|
+
//#region src/oxc/guardrails/rules/no-unsafe-object-assign.ts
|
|
2154
|
+
const UNTYPED = ts.TypeFlags.Any | ts.TypeFlags.Unknown;
|
|
2155
|
+
const MAX_DEPTH = 4;
|
|
2156
|
+
function members(type) {
|
|
2157
|
+
return type.isUnion() ? type.types : [type];
|
|
2158
|
+
}
|
|
2159
|
+
/** Whether `type` is `any`, or holds `any` in a union, an intersection or the elements of an array or tuple, as in `any[]`. */
|
|
2160
|
+
function holdsAny(checker, type, depth = 0) {
|
|
2161
|
+
if (type.flags & ts.TypeFlags.Any) return true;
|
|
2162
|
+
if (depth === MAX_DEPTH) return false;
|
|
2163
|
+
const element = checker.isArrayType(type) || checker.isTupleType(type) ? checker.getIndexTypeOfType(type, ts.IndexKind.Number) : void 0;
|
|
2164
|
+
return (type.isUnionOrIntersection() ? type.types : element === void 0 ? [] : [element]).some((member) => holdsAny(checker, member, depth + 1));
|
|
2165
|
+
}
|
|
2166
|
+
/** Whether a declaration sits inside a generic class, interface or type alias, whose type parameters the public checker API cannot instantiate for a declaration. */
|
|
2167
|
+
function insideGeneric(declaration) {
|
|
2168
|
+
for (let node = declaration.parent; !ts.isSourceFile(node); node = node.parent) if ((ts.isClassLike(node) || ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node)) && node.typeParameters !== void 0) return true;
|
|
2169
|
+
return false;
|
|
2170
|
+
}
|
|
2171
|
+
/** The types a field accepts: its read type, and its setter's parameter type. Undefined when the setter sits inside a generic declaration, whose parameter type this rule cannot instantiate. */
|
|
2172
|
+
function acceptedTypes(checker, field, source) {
|
|
2173
|
+
const setter = field.declarations?.find(ts.isSetAccessorDeclaration);
|
|
2174
|
+
const parameter = setter?.parameters[0];
|
|
2175
|
+
const read = checker.getTypeOfSymbolAtLocation(field, source);
|
|
2176
|
+
if (setter === void 0 || parameter === void 0) return [read];
|
|
2177
|
+
return insideGeneric(setter) ? void 0 : [read, checker.getTypeAtLocation(parameter)];
|
|
2178
|
+
}
|
|
2179
|
+
function untyped(type) {
|
|
2180
|
+
return (type.flags & UNTYPED) !== 0;
|
|
2181
|
+
}
|
|
2182
|
+
function typeVerdict({ checker, exactOptional }, type, accepted, optional) {
|
|
2183
|
+
if (accepted.length === 0 || accepted.some(untyped)) return void 0;
|
|
2184
|
+
if (untyped(type) || holdsAny(checker, type) && !accepted.some((member) => holdsAny(checker, member))) return "untypedField";
|
|
2185
|
+
return (optional && !exactOptional ? members(type).filter((member) => !(member.flags & ts.TypeFlags.Undefined)) : [type]).every((member) => accepted.some((field) => checker.isTypeAssignableTo(member, field))) ? void 0 : "mistypedField";
|
|
2186
|
+
}
|
|
2187
|
+
function fieldWrite(check, property, field, source) {
|
|
2188
|
+
const { checker } = check;
|
|
2189
|
+
const accepted = acceptedTypes(checker, field, source);
|
|
2190
|
+
if (accepted === void 0) return void 0;
|
|
2191
|
+
const declaration = property.valueDeclaration;
|
|
2192
|
+
const written = declaration !== void 0 && declaration.getSourceFile() === source.getSourceFile() && declaration.pos >= source.pos && declaration.end <= source.end && (ts.isPropertyAssignment(declaration) || ts.isShorthandPropertyAssignment(declaration)) ? ts.isPropertyAssignment(declaration) ? declaration.initializer : declaration.name : void 0;
|
|
2193
|
+
const kind = written === void 0 ? typeVerdict(check, checker.getTypeOfSymbolAtLocation(property, source), accepted, (property.flags & ts.SymbolFlags.Optional) !== 0) : typeVerdict(check, valueType(checker, written), accepted, false);
|
|
2194
|
+
if (kind === void 0 || kind === "mistypedField" && written !== void 0 && accepted.some((type) => literalFits(checker, written, type))) return void 0;
|
|
2195
|
+
return {
|
|
2196
|
+
kind,
|
|
2197
|
+
name: property.name
|
|
2198
|
+
};
|
|
2199
|
+
}
|
|
2200
|
+
/** A primitive source member, such as the `false` of `flag && { ... }`, whose apparent members (`valueOf`) `Object.assign` never copies. */
|
|
2201
|
+
const PRIMITIVE = ts.TypeFlags.BooleanLike | ts.TypeFlags.NumberLike | ts.TypeFlags.StringLike | ts.TypeFlags.BigIntLike | ts.TypeFlags.ESSymbolLike | ts.TypeFlags.Null | ts.TypeFlags.Undefined | ts.TypeFlags.Void | ts.TypeFlags.Never;
|
|
2202
|
+
/** Whether the target declares a field that does not take `unknown`, which an untyped string-keyed source could overwrite. */
|
|
2203
|
+
function hasTypedField(checker, target) {
|
|
2204
|
+
return checker.getPropertiesOfType(target).some((field) => !untyped(checker.getTypeOfSymbol(field)));
|
|
2205
|
+
}
|
|
2206
|
+
function untypedKeys(checker, member) {
|
|
2207
|
+
return checker.getIndexInfosOfType(member).some((info) => (info.keyType.flags & ts.TypeFlags.String) !== 0 && untyped(info.type));
|
|
2208
|
+
}
|
|
2209
|
+
function sourceWrites(check, target, source) {
|
|
2210
|
+
const { checker } = check;
|
|
2211
|
+
const writes = /* @__PURE__ */ new Map();
|
|
2212
|
+
for (const member of members(checker.getTypeAtLocation(source))) {
|
|
2213
|
+
if (untyped(member) || untypedKeys(checker, member) && hasTypedField(checker, target)) writes.set("", { kind: "untypedSource" });
|
|
2214
|
+
if (member.flags & PRIMITIVE) continue;
|
|
2215
|
+
for (const property of checker.getPropertiesOfType(member)) {
|
|
2216
|
+
const field = checker.getPropertyOfType(target, property.name);
|
|
2217
|
+
const write = field && fieldWrite(check, property, field, source);
|
|
2218
|
+
if (write !== void 0) writes.set(property.name, write);
|
|
2219
|
+
}
|
|
2220
|
+
}
|
|
2221
|
+
return [...writes.values()];
|
|
2222
|
+
}
|
|
2223
|
+
/** `Object.assign<T, U>` returns `T & U` without comparing `U` with `T`, so a source can write `unknown`, `any` or a wrong type over a field `T` declares. */
|
|
2224
|
+
const noUnsafeObjectAssignRule = defineRule({
|
|
2225
|
+
meta: {
|
|
2226
|
+
type: "problem",
|
|
2227
|
+
docs: { description: "Object.assign writes no unknown, any or unassignable value over a field its target declares." },
|
|
2228
|
+
messages: {
|
|
2229
|
+
untypedSource: "This Object.assign source is typed `unknown` or `any`, or holds such values under an index signature, and it overwrites fields the target declares without a check: `Object.assign<T, U>` returns `T & U`. Parse it into a typed value first.",
|
|
2230
|
+
untypedField: "Object.assign writes an `unknown` or `any` value over `{{name}}`, a field the target declares, and TypeScript never compares the two: `Object.assign<T, U>` returns `T & U`. Parse the value into the field's type first.",
|
|
2231
|
+
mistypedField: "Object.assign writes a value over `{{name}}` that the target's `{{name}}` type does not accept, and TypeScript never compares the two: `Object.assign<T, U>` returns `T & U`. Assign the field directly so the compiler checks it."
|
|
2232
|
+
}
|
|
2233
|
+
},
|
|
2234
|
+
createOnce(context) {
|
|
2235
|
+
return { CallExpression(node) {
|
|
2236
|
+
if (callName(node.callee) !== "Object.assign" || node.arguments.length < 2 || context.filename.endsWith(".vue") && declaresTypeParameters(context.sourceCode.text)) return;
|
|
2237
|
+
const typed = typedFile(context.filename, context.sourceCode.text);
|
|
2238
|
+
const call = typed && nodeAt(typed.sourceFile, node, ts.isCallExpression);
|
|
2239
|
+
if (typed === void 0 || call === void 0) return;
|
|
2240
|
+
const declared = typed.checker.getTypeAtLocation(call.arguments[0]);
|
|
2241
|
+
const target = declared.isTypeParameter() ? typed.checker.getBaseConstraintOfType(declared) ?? declared : declared;
|
|
2242
|
+
if (typed.checker.getPropertiesOfType(target).length === 0) return;
|
|
2243
|
+
const check = {
|
|
2244
|
+
checker: typed.checker,
|
|
2245
|
+
exactOptional: typed.options.exactOptionalPropertyTypes === true
|
|
2246
|
+
};
|
|
2247
|
+
call.arguments.slice(1).forEach((source, index) => {
|
|
2248
|
+
if (ts.isSpreadElement(source)) return;
|
|
2249
|
+
for (const write of sourceWrites(check, target, source)) context.report({
|
|
2250
|
+
node: node.arguments[index + 1],
|
|
2251
|
+
messageId: write.kind,
|
|
2252
|
+
data: "name" in write ? { name: write.name } : {}
|
|
2253
|
+
});
|
|
2254
|
+
});
|
|
2255
|
+
} };
|
|
2256
|
+
}
|
|
2257
|
+
});
|
|
2258
|
+
//#endregion
|
|
2259
|
+
//#region src/oxc/guardrails/rules/require-assertion.ts
|
|
2260
|
+
function isInCallback(node, frame) {
|
|
2261
|
+
for (let current = node.parent; current !== null && current !== frame.node; current = current.parent) if (current === frame.callback) return true;
|
|
2262
|
+
return false;
|
|
2263
|
+
}
|
|
2264
|
+
/** Test callbacks must contain an assertion regardless of their framework or filename. */
|
|
2265
|
+
const requireAssertionRule = defineRule({
|
|
2266
|
+
meta: {
|
|
2267
|
+
type: "problem",
|
|
2268
|
+
docs: { description: "Require an assertion in every executable test callback." },
|
|
2269
|
+
messages: { missingAssertion: "Call an assertion in this test to verify its behavior." }
|
|
2270
|
+
},
|
|
2271
|
+
createOnce(context) {
|
|
2272
|
+
const frames = [];
|
|
2273
|
+
function recordAssertion(node) {
|
|
2274
|
+
const frame = frames.at(-1);
|
|
2275
|
+
if (frame !== void 0 && isAssertion(node) && isInCallback(node, frame)) frame.hasAssertion = true;
|
|
2276
|
+
}
|
|
2277
|
+
return {
|
|
2278
|
+
before() {
|
|
2279
|
+
frames.length = 0;
|
|
2280
|
+
},
|
|
2281
|
+
CallExpression(node) {
|
|
2282
|
+
const callback = testCallback(node);
|
|
2283
|
+
if (callback !== void 0) frames.push({
|
|
2284
|
+
node,
|
|
2285
|
+
callback,
|
|
2286
|
+
hasAssertion: false
|
|
2287
|
+
});
|
|
2288
|
+
else recordAssertion(node);
|
|
2289
|
+
},
|
|
2290
|
+
MemberExpression: recordAssertion,
|
|
2291
|
+
"CallExpression:exit"(node) {
|
|
2292
|
+
const frame = frames.at(-1);
|
|
2293
|
+
if (frame?.node !== node) return;
|
|
2294
|
+
frames.pop();
|
|
2295
|
+
if (!frame.hasAssertion) context.report({
|
|
2296
|
+
node,
|
|
2297
|
+
messageId: "missingAssertion"
|
|
2298
|
+
});
|
|
2299
|
+
}
|
|
2300
|
+
};
|
|
2301
|
+
}
|
|
2302
|
+
});
|
|
2303
|
+
//#endregion
|
|
2304
|
+
//#region src/oxc/guardrails/rules/require-tsconfig-include.ts
|
|
2305
|
+
const TYPE_LINTED$1 = /\.[cm]?[jt]sx?$/u;
|
|
2306
|
+
/** The files tsc reports an unresolved import in: TypeScript and Vue components, not JavaScript, which tsc leaves unchecked without `checkJs`. */
|
|
2307
|
+
const IMPORT_CHECKED = /\.(?:[cm]?ts|tsx|vue)$/u;
|
|
2308
|
+
/** A component import, which only Volar resolves: no tsconfig can, so a report would ask for a fix that does not exist. */
|
|
2309
|
+
const COMPONENT = /\.vue$/u;
|
|
2310
|
+
function isStringLiteral(node) {
|
|
2311
|
+
return node.type === "Literal" && typeof node.value === "string";
|
|
2312
|
+
}
|
|
2313
|
+
/** Whether the program of `config` finds the module `source` names in `filename`, by file, package or ambient declaration. */
|
|
2314
|
+
function moduleResolves(config, filename, text, source) {
|
|
2315
|
+
if (resolves(config, source.value, filename)) return true;
|
|
2316
|
+
const typed = typedFile(filename, text);
|
|
2317
|
+
const literal = typed && nodeAt(typed.sourceFile, source, ts.isStringLiteral);
|
|
2318
|
+
return typed === void 0 || literal === void 0 || typed.checker.getSymbolAtLocation(literal) !== void 0;
|
|
2319
|
+
}
|
|
2320
|
+
/** A file type-aware lint reads must be listed by a tsconfig, or tsgolint types it without the repository's compiler options and `types`; and that tsconfig must resolve its imports, or the program types everything imported through one as an error type. */
|
|
2321
|
+
const requireTsconfigIncludeRule = defineRule({
|
|
2322
|
+
meta: {
|
|
2323
|
+
type: "problem",
|
|
2324
|
+
docs: { description: "Every file type-aware lint reads is listed by a tsconfig.json that resolves its imports, so its findings use the repository's compiler options and types." },
|
|
2325
|
+
messages: {
|
|
2326
|
+
outside: "No tsconfig.json lists this file, so type-aware lint reads it in tsgolint's inferred program, without this repository's compiler options or `types`: Node and test-runner globals are error types, so `typescript/no-unsafe-*` reports here are false and findings that need those types go missing. List the file in a tsconfig.json that names the `types` it uses: one beside it that extends the root tsconfig, or, for a file at the root, a composite tsconfig that the root tsconfig references.",
|
|
2327
|
+
unresolved: "The tsconfig that lists this file cannot resolve `{{specifier}}`, so the program types everything imported from it as an error type: `typescript/no-unsafe-*` reports on those values are false and rules that need their types find none. List the file in a tsconfig that resolves the import: its `paths`, `baseUrl` or `node_modules` must reach the module. In a Nuxt layer that is a `tsconfig.json` that extends the generated `.nuxt/tsconfig.json`, with the lint script running `nuxt prepare` first."
|
|
2328
|
+
}
|
|
2329
|
+
},
|
|
2330
|
+
createOnce(context) {
|
|
2331
|
+
let importConfig;
|
|
2332
|
+
function check(source) {
|
|
2333
|
+
if (importConfig !== void 0 && !COMPONENT.test(source.value) && !moduleResolves(importConfig, context.filename, context.sourceCode.text, source)) context.report({
|
|
2334
|
+
node: source,
|
|
2335
|
+
messageId: "unresolved",
|
|
2336
|
+
data: { specifier: source.value }
|
|
2337
|
+
});
|
|
2338
|
+
}
|
|
2339
|
+
return {
|
|
2340
|
+
Program(node) {
|
|
2341
|
+
const config = programConfig(context.filename);
|
|
2342
|
+
importConfig = IMPORT_CHECKED.test(context.filename) ? config : void 0;
|
|
2343
|
+
if (TYPE_LINTED$1.test(context.filename) && config === void 0) context.report({
|
|
2344
|
+
node,
|
|
2345
|
+
messageId: "outside"
|
|
2346
|
+
});
|
|
2347
|
+
},
|
|
2348
|
+
ImportDeclaration(node) {
|
|
2349
|
+
if (node.specifiers.length > 0) check(node.source);
|
|
2350
|
+
},
|
|
2351
|
+
ExportNamedDeclaration(node) {
|
|
2352
|
+
if (node.source !== null) check(node.source);
|
|
2353
|
+
},
|
|
2354
|
+
ExportAllDeclaration(node) {
|
|
2355
|
+
check(node.source);
|
|
2356
|
+
},
|
|
2357
|
+
ImportExpression(node) {
|
|
2358
|
+
if (isStringLiteral(node.source)) check(node.source);
|
|
2359
|
+
}
|
|
2360
|
+
};
|
|
2361
|
+
}
|
|
2362
|
+
});
|
|
2363
|
+
//#endregion
|
|
2364
|
+
//#region src/oxc/guardrails/rules/require-tsconfig-types.ts
|
|
2365
|
+
const TYPE_LINTED = /\.[cm]?[jt]sx?$/u;
|
|
2366
|
+
/** The tsconfig of a file type-aware lint reads must list `compilerOptions.types`, or tsgolint loads none of the `@types` packages tsc loads. */
|
|
2367
|
+
const requireTsconfigTypesRule = defineRule({
|
|
2368
|
+
meta: {
|
|
2369
|
+
type: "problem",
|
|
2370
|
+
docs: { description: "The tsconfig.json of every file type-aware lint reads lists compilerOptions.types, so tsgolint loads the same @types packages as tsc." },
|
|
2371
|
+
messages: { missing: "{{config}} lists no `compilerOptions.types`, so tsc loads every `@types` package ({{packages}}) but type-aware lint, which runs TypeScript 7, loads none: Node and test-runner globals are error types, so `typescript/no-unsafe-*` reports here are false and findings that need those types go missing. Add `\"types\": [{{list}}]` to its compilerOptions, or to the config that generates it, listing the packages this code uses." }
|
|
2372
|
+
},
|
|
2373
|
+
createOnce(context) {
|
|
2374
|
+
return { Program(node) {
|
|
2375
|
+
if (!TYPE_LINTED.test(context.filename)) return;
|
|
2376
|
+
const config = programConfig(context.filename);
|
|
2377
|
+
if (config === void 0) return;
|
|
2378
|
+
const packages = automaticTypes(config);
|
|
2379
|
+
if (packages.length === 0) return;
|
|
2380
|
+
context.report({
|
|
2381
|
+
node,
|
|
2382
|
+
messageId: "missing",
|
|
2383
|
+
data: {
|
|
2384
|
+
config: relative(context.cwd, config).replaceAll("\\", "/"),
|
|
2385
|
+
packages: packages.join(", "),
|
|
2386
|
+
list: packages.map((name) => JSON.stringify(name)).join(", ")
|
|
2387
|
+
}
|
|
2388
|
+
});
|
|
2389
|
+
} };
|
|
2390
|
+
}
|
|
2391
|
+
});
|
|
2392
|
+
//#endregion
|
|
2393
|
+
//#region src/oxc/guardrails/index.ts
|
|
2394
|
+
/** AntelopeJS rules that reject comments, inherited-key lookups, presence-only type guards, inserts that follow a read of the same table or that a called function makes once an awaited read found nothing, unrun interface-database queries an awaited function returns, unchecked Object.assign writes, low-evidence tests, files no tsconfig lists and tsconfigs that list no `types`. */
|
|
2395
|
+
const guardrailsPlugin = eslintCompatPlugin({
|
|
2396
|
+
meta: { name: "antelopejs" },
|
|
2397
|
+
rules: {
|
|
2398
|
+
"no-comments": noCommentsRule,
|
|
2399
|
+
"no-fs-read-in-tests": noFsReadInTestsRule,
|
|
2400
|
+
"no-inherited-key-lookup": noInheritedKeyLookupRule,
|
|
2401
|
+
"no-presence-only-type-guard": noPresenceOnlyTypeGuardRule,
|
|
2402
|
+
"no-read-then-insert": noReadThenInsertRule,
|
|
2403
|
+
"no-constant-value-assertion": noConstantValueAssertionRule,
|
|
2404
|
+
"no-self-import-in-tests": noSelfImportInTestsRule,
|
|
2405
|
+
"no-typeof-function-only-test": noTypeofFunctionOnlyTestRule,
|
|
2406
|
+
"no-unrun-query": noUnrunQueryRule,
|
|
2407
|
+
"no-unsafe-object-assign": noUnsafeObjectAssignRule,
|
|
2408
|
+
"require-assertion": requireAssertionRule,
|
|
2409
|
+
"require-tsconfig-include": requireTsconfigIncludeRule,
|
|
2410
|
+
"require-tsconfig-types": requireTsconfigTypesRule
|
|
2411
|
+
}
|
|
2412
|
+
});
|
|
2413
|
+
//#endregion
|
|
2414
|
+
export { guardrailsPlugin as t };
|