Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no-array-for-each rule #1017

Merged
merged 43 commits into from Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
ea0b64c
Add `no-array-for-each`
fisker Jan 11, 2021
2a638a3
Merge branch 'master' into no-array-for-each
fisker Jan 14, 2021
aa5786e
Refactor
fisker Jan 14, 2021
298e2e1
Tests
fisker Jan 14, 2021
59c70a7
Tests
fisker Jan 14, 2021
5c18e07
Tests
fisker Jan 14, 2021
507863f
fix
fisker Jan 14, 2021
4782010
Update snapshot
fisker Jan 14, 2021
b17181b
Fix
fisker Jan 14, 2021
f1e723b
Rename
fisker Jan 14, 2021
b57da17
Style
fisker Jan 14, 2021
f871353
Support check `this`
fisker Jan 18, 2021
3bc64aa
Support check `function.id` and `arguments`
fisker Jan 18, 2021
d9f6141
Fix wrongly fix
fisker Jan 18, 2021
6b0b592
More tests
fisker Jan 18, 2021
109ab2b
Merge branch 'master' into no-array-for-each
fisker Jan 18, 2021
a747d93
Fix logic
fisker Jan 18, 2021
4f374e5
Style
fisker Jan 18, 2021
dcafc69
Style
fisker Jan 18, 2021
1a7e6d4
More tests
fisker Jan 18, 2021
3f2c9ef
Clean
fisker Jan 18, 2021
bfa8ea0
Simplify array parentheses check
fisker Jan 18, 2021
a9c1713
Fix ASI problem
fisker Jan 18, 2021
71cd4eb
Fix parameter check
fisker Jan 18, 2021
2bcc256
Add `return` in `switch`
fisker Jan 18, 2021
8dac1cd
Fix parenthesized callback
fisker Jan 18, 2021
2599ae5
Keep `semi` for arrow functions
fisker Jan 18, 2021
edd49ba
Ignore unreachable
fisker Jan 18, 2021
80cde64
Fix style
fisker Jan 18, 2021
0917e76
Fix `arguments` check
fisker Jan 18, 2021
c33b426
Test possible conflicts
fisker Jan 18, 2021
078e93b
Extend fix range
fisker Jan 18, 2021
f035aca
Add docs
fisker Jan 18, 2021
4941a08
Fix `lint`
fisker Jan 18, 2021
e32cb9f
Rename a function
fisker Jan 18, 2021
34e5a58
Improve `=>` token search, fix crash on `typescript` parser
fisker Jan 18, 2021
6a91f93
Fix code style
fisker Jan 18, 2021
bc47538
One more test
fisker Jan 18, 2021
d8447b6
Merge branch 'master' into no-array-for-each
fisker Jan 19, 2021
de64278
Update no-array-for-each.js
fisker Jan 19, 2021
1f1dbb8
Rename file
fisker Jan 19, 2021
6982ad4
Update no-array-for-each.md
sindresorhus Jan 19, 2021
c767fe0
Update no-array-for-each.js
sindresorhus Jan 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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