@timmo001/oxlint-rules 0.1.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.
Files changed (51) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +102 -0
  3. package/THIRD_PARTY_NOTICES.md +10 -0
  4. package/dist/cli.js +78 -0
  5. package/dist/configs/effect.js +52 -0
  6. package/dist/configs/recommended.js +35 -0
  7. package/dist/effect/index.js +106 -0
  8. package/dist/upstream/anti-slop.js +1838 -0
  9. package/dist/upstream/effect.js +59 -0
  10. package/package.json +61 -0
  11. package/skills/add-oxlint-rule/SKILL.md +31 -0
  12. package/skills/install-timmo-oxlint-rules/SKILL.md +45 -0
  13. package/skills/install-timmo-oxlint-rules/scripts/copy.mjs +30 -0
  14. package/src/cli.ts +35 -0
  15. package/src/configs/effect.ts +20 -0
  16. package/src/configs/recommended.ts +29 -0
  17. package/src/effect/index.ts +12 -0
  18. package/src/effect/rules/no-try-catch-in-effect-generators.ts +140 -0
  19. package/src/install/copy.ts +77 -0
  20. package/src/jsr/effect-config.ts +7 -0
  21. package/src/jsr/effect.ts +7 -0
  22. package/src/jsr/recommended.ts +7 -0
  23. package/src/jsr/upstream-anti-slop.ts +7 -0
  24. package/src/jsr/upstream-effect.ts +7 -0
  25. package/src/upstream/anti-slop.ts +1 -0
  26. package/src/upstream/effect.ts +1 -0
  27. package/vendor/anti-slop/LICENSE +21 -0
  28. package/vendor/anti-slop/README.md +299 -0
  29. package/vendor/anti-slop/src/effect/index.ts +13 -0
  30. package/vendor/anti-slop/src/effect/rules/no-service-constructor-imports.ts +52 -0
  31. package/vendor/anti-slop/src/index.ts +41 -0
  32. package/vendor/anti-slop/src/rules/no-chained-type-assertions.ts +77 -0
  33. package/vendor/anti-slop/src/rules/no-conditional-empty-object-spread.ts +49 -0
  34. package/vendor/anti-slop/src/rules/no-known-value-widening.ts +427 -0
  35. package/vendor/anti-slop/src/rules/no-module-mocking.ts +91 -0
  36. package/vendor/anti-slop/src/rules/no-object-parameters.ts +83 -0
  37. package/vendor/anti-slop/src/rules/no-reflect-apply.ts +28 -0
  38. package/vendor/anti-slop/src/rules/no-reflect-get.ts +28 -0
  39. package/vendor/anti-slop/src/rules/no-runtime-typeof.ts +77 -0
  40. package/vendor/anti-slop/src/rules/no-shape-in-symbol-names.ts +46 -0
  41. package/vendor/anti-slop/src/rules/no-unknown-parameters.ts +69 -0
  42. package/vendor/anti-slop/src/rules/no-unknown-returns.ts +82 -0
  43. package/vendor/anti-slop/src/rules/no-unknown-type-aliases.ts +54 -0
  44. package/vendor/anti-slop/src/rules/no-unsafe-dictionary-type.ts +154 -0
  45. package/vendor/anti-slop/src/rules/no-widen-then-assert.ts +366 -0
  46. package/vendor/anti-slop/src/rules/require-safety-comment-for-type-assertion.ts +131 -0
  47. package/vendor/anti-slop/src/shared/dictionary-types.ts +515 -0
  48. package/vendor/anti-slop/src/shared/function-parameters.ts +49 -0
  49. package/vendor/anti-slop/src/shared/lexical-type-parameters.ts +61 -0
  50. package/vendor/anti-slop/src/shared/reflect-method.ts +35 -0
  51. package/vendor/anti-slop/src/shared/type-alias-resolution.ts +250 -0
@@ -0,0 +1,427 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import {
4
+ classifyUnsafeDictionaryValue,
5
+ classifyWideningTarget,
6
+ createTypeEnvironment,
7
+ isKnownEvidenceExpression,
8
+ type TypeEnvironment,
9
+ type WideningTarget,
10
+ } from "../shared/dictionary-types.ts";
11
+ import {
12
+ containsUnknownType,
13
+ functionParameterBindingName,
14
+ functionParameterTypeAnnotation,
15
+ } from "../shared/function-parameters.ts";
16
+
17
+ import type { ESTree, Scope, SourceCode, Variable } from "@oxlint/plugins";
18
+
19
+ type FunctionExpression = ESTree.ArrowFunctionExpression | ESTree.Function;
20
+
21
+ function unwrapExpression(expression: ESTree.Expression): ESTree.Expression {
22
+ let current = expression;
23
+ while (
24
+ current.type === "ParenthesizedExpression" ||
25
+ current.type === "TSAsExpression" ||
26
+ current.type === "TSSatisfiesExpression" ||
27
+ current.type === "TSTypeAssertion" ||
28
+ current.type === "TSNonNullExpression"
29
+ ) {
30
+ current = current.expression;
31
+ }
32
+ return current;
33
+ }
34
+
35
+ function resolveVariable(
36
+ sourceCode: SourceCode,
37
+ identifier: ESTree.IdentifierReference,
38
+ ): Variable | null {
39
+ let scope: Scope | null = sourceCode.getScope(identifier);
40
+ while (scope !== null) {
41
+ const variable = scope.set.get(identifier.name);
42
+ if (variable !== undefined) return variable;
43
+ scope = scope.upper;
44
+ }
45
+ return null;
46
+ }
47
+
48
+ function variableDeclarator(variable: Variable): ESTree.VariableDeclarator | null {
49
+ if (variable.defs.length !== 1) return null;
50
+ const [definition] = variable.defs;
51
+ return definition?.type === "Variable" && definition.node.type === "VariableDeclarator"
52
+ ? definition.node
53
+ : null;
54
+ }
55
+
56
+ function isStableConstVariable(variable: Variable, declarator: ESTree.VariableDeclarator): boolean {
57
+ return (
58
+ declarator.parent.type === "VariableDeclaration" &&
59
+ declarator.parent.kind === "const" &&
60
+ variable.references.every((reference) => reference.init || !reference.isWrite())
61
+ );
62
+ }
63
+
64
+ function hasKnownEvidence(
65
+ sourceCode: SourceCode,
66
+ expression: ESTree.Expression,
67
+ visitedVariables = new Set<Variable>(),
68
+ ): boolean {
69
+ if (isKnownEvidenceExpression(expression)) return true;
70
+ const unwrapped = unwrapExpression(expression);
71
+ if (unwrapped.type !== "Identifier") return false;
72
+ const variable = resolveVariable(sourceCode, unwrapped);
73
+ if (variable === null || visitedVariables.has(variable)) return false;
74
+ const declarator = variableDeclarator(variable);
75
+ if (
76
+ declarator === null ||
77
+ declarator.init === null ||
78
+ !isStableConstVariable(variable, declarator)
79
+ ) {
80
+ return false;
81
+ }
82
+ visitedVariables.add(variable);
83
+ return hasKnownEvidence(sourceCode, declarator.init, visitedVariables);
84
+ }
85
+
86
+ function isFunctionExpression(node: ESTree.Node): node is FunctionExpression {
87
+ return (
88
+ node.type === "ArrowFunctionExpression" ||
89
+ node.type === "FunctionDeclaration" ||
90
+ node.type === "FunctionExpression" ||
91
+ node.type === "TSDeclareFunction" ||
92
+ node.type === "TSEmptyBodyFunctionExpression"
93
+ );
94
+ }
95
+
96
+ function localFunctionForCall(
97
+ sourceCode: SourceCode,
98
+ callee: ESTree.Expression,
99
+ ): FunctionExpression | null {
100
+ const unwrapped = unwrapExpression(callee);
101
+ if (isFunctionExpression(unwrapped)) return unwrapped;
102
+ if (unwrapped.type !== "Identifier") return null;
103
+ const variable = resolveVariable(sourceCode, unwrapped);
104
+ if (variable === null || variable.defs.length !== 1) return null;
105
+ const [definition] = variable.defs;
106
+ if (definition === undefined) return null;
107
+ if (definition.type === "FunctionName" && isFunctionExpression(definition.node)) {
108
+ return definition.node;
109
+ }
110
+ if (definition.type !== "Variable" || definition.node.type !== "VariableDeclarator") {
111
+ return null;
112
+ }
113
+ const initializer = definition.node.init;
114
+ if (initializer === null) return null;
115
+ const unwrappedInitializer = unwrapExpression(initializer);
116
+ return isFunctionExpression(unwrappedInitializer) ? unwrappedInitializer : null;
117
+ }
118
+
119
+ function variableTypeAnnotation(
120
+ sourceCode: SourceCode,
121
+ variable: Variable,
122
+ ): ESTree.TSTypeAnnotation | null {
123
+ if (variable.defs.length !== 1) return null;
124
+ const [definition] = variable.defs;
125
+ if (definition === undefined) return null;
126
+ if (
127
+ definition.type === "Variable" &&
128
+ definition.node.type === "VariableDeclarator" &&
129
+ definition.node.id.type === "Identifier"
130
+ ) {
131
+ return definition.node.id.typeAnnotation ?? null;
132
+ }
133
+ if (definition.type !== "Parameter" || !isFunctionExpression(definition.node)) {
134
+ return null;
135
+ }
136
+ const parameter = definition.node.params.find(
137
+ (candidate) =>
138
+ functionParameterBindingName(candidate, sourceCode) === variable.name,
139
+ );
140
+ return parameter === undefined ? null : (functionParameterTypeAnnotation(parameter) ?? null);
141
+ }
142
+
143
+ function hasInformativeType(
144
+ type: ESTree.TSType,
145
+ environment: TypeEnvironment,
146
+ ): boolean {
147
+ return classifyUnsafeDictionaryValue(type, environment) === null;
148
+ }
149
+
150
+ function hasKnownCallArgumentEvidence(
151
+ sourceCode: SourceCode,
152
+ expression: ESTree.Expression,
153
+ environment: TypeEnvironment,
154
+ visitedVariables = new Set<Variable>(),
155
+ ): boolean {
156
+ if (expression.type === "ParenthesizedExpression" || expression.type === "TSNonNullExpression") {
157
+ return hasKnownCallArgumentEvidence(
158
+ sourceCode,
159
+ expression.expression,
160
+ environment,
161
+ visitedVariables,
162
+ );
163
+ }
164
+ if (expression.type === "TSAsExpression" || expression.type === "TSTypeAssertion") {
165
+ return hasInformativeType(expression.typeAnnotation, environment);
166
+ }
167
+ if (expression.type === "TSSatisfiesExpression") {
168
+ return hasKnownCallArgumentEvidence(
169
+ sourceCode,
170
+ expression.expression,
171
+ environment,
172
+ visitedVariables,
173
+ );
174
+ }
175
+ if (expression.type === "CallExpression") {
176
+ const owner = localFunctionForCall(sourceCode, expression.callee);
177
+ const returnType = owner?.returnType?.typeAnnotation;
178
+ return returnType !== undefined && hasInformativeType(returnType, environment);
179
+ }
180
+ if (expression.type !== "Identifier") return isKnownEvidenceExpression(expression);
181
+ const variable = resolveVariable(sourceCode, expression);
182
+ if (variable === null || visitedVariables.has(variable)) return false;
183
+ const annotation = variableTypeAnnotation(sourceCode, variable);
184
+ if (annotation !== null) {
185
+ return hasInformativeType(annotation.typeAnnotation, environment);
186
+ }
187
+ const declarator = variableDeclarator(variable);
188
+ if (
189
+ declarator === null ||
190
+ declarator.init === null ||
191
+ !isStableConstVariable(variable, declarator)
192
+ ) {
193
+ return false;
194
+ }
195
+ visitedVariables.add(variable);
196
+ return hasKnownCallArgumentEvidence(
197
+ sourceCode,
198
+ declarator.init,
199
+ environment,
200
+ visitedVariables,
201
+ );
202
+ }
203
+
204
+ function typePredicateSubjectIndex(
205
+ sourceCode: SourceCode,
206
+ owner: FunctionExpression,
207
+ ): number | null {
208
+ const predicate = owner.returnType?.typeAnnotation;
209
+ if (predicate?.type !== "TSTypePredicate" || predicate.parameterName.type !== "Identifier") {
210
+ return null;
211
+ }
212
+ const predicateParameterName = predicate.parameterName.name;
213
+ const index = owner.params.findIndex(
214
+ (parameter) =>
215
+ functionParameterBindingName(parameter, sourceCode) === predicateParameterName,
216
+ );
217
+ return index === -1 ? null : index;
218
+ }
219
+
220
+ function annotationTarget(
221
+ annotation: ESTree.TSTypeAnnotation | null | undefined,
222
+ environment: TypeEnvironment,
223
+ ): WideningTarget | null {
224
+ return annotation === null || annotation === undefined
225
+ ? null
226
+ : classifyWideningTarget(annotation.typeAnnotation, environment);
227
+ }
228
+
229
+ function enclosingFunction(node: ESTree.Node): FunctionExpression | null {
230
+ let current: ESTree.Node | null = node.parent;
231
+ while (current !== null && current.type !== "Program") {
232
+ if (
233
+ current.type === "ArrowFunctionExpression" ||
234
+ current.type === "FunctionDeclaration" ||
235
+ current.type === "FunctionExpression"
236
+ ) {
237
+ return current;
238
+ }
239
+ current = current.parent;
240
+ }
241
+ return null;
242
+ }
243
+
244
+ function sourceKeyName(sourceCode: SourceCode, key: ESTree.PropertyKey): string {
245
+ if (key.type === "Identifier" || key.type === "PrivateIdentifier") return key.name;
246
+ if (key.type === "Literal") return String(key.value);
247
+ return sourceCode.getText(key);
248
+ }
249
+
250
+ function functionName(sourceCode: SourceCode, owner: FunctionExpression | null): string {
251
+ if (owner === null) return "anonymous function";
252
+ if (owner.id !== null) return owner.id.name;
253
+ const parent = owner.parent;
254
+ if (parent.type === "VariableDeclarator" && parent.id.type === "Identifier")
255
+ return parent.id.name;
256
+ if (parent.type === "MethodDefinition") return sourceKeyName(sourceCode, parent.key);
257
+ return "anonymous function";
258
+ }
259
+
260
+ function isEmptyObjectExpression(expression: ESTree.Expression): boolean {
261
+ const unwrapped = unwrapExpression(expression);
262
+ return unwrapped.type === "ObjectExpression" && unwrapped.properties.length === 0;
263
+ }
264
+
265
+ function isDictionaryAccumulatorTarget(destination: WideningTarget): boolean {
266
+ return destination.kind === "open dictionary" || destination.kind === "generic container";
267
+ }
268
+
269
+ function hasParentAssertion(node: ESTree.Node): boolean {
270
+ return node.parent?.type === "TSAsExpression" || node.parent?.type === "TSTypeAssertion";
271
+ }
272
+
273
+ /** Detect sound syntactic cases where a known value is explicitly widened and loses evidence. */
274
+ export const noKnownValueWideningRule = defineRule({
275
+ meta: {
276
+ type: "problem",
277
+ docs: {
278
+ description:
279
+ "Disallow syntactically established values from flowing into explicitly broad or anonymous target types that discard useful evidence.",
280
+ },
281
+ messages: {
282
+ widening:
283
+ "The explicit {{target}} type on {{subject}} discards known type evidence. Keep inference, validate with `satisfies`, or use a named owner contract.",
284
+ },
285
+ },
286
+ createOnce(context) {
287
+ let environment: TypeEnvironment | null = null;
288
+
289
+ const reportFlow = (
290
+ expression: ESTree.Expression,
291
+ destination: WideningTarget | null,
292
+ subject: string,
293
+ ) => {
294
+ if (destination === null) return;
295
+ if (
296
+ isDictionaryAccumulatorTarget(destination) &&
297
+ isEmptyObjectExpression(expression)
298
+ ) {
299
+ return;
300
+ }
301
+ if (!hasKnownEvidence(context.sourceCode, expression)) return;
302
+ context.report({
303
+ node: expression,
304
+ messageId: "widening",
305
+ data: { subject, target: destination.kind },
306
+ });
307
+ };
308
+
309
+ const targetFromAnnotation = (annotation: ESTree.TSTypeAnnotation | null | undefined) =>
310
+ environment === null ? null : annotationTarget(annotation, environment);
311
+
312
+ return {
313
+ Program(node) {
314
+ environment = createTypeEnvironment(
315
+ node,
316
+ context.sourceCode.visitorKeys,
317
+ );
318
+ },
319
+ VariableDeclarator(node) {
320
+ if (node.init === null || node.id.type !== "Identifier") return;
321
+ reportFlow(
322
+ node.init,
323
+ targetFromAnnotation(node.id.typeAnnotation),
324
+ `binding \`${node.id.name}\``,
325
+ );
326
+ },
327
+ PropertyDefinition(node) {
328
+ if (node.value === null) return;
329
+ reportFlow(
330
+ node.value,
331
+ targetFromAnnotation(node.typeAnnotation),
332
+ `property \`${sourceKeyName(context.sourceCode, node.key)}\``,
333
+ );
334
+ },
335
+ AccessorProperty(node) {
336
+ if (node.value === null) return;
337
+ reportFlow(
338
+ node.value,
339
+ targetFromAnnotation(node.typeAnnotation),
340
+ `property \`${sourceKeyName(context.sourceCode, node.key)}\``,
341
+ );
342
+ },
343
+ AssignmentExpression(node) {
344
+ if (node.operator !== "=" || node.left.type !== "Identifier") return;
345
+ const variable = resolveVariable(context.sourceCode, node.left);
346
+ if (variable === null) return;
347
+ const declarator = variableDeclarator(variable);
348
+ if (declarator === null || declarator.id.type !== "Identifier") return;
349
+ reportFlow(
350
+ node.right,
351
+ targetFromAnnotation(declarator.id.typeAnnotation),
352
+ `binding \`${declarator.id.name}\``,
353
+ );
354
+ },
355
+ CallExpression(node) {
356
+ if (environment === null) return;
357
+ const owner = localFunctionForCall(context.sourceCode, node.callee);
358
+ if (owner === null) return;
359
+ const parameterIndex = typePredicateSubjectIndex(context.sourceCode, owner);
360
+ if (parameterIndex === null) return;
361
+ const parameter = owner.params[parameterIndex];
362
+ const argument = node.arguments[parameterIndex];
363
+ if (parameter === undefined || argument === undefined || argument.type === "SpreadElement") {
364
+ return;
365
+ }
366
+ const parameterAnnotation = functionParameterTypeAnnotation(parameter);
367
+ if (
368
+ parameterAnnotation === null ||
369
+ parameterAnnotation === undefined ||
370
+ !containsUnknownType(parameterAnnotation.typeAnnotation)
371
+ ) {
372
+ return;
373
+ }
374
+ if (
375
+ !hasKnownCallArgumentEvidence(
376
+ context.sourceCode,
377
+ argument,
378
+ environment,
379
+ )
380
+ ) {
381
+ return;
382
+ }
383
+ context.report({
384
+ node: argument,
385
+ messageId: "widening",
386
+ data: {
387
+ subject: `argument for parameter \`${functionParameterBindingName(parameter, context.sourceCode)}\` of \`${functionName(context.sourceCode, owner)}\``,
388
+ target: "unknown",
389
+ },
390
+ });
391
+ },
392
+ ReturnStatement(node) {
393
+ if (node.argument === null) return;
394
+ const owner = enclosingFunction(node);
395
+ reportFlow(
396
+ node.argument,
397
+ targetFromAnnotation(owner?.returnType),
398
+ `return value of \`${functionName(context.sourceCode, owner)}\``,
399
+ );
400
+ },
401
+ ArrowFunctionExpression(node) {
402
+ if (node.body.type === "BlockStatement") return;
403
+ reportFlow(
404
+ node.body,
405
+ targetFromAnnotation(node.returnType),
406
+ `return value of \`${functionName(context.sourceCode, node)}\``,
407
+ );
408
+ },
409
+ TSAsExpression(node) {
410
+ if (environment === null || hasParentAssertion(node)) return;
411
+ reportFlow(
412
+ node.expression,
413
+ classifyWideningTarget(node.typeAnnotation, environment),
414
+ "assertion",
415
+ );
416
+ },
417
+ TSTypeAssertion(node) {
418
+ if (environment === null || hasParentAssertion(node)) return;
419
+ reportFlow(
420
+ node.expression,
421
+ classifyWideningTarget(node.typeAnnotation, environment),
422
+ "assertion",
423
+ );
424
+ },
425
+ };
426
+ },
427
+ });
@@ -0,0 +1,91 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import type { ESTree, Scope, SourceCode, Variable } from "@oxlint/plugins";
4
+
5
+ const moduleMockMethods = new Set(["doMock", "mock", "unstable_mockModule"]);
6
+
7
+ function resolveVariable(
8
+ sourceCode: SourceCode,
9
+ identifier: ESTree.IdentifierReference,
10
+ ): Variable | null {
11
+ let scope: Scope | null = sourceCode.getScope(identifier);
12
+ while (scope !== null) {
13
+ const variable = scope.set.get(identifier.name);
14
+ if (variable !== undefined) return variable;
15
+ scope = scope.upper;
16
+ }
17
+ return null;
18
+ }
19
+
20
+ function importedName(node: ESTree.Node): string | null {
21
+ if (node.type !== "ImportSpecifier") return null;
22
+ return node.imported.type === "Identifier" ? node.imported.name : node.imported.value;
23
+ }
24
+
25
+ function isTestFrameworkObject(
26
+ sourceCode: SourceCode,
27
+ expression: ESTree.Expression,
28
+ ): expression is ESTree.IdentifierReference {
29
+ if (expression.type !== "Identifier") return false;
30
+ if (
31
+ (expression.name === "vi" || expression.name === "jest") &&
32
+ sourceCode.isGlobalReference(expression)
33
+ ) {
34
+ return true;
35
+ }
36
+
37
+ const variable = resolveVariable(sourceCode, expression);
38
+ if (variable === null || variable.defs.length === 0) {
39
+ return expression.name === "vi" || expression.name === "jest";
40
+ }
41
+ return variable.defs.some((definition) => {
42
+ if (definition.type !== "ImportBinding" || definition.parent?.type !== "ImportDeclaration") {
43
+ return false;
44
+ }
45
+ const source = definition.parent.source.value;
46
+ const name = importedName(definition.node);
47
+ return (source === "vitest" && name === "vi") || (source === "@jest/globals" && name === "jest");
48
+ });
49
+ }
50
+
51
+ function moduleMockCall(sourceCode: SourceCode, callee: ESTree.Expression): boolean {
52
+ if (!("property" in callee) || !("object" in callee) || !("computed" in callee)) return false;
53
+ if (!isTestFrameworkObject(sourceCode, callee.object)) return false;
54
+ const property = callee.property;
55
+ const method = callee.computed
56
+ ? property.type === "Literal" &&
57
+ (property.value === "doMock" ||
58
+ property.value === "mock" ||
59
+ property.value === "unstable_mockModule")
60
+ ? property.value
61
+ : null
62
+ : property.type === "Identifier"
63
+ ? property.name
64
+ : null;
65
+ return method !== null && moduleMockMethods.has(method);
66
+ }
67
+
68
+ /** Ban test framework module mocking in favor of real dependency seams. */
69
+ export const noModuleMockingRule = defineRule({
70
+ meta: {
71
+ type: "problem",
72
+ docs: {
73
+ description:
74
+ "Disallow Vitest and Jest module mocking; tests must replace dependencies through real interfaces.",
75
+ },
76
+ messages: {
77
+ moduleMock:
78
+ "Replace module mocking with dependency injection through a real interface, service layer, or faithful test implementation.",
79
+ },
80
+ },
81
+ createOnce(context) {
82
+ return {
83
+ CallExpression(node) {
84
+ if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression") return;
85
+ if (moduleMockCall(context.sourceCode, node.callee)) {
86
+ context.report({ node, messageId: "moduleMock" });
87
+ }
88
+ },
89
+ };
90
+ },
91
+ });
@@ -0,0 +1,83 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import type { ESTree } from "@oxlint/plugins";
4
+
5
+ import {
6
+ functionParameterBindingName,
7
+ functionParameterTypeAnnotation,
8
+ } from "../shared/function-parameters.ts";
9
+ import {
10
+ createTypeAliasEnvironment,
11
+ resolvedTypeMatches,
12
+ type TypeAliasEnvironment,
13
+ } from "../shared/type-alias-resolution.ts";
14
+ type ParameterOwner =
15
+ | ESTree.ArrowFunctionExpression
16
+ | ESTree.Function
17
+ | ESTree.TSCallSignatureDeclaration
18
+ | ESTree.TSConstructSignatureDeclaration
19
+ | ESTree.TSConstructorType
20
+ | ESTree.TSFunctionType
21
+ | ESTree.TSMethodSignature;
22
+
23
+ /** Ban the broad object type on function inputs, including local aliases to object. */
24
+ export const noObjectParametersRule = defineRule({
25
+ meta: {
26
+ type: "problem",
27
+ docs: {
28
+ description:
29
+ "Disallow object function parameters; inputs must use an owner-provided type and be parsed at their boundary.",
30
+ },
31
+ messages: {
32
+ objectParameter:
33
+ "Parameter `{{parameter}}` uses the broad `object` type. Accept a named owner type; parse external input at its boundary before calling this function.",
34
+ },
35
+ },
36
+ createOnce(context) {
37
+ let environment: TypeAliasEnvironment | null = null;
38
+
39
+ const resolvesToObject = (type: ESTree.TSType): boolean =>
40
+ environment !== null &&
41
+ resolvedTypeMatches(type, environment, (resolved, matches) => {
42
+ if (resolved.type === "TSObjectKeyword") return true;
43
+ if (resolved.type === "TSParenthesizedType") {
44
+ return matches(resolved.typeAnnotation);
45
+ }
46
+ return (
47
+ resolved.type === "TSUnionType" && resolved.types.some(matches)
48
+ );
49
+ });
50
+
51
+ const checkParameters = (node: ParameterOwner) => {
52
+ for (const parameter of node.params) {
53
+ const annotation = functionParameterTypeAnnotation(parameter);
54
+ if (annotation === null || annotation === undefined) continue;
55
+ if (!resolvesToObject(annotation.typeAnnotation)) continue;
56
+ context.report({
57
+ node: annotation.typeAnnotation,
58
+ messageId: "objectParameter",
59
+ data: { parameter: functionParameterBindingName(parameter, context.sourceCode) },
60
+ });
61
+ }
62
+ };
63
+
64
+ return {
65
+ Program(node) {
66
+ environment = createTypeAliasEnvironment(
67
+ node,
68
+ context.sourceCode.visitorKeys,
69
+ );
70
+ },
71
+ ArrowFunctionExpression: checkParameters,
72
+ FunctionDeclaration: checkParameters,
73
+ FunctionExpression: checkParameters,
74
+ TSCallSignatureDeclaration: checkParameters,
75
+ TSConstructSignatureDeclaration: checkParameters,
76
+ TSConstructorType: checkParameters,
77
+ TSDeclareFunction: checkParameters,
78
+ TSEmptyBodyFunctionExpression: checkParameters,
79
+ TSFunctionType: checkParameters,
80
+ TSMethodSignature: checkParameters,
81
+ };
82
+ },
83
+ });
@@ -0,0 +1,28 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import { isGlobalReflectMethodCall } from "../shared/reflect-method.ts";
4
+
5
+ /** Ban Reflect.apply, which bypasses ordinary typed function calls. */
6
+ export const noReflectApplyRule = defineRule({
7
+ meta: {
8
+ type: "problem",
9
+ docs: {
10
+ description:
11
+ "Disallow Reflect.apply; call typed functions directly or model dynamic dispatch behind an interface.",
12
+ },
13
+ messages: {
14
+ reflectApply:
15
+ "Replace `Reflect.apply` with a typed function call. Model dynamic dispatch behind a named interface.",
16
+ },
17
+ },
18
+ createOnce(context) {
19
+ return {
20
+ CallExpression(node) {
21
+ if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression") return;
22
+ if (isGlobalReflectMethodCall(context.sourceCode, node.callee, "apply")) {
23
+ context.report({ node, messageId: "reflectApply" });
24
+ }
25
+ },
26
+ };
27
+ },
28
+ });
@@ -0,0 +1,28 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import { isGlobalReflectMethodCall } from "../shared/reflect-method.ts";
4
+
5
+ /** Ban Reflect.get, which bypasses ordinary property access and useful type evidence. */
6
+ export const noReflectGetRule = defineRule({
7
+ meta: {
8
+ type: "problem",
9
+ docs: {
10
+ description:
11
+ "Disallow Reflect.get; use typed property access or parse dynamic input into a domain type.",
12
+ },
13
+ messages: {
14
+ reflectGet:
15
+ "Replace `Reflect.get` with typed property access. Parse dynamic input into a named domain type before reading it.",
16
+ },
17
+ },
18
+ createOnce(context) {
19
+ return {
20
+ CallExpression(node) {
21
+ if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression") return;
22
+ if (isGlobalReflectMethodCall(context.sourceCode, node.callee, "get")) {
23
+ context.report({ node, messageId: "reflectGet" });
24
+ }
25
+ },
26
+ };
27
+ },
28
+ });