question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New rule: disallow push in Array.forEach

See original GitHub issue

Please 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:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
not-an-aardvarkcommented, Oct 28, 2016

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:

foo.forEach(element => {
  doSomethingWithSideEffects();
  bar.push(element);
});

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.

2reactions
Wilfredcommented, Oct 27, 2016

FWIW, array-callback-return has exactly the same limitation.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found