Skip to content

Commit 719cd00

Browse files
authored
Add no-array-for-each rule (#1017)
1 parent 306c9e7 commit 719cd00

12 files changed

+2037
-10
lines changed

docs/rules/no-array-for-each.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Prefer `for…of` over `Array#forEach(…)`
2+
3+
A [`for…of` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) is more readable than [`Array#forEach(…)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach).
4+
5+
This rule is partly fixable.
6+
7+
## Fail
8+
9+
```js
10+
array.forEach(element => {
11+
bar(element);
12+
});
13+
```
14+
15+
```js
16+
array.forEach((element, index) => {
17+
bar(element, index);
18+
});
19+
```
20+
21+
```js
22+
array.forEach((element, index, array) => {
23+
bar(element, index, array);
24+
});
25+
```
26+
27+
## Pass
28+
29+
```js
30+
for (const element of array) {
31+
bar(element);
32+
}
33+
```
34+
35+
```js
36+
for (const [index, element] of array.entries()) {
37+
bar(element, index);
38+
}
39+
```
40+
41+
```js
42+
for (const [index, element] of array.entries()) {
43+
bar(element, index, array);
44+
}
45+
```

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ module.exports = {
5555
'unicorn/new-for-builtins': 'error',
5656
'unicorn/no-abusive-eslint-disable': 'error',
5757
'unicorn/no-array-callback-reference': 'error',
58+
'unicorn/no-array-for-each': 'error',
5859
'unicorn/no-array-push-push': 'error',
5960
'unicorn/no-array-reduce': 'error',
6061
'unicorn/no-console-spaces': 'error',

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Configure it in `package.json`.
4949
"unicorn/new-for-builtins": "error",
5050
"unicorn/no-abusive-eslint-disable": "error",
5151
"unicorn/no-array-callback-reference": "error",
52+
"unicorn/no-array-for-each": "error",
5253
"unicorn/no-array-push-push": "error",
5354
"unicorn/no-array-reduce": "error",
5455
"unicorn/no-console-spaces": "error",
@@ -127,6 +128,7 @@ Configure it in `package.json`.
127128
- [new-for-builtins](docs/rules/new-for-builtins.md) - Enforce the use of `new` for all builtins, except `String`, `Number`, `Boolean`, `Symbol` and `BigInt`. *(partly fixable)*
128129
- [no-abusive-eslint-disable](docs/rules/no-abusive-eslint-disable.md) - Enforce specifying rules to disable in `eslint-disable` comments.
129130
- [no-array-callback-reference](docs/rules/no-array-callback-reference.md) - Prevent passing a function reference directly to iterator methods.
131+
- [no-array-for-each](docs/rules/no-array-for-each.md) - Prefer `for…of` over `Array#forEach(…)`. *(partly fixable)*
130132
- [no-array-push-push](docs/rules/no-array-push-push.md) - Enforce combining multiple `Array#push()` into one call. *(partly fixable)*
131133
- [no-array-reduce](docs/rules/no-array-reduce.md) - Disallow `Array#reduce()` and `Array#reduceRight()`.
132134
- [no-console-spaces](docs/rules/no-console-spaces.md) - Do not use leading/trailing space between `console.log` parameters. *(fixable)*

0 commit comments

Comments
 (0)