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.

no-loop-func shouldn't warn when a function is used inside a forEach or such functions

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:33 (19 by maintainers)

github_iconTop GitHub Comments

2reactions
mysticateacommented, Jan 24, 2016

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…

1reaction
not-an-aardvarkcommented, Sep 12, 2017

@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
}
Read more comments on GitHub >

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

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 Hashnode Post

No results found