/
prefer-for-of.js
623 lines (563 loc) · 20.3 KB
/
prefer-for-of.js
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("assert")
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const SENTINEL_TYPE = /(?:Declaration|Statement)$/u
const MESSAGE = "Expected for-of statement."
/**
* Checks whether the given outer node contains the given inner node.
*
* @param {ASTNode} outerNode - The outer node to check.
* @param {ASTNode} innerNode - The inner node to check.
* @returns {boolean} `true` if the outerNode contains the innerNode.
*/
function contains(outerNode, innerNode) {
return (
outerNode.range[0] <= innerNode.range[0] &&
outerNode.range[1] >= innerNode.range[1]
)
}
/**
* Checks whether the given function node is the callback of `Array#forEach`
* which can be replaced by a statement or not.
*
* @param {ASTNode} node - The function node to check.
* @returns {boolean} `true` if the node is the callback of `Array#forEach`.
*/
function isCallbackOfArrayForEach(node) {
const parent = node.parent
return (
parent.type === "CallExpression" &&
parent.parent.type === "ExpressionStatement" &&
parent.callee.type === "MemberExpression" &&
parent.callee.property.type === "Identifier" &&
parent.callee.property.name === "forEach" &&
parent.arguments.length >= 1 &&
parent.arguments[0] === node
)
}
/**
* Checks whether the given function node has valid parameters to replace by a
* for-of statement.
*
* @param {ASTNode} node - The function node to check.
* @returns {boolean} `true` if the node has valid parameters.
*/
function isValidParams(node) {
return (
node.params.length === 1 && node.params[0].type !== "AssignmentPattern"
)
}
/**
* Checks whether the given node is an identifier or a member expression which
* does not include call expressions.
*
* @param {ASTNode} node - The node to check.
* @returns {boolean} `true` if the node is a simple reference.
*/
function isSimpleReference(node) {
return (
node.type === "Identifier" ||
node.type === "Literal" ||
(node.type === "MemberExpression" &&
isSimpleReference(node.object) &&
isSimpleReference(node.property))
)
}
/**
* Checks whether the given function node is called recursively.
*
* @param {RuleContext} context - The rule context to get variables.
* @param {ASTNode} node - The function node to check.
* @returns {boolean} `true` if the node is called recursively.
*/
function isCalledRecursively(context, node) {
return (
node.id != null &&
context.getDeclaredVariables(node)[0].references.length > 0
)
}
/**
* Checks whether the given `for` loop statement is a simple array traversing.
*
* for (let i = 0; i < array.length; ++i) {
* // do something.
* }
*
* @param {ASTNode} node - The `for` loop node to check.
* @returns {boolean} `true` if the node is a simple array traversing.
*/
// eslint-disable-next-line complexity -- We know it's too complex for ESLint
function isTraversingArray(node) {
const init = node.init
const test = node.test
const update = node.update
let indexDecl = null
let lengthDecl = null
/* eslint-disable @mysticatea/prettier -- start */
return (
init != null &&
init.type === "VariableDeclaration" &&
init.kind === "let" &&
init.declarations.length >= 1 &&
(indexDecl = init.declarations[0]) &&
indexDecl.id.type === "Identifier" &&
indexDecl.init != null &&
indexDecl.init.type === "Literal" &&
indexDecl.init.value === 0 &&
test != null &&
test.type === "BinaryExpression" &&
test.operator === "<" &&
test.left.type === "Identifier" &&
test.left.name === indexDecl.id.name &&
(
(
init.declarations.length === 1 &&
test.right.type === "MemberExpression" &&
test.right.property.type === "Identifier" &&
test.right.property.name === "length"
) || (
init.declarations.length === 2 &&
(lengthDecl = init.declarations[1]) &&
lengthDecl.id.type === "Identifier" &&
lengthDecl.init != null &&
lengthDecl.init.type === "MemberExpression" &&
lengthDecl.init.property.type === "Identifier" &&
lengthDecl.init.property.name === "length" &&
test.right.type === "Identifier" &&
test.right.name === lengthDecl.id.name
)
) &&
update != null &&
(
(
update.type === "UpdateExpression" &&
update.operator === "++" &&
update.argument.type === "Identifier" &&
update.argument.name === indexDecl.id.name
) || (
update.type === "AssignmentExpression" &&
update.operator === "+=" &&
update.left.type === "Identifier" &&
update.left.name === indexDecl.id.name &&
update.right.type === "Literal" &&
update.right.value === 1
) || (
update.type === "AssignmentExpression" &&
update.operator === "=" &&
update.left.type === "Identifier" &&
update.left.name === indexDecl.id.name &&
update.right.type === "BinaryExpression" &&
update.right.operator === "+" &&
(
(
update.right.left.type === "Identifier" &&
update.right.left.name === indexDecl.id.name &&
update.right.right.type === "Literal" &&
update.right.right.value === 1
) || (
update.right.left.type === "Literal" &&
update.right.left.value === 1 &&
update.right.right.type === "Identifier" &&
update.right.right.name === indexDecl.id.name
)
)
)
)
)
/* eslint-enable @mysticatea/prettier -- end */
}
/**
* Gets the iterating array's text of the given `for` loop.
*
* @param {SourceCode} sourceCode - The source code object to get text.
* @param {ASTNode} node - The node of `for` loop statement.
* @returns {string} The iterating array's text of the `for` loop.
*/
function getArrayTextOfForStatement(sourceCode, node) {
return node.init.declarations.length === 2
? sourceCode.getText(node.init.declarations[1].init.object)
: sourceCode.getText(node.test.right.object)
}
/**
* Checks whether the given node is in an assignee or not.
* @param {ASTNode} startNode The ndoe to check.
* @returns {boolean} `true` if the node is in an assignee.
*/
function isAssignee(startNode) {
let node = startNode
while (node && node.parent && !SENTINEL_TYPE.test(node.type)) {
const parent = node.parent
const assignee =
(parent.type === "AssignmentExpression" && parent.left === node) ||
(parent.type === "AssignmentPattern" && parent.left === node) ||
(parent.type === "VariableDeclarator" && parent.id === node) ||
(parent.type === "UpdateExpression" && parent.argument === node)
if (assignee) {
return true
}
node = parent
}
return false
}
/**
* Checks whether the all references of the index variable are used to get
* array elements.
*
* @param {RuleContext} context - The rule context object.
* @param {ASTNode} node - The `for` loop node which is a simple array
* traversing.
* @returns {boolean} `true` if the the all references of the index variable are
* used to get array elements.
*/
function isIndexVarOnlyUsedToGetArrayElements(context, node) {
const sourceCode = context.getSourceCode()
const arrayText = getArrayTextOfForStatement(sourceCode, node)
const indexVar = context.getDeclaredVariables(node.init)[0]
return indexVar.references.every((reference) => {
const id = reference.identifier
return (
!contains(node.body, id) ||
(id.parent.type === "MemberExpression" &&
id.parent.property === id &&
sourceCode.getText(id.parent.object) === arrayText &&
!isAssignee(id.parent))
)
})
}
/**
* Checks whether the all references of the index variable are used to get
* array elements.
*
* @param {RuleContext} context - The rule context object.
* @param {ASTNode} node - The `for` loop node which is a simple array
* traversing.
* @returns {boolean} `true` if the the all references of the index variable are
* used to get array elements.
*/
function isLengthVarOnlyUsedToTest(context, node) {
if (node.init.declarations.length !== 2) {
return true
}
const lengthVar = context.getDeclaredVariables(node.init.declarations[1])[0]
return lengthVar.references.every(
(reference) =>
reference.init || contains(node.test, reference.identifier)
)
}
/**
* Gets the variable object of the given name.
*
* @param {RuleContext} context - The rule context to get variables.
* @param {string} name - The variable name to get.
* @returns {escope.Variable|null} The found variable.
*/
function getVariableByName(context, name) {
let scope = context.getScope()
while (scope != null) {
const variable = scope.set.get(name)
if (variable != null) {
return variable
}
scope = scope.upper
}
return null
}
/**
* Gets the context node, the node which is allocated to `this` of a function,
* of the given function node.
* If the found context node contains CallExpression, this ignores it.
*
* @param {ASTNode} node - The function node to get.
* @returns {ASTNode|null} The found context node.
*/
function getContextNode(node) {
const callNode = node.parent
const contextNode =
callNode.arguments.length >= 2
? callNode.arguments[1]
: callNode.callee.object
if (isSimpleReference(contextNode)) {
return contextNode
}
return null
}
/**
* Gets the variable object of the given context node.
*
* @param {RuleContext} context - The rule context to get variables.
* @param {ASTNode} contextNode - The context node to get.
* @returns {escope.Variable|null} The found variable of the context node.
*/
function getContextVariable(context, contextNode) {
let node = contextNode
while (node.type === "MemberExpression") {
node = node.object
}
assert(node.type === "Identifier")
const scope = context.getScope().upper
return scope.set.get(node.name) || null
}
/**
* Gets the 1st statement of the given `for` loop statement if the 1st statement
* is variable declaration for the element variable.
*
* for (let i = 0; i < list.length; ++i) {
* const element = list[i]
* // do something.
* }
*
* @param {SourceCode} sourceCode - The source code object to get the text of
* nodes.
* @param {ASTNode} node - The `for` loop statement to get.
* @returns {ASTNode} The found declaration node.
*/
function getElementVariableDeclaration(sourceCode, node) {
let declaration = null
let declarator = null
const indexText = node.test.left.name
const arrayText = getArrayTextOfForStatement(sourceCode, node)
const isElementVariableDeclaration =
node.body.type === "BlockStatement" &&
node.body.body.length > 0 &&
(declaration = node.body.body[0]) &&
declaration.type === "VariableDeclaration" &&
(declarator = declaration.declarations[0]) &&
declarator.init.type === "MemberExpression" &&
declarator.init.computed &&
declarator.init.property.type === "Identifier" &&
declarator.init.property.name === indexText &&
sourceCode.getText(declarator.init.object) === arrayText
if (isElementVariableDeclaration) {
return declaration
}
return null
}
/**
* Converts the given node to a replacement fix object.
*
* @param {string} replaceText - The replacement text.
* @param {number} offset - The offset of node's range.
* @param {ASTNode} node - The node to replace.
* @returns {Fix} The created fix object.
*/
function convertToFix(replaceText, offset, node) {
return {
range: [node.range[0] - offset, node.range[1] - offset],
text: replaceText,
}
}
/**
* Applies the given fixes to the given text.
*
* @param {string} originalText - The text to fix.
* @param {Fix[]} fixes - The fixes to apply.
* @returns {string} The replaced text.
*/
function applyFixes(originalText, fixes) {
let text = ""
let lastPos = 0
fixes.sort((a, b) => a.range[0] - b.range[0])
for (const fix of fixes) {
assert(fix.range[0] >= lastPos)
text += originalText.slice(lastPos, fix.range[0])
text += fix.text
lastPos = fix.range[1]
}
text += originalText.slice(lastPos)
return text
}
/**
* Fixes the given `Array#forEach` to for-of statement.
*
* @param {RuleContext} context - The rule context object.
* @param {object} callbackInfo - The information of the callback function of
* `Array#forEach`.
* @param {RuleFixer} fixer - The fixer to fix.
* @returns {Fix|null} The created fix object.
*/
function fixArrayForEach(context, callbackInfo, fixer) {
const sourceCode = context.getSourceCode()
const funcNode = callbackInfo.node
const callNode = funcNode.parent
const calleeNode = callNode.callee
const returnNodes = callbackInfo.returnNodes
const thisNodes = callbackInfo.thisNodes
const contextNode = callbackInfo.contextNode
const canReplaceAllThis = callbackInfo.canReplaceAllThis
// Not fix if the callee is multiline.
if (calleeNode.loc.start.line !== calleeNode.loc.end.line) {
return null
}
// Not fix if thisNodes exist and cannot replace those.
if (thisNodes.length > 0 && !canReplaceAllThis) {
return null
}
const arrayText = sourceCode.getText(calleeNode.object)
const elementText = sourceCode.getText(funcNode.params[0])
const originalBodyText = sourceCode.getText(funcNode.body)
const contextText = contextNode && sourceCode.getText(contextNode)
const semiText = funcNode.body.type !== "BlockStatement" ? ";" : ""
const bodyOffset = funcNode.body.range[0]
const bodyFixes = [].concat(
returnNodes.map(convertToFix.bind(null, "continue;", bodyOffset)),
thisNodes.map(convertToFix.bind(null, contextText, bodyOffset))
)
const bodyText =
bodyFixes.length > 0
? applyFixes(originalBodyText, bodyFixes)
: originalBodyText
return fixer.replaceText(
callNode.parent,
`for (let ${elementText} of ${arrayText}) ${bodyText}${semiText}`
)
}
/**
* Fixes the given `for` loop statement to for-of statement.
*
* @param {RuleContext} context - The rule context object.
* @param {ASTNode} node - The `for` loop statement to fix.
* @param {RuleFixer} fixer - The fixer to fix.
* @returns {Fix|null} The created fix object.
*/
function fixForStatement(context, node, fixer) {
const sourceCode = context.getSourceCode()
const element = getElementVariableDeclaration(sourceCode, node)
// Cannot fix if element name is unknown.
if (element == null || !isLengthVarOnlyUsedToTest(context, node)) {
return null
}
const arrayText = getArrayTextOfForStatement(sourceCode, node)
const elementText = sourceCode.getText(element.declarations[0].id)
return fixer.replaceTextRange(
[node.range[0], element.range[1]],
`for (let ${elementText} of ${arrayText}) {`
)
}
module.exports = {
meta: {
docs: {
description: "requires for-of statements instead of Array#forEach",
category: "Best Practices",
recommended: false,
url: "https://github.com/mysticatea/eslint-plugin/blob/v13.0.0/docs/rules/prefer-for-of.md",
},
fixable: "code",
schema: [],
type: "suggestion",
},
create(context) {
let funcInfo = null
/**
* Processes to enter a function.
* Push new information object to the function stack.
*
* @param {ASTNode} node - The function node which is entered.
* @returns {void}
*/
function enterFunction(node) {
const isTarget =
isCallbackOfArrayForEach(node) &&
isValidParams(node) &&
!isCalledRecursively(context, node)
const contextNode = isTarget ? getContextNode(node) : null
const contextVar =
contextNode && getContextVariable(context, contextNode)
funcInfo = {
upper: funcInfo,
isTarget,
node,
contextNode,
contextVar,
returnNodes: [],
thisNodes: [],
canReplaceAllThis: contextVar != null,
}
}
/**
* Processes to exit a function.
* Pop the last item of the function stack and report it.
*
* @param {ASTNode} node - The function node which is exited.
* @returns {void}
*/
function exitFunction() {
if (funcInfo.isTarget) {
const expressionStatementNode = funcInfo.node.parent.parent
context.report({
node: expressionStatementNode,
message: MESSAGE,
fix: fixArrayForEach.bind(null, context, funcInfo),
})
}
funcInfo = funcInfo.upper
}
return {
ArrowFunctionExpression: enterFunction,
FunctionExpression: enterFunction,
FunctionDeclaration: enterFunction,
"ArrowFunctionExpression:exit": exitFunction,
"FunctionExpression:exit": exitFunction,
"FunctionDeclaration:exit": exitFunction,
ReturnStatement(node) {
if (funcInfo != null && funcInfo.isTarget) {
funcInfo.returnNodes.push(node)
}
},
ThisExpression(node) {
let thisFuncInfo = funcInfo
while (
thisFuncInfo != null &&
thisFuncInfo.node.type === "ArrowFunctionExpression"
) {
thisFuncInfo = thisFuncInfo.upper
}
if (
thisFuncInfo != null &&
thisFuncInfo.isTarget &&
!thisFuncInfo.returnNodes.some((returnNode) =>
contains(returnNode, node)
)
) {
thisFuncInfo.thisNodes.push(node)
// If it replaced this by the context variable name,
// verify whether the reference gets the context variable or not.
if (thisFuncInfo.canReplaceAllThis) {
if (thisFuncInfo.contextVar != null) {
const variable = getVariableByName(
context,
thisFuncInfo.contextVar.name
)
thisFuncInfo.canReplaceAllThis =
variable === thisFuncInfo.contextVar
}
}
}
},
"ForStatement:exit"(node) {
if (
isTraversingArray(node) &&
isIndexVarOnlyUsedToGetArrayElements(context, node)
) {
context.report({
node,
message: MESSAGE,
fix: fixForStatement.bind(null, context, node),
})
}
},
ForInStatement(node) {
context.report({ node, message: MESSAGE })
},
}
},
}