Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add prefer-for-of rule (#338)
- Loading branch information
1 parent
d476f15
commit 3e26ab6
Showing
5 changed files
with
578 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Use for-of loops instead of standard for loops over arrays (prefer-for-of) | ||
|
||
This rule recommends a for-of loop when the loop index is only used to read from an array that is being iterated. | ||
|
||
## Rule Details | ||
|
||
For cases where the index is only used to read from the array being iterated, a for-of loop is easier to read and write. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
for (let i = 0; i < arr.length; i++) { | ||
console.log(arr[i]); | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
for (const x of arr) { | ||
console.log(x); | ||
} | ||
|
||
for (let i = 0; i < arr.length; i++) { | ||
// i is used to write to arr, so for-of could not be used. | ||
arr[i] = 0; | ||
} | ||
|
||
for (let i = 0; i < arr.length; i++) { | ||
// i is used independent of arr, so for-of could not be used. | ||
console.log(i, arr[i]); | ||
} | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you transpile for browsers that do not support for-of loops, you may wish to use traditional for loops that produce more compact code. | ||
|
||
## Related to | ||
|
||
- TSLint: ['prefer-for-of'](https://palantir.github.io/tslint/rules/prefer-for-of/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree'; | ||
import * as util from '../util'; | ||
import { Scope } from 'ts-eslint'; | ||
|
||
export default util.createRule({ | ||
name: 'prefer-for-of', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'Prefer a ‘for-of’ loop over a standard ‘for’ loop if the index is only used to access the array being iterated.', | ||
category: 'Stylistic Issues', | ||
recommended: false, | ||
tslintName: 'prefer-for-of', | ||
}, | ||
messages: { | ||
preferForOf: | ||
'Expected a `for-of` loop instead of a `for` loop with this simple iteration', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
function isSingleVariableDeclaration( | ||
node: TSESTree.Node | null, | ||
): node is TSESTree.VariableDeclaration { | ||
return ( | ||
node !== null && | ||
node.type === AST_NODE_TYPES.VariableDeclaration && | ||
node.kind !== 'const' && | ||
node.declarations.length === 1 | ||
); | ||
} | ||
|
||
function isLiteral(node: TSESTree.Expression, value: number): boolean { | ||
return node.type === AST_NODE_TYPES.Literal && node.value === value; | ||
} | ||
|
||
function isZeroInitialized(node: TSESTree.VariableDeclarator): boolean { | ||
return node.init !== null && isLiteral(node.init, 0); | ||
} | ||
|
||
function isMatchingIdentifier( | ||
node: TSESTree.Expression, | ||
name: string, | ||
): boolean { | ||
return node.type === AST_NODE_TYPES.Identifier && node.name === name; | ||
} | ||
|
||
function isLessThanLengthExpression( | ||
node: TSESTree.Node | null, | ||
name: string, | ||
): TSESTree.Expression | null { | ||
if ( | ||
node !== null && | ||
node.type === AST_NODE_TYPES.BinaryExpression && | ||
node.operator === '<' && | ||
isMatchingIdentifier(node.left, name) && | ||
node.right.type === AST_NODE_TYPES.MemberExpression && | ||
isMatchingIdentifier(node.right.property, 'length') | ||
) { | ||
return node.right.object; | ||
} | ||
return null; | ||
} | ||
|
||
function isIncrement(node: TSESTree.Node | null, name: string): boolean { | ||
if (!node) { | ||
return false; | ||
} | ||
switch (node.type) { | ||
case AST_NODE_TYPES.UpdateExpression: | ||
// x++ or ++x | ||
return ( | ||
node.operator === '++' && isMatchingIdentifier(node.argument, name) | ||
); | ||
case AST_NODE_TYPES.AssignmentExpression: | ||
if (isMatchingIdentifier(node.left, name)) { | ||
if (node.operator === '+=') { | ||
// x += 1 | ||
return isLiteral(node.right, 1); | ||
} else if (node.operator === '=') { | ||
// x = x + 1 or x = 1 + x | ||
const expr = node.right; | ||
return ( | ||
expr.type === AST_NODE_TYPES.BinaryExpression && | ||
expr.operator === '+' && | ||
((isMatchingIdentifier(expr.left, name) && | ||
isLiteral(expr.right, 1)) || | ||
(isLiteral(expr.left, 1) && | ||
isMatchingIdentifier(expr.right, name))) | ||
); | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function contains(outer: TSESTree.Node, inner: TSESTree.Node): boolean { | ||
return ( | ||
outer.range[0] <= inner.range[0] && outer.range[1] >= inner.range[1] | ||
); | ||
} | ||
|
||
function isAssignee(node: TSESTree.Node): boolean { | ||
const parent = node.parent; | ||
if (!parent) { | ||
return false; | ||
} | ||
|
||
// a[i] = 1, a[i] += 1, etc. | ||
if ( | ||
parent.type === AST_NODE_TYPES.AssignmentExpression && | ||
parent.left === node | ||
) { | ||
return true; | ||
} | ||
|
||
// delete a[i] | ||
if ( | ||
parent.type === AST_NODE_TYPES.UnaryExpression && | ||
parent.operator === 'delete' && | ||
parent.argument === node | ||
) { | ||
return true; | ||
} | ||
|
||
// a[i]++, --a[i], etc. | ||
if ( | ||
parent.type === AST_NODE_TYPES.UpdateExpression && | ||
parent.argument === node | ||
) { | ||
return true; | ||
} | ||
|
||
// [a[i]] = [0] | ||
if (parent.type === AST_NODE_TYPES.ArrayPattern) { | ||
return true; | ||
} | ||
|
||
// [...a[i]] = [0] | ||
if (parent.type === AST_NODE_TYPES.RestElement) { | ||
return true; | ||
} | ||
|
||
// ({ foo: a[i] }) = { foo: 0 } | ||
if ( | ||
parent.type === AST_NODE_TYPES.Property && | ||
parent.parent !== undefined && | ||
parent.parent.type === AST_NODE_TYPES.ObjectExpression && | ||
parent.value === node && | ||
isAssignee(parent.parent) | ||
) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function isIndexOnlyUsedWithArray( | ||
body: TSESTree.Statement, | ||
indexVar: Scope.Variable, | ||
arrayExpression: TSESTree.Expression, | ||
): boolean { | ||
const sourceCode = context.getSourceCode(); | ||
const arrayText = sourceCode.getText(arrayExpression); | ||
return indexVar.references.every(reference => { | ||
const id = reference.identifier; | ||
const node = id.parent; | ||
return ( | ||
!contains(body, id) || | ||
(node !== undefined && | ||
node.type === AST_NODE_TYPES.MemberExpression && | ||
node.property === id && | ||
sourceCode.getText(node.object) === arrayText && | ||
!isAssignee(node)) | ||
); | ||
}); | ||
} | ||
|
||
return { | ||
'ForStatement:exit'(node: TSESTree.ForStatement) { | ||
if (!isSingleVariableDeclaration(node.init)) { | ||
return; | ||
} | ||
|
||
const [declarator] = node.init.declarations; | ||
if ( | ||
!isZeroInitialized(declarator) || | ||
declarator.id.type !== AST_NODE_TYPES.Identifier | ||
) { | ||
return; | ||
} | ||
|
||
const indexName = declarator.id.name; | ||
const arrayExpression = isLessThanLengthExpression( | ||
node.test, | ||
indexName, | ||
); | ||
if (!arrayExpression) { | ||
return; | ||
} | ||
|
||
const [indexVar] = context.getDeclaredVariables(node.init); | ||
if ( | ||
isIncrement(node.update, indexName) && | ||
isIndexOnlyUsedWithArray(node.body, indexVar, arrayExpression) | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'preferForOf', | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
Oops, something went wrong.