no-loop-func shouldn't warn when a function is used inside a forEach or such functions
See original GitHub issueWhat version are you using? 2.0.0-beta.1
What did you do? I tested this code using the no-loop-func rule:
for (const score in scores) {
const letters = scores[score];
letters.split('').forEach(letter => {
result[letter] = score;
});
}
Because of the function in forEach, the no-loop-func gives a warning here. Yet this is not an issue because forEach’s parameter actually runs right away, it’s not an asynchronous callback.
This should be relaxed at least for all Array iteration methods (also for Map and Set, but this is also a forEach
method; maybe we can accept that all forEach/map/etc methods will behave the same).
And also the Promise constructor.
I’d be happy enough with a rule configuration.
Issue Analytics
- State:
- Created 8 years ago
- Comments:33 (19 by maintainers)
Top Results From Across the Web
no-loop-func - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >"ESLint no-loop-func rule" What to do when loop variables are ...
"ESLint no-loop-func rule" What to do when loop variables are required in a callback · 1. Create a function that accepts those as...
Read more >no-loop-func | typescript-eslint
Disallow function declarations that contain unsafe references inside loop statements. Examples. This rule extends the base eslint/no-loop-func rule.
Read more >this - JavaScript | MDN
It can't be set by assignment during execution, and it may be different each time the function is called. The bind() method can...
Read more >Array methods - The Modern JavaScript Tutorial
The splice method is also able to insert the elements without any ... forEach(function(item, index, array) { // ... do something with item...
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
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you for this issue.
I know that immediate called functions are no problem even if those exist in a loop. But it’s too hard to detect whether it’s immediate called or not in static analysis. So I guess
no-loop-func
is warning all functions in a loop.I often write
// eslint-disable-line no-loop-func
, so I think it’s inconvenient, though…@krassowski No, it’s not a false positive. You should move the function outside of the loop: