Skip to content

Commit

Permalink
Add no-array-for-each rule (#1017)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 19, 2021
1 parent 306c9e7 commit 719cd00
Show file tree
Hide file tree
Showing 12 changed files with 2,037 additions and 10 deletions.
45 changes: 45 additions & 0 deletions docs/rules/no-array-for-each.md
@@ -0,0 +1,45 @@
# Prefer `for…of` over `Array#forEach(…)`

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).

This rule is partly fixable.

## Fail

```js
array.forEach(element => {
bar(element);
});
```

```js
array.forEach((element, index) => {
bar(element, index);
});
```

```js
array.forEach((element, index, array) => {
bar(element, index, array);
});
```

## Pass

```js
for (const element of array) {
bar(element);
}
```

```js
for (const [index, element] of array.entries()) {
bar(element, index);
}
```

```js
for (const [index, element] of array.entries()) {
bar(element, index, array);
}
```
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -55,6 +55,7 @@ module.exports = {
'unicorn/new-for-builtins': 'error',
'unicorn/no-abusive-eslint-disable': 'error',
'unicorn/no-array-callback-reference': 'error',
'unicorn/no-array-for-each': 'error',
'unicorn/no-array-push-push': 'error',
'unicorn/no-array-reduce': 'error',
'unicorn/no-console-spaces': 'error',
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Expand Up @@ -49,6 +49,7 @@ Configure it in `package.json`.
"unicorn/new-for-builtins": "error",
"unicorn/no-abusive-eslint-disable": "error",
"unicorn/no-array-callback-reference": "error",
"unicorn/no-array-for-each": "error",
"unicorn/no-array-push-push": "error",
"unicorn/no-array-reduce": "error",
"unicorn/no-console-spaces": "error",
Expand Down Expand Up @@ -127,6 +128,7 @@ Configure it in `package.json`.
- [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)*
- [no-abusive-eslint-disable](docs/rules/no-abusive-eslint-disable.md) - Enforce specifying rules to disable in `eslint-disable` comments.
- [no-array-callback-reference](docs/rules/no-array-callback-reference.md) - Prevent passing a function reference directly to iterator methods.
- [no-array-for-each](docs/rules/no-array-for-each.md) - Prefer `for…of` over `Array#forEach(…)`. *(partly fixable)*
- [no-array-push-push](docs/rules/no-array-push-push.md) - Enforce combining multiple `Array#push()` into one call. *(partly fixable)*
- [no-array-reduce](docs/rules/no-array-reduce.md) - Disallow `Array#reduce()` and `Array#reduceRight()`.
- [no-console-spaces](docs/rules/no-console-spaces.md) - Do not use leading/trailing space between `console.log` parameters. *(fixable)*
Expand Down

0 comments on commit 719cd00

Please sign in to comment.