Skip to content

Commit

Permalink
Modify rule S4138: Include warning about browser array-like collectio…
Browse files Browse the repository at this point in the history
…ns in TypeScript (#1303)
  • Loading branch information
ilia-kebets-sonarsource committed Oct 3, 2022
1 parent 792b2cd commit 8c3a205
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions rules/S4138/javascript/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
If you have an iterable, such as an array, set, or list, your best option for looping through its values is the ``++for of++`` syntax. Use a counter, and ... well you'll get the right behavior, but your code just isn't as clean or clear.

In a browser environment, `NodeList` and other array-like collections should work by default. If you are using TypeScript and seeing a type error, make sure your configuration is correct.

== Noncompliant Code Example

Expand All @@ -8,7 +9,7 @@ If you have an iterable, such as an array, set, or list, your best option for lo
const arr = [4, 3, 2, 1];
for (let i = 0; i < arr.length; i++) { // Noncompliant
console.log(arr[i]);
console.log(arr[i]);
}
----

Expand All @@ -19,8 +20,8 @@ for (let i = 0; i < arr.length; i++) { // Noncompliant
----
const arr = [4, 3, 2, 1];
for (let value of arr) {
console.log(value);
for (let value of arr) {
console.log(value);
}
----

Expand Down

0 comments on commit 8c3a205

Please sign in to comment.