S7728 FP Use “for…of” loops instead of “forEach” method calls when object is not an iterable type

Make sure to read this post before raising a thread here:

Then tell us:

  • What language is this for?
    • JavaScript / Typescript
  • Which rule?
    • S7728
  • Why do you believe it’s a false-positive/false-negative?
    • Flags object which is not iterable.
    • When typing is available (Typescript or type check JS), Sonar should be able to tell that the .forEach is not replaceable with for…each.
  • Are you using
    • SonarQube Cloud?
    • SonarQube Server / Community Build - which version?
      • Community Build v25.10.0.114319
    • SonarQube for IDE - which IDE/version?
      • Unconnected VSC plugin
  • How can we reproduce the problem? Give us a self-contained snippet of code
class Test {
  private nodes: Element[];
  public forEach(validator: (e: Element) => boolean): void {
    //internal loop
    for (const n of this.nodes) {
      if (validator(n)) {
        // do something
        n.remove();
      }
    }
  }
}

new Test().forEach(() => true);

This a minimal example of the issue, for a real life example, see “prosemirror-model” npm package, the “Node” object type, which is an object with a forEach that is not itself an iterable object.

Hi @Tezra,

thanks for raising this. I agree, we should be checking if reported node is iterable, I create a ticket to fix the rule.

Cheers!