eslint-plugin-ts-data-forge 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.
- package/LICENSE +201 -0
- package/README.md +57 -0
- package/package.json +74 -0
- package/src/index.mts +7 -0
- package/src/plugin.mts +9 -0
- package/src/rule-types.mts +326 -0
- package/src/rules/ast-utils.mts +169 -0
- package/src/rules/branded-number-types.mts +47 -0
- package/src/rules/constants.mts +1 -0
- package/src/rules/import-utils.mts +152 -0
- package/src/rules/index.mts +1 -0
- package/src/rules/no-unnecessary-type-guard.mts +515 -0
- package/src/rules/no-unnecessary-type-guard.test.mts +368 -0
- package/src/rules/prefer-arr-is-array.mts +98 -0
- package/src/rules/prefer-arr-is-array.test.mts +62 -0
- package/src/rules/prefer-arr-is-bounded-length-array.mts +143 -0
- package/src/rules/prefer-arr-is-bounded-length-array.test.mts +154 -0
- package/src/rules/prefer-arr-is-fixed-length-array.mts +149 -0
- package/src/rules/prefer-arr-is-fixed-length-array.test.mts +214 -0
- package/src/rules/prefer-arr-is-max-length-array.mts +141 -0
- package/src/rules/prefer-arr-is-max-length-array.test.mts +188 -0
- package/src/rules/prefer-arr-is-min-length-array.mts +141 -0
- package/src/rules/prefer-arr-is-min-length-array.test.mts +202 -0
- package/src/rules/prefer-arr-is-non-empty.mts +131 -0
- package/src/rules/prefer-arr-is-non-empty.test.mts +90 -0
- package/src/rules/prefer-arr-sum.mts +269 -0
- package/src/rules/prefer-arr-sum.test.mts +171 -0
- package/src/rules/prefer-as-int.mts +131 -0
- package/src/rules/prefer-as-int.test.mts +267 -0
- package/src/rules/prefer-canonical-array-slicing.mts +800 -0
- package/src/rules/prefer-canonical-array-slicing.test.mts +588 -0
- package/src/rules/prefer-comparison-over-nullish-guard.mts +141 -0
- package/src/rules/prefer-comparison-over-nullish-guard.test.mts +171 -0
- package/src/rules/prefer-is-non-null-object.mts +145 -0
- package/src/rules/prefer-is-non-null-object.test.mts +156 -0
- package/src/rules/prefer-is-record-and-has-key.mts +156 -0
- package/src/rules/prefer-is-record-and-has-key.test.mts +217 -0
- package/src/rules/prefer-num-safe-parse-float.mts +166 -0
- package/src/rules/prefer-num-safe-parse-float.test.mts +163 -0
- package/src/rules/prefer-num-safe-parse-int.mts +166 -0
- package/src/rules/prefer-num-safe-parse-int.test.mts +137 -0
- package/src/rules/prefer-range-for-loop.mts +179 -0
- package/src/rules/prefer-range-for-loop.test.mts +156 -0
- package/src/rules/rules.mts +36 -0
- package/src/types.mts +4 -0
|
@@ -0,0 +1,800 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AST_NODE_TYPES,
|
|
3
|
+
ESLintUtils,
|
|
4
|
+
type TSESLint,
|
|
5
|
+
type TSESTree,
|
|
6
|
+
} from '@typescript-eslint/utils';
|
|
7
|
+
import * as ts from 'typescript';
|
|
8
|
+
import {
|
|
9
|
+
buildImportFixes,
|
|
10
|
+
getImportedLocalName,
|
|
11
|
+
getTsDataForgeImport,
|
|
12
|
+
} from './import-utils.mjs';
|
|
13
|
+
|
|
14
|
+
/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
|
|
15
|
+
|
|
16
|
+
type Options = readonly [];
|
|
17
|
+
|
|
18
|
+
type MessageIds = 'preferCanonicalArraySlicing';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A canonical ts-data-forge `Arr` operation a detected pattern is rewritten to:
|
|
22
|
+
*
|
|
23
|
+
* - `tail` / `butLast` — drop the first / last element
|
|
24
|
+
* - `skip N` / `take N` — drop / keep the first `N` elements
|
|
25
|
+
* - `skipLast N` / `takeLast N` — drop / keep the last `N` elements
|
|
26
|
+
* - `toUnshifted item` / `toPushed item` — prepend / append one element
|
|
27
|
+
* (`toUnshifted` is emitted in its curried form `toUnshifted(item)(xs)` so
|
|
28
|
+
* the new element keeps the leading position it had in `[item, ...xs]`)
|
|
29
|
+
*/
|
|
30
|
+
type CopyOp = Readonly<
|
|
31
|
+
| { fn: 'butLast' | 'tail' }
|
|
32
|
+
| { fn: 'skip' | 'skipLast' | 'take' | 'takeLast'; count: number }
|
|
33
|
+
| { fn: 'toPushed' | 'toUnshifted'; itemText: string }
|
|
34
|
+
>;
|
|
35
|
+
|
|
36
|
+
/** `skip 1` / `skipLast 1` have dedicated single-element names. */
|
|
37
|
+
const normalizeCountedOp = (
|
|
38
|
+
fn: 'skip' | 'skipLast' | 'take' | 'takeLast',
|
|
39
|
+
count: number,
|
|
40
|
+
): CopyOp | undefined =>
|
|
41
|
+
count < 1
|
|
42
|
+
? undefined
|
|
43
|
+
: fn === 'skip' && count === 1
|
|
44
|
+
? ({ fn: 'tail' } as const)
|
|
45
|
+
: fn === 'skipLast' && count === 1
|
|
46
|
+
? ({ fn: 'butLast' } as const)
|
|
47
|
+
: ({ fn, count } as const);
|
|
48
|
+
|
|
49
|
+
const asNonNegativeIntegerLiteral = (
|
|
50
|
+
node: TSESTree.Node,
|
|
51
|
+
): number | undefined =>
|
|
52
|
+
node.type === AST_NODE_TYPES.Literal &&
|
|
53
|
+
typeof node.value === 'number' &&
|
|
54
|
+
Number.isInteger(node.value) &&
|
|
55
|
+
node.value >= 0
|
|
56
|
+
? node.value
|
|
57
|
+
: undefined;
|
|
58
|
+
|
|
59
|
+
/** Matches `-N` (unary minus of a positive integer literal) and returns `N`. */
|
|
60
|
+
const asNegatedPositiveIntegerLiteral = (
|
|
61
|
+
node: TSESTree.Node,
|
|
62
|
+
): number | undefined => {
|
|
63
|
+
if (
|
|
64
|
+
node.type !== AST_NODE_TYPES.UnaryExpression ||
|
|
65
|
+
node.operator !== '-' ||
|
|
66
|
+
node.argument.type !== AST_NODE_TYPES.Literal
|
|
67
|
+
) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const n = asNonNegativeIntegerLiteral(node.argument);
|
|
72
|
+
|
|
73
|
+
return n !== undefined && n >= 1 ? n : undefined;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Whether re-reading the expression is free of side effects (a plain variable
|
|
78
|
+
* or a non-computed property chain). Required for patterns that reference the
|
|
79
|
+
* receiver twice, such as `xs.filter((_, i) => i < xs.length - 1)`.
|
|
80
|
+
*/
|
|
81
|
+
const isSimpleReference = (node: TSESTree.Node): boolean =>
|
|
82
|
+
node.type === AST_NODE_TYPES.Identifier ||
|
|
83
|
+
node.type === AST_NODE_TYPES.ThisExpression ||
|
|
84
|
+
(node.type === AST_NODE_TYPES.MemberExpression &&
|
|
85
|
+
!node.computed &&
|
|
86
|
+
node.property.type === AST_NODE_TYPES.Identifier &&
|
|
87
|
+
isSimpleReference(node.object));
|
|
88
|
+
|
|
89
|
+
const rootIdentifierName = (node: TSESTree.Node): string | undefined =>
|
|
90
|
+
node.type === AST_NODE_TYPES.Identifier
|
|
91
|
+
? node.name
|
|
92
|
+
: node.type === AST_NODE_TYPES.MemberExpression
|
|
93
|
+
? rootIdentifierName(node.object)
|
|
94
|
+
: undefined;
|
|
95
|
+
|
|
96
|
+
type ComparisonOperator = '!==' | '<' | '<=' | '>' | '>=';
|
|
97
|
+
|
|
98
|
+
const COMPARISON_OPERATORS: ReadonlySet<string> = new Set([
|
|
99
|
+
'!==',
|
|
100
|
+
'<',
|
|
101
|
+
'<=',
|
|
102
|
+
'>',
|
|
103
|
+
'>=',
|
|
104
|
+
] as const satisfies readonly ComparisonOperator[]);
|
|
105
|
+
|
|
106
|
+
/** Mirror of each comparison when its operands are swapped. */
|
|
107
|
+
const FLIPPED_OPERATORS = {
|
|
108
|
+
'!==': '!==',
|
|
109
|
+
'<': '>',
|
|
110
|
+
'<=': '>=',
|
|
111
|
+
'>': '<',
|
|
112
|
+
'>=': '<=',
|
|
113
|
+
} as const satisfies Record<ComparisonOperator, ComparisonOperator>;
|
|
114
|
+
|
|
115
|
+
/** Classifies `index OP N` (a comparison against an integer literal). */
|
|
116
|
+
const classifyLiteralComparison = (
|
|
117
|
+
op: ComparisonOperator,
|
|
118
|
+
n: number,
|
|
119
|
+
): CopyOp | undefined => {
|
|
120
|
+
switch (op) {
|
|
121
|
+
case '>=':
|
|
122
|
+
return normalizeCountedOp('skip', n);
|
|
123
|
+
|
|
124
|
+
case '>':
|
|
125
|
+
return normalizeCountedOp('skip', n + 1);
|
|
126
|
+
|
|
127
|
+
case '!==':
|
|
128
|
+
return n === 0 ? { fn: 'tail' } : undefined;
|
|
129
|
+
|
|
130
|
+
case '<':
|
|
131
|
+
return normalizeCountedOp('take', n);
|
|
132
|
+
|
|
133
|
+
case '<=':
|
|
134
|
+
return normalizeCountedOp('take', n + 1);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/** Classifies `index OP <xs>.length - M`. */
|
|
139
|
+
const classifyLengthComparison = (
|
|
140
|
+
op: ComparisonOperator,
|
|
141
|
+
m: number,
|
|
142
|
+
): CopyOp | undefined => {
|
|
143
|
+
switch (op) {
|
|
144
|
+
case '<':
|
|
145
|
+
return normalizeCountedOp('skipLast', m);
|
|
146
|
+
|
|
147
|
+
case '<=':
|
|
148
|
+
return normalizeCountedOp('skipLast', m - 1);
|
|
149
|
+
|
|
150
|
+
case '!==':
|
|
151
|
+
return m === 1 ? { fn: 'butLast' } : undefined;
|
|
152
|
+
|
|
153
|
+
case '>=':
|
|
154
|
+
return normalizeCountedOp('takeLast', m);
|
|
155
|
+
|
|
156
|
+
case '>':
|
|
157
|
+
return normalizeCountedOp('takeLast', m - 1);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
export const preferCanonicalArraySlicing: TSESLint.RuleModule<
|
|
162
|
+
MessageIds,
|
|
163
|
+
Options
|
|
164
|
+
> = {
|
|
165
|
+
meta: {
|
|
166
|
+
type: 'suggestion',
|
|
167
|
+
docs: {
|
|
168
|
+
description:
|
|
169
|
+
'Unify non-mutating array element addition/removal patterns (`slice` / `toSpliced` / `filter` / `concat` / spread) into the corresponding ts-data-forge functions: `Arr.tail`, `Arr.skip`, `Arr.take`, `Arr.butLast`, `Arr.skipLast`, `Arr.takeLast`, `Arr.toUnshifted`, `Arr.toPushed`.',
|
|
170
|
+
},
|
|
171
|
+
fixable: 'code',
|
|
172
|
+
schema: [],
|
|
173
|
+
messages: {
|
|
174
|
+
preferCanonicalArraySlicing:
|
|
175
|
+
'Replace with `{{replacement}}` from ts-data-forge.',
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
create: (context) => {
|
|
180
|
+
const sourceCode = context.sourceCode;
|
|
181
|
+
|
|
182
|
+
const program = sourceCode.ast;
|
|
183
|
+
|
|
184
|
+
const tsDataForgeImport = getTsDataForgeImport(program);
|
|
185
|
+
|
|
186
|
+
const parserServices = ESLintUtils.getParserServices(context);
|
|
187
|
+
|
|
188
|
+
const checker = parserServices.program.getTypeChecker();
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The in-scope way to reference the `Arr` namespace: a named import
|
|
192
|
+
* (possibly aliased), a namespace import member, or the canonical `Arr`
|
|
193
|
+
* (added by the import fix when nothing is imported yet).
|
|
194
|
+
*/
|
|
195
|
+
const arrLocalName = getImportedLocalName(tsDataForgeImport, 'Arr');
|
|
196
|
+
|
|
197
|
+
const namespaceLocalName = tsDataForgeImport?.specifiers.find(
|
|
198
|
+
(specifier) => specifier.type === AST_NODE_TYPES.ImportNamespaceSpecifier,
|
|
199
|
+
)?.local.name;
|
|
200
|
+
|
|
201
|
+
const arrRef =
|
|
202
|
+
arrLocalName ??
|
|
203
|
+
(namespaceLocalName === undefined ? 'Arr' : `${namespaceLocalName}.Arr`);
|
|
204
|
+
|
|
205
|
+
const needsArrImport =
|
|
206
|
+
arrLocalName === undefined && namespaceLocalName === undefined;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Deferred (non-concrete) types are reduced to their base constraint
|
|
210
|
+
* before checking; `any` / `unknown` are conservatively not array-like.
|
|
211
|
+
*/
|
|
212
|
+
const isArrayOrTupleType = (type: ts.Type): boolean => {
|
|
213
|
+
if ((type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) !== 0) {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (type.isUnion()) {
|
|
218
|
+
return type.types.every(isArrayOrTupleType);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (
|
|
222
|
+
(type.flags &
|
|
223
|
+
(ts.TypeFlags.TypeParameter |
|
|
224
|
+
ts.TypeFlags.IndexedAccess |
|
|
225
|
+
ts.TypeFlags.Conditional |
|
|
226
|
+
ts.TypeFlags.Substitution)) !==
|
|
227
|
+
0
|
|
228
|
+
) {
|
|
229
|
+
const constraint = checker.getBaseConstraintOfType(type);
|
|
230
|
+
|
|
231
|
+
return constraint !== undefined && isArrayOrTupleType(constraint);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return checker.isArrayType(type) || checker.isTupleType(type);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const getTypeOf = (node: TSESTree.Node): ts.Type =>
|
|
238
|
+
checker.getTypeAtLocation(parserServices.esTreeNodeToTSNodeMap.get(node));
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* `true` only when the type can never be an array in any branch, so
|
|
242
|
+
* `xs.concat(v)` is guaranteed to append `v` as one element rather than
|
|
243
|
+
* flattening it.
|
|
244
|
+
*/
|
|
245
|
+
const isDefinitelyNotArrayType = (type: ts.Type): boolean => {
|
|
246
|
+
if ((type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) !== 0) {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (type.isUnion()) {
|
|
251
|
+
return type.types.every(isDefinitelyNotArrayType);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (
|
|
255
|
+
(type.flags &
|
|
256
|
+
(ts.TypeFlags.TypeParameter |
|
|
257
|
+
ts.TypeFlags.IndexedAccess |
|
|
258
|
+
ts.TypeFlags.Conditional |
|
|
259
|
+
ts.TypeFlags.Substitution)) !==
|
|
260
|
+
0
|
|
261
|
+
) {
|
|
262
|
+
const constraint = checker.getBaseConstraintOfType(type);
|
|
263
|
+
|
|
264
|
+
return constraint !== undefined && isDefinitelyNotArrayType(constraint);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return !checker.isArrayType(type) && !checker.isTupleType(type);
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
/** Argument-position text; a comma (sequence) expression needs parens. */
|
|
271
|
+
const toArgText = (node: TSESTree.Node): string => {
|
|
272
|
+
const text = sourceCode.getText(node);
|
|
273
|
+
|
|
274
|
+
return node.type === AST_NODE_TYPES.SequenceExpression
|
|
275
|
+
? `(${text})`
|
|
276
|
+
: text;
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
const toReplacementText = (op: CopyOp, xsText: string): string => {
|
|
280
|
+
switch (op.fn) {
|
|
281
|
+
case 'tail':
|
|
282
|
+
case 'butLast':
|
|
283
|
+
return `${arrRef}.${op.fn}(${xsText})`;
|
|
284
|
+
|
|
285
|
+
case 'skip':
|
|
286
|
+
case 'take':
|
|
287
|
+
case 'skipLast':
|
|
288
|
+
case 'takeLast':
|
|
289
|
+
return `${arrRef}.${op.fn}(${xsText}, ${op.count})`;
|
|
290
|
+
|
|
291
|
+
// Emit the curried form for `toUnshifted` so the new element stays in
|
|
292
|
+
// the same textual position as in the `[item, ...xs]` source, keeping
|
|
293
|
+
// the rewrite easy to read.
|
|
294
|
+
case 'toUnshifted':
|
|
295
|
+
return `${arrRef}.toUnshifted(${op.itemText})(${xsText})`;
|
|
296
|
+
|
|
297
|
+
case 'toPushed':
|
|
298
|
+
return `${arrRef}.toPushed(${xsText}, ${op.itemText})`;
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const mut_nodesToFix: {
|
|
303
|
+
node: TSESTree.Node;
|
|
304
|
+
replacement: string;
|
|
305
|
+
}[] = [];
|
|
306
|
+
|
|
307
|
+
const report = (node: TSESTree.Node, op: CopyOp, xsText: string): void => {
|
|
308
|
+
mut_nodesToFix.push({
|
|
309
|
+
node,
|
|
310
|
+
replacement: toReplacementText(op, xsText),
|
|
311
|
+
});
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
/** Matches `<xsText>.length` (non-computed). */
|
|
315
|
+
const isReceiverLengthAccess = (
|
|
316
|
+
node: TSESTree.Node,
|
|
317
|
+
receiverText: string,
|
|
318
|
+
): boolean =>
|
|
319
|
+
node.type === AST_NODE_TYPES.MemberExpression &&
|
|
320
|
+
!node.computed &&
|
|
321
|
+
node.property.type === AST_NODE_TYPES.Identifier &&
|
|
322
|
+
node.property.name === 'length' &&
|
|
323
|
+
sourceCode.getText(node.object) === receiverText;
|
|
324
|
+
|
|
325
|
+
/** Matches `<xsText>.length - M` (M ≥ 1) and returns `M`. */
|
|
326
|
+
const matchLengthMinus = (
|
|
327
|
+
node: TSESTree.Node,
|
|
328
|
+
receiverText: string,
|
|
329
|
+
): number | undefined => {
|
|
330
|
+
if (
|
|
331
|
+
node.type !== AST_NODE_TYPES.BinaryExpression ||
|
|
332
|
+
node.operator !== '-' ||
|
|
333
|
+
!isReceiverLengthAccess(node.left, receiverText)
|
|
334
|
+
) {
|
|
335
|
+
return undefined;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const m = asNonNegativeIntegerLiteral(node.right);
|
|
339
|
+
|
|
340
|
+
return m !== undefined && m >= 1 ? m : undefined;
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Matches an index-only filter callback (`(_, i) => <comparison>`) and
|
|
345
|
+
* classifies the comparison. Both a leading index (`i >= 1`) and a
|
|
346
|
+
* trailing index (`1 <= i`) operand order are accepted.
|
|
347
|
+
*/
|
|
348
|
+
const classifyFilterCallback = (
|
|
349
|
+
callback: TSESTree.CallExpressionArgument,
|
|
350
|
+
receiver: TSESTree.Expression,
|
|
351
|
+
receiverText: string,
|
|
352
|
+
): CopyOp | undefined => {
|
|
353
|
+
if (
|
|
354
|
+
callback.type !== AST_NODE_TYPES.ArrowFunctionExpression ||
|
|
355
|
+
callback.async ||
|
|
356
|
+
callback.body.type === AST_NODE_TYPES.BlockStatement ||
|
|
357
|
+
callback.params.length !== 2
|
|
358
|
+
) {
|
|
359
|
+
return undefined;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const [elementParam, indexParam] = callback.params;
|
|
363
|
+
|
|
364
|
+
if (
|
|
365
|
+
elementParam === undefined ||
|
|
366
|
+
indexParam === undefined ||
|
|
367
|
+
elementParam.type !== AST_NODE_TYPES.Identifier ||
|
|
368
|
+
indexParam.type !== AST_NODE_TYPES.Identifier
|
|
369
|
+
) {
|
|
370
|
+
return undefined;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// A callback parameter shadowing the receiver's root binding (e.g.
|
|
374
|
+
// `xs.filter((xs, i) => i < xs.length - 1)`) would make the textual
|
|
375
|
+
// `xs.length` comparison refer to the element, not the receiver.
|
|
376
|
+
const receiverRoot = rootIdentifierName(receiver);
|
|
377
|
+
|
|
378
|
+
if (
|
|
379
|
+
receiverRoot !== undefined &&
|
|
380
|
+
(elementParam.name === receiverRoot || indexParam.name === receiverRoot)
|
|
381
|
+
) {
|
|
382
|
+
return undefined;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const body = callback.body;
|
|
386
|
+
|
|
387
|
+
if (
|
|
388
|
+
body.type !== AST_NODE_TYPES.BinaryExpression ||
|
|
389
|
+
!COMPARISON_OPERATORS.has(body.operator)
|
|
390
|
+
) {
|
|
391
|
+
return undefined;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
395
|
+
const rawOperator = body.operator as ComparisonOperator;
|
|
396
|
+
|
|
397
|
+
const isIndex = (side: TSESTree.Node): boolean =>
|
|
398
|
+
side.type === AST_NODE_TYPES.Identifier &&
|
|
399
|
+
side.name === indexParam.name;
|
|
400
|
+
|
|
401
|
+
const [op, other] = isIndex(body.left)
|
|
402
|
+
? ([rawOperator, body.right] as const)
|
|
403
|
+
: isIndex(body.right)
|
|
404
|
+
? ([FLIPPED_OPERATORS[rawOperator], body.left] as const)
|
|
405
|
+
: [undefined, undefined];
|
|
406
|
+
|
|
407
|
+
// `op` and `other` are set together (both from the same tuple), so
|
|
408
|
+
// checking `op` also guarantees `other` is defined.
|
|
409
|
+
if (op === undefined || isIndex(other)) {
|
|
410
|
+
return undefined;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// The element parameter must not participate in the comparison (only
|
|
414
|
+
// the index side and a receiver-independent bound are recognized, so
|
|
415
|
+
// reaching here with an element reference is impossible for literals,
|
|
416
|
+
// but `xs.length - N` needs a stable receiver).
|
|
417
|
+
const n = asNonNegativeIntegerLiteral(other);
|
|
418
|
+
|
|
419
|
+
if (n !== undefined) {
|
|
420
|
+
return classifyLiteralComparison(op, n);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
if (!isSimpleReference(receiver)) return undefined;
|
|
424
|
+
|
|
425
|
+
const m = matchLengthMinus(other, receiverText);
|
|
426
|
+
|
|
427
|
+
return m === undefined ? undefined : classifyLengthComparison(op, m);
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
const classifySliceArgs = (
|
|
431
|
+
args: readonly TSESTree.CallExpressionArgument[],
|
|
432
|
+
receiver: TSESTree.Expression,
|
|
433
|
+
): CopyOp | undefined => {
|
|
434
|
+
if (args.length === 1) {
|
|
435
|
+
const [start] = args;
|
|
436
|
+
|
|
437
|
+
if (start === undefined) return undefined;
|
|
438
|
+
|
|
439
|
+
const n = asNonNegativeIntegerLiteral(start);
|
|
440
|
+
|
|
441
|
+
if (n !== undefined) return normalizeCountedOp('skip', n);
|
|
442
|
+
|
|
443
|
+
const negated = asNegatedPositiveIntegerLiteral(start);
|
|
444
|
+
|
|
445
|
+
return negated === undefined
|
|
446
|
+
? undefined
|
|
447
|
+
: normalizeCountedOp('takeLast', negated);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if (args.length === 2) {
|
|
451
|
+
const [start, end] = args;
|
|
452
|
+
|
|
453
|
+
if (
|
|
454
|
+
start === undefined ||
|
|
455
|
+
end === undefined ||
|
|
456
|
+
asNonNegativeIntegerLiteral(start) !== 0
|
|
457
|
+
) {
|
|
458
|
+
return undefined;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const n = asNonNegativeIntegerLiteral(end);
|
|
462
|
+
|
|
463
|
+
if (n !== undefined) return normalizeCountedOp('take', n);
|
|
464
|
+
|
|
465
|
+
const negated = asNegatedPositiveIntegerLiteral(end);
|
|
466
|
+
|
|
467
|
+
if (negated !== undefined) {
|
|
468
|
+
return normalizeCountedOp('skipLast', negated);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
if (!isSimpleReference(receiver)) return undefined;
|
|
472
|
+
|
|
473
|
+
const m = matchLengthMinus(end, sourceCode.getText(receiver));
|
|
474
|
+
|
|
475
|
+
return m === undefined ? undefined : normalizeCountedOp('skipLast', m);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return undefined;
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
const classifyToSplicedArgs = (
|
|
482
|
+
args: readonly TSESTree.CallExpressionArgument[],
|
|
483
|
+
receiver: TSESTree.Expression,
|
|
484
|
+
receiverText: string,
|
|
485
|
+
): CopyOp | undefined => {
|
|
486
|
+
if (args.length === 1) {
|
|
487
|
+
const [start] = args;
|
|
488
|
+
|
|
489
|
+
if (start === undefined) return undefined;
|
|
490
|
+
|
|
491
|
+
const n = asNonNegativeIntegerLiteral(start);
|
|
492
|
+
|
|
493
|
+
// `xs.toSpliced(N)` removes everything from index N on → take N.
|
|
494
|
+
if (n !== undefined) return normalizeCountedOp('take', n);
|
|
495
|
+
|
|
496
|
+
const negated = asNegatedPositiveIntegerLiteral(start);
|
|
497
|
+
|
|
498
|
+
// `xs.toSpliced(-N)` removes the last N elements → skipLast N.
|
|
499
|
+
return negated === undefined
|
|
500
|
+
? undefined
|
|
501
|
+
: normalizeCountedOp('skipLast', negated);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
if (args.length === 2) {
|
|
505
|
+
const [start, deleteCount] = args;
|
|
506
|
+
|
|
507
|
+
if (
|
|
508
|
+
start === undefined ||
|
|
509
|
+
deleteCount === undefined ||
|
|
510
|
+
asNonNegativeIntegerLiteral(start) !== 0
|
|
511
|
+
) {
|
|
512
|
+
return undefined;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// `xs.toSpliced(0, N)` removes the first N elements → skip N.
|
|
516
|
+
const n = asNonNegativeIntegerLiteral(deleteCount);
|
|
517
|
+
|
|
518
|
+
if (n !== undefined) return normalizeCountedOp('skip', n);
|
|
519
|
+
|
|
520
|
+
// `xs.toSpliced(0, xs.length - N)` keeps the last N → takeLast N.
|
|
521
|
+
// (Note: `xs.toSpliced(0, -N)` clamps the delete count to 0 and is a
|
|
522
|
+
// plain copy, so it is deliberately NOT treated as takeLast.)
|
|
523
|
+
if (!isSimpleReference(receiver)) return undefined;
|
|
524
|
+
|
|
525
|
+
const m = matchLengthMinus(deleteCount, receiverText);
|
|
526
|
+
|
|
527
|
+
return m === undefined ? undefined : normalizeCountedOp('takeLast', m);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
if (args.length === 3) {
|
|
531
|
+
const [start, deleteCount, item] = args;
|
|
532
|
+
|
|
533
|
+
if (
|
|
534
|
+
start === undefined ||
|
|
535
|
+
deleteCount === undefined ||
|
|
536
|
+
item === undefined ||
|
|
537
|
+
item.type === AST_NODE_TYPES.SpreadElement ||
|
|
538
|
+
asNonNegativeIntegerLiteral(deleteCount) !== 0
|
|
539
|
+
) {
|
|
540
|
+
return undefined;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// `xs.toSpliced(0, 0, item)` prepends → toUnshifted.
|
|
544
|
+
if (asNonNegativeIntegerLiteral(start) === 0) {
|
|
545
|
+
return { fn: 'toUnshifted', itemText: toArgText(item) };
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// `xs.toSpliced(xs.length, 0, item)` appends → toPushed.
|
|
549
|
+
if (
|
|
550
|
+
isSimpleReference(receiver) &&
|
|
551
|
+
isReceiverLengthAccess(start, receiverText)
|
|
552
|
+
) {
|
|
553
|
+
return { fn: 'toPushed', itemText: toArgText(item) };
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
return undefined;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
return undefined;
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
return {
|
|
563
|
+
CallExpression: (node) => {
|
|
564
|
+
const callee = node.callee;
|
|
565
|
+
|
|
566
|
+
if (
|
|
567
|
+
callee.type !== AST_NODE_TYPES.MemberExpression ||
|
|
568
|
+
callee.computed ||
|
|
569
|
+
callee.property.type !== AST_NODE_TYPES.Identifier
|
|
570
|
+
) {
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
const methodName = callee.property.name;
|
|
575
|
+
|
|
576
|
+
const receiver = callee.object;
|
|
577
|
+
|
|
578
|
+
// `Arr.skip(xs, 1)` → `Arr.tail(xs)`, `Arr.skipLast(xs, 1)` →
|
|
579
|
+
// `Arr.butLast(xs)` (also via a namespace import or an alias).
|
|
580
|
+
if (methodName === 'skip' || methodName === 'skipLast') {
|
|
581
|
+
const isArrObject =
|
|
582
|
+
(receiver.type === AST_NODE_TYPES.Identifier &&
|
|
583
|
+
receiver.name === arrLocalName) ||
|
|
584
|
+
(receiver.type === AST_NODE_TYPES.MemberExpression &&
|
|
585
|
+
!receiver.computed &&
|
|
586
|
+
receiver.object.type === AST_NODE_TYPES.Identifier &&
|
|
587
|
+
receiver.object.name === namespaceLocalName &&
|
|
588
|
+
receiver.property.type === AST_NODE_TYPES.Identifier &&
|
|
589
|
+
receiver.property.name === 'Arr');
|
|
590
|
+
|
|
591
|
+
const [arrayArg, countArg] = node.arguments;
|
|
592
|
+
|
|
593
|
+
if (
|
|
594
|
+
isArrObject &&
|
|
595
|
+
node.arguments.length === 2 &&
|
|
596
|
+
arrayArg !== undefined &&
|
|
597
|
+
arrayArg.type !== AST_NODE_TYPES.SpreadElement &&
|
|
598
|
+
countArg !== undefined &&
|
|
599
|
+
asNonNegativeIntegerLiteral(countArg) === 1
|
|
600
|
+
) {
|
|
601
|
+
report(
|
|
602
|
+
node,
|
|
603
|
+
{ fn: methodName === 'skip' ? 'tail' : 'butLast' },
|
|
604
|
+
toArgText(arrayArg),
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
// `[item].concat(xs)` → `Arr.toUnshifted(xs, item)`.
|
|
612
|
+
if (
|
|
613
|
+
methodName === 'concat' &&
|
|
614
|
+
receiver.type === AST_NODE_TYPES.ArrayExpression
|
|
615
|
+
) {
|
|
616
|
+
const [element] = receiver.elements;
|
|
617
|
+
|
|
618
|
+
const [arg] = node.arguments;
|
|
619
|
+
|
|
620
|
+
if (
|
|
621
|
+
receiver.elements.length === 1 &&
|
|
622
|
+
element !== null &&
|
|
623
|
+
element !== undefined &&
|
|
624
|
+
element.type !== AST_NODE_TYPES.SpreadElement &&
|
|
625
|
+
node.arguments.length === 1 &&
|
|
626
|
+
arg !== undefined &&
|
|
627
|
+
arg.type !== AST_NODE_TYPES.SpreadElement &&
|
|
628
|
+
isArrayOrTupleType(getTypeOf(arg))
|
|
629
|
+
) {
|
|
630
|
+
report(
|
|
631
|
+
node,
|
|
632
|
+
{ fn: 'toUnshifted', itemText: toArgText(element) },
|
|
633
|
+
toArgText(arg),
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
if (
|
|
641
|
+
methodName !== 'slice' &&
|
|
642
|
+
methodName !== 'toSpliced' &&
|
|
643
|
+
methodName !== 'filter' &&
|
|
644
|
+
methodName !== 'concat'
|
|
645
|
+
) {
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
// The receiver must be an array or tuple (not a string / typed array
|
|
650
|
+
// / other object that happens to share the method name).
|
|
651
|
+
if (!isArrayOrTupleType(getTypeOf(receiver))) return;
|
|
652
|
+
|
|
653
|
+
const receiverText = sourceCode.getText(receiver);
|
|
654
|
+
|
|
655
|
+
switch (methodName) {
|
|
656
|
+
case 'slice': {
|
|
657
|
+
const op = classifySliceArgs(node.arguments, receiver);
|
|
658
|
+
|
|
659
|
+
if (op !== undefined) report(node, op, toArgText(receiver));
|
|
660
|
+
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
case 'toSpliced': {
|
|
665
|
+
const op = classifyToSplicedArgs(
|
|
666
|
+
node.arguments,
|
|
667
|
+
receiver,
|
|
668
|
+
receiverText,
|
|
669
|
+
);
|
|
670
|
+
|
|
671
|
+
if (op !== undefined) report(node, op, toArgText(receiver));
|
|
672
|
+
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
case 'filter': {
|
|
677
|
+
const [callback] = node.arguments;
|
|
678
|
+
|
|
679
|
+
if (callback === undefined || node.arguments.length !== 1) {
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
const op = classifyFilterCallback(callback, receiver, receiverText);
|
|
684
|
+
|
|
685
|
+
if (op !== undefined) report(node, op, toArgText(receiver));
|
|
686
|
+
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
case 'concat': {
|
|
691
|
+
const [arg] = node.arguments;
|
|
692
|
+
|
|
693
|
+
if (
|
|
694
|
+
node.arguments.length !== 1 ||
|
|
695
|
+
arg === undefined ||
|
|
696
|
+
arg.type === AST_NODE_TYPES.SpreadElement
|
|
697
|
+
) {
|
|
698
|
+
return;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// `xs.concat([item])` → `Arr.toPushed(xs, item)`.
|
|
702
|
+
if (arg.type === AST_NODE_TYPES.ArrayExpression) {
|
|
703
|
+
const [element] = arg.elements;
|
|
704
|
+
|
|
705
|
+
if (
|
|
706
|
+
arg.elements.length === 1 &&
|
|
707
|
+
element !== null &&
|
|
708
|
+
element !== undefined &&
|
|
709
|
+
element.type !== AST_NODE_TYPES.SpreadElement
|
|
710
|
+
) {
|
|
711
|
+
report(
|
|
712
|
+
node,
|
|
713
|
+
{ fn: 'toPushed', itemText: toArgText(element) },
|
|
714
|
+
toArgText(receiver),
|
|
715
|
+
);
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
// `xs.concat(item)` appends only when `item` can never be an
|
|
722
|
+
// array (otherwise `concat` would flatten it).
|
|
723
|
+
if (isDefinitelyNotArrayType(getTypeOf(arg))) {
|
|
724
|
+
report(
|
|
725
|
+
node,
|
|
726
|
+
{ fn: 'toPushed', itemText: toArgText(arg) },
|
|
727
|
+
toArgText(receiver),
|
|
728
|
+
);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
},
|
|
733
|
+
|
|
734
|
+
ArrayExpression: (node) => {
|
|
735
|
+
// `[item, ...xs]` → `Arr.toUnshifted(xs, item)`,
|
|
736
|
+
// `[...xs, item]` → `Arr.toPushed(xs, item)`.
|
|
737
|
+
if (node.elements.length !== 2) return;
|
|
738
|
+
|
|
739
|
+
const [first, second] = node.elements;
|
|
740
|
+
|
|
741
|
+
if (
|
|
742
|
+
first === null ||
|
|
743
|
+
first === undefined ||
|
|
744
|
+
second === null ||
|
|
745
|
+
second === undefined
|
|
746
|
+
) {
|
|
747
|
+
return;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
const [spread, item, fn] =
|
|
751
|
+
first.type === AST_NODE_TYPES.SpreadElement &&
|
|
752
|
+
second.type !== AST_NODE_TYPES.SpreadElement
|
|
753
|
+
? ([first, second, 'toPushed'] as const)
|
|
754
|
+
: first.type !== AST_NODE_TYPES.SpreadElement &&
|
|
755
|
+
second.type === AST_NODE_TYPES.SpreadElement
|
|
756
|
+
? ([second, first, 'toUnshifted'] as const)
|
|
757
|
+
: [undefined, undefined, undefined];
|
|
758
|
+
|
|
759
|
+
// `spread`, `item`, and `fn` are set together (all from the same
|
|
760
|
+
// tuple), so checking `spread` also guarantees the others are
|
|
761
|
+
// defined.
|
|
762
|
+
if (spread === undefined) return;
|
|
763
|
+
|
|
764
|
+
// Spreading a non-array iterable (`[v, ...someSet]`) is a genuine
|
|
765
|
+
// conversion, not an element addition to an array.
|
|
766
|
+
if (!isArrayOrTupleType(getTypeOf(spread.argument))) return;
|
|
767
|
+
|
|
768
|
+
report(
|
|
769
|
+
node,
|
|
770
|
+
{ fn, itemText: toArgText(item) },
|
|
771
|
+
toArgText(spread.argument),
|
|
772
|
+
);
|
|
773
|
+
},
|
|
774
|
+
|
|
775
|
+
'Program:exit': () => {
|
|
776
|
+
for (const [index, nodeInfo] of mut_nodesToFix.entries()) {
|
|
777
|
+
context.report({
|
|
778
|
+
node: nodeInfo.node,
|
|
779
|
+
messageId: 'preferCanonicalArraySlicing',
|
|
780
|
+
data: { replacement: nodeInfo.replacement },
|
|
781
|
+
fix: (fixer) => {
|
|
782
|
+
const importFixes =
|
|
783
|
+
index === 0 && needsArrImport
|
|
784
|
+
? buildImportFixes(fixer, program, tsDataForgeImport, ['Arr'])
|
|
785
|
+
: [];
|
|
786
|
+
|
|
787
|
+
return [
|
|
788
|
+
...importFixes,
|
|
789
|
+
fixer.replaceText(nodeInfo.node, nodeInfo.replacement),
|
|
790
|
+
];
|
|
791
|
+
},
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
},
|
|
795
|
+
};
|
|
796
|
+
},
|
|
797
|
+
defaultOptions: [],
|
|
798
|
+
} as const;
|
|
799
|
+
|
|
800
|
+
/* eslint-enable @typescript-eslint/prefer-readonly-parameter-types */
|