New rule: disallow push in Array.forEach
See original GitHub issuePlease describe what the rule should do:
The rule should disallow pushing results from Array.prototype.forEach
to another array. Instead, Array.prototype.map
should be used.
What category of rule is this? (place an “X” next to just one item)
[ ] Enforces code style [ ] Warns about a potential error [x] Suggests an alternate way of doing something [ ] Other (please specify:)
Provide 2-3 code examples that this rule will warn about:
// Disallowed
const result = [];
input.forEach(item => {
result.push(item + 1);
});
// Alternative construct
const result = input.map(item => item +1);
// Disallowed
const result = [];
input.forEach(item => {
if (item > 10) {
result.push(item + 1);
}
});
// Alternative construct
const result = input
.filter(item => item > 10)
.map(item => item +1);
Why should this rule be included in ESLint (instead of a plugin)?
- It is generic to a core part of JavaScript.
- I’ve manually detected the disallowed pattern many times in code review. Many people don’t seem to consider the alternative.
- It doesn’t fit in any existing plugin I’m aware of.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:4
- Comments:9 (9 by maintainers)
Top Results From Across the Web
forEach loop to pull element from array & push into new array
I use this foreach sequence in one of my code...maybe will help you. string[] Body;. string first = Body.First(); // get content of...
Read more >forEach is for side-effects - Nick Gard - Medium
It's a great method as it allows us to bypass all the boilerplate of making a for-loop: no temporary index variable, no checking...
Read more >no-await-in-loop - ESLint - Pluggable JavaScript Linter
This rule disallows the use of await within loop bodies. ... Good: all asynchronous operations are immediately started. results.push(bar(thing)); ...
Read more >Array - Adobe ActionScript® 3 (AS3 ) API Reference
Executes a test function on each item in the array and constructs a new array for all items that return true for the...
Read more >5. Working with Arrays and Loops - JavaScript Cookbook [Book]
To the developer, there is no difference: you can invoke an Array method on ... The Array push method creates a new array...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So just to clarify, would this rule disallow any call to a
.push()
function from inside a.forEach()
function? There are a few other cases where it can’t be directly translated to a map/filter:I’m not sure how I feel about the rule yet; I agree that using something like
.map()
is generally better than.forEach()
and.push()
, but I’m concerned that we won’t be able to detect this accurately enough.FWIW,
array-callback-return
has exactly the same limitation.