Skip to content

fix: replace for loop with for-of loop in dbSchemaChallenge_1.ts#17

Open
sonarqube-agent[bot] wants to merge 1 commit intomasterfrom
remediate-master-20260330-075801-563f63d2
Open

fix: replace for loop with for-of loop in dbSchemaChallenge_1.ts#17
sonarqube-agent[bot] wants to merge 1 commit intomasterfrom
remediate-master-20260330-075801-563f63d2

Conversation

@sonarqube-agent
Copy link
Copy Markdown

Replaced a traditional index-based for loop with a for-of loop to improve code readability and eliminate unnecessary counter variables. This change addresses a SonarQube code smell (S4138) and follows modern JavaScript best practices for iterating through arrays.

View Project in SonarCloud


Fixed Issues

typescript:S4138 - Expected a `for-of` loop instead of a `for` loop with this simple iteration. • MINORView issue

Location: data/static/codefixes/dbSchemaChallenge_1.ts:8

Why is this an issue?

for...of statements are used to iterate over the values of an iterable object. Iterables are objects implementing the @@iterator method, which returns an object conforming to the iterator protocol. JavaScript provides many built-in iterables that can and should be used with this looping statement.

What changed

Replaces the traditional index-based for loop (for (let i = 0; i < products.length; i++)) with a for...of loop (for (const product of products)), and updates the loop body to reference the iteration variable product directly instead of using products[i]. This addresses the code smell about preferring for...of over a simple for loop when iterating through an iterable object like an array, improving readability and eliminating the unnecessary counter variable.

--- a/data/static/codefixes/dbSchemaChallenge_1.ts
+++ b/data/static/codefixes/dbSchemaChallenge_1.ts
@@ -8,3 +8,3 @@ module.exports = function searchProducts () {
-        for (let i = 0; i < products.length; i++) {
-          products[i].name = req.__(products[i].name)
-          products[i].description = req.__(products[i].description)
+        for (const product of products) {
+          product.name = req.__(product.name)
+          product.description = req.__(product.description)

Have a suggestion or found an issue? Share your feedback here.


SonarQube Remediation Agent uses AI. Check for mistakes.

Fixed issues:
- AZWU-tYyYJSZqVQVbSdY for typescript:S4138 rule

Generated by SonarQube Agent (task: a92c7fe3-5764-422c-9956-fd4eee497b80)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants