eslint: no-loop-func shouldn't warn when a function is used inside a forEach or such functions
What 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.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 33 (19 by maintainers)
Commits related to this issue
- Fix: reduced `no-loop-func` false positive (fixes #5044) — committed to eslint/eslint by mysticatea 8 years ago
- Fix: reduced `no-loop-func` false positive (fixes #5044) — committed to eslint/eslint by mysticatea 8 years ago
- Merge pull request #5094 from eslint/no-loop-func/allow-unmodified-vars Fix: reduced `no-loop-func` false positive (fixes #5044) — committed to eslint/eslint by nzakas 8 years ago
- remote no-loop-func due to https://github.com/eslint/eslint/issues/5044#issuecomment-195665043 — committed to jiangfengming/eslint-config-unambiguous by deleted user 8 years ago
- remove no-loop-func due to https://github.com/eslint/eslint/issues/5044#issuecomment-195665043 — committed to jiangfengming/eslint-config-unambiguous by deleted user 8 years ago
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: