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