Skip to content

Commit a4bf617

Browse files
Vibe Botclaude
andcommitted
Address PR comment from francois-mora-sonarsource
Comment: The PR is missing unit tests for the nested loop case. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 2f31159 commit a4bf617

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

packages/jsts/src/rules/S2310/unit.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,34 @@ describe('S2310 - valid patterns', () => {
318318
ctx.lineTo(x, y);
319319
}
320320
}
321+
`,
322+
},
323+
// Nested loop: UpdateExpression on outer counter in nested loop body is compliant
324+
{
325+
code: `
326+
function processMatrix(matrix) {
327+
for (var i = 0; i < matrix.length; i++) {
328+
for (var j = 0; j < matrix[i].length; j++) {
329+
if (matrix[i][j] === 'skip') {
330+
i++; // Compliant: intentional skip-ahead in nested loop body
331+
break;
332+
}
333+
}
334+
}
335+
}
336+
`,
337+
},
338+
// Nested loop: compound assignment on outer counter in nested loop body is compliant
339+
{
340+
code: `
341+
function processBatches(items, batchSize) {
342+
for (var i = 0; i < items.length; i++) {
343+
for (var j = 0; j < batchSize && i + j < items.length; j++) {
344+
process(items[i + j]);
345+
}
346+
i += batchSize - 1; // Compliant: compound assignment skip-ahead after nested loop
347+
}
348+
}
321349
`,
322350
},
323351
],
@@ -474,6 +502,29 @@ describe('S2310 - invalid patterns', () => {
474502
],
475503
settings: { sonarRuntime: true },
476504
},
505+
// Nested loop: outer counter in nested for-loop's update clause is flagged
506+
// even with compound assignment operator
507+
{
508+
code: `
509+
for (var i = 0; i < 10; i++) {
510+
for (var j = 0; j < 5; j++, i += 2) { // Noncompliant
511+
console.log(i, j);
512+
}
513+
}
514+
`,
515+
errors: [{ message: 'Remove this assignment of "i".', line: 3 }],
516+
},
517+
// Nested loop: simple assignment to outer counter in nested loop body is flagged
518+
{
519+
code: `
520+
for (var i = 0; i < 10; i++) {
521+
for (var j = 0; j < 5; j++) {
522+
i = j; // Noncompliant: simple assignment in nested loop body
523+
}
524+
}
525+
`,
526+
errors: [{ message: 'Remove this assignment of "i".', line: 4 }],
527+
},
477528
],
478529
});
479530
});

0 commit comments

Comments
 (0)