-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathprefer-array-some.ts
More file actions
306 lines (277 loc) · 8.11 KB
/
Copy pathprefer-array-some.ts
File metadata and controls
306 lines (277 loc) · 8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import type {Rule} from 'eslint';
import type {
BinaryExpression,
BinaryOperator,
CallExpression,
Expression,
Literal,
Identifier,
PrivateIdentifier,
MemberExpression,
Node,
UnaryExpression
} from 'estree';
import {
formatArguments,
isInBooleanContext,
isNullish,
needsParensForPropertyAccess
} from '../utils/ast.js';
function isIdentifierOrStringLiteral(
identifierName: string,
node: Expression | PrivateIdentifier
): node is Identifier | Literal {
return (
(node.type === 'Identifier' && node.name === identifierName) ||
(node.type === 'Literal' && node.value === identifierName)
);
}
function isArrayMethodCall(
methodName: string
): (node: Expression) => node is CallExpression {
return function (node: Expression): node is CallExpression {
return (
node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
isIdentifierOrStringLiteral(methodName, node.callee.property) &&
node.arguments.length >= 1
);
};
}
const isFindCall = isArrayMethodCall('find');
const isFilterCall = isArrayMethodCall('filter');
function isFilterLengthCall(node: Expression): node is MemberExpression {
return (
node.type === 'MemberExpression' &&
isIdentifierOrStringLiteral('length', node.property) &&
node.object.type === 'CallExpression' &&
isFilterCall(node.object)
);
}
function reportFind(
context: Rule.RuleContext,
node: Node,
findCall: CallExpression,
shouldNegate: boolean
) {
if (findCall.callee.type !== 'MemberExpression') {
return;
}
const sourceCode = context.sourceCode;
const arrayNode = findCall.callee.object;
const rawArrayText = sourceCode.getText(arrayNode);
const arrayText = needsParensForPropertyAccess(arrayNode)
? `(${rawArrayText})`
: rawArrayText;
const argsText = formatArguments(findCall.arguments, sourceCode);
const replacement = shouldNegate
? `!${arrayText}.some(${argsText})`
: `${arrayText}.some(${argsText})`;
context.report({
node,
messageId: 'preferArraySome',
fix(fixer) {
return fixer.replaceText(node, replacement);
}
});
}
function reportFilterLength(
context: Rule.RuleContext,
node: Node,
filterLengthCall: MemberExpression,
shouldNegate: boolean
) {
if (filterLengthCall.object.type !== 'CallExpression') {
return;
}
if (filterLengthCall.object.callee.type !== 'MemberExpression') {
return;
}
const sourceCode = context.sourceCode;
const arrayNode = filterLengthCall.object.callee.object;
const rawArrayText = sourceCode.getText(arrayNode);
const arrayText = needsParensForPropertyAccess(arrayNode)
? `(${rawArrayText})`
: rawArrayText;
const argsText = formatArguments(
filterLengthCall.object.arguments,
sourceCode
);
const replacement = shouldNegate
? `!${arrayText}.some(${argsText})`
: `${arrayText}.some(${argsText})`;
context.report({
node,
messageId: 'preferArraySome',
fix(fixer) {
return fixer.replaceText(node, replacement);
}
});
}
function checkBinaryExpression(
node: BinaryExpression & Rule.NodeParentExtension,
context: Rule.RuleContext
) {
const {left, right, operator} = node;
if (left.type === 'PrivateIdentifier') {
return;
}
let findCall: CallExpression | undefined;
let filterLengthCall: MemberExpression | undefined;
let constantSide: Expression;
if (isFindCall(left)) {
findCall = left;
constantSide = right;
} else if (isFindCall(right)) {
findCall = right;
constantSide = left;
} else if (isFilterLengthCall(left)) {
filterLengthCall = left;
constantSide = right;
} else if (isFilterLengthCall(right)) {
filterLengthCall = right;
constantSide = left;
} else {
return;
}
if (findCall !== undefined) {
const nullishType = isNullish(constantSide);
if (!nullishType) {
return;
}
if (operator === '===' || operator === '!==') {
if (nullishType !== 'undefined') {
return;
}
const shouldNegate = operator === '===';
reportFind(context, node, findCall, shouldNegate);
return;
}
} else if (filterLengthCall !== undefined) {
// Map the operator direction to effectively be filterLengthSide <op> constantSide
let ltrOperator = operator;
if (left === constantSide) {
ltrOperator =
(
{
'>': '<',
'<': '>',
'<=': '>=',
'>=': '<='
} as Partial<Record<BinaryOperator, BinaryOperator>>
)[operator] ?? operator;
}
if (constantSide.type === 'Literal' && constantSide.value === 0) {
if (ltrOperator === '===' || ltrOperator === '<=') {
reportFilterLength(context, node, filterLengthCall, true);
} else if (ltrOperator === '!==' || ltrOperator === '>') {
reportFilterLength(context, node, filterLengthCall, false);
}
} else if (constantSide.type === 'Literal' && constantSide.value === 1) {
if (ltrOperator === '<') {
reportFilterLength(context, node, filterLengthCall, true);
} else if (ltrOperator === '>=') {
reportFilterLength(context, node, filterLengthCall, false);
}
}
}
}
function checkUnaryExpression(
node: UnaryExpression & Rule.NodeParentExtension,
context: Rule.RuleContext
) {
// !arr.find(fn) -> !arr.some(fn)
if (node.operator === '!' && isFindCall(node.argument)) {
reportFind(context, node, node.argument, true);
return;
}
// !arr.filter(fn).length -> !array.some(fn)
if (node.operator === '!' && isFilterLengthCall(node.argument)) {
reportFilterLength(context, node, node.argument, true);
return;
}
// !!arr.find(fn) -> arr.some(fn)
if (
node.operator === '!' &&
node.argument.type === 'UnaryExpression' &&
node.argument.operator === '!' &&
isFindCall(node.argument.argument)
) {
reportFind(context, node, node.argument.argument, false);
return;
}
// !!arr.filter(fn).length -> arr.some(fn)
if (
node.operator === '!' &&
node.argument.type === 'UnaryExpression' &&
node.argument.operator === '!' &&
isFilterLengthCall(node.argument.argument)
) {
reportFilterLength(context, node, node.argument.argument, false);
}
}
export const preferArraySome: Rule.RuleModule = {
meta: {
type: 'suggestion',
docs: {
description:
'Prefer Array.some() over Array.find() and Array.filter().length checks when checking for element existence',
recommended: true
},
fixable: 'code',
schema: [],
messages: {
preferArraySome:
'Use Array.some() instead of Array.find() and Array.filter().length checks when checking for element existence'
}
},
create(context) {
return {
BinaryExpression(node: BinaryExpression & Rule.NodeParentExtension) {
checkBinaryExpression(node, context);
},
UnaryExpression(node: UnaryExpression & Rule.NodeParentExtension) {
// Skip inner ! if it's inside !! (the outer will handle it)
if (node.operator === '!' && node.parent) {
if (
node.parent.type === 'UnaryExpression' &&
node.parent.operator === '!'
) {
return;
}
}
checkUnaryExpression(node, context);
},
CallExpression(node: CallExpression & Rule.NodeParentExtension) {
// Skip if handled by UnaryExpression or BinaryExpression
if (
node.parent?.type === 'UnaryExpression' ||
node.parent?.type === 'BinaryExpression'
) {
return;
}
if (!isFindCall(node)) {
return;
}
if (isInBooleanContext(node)) {
reportFind(context, node, node, false);
}
},
MemberExpression(node: MemberExpression & Rule.NodeParentExtension) {
// Skip if handled by UnaryExpression or BinaryExpression
if (
node.parent?.type === 'UnaryExpression' ||
node.parent?.type === 'BinaryExpression'
) {
return;
}
if (!isFilterLengthCall(node)) {
return;
}
if (isInBooleanContext(node)) {
reportFilterLength(context, node, node, false);
}
}
};
}
};