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

Most upvoted 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:

var width_per_group = 10
var width = groups.length * width_per_group
var shift = - width / 2

function handleNode(node) {
    return do_some_stuff(node, shift);
}

for(var i = 0; i < groups.length; i++)
{
    groups[i].attr('transform', handleNode)
    shift += width_per_group
}