-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathprefer-inline-equality.ts
More file actions
210 lines (179 loc) · 4.68 KB
/
Copy pathprefer-inline-equality.ts
File metadata and controls
210 lines (179 loc) · 4.68 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
import type {TSESLint, TSESTree} from '@typescript-eslint/utils';
import {isArrayType, isSetType} from '../utils/typescript.js';
type MessageIds = 'preferEquality';
/**
* Checks if a node is safe to repeat (i.e. no side effects)
*/
function isSafeToRepeat(node: TSESTree.Node): boolean {
if (node.type === 'Identifier') {
return true;
}
if (node.type === 'Literal') {
return true;
}
if (node.type === 'TemplateLiteral' && node.expressions.length === 0) {
return true;
}
if (node.type === 'MemberExpression' && !node.computed) {
return isSafeToRepeat(node.object);
}
return false;
}
/**
* Checks if a node is NaN
*/
function isNaN(node: TSESTree.Node): boolean {
return node.type === 'Identifier' && node.name === 'NaN';
}
/**
* Checks if an array element is an identifier or literal
*/
function isSimpleElement(node: TSESTree.Node): boolean {
if (isNaN(node)) {
return false;
}
return node.type === 'Identifier' || node.type === 'Literal';
}
/**
* Checks if the node is negated
*/
function isNegated(node: TSESTree.Node): boolean {
return (
node.parent !== undefined &&
node.parent.type === 'UnaryExpression' &&
node.parent.operator === '!'
);
}
/**
* Checks if the replacement expression needs to be wrapped in parentheses
* based on the parent node context
*/
function needsParentheses(node: TSESTree.Node): boolean {
if (!node.parent) {
return false;
}
switch (node.parent.type) {
case 'CallExpression':
case 'NewExpression':
case 'MemberExpression':
case 'ConditionalExpression':
case 'BinaryExpression':
case 'LogicalExpression':
case 'UnaryExpression':
case 'TaggedTemplateExpression':
case 'SpreadElement':
case 'AwaitExpression':
return true;
default:
return false;
}
}
function checkArrayIncludes(
node: TSESTree.CallExpression,
context: TSESLint.RuleContext<MessageIds, []>
): void {
const {callee} = node;
if (callee.type !== 'MemberExpression') {
return;
}
const property = callee.property;
if (
property.type !== 'Identifier' ||
property.name !== 'includes' ||
callee.computed
) {
return;
}
if (node.arguments.length !== 1) {
return;
}
const arrayNode = callee.object;
if (arrayNode.type !== 'ArrayExpression') {
return;
}
const elements = arrayNode.elements;
if (elements.length === 0 || elements.length > 6) {
return;
}
const val = node.arguments[0]!;
if (!isSafeToRepeat(val)) {
return;
}
for (const element of elements) {
if (element === null) {
return;
}
if (element.type === 'SpreadElement') {
const arg = element.argument;
if (!isSafeToRepeat(arg)) {
return;
}
if (
isArrayType(arg, context) !== true &&
isSetType(arg, context) !== true
) {
return;
}
} else if (!isSimpleElement(element)) {
return;
}
}
const sourceCode = context.sourceCode;
const negated = isNegated(node);
const operator = negated ? '!==' : '===';
const joiner = negated ? ' && ' : ' || ';
const parts: string[] = [];
for (const element of elements) {
if (element === null) {
return;
}
if (element.type === 'SpreadElement') {
const argText = sourceCode.getText(element.argument);
const valText = sourceCode.getText(val);
const method = isSetType(element.argument, context) ? 'has' : 'includes';
if (negated) {
parts.push(`!${argText}.${method}(${valText})`);
} else {
parts.push(`${argText}.${method}(${valText})`);
}
} else {
const elemText = sourceCode.getText(element);
const valText = sourceCode.getText(val);
parts.push(`${elemText} ${operator} ${valText}`);
}
}
const replacement = parts.join(joiner);
const reportNode = negated ? node.parent : node;
const needsParens = needsParentheses(reportNode);
const fixText = needsParens ? `(${replacement})` : replacement;
context.report({
node: reportNode,
messageId: 'preferEquality',
fix(fixer) {
return fixer.replaceText(reportNode, fixText);
}
});
}
export const preferInlineEquality: TSESLint.RuleModule<MessageIds, []> = {
meta: {
type: 'suggestion',
docs: {
description:
'Prefer inline equality checks over temporary object creation for simple comparisons'
},
fixable: 'code',
messages: {
preferEquality:
'Avoid creating a temporary array just to call `.includes()`. Use equality checks instead.'
},
schema: []
},
defaultOptions: [],
create(context) {
return {
CallExpression(node: TSESTree.CallExpression) {
checkArrayIncludes(node, context);
}
};
}
};