org.sonar.l10n.javascript.rules.javascript.S4158.html Maven / Gradle / Ivy
Why is this an issue?
When a collection is empty with absolute certainty, it makes no sense to access or iterate it. Doing so can lead to unexpected behavior or errors
in the code. The most common cause is that population was accidentally omitted or removed.
const strings = [];
if (strings.includes("foo")) {} // Noncompliant: strings is always empty
for (const str of strings) {} // Noncompliant
strings.forEach(str => doSomething(str)); // Noncompliant
Make sure your code provides some way to populate the collection if their elements are to be accessed.
const strings = [];
strings.push("foo");
if (strings.includes("foo")) {}
for (const str of strings) {}
strings.forEach(str => doSomething(str));
Resources
Documentation
- MDN web docs - Array
- MDN web docs - Iterable
protocol