sindresorhus/eslint-plugin-unicorn

`no-for-loop` breaks on ArrayLike DOM nodes (like `<form>`)

Geschlossen

#1.531 geöffnet am 18.09.2021

 (5 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)JavaScript (468 Forks)user submission
bughelp wanted

Repository-Metriken

Stars
 (5.022 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 4h 30m) (26 gemergte PRs in 30 T)

Beschreibung

For loops for non-iterable DOM nodes are treated as iterable arrays.

I'm not sure if this is detectable, surely at least some edge cases wouldn't be - but when the variable is populated immediately above the loop it seems at least theoretically feasible.

Affected rule: no-for-loop

Input:

const visibleItems = document.querySelector('.visible');
for (let x = 0; x < visibleItems.length; x++) {
  someFunc(visibleItems[x])
}

Output:

const visibleItems = document.querySelector('.visible');
for (const [x, visibleItem] of visibleItems.entries()) {
  someFunc(visibleItem)
}

As an aside, the fact that the auto-fix depluralizes visibleItems[x] to visibleItem is a very nice touch!

Contributor Guide