forked from e18e/eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefer-timer-args.ts
More file actions
230 lines (198 loc) · 6.29 KB
/
Copy pathprefer-timer-args.ts
File metadata and controls
230 lines (198 loc) · 6.29 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
import type {Rule} from 'eslint';
import type {CallExpression, Expression, SpreadElement} from 'estree';
function isNullOrUndefined(node: Expression): boolean {
if (node.type === 'Literal' && node.value === null) {
return true;
}
return node.type === 'Identifier' && node.name === 'undefined';
}
function isTimerCall(node: CallExpression): boolean {
if (
node.callee.type === 'Identifier' &&
(node.callee.name === 'setTimeout' || node.callee.name === 'setInterval')
) {
return true;
}
if (
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
(node.callee.object.name === 'window' ||
node.callee.object.name === 'globalThis') &&
node.callee.property.type === 'Identifier' &&
(node.callee.property.name === 'setTimeout' ||
node.callee.property.name === 'setInterval')
) {
return true;
}
return false;
}
function isSafeArgument(arg: Expression | SpreadElement): boolean {
if (arg.type === 'SpreadElement') {
return arg.argument.type === 'Identifier';
}
switch (arg.type) {
case 'Identifier':
case 'Literal':
case 'TemplateLiteral':
return true;
case 'MemberExpression':
if (
arg.object.type === 'Super' ||
arg.property.type === 'PrivateIdentifier'
) {
return false;
}
if (!isSafeArgument(arg.object)) {
return false;
}
if (arg.computed) {
return isSafeArgument(arg.property);
}
return true;
case 'ArrayExpression':
return arg.elements.every((el) => el === null || isSafeArgument(el));
case 'ObjectExpression':
return arg.properties.every((prop) => {
if (prop.type === 'SpreadElement') {
return isSafeArgument(prop.argument);
}
const valueType = prop.value.type;
if (
valueType === 'ObjectPattern' ||
valueType === 'ArrayPattern' ||
valueType === 'RestElement' ||
valueType === 'AssignmentPattern'
) {
return false;
}
return isSafeArgument(prop.value);
});
case 'UnaryExpression':
case 'UpdateExpression':
return isSafeArgument(arg.argument);
case 'BinaryExpression':
case 'LogicalExpression':
return (
arg.left.type !== 'PrivateIdentifier' &&
isSafeArgument(arg.left) &&
isSafeArgument(arg.right)
);
case 'ConditionalExpression':
return (
isSafeArgument(arg.test) &&
isSafeArgument(arg.consequent) &&
isSafeArgument(arg.alternate)
);
// CallExpression, NewExpression, etc.
default:
return false;
}
}
export const preferTimerArgs: Rule.RuleModule = {
meta: {
type: 'suggestion',
docs: {
description:
'Prefer passing function and arguments directly to setTimeout/setInterval instead of wrapping in an arrow function or using bind',
recommended: true
},
fixable: 'code',
schema: [],
messages: {
preferArgs:
'Pass function and arguments directly to timer function to avoid allocating an extra function'
}
},
create(context) {
const sourceCode = context.sourceCode;
return {
CallExpression(node: CallExpression) {
if (!isTimerCall(node)) {
return;
}
if (node.arguments.length < 2) {
return;
}
const firstArg = node.arguments[0];
if (!firstArg || firstArg.type === 'SpreadElement') {
return;
}
const delayText = sourceCode.getText(node.arguments[1]!);
const timerCall = sourceCode.getText(node.callee);
let replacement: string | null = null;
// simple arrow functions, e.g. () => fn(args)
if (firstArg.type === 'ArrowFunctionExpression') {
// skip if it is a block body
if (firstArg.body.type === 'BlockStatement') {
return;
}
// skip if it has parameters
if (firstArg.params.length > 0) {
return;
}
if (firstArg.body.type !== 'CallExpression') {
return;
}
const callExpression = firstArg.body;
const callee = callExpression.callee;
const callArgs = callExpression.arguments;
if (callee.type === 'MemberExpression') {
return;
}
if (!callArgs.every(isSafeArgument)) {
return;
}
const calleeText = sourceCode.getText(callee);
if (callArgs.length === 0) {
replacement = `${timerCall}(${calleeText}, ${delayText})`;
} else {
const argsTexts = callArgs.map((arg) => sourceCode.getText(arg));
replacement = `${timerCall}(${calleeText}, ${delayText}, ${argsTexts.join(', ')})`;
}
}
// fn.bind(null/undefined, args)
else if (firstArg.type === 'CallExpression') {
const bindCall = firstArg;
if (
bindCall.callee.type !== 'MemberExpression' ||
bindCall.callee.property.type !== 'Identifier' ||
bindCall.callee.property.name !== 'bind' ||
bindCall.arguments.length === 0
) {
return;
}
const bindContext = bindCall.arguments[0];
if (!bindContext || bindContext.type === 'SpreadElement') {
return;
}
if (!isNullOrUndefined(bindContext)) {
return;
}
const fnText = sourceCode.getText(bindCall.callee.object);
const bindArgs = bindCall.arguments.slice(1);
// Check if any bind argument contains a call expression or other unsafe construct
if (!bindArgs.every(isSafeArgument)) {
return;
}
if (bindArgs.length === 0) {
replacement = `${timerCall}(${fnText}, ${delayText})`;
} else {
const argsTexts = bindArgs.map((arg) => sourceCode.getText(arg));
replacement = `${timerCall}(${fnText}, ${delayText}, ${argsTexts.join(', ')})`;
}
} else {
return;
}
if (replacement) {
context.report({
node,
messageId: 'preferArgs',
fix(fixer) {
return fixer.replaceText(node, replacement);
}
});
}
}
};
}
};