Skip to content

noBreakInNestedLoop (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noBreakInNestedLoop": "error"
}
}
}
}

Disallow break statements inside nested loops and inside switch statements in loops.

When a loop or a switch statement is inside another loop, it’s easy to misread which one a break statement exits. Move the inner loop or switch statement into a separate function, so that each break statement clearly belongs to one loop.

A break or continue statement with a label, such as break outer;, isn’t reported, because the label names the statement it applies to. Statements inside a function that is written inside a loop are checked separately from that loop.

for (const item of items) {
for (const child of item.children) {
if (child.done) {
break;
}
}
}
code-block.js:4:13 lint/nursery/noBreakInNestedLoop ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This break statement applies to a loop that is inside an outer loop.

2 │ for (const child of item.children) {
3 │ if (child.done) {
> 4 │ break;
│ ^^^^^^
5 │ }
6 │ }

ℹ The break statement applies to this loop.

1 │ for (const item of items) {
> 2 │ for (const child of item.children) {
│ ^^^
3 │ if (child.done) {
4 │ break;

ℹ This is the outer loop.

> 1 │ for (const item of items) {
│ ^^^
2 │ for (const child of item.children) {
3 │ if (child.done) {

ℹ When loops and switch statements are nested, it's easy to misread which one a break statement applies to.

ℹ Move the inner loop into a separate function.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

for (const item of items) {
switch (item.type) {
case "hidden":
break;
default:
process(item);
}
}
code-block.js:4:13 lint/nursery/noBreakInNestedLoop ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This break statement applies to a switch statement that is inside an outer loop.

2 │ switch (item.type) {
3 │ case "hidden":
> 4 │ break;
│ ^^^^^^
5 │ default:
6 │ process(item);

ℹ The break statement applies to this switch statement.

1 │ for (const item of items) {
> 2 │ switch (item.type) {
│ ^^^^^^
3 │ case "hidden":
4 │ break;

ℹ This is the outer loop.

> 1 │ for (const item of items) {
│ ^^^
2 │ switch (item.type) {
3 │ case "hidden":

ℹ When loops and switch statements are nested, it's easy to misread which one a break statement applies to.

ℹ Move the inner switch statement into a separate function.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

function processChildren(item) {
for (const child of item.children) {
if (child.done) {
break;
}
}
}
for (const item of items) {
processChildren(item);
}
for (const item of items) {
for (const child of item.children) {
check(child);
}
}
outer: for (const item of items) {
for (const child of item.children) {
if (child.done) {
break outer;
}
}
}

Type: boolean

Default: false

Also report continue statements without a label inside nested loops and inside switch statements in loops. It’s off by default so that early continue statements in an inner loop, such as if (!item.isValid) continue;, are allowed.

A continue statement inside a switch statement doesn’t move to the next case: it skips to the next iteration of the loop around the switch statement. When this option is enabled, add a label to that loop and use it in the continue statement to make this clear.

biome.json
{
"linter": {
"rules": {
"nursery": {
"noBreakInNestedLoop": {
"level": "on",
"options": {
"checkContinue": true
}
}
}
}
}
}
for (const group of groups) {
for (const item of group) {
if (!item.isValid) {
continue;
}
save(item);
}
}
code-block.js:4:13 lint/nursery/noBreakInNestedLoop ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This continue statement applies to a loop that is inside an outer loop.

2 │ for (const item of group) {
3 │ if (!item.isValid) {
> 4 │ continue;
│ ^^^^^^^^^
5 │ }
6 │ save(item);

ℹ The continue statement applies to this loop.

1 │ for (const group of groups) {
> 2 │ for (const item of group) {
│ ^^^
3 │ if (!item.isValid) {
4 │ continue;

ℹ This is the outer loop.

> 1 │ for (const group of groups) {
│ ^^^
2 │ for (const item of group) {
3 │ if (!item.isValid) {

ℹ When loops and switch statements are nested, it's easy to misread which one a continue statement applies to.

ℹ Move the inner loop into a separate function.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

for (const item of items) {
switch (item.type) {
case "hidden":
continue;
}
process(item);
}
code-block.js:4:13 lint/nursery/noBreakInNestedLoop ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This continue statement is inside a switch statement, but it skips to the next iteration of the loop instead of moving to the next case.

2 │ switch (item.type) {
3 │ case "hidden":
> 4 │ continue;
│ ^^^^^^^^^
5 │ }
6 │ process(item);

ℹ The continue statement applies to this loop.

> 1 │ for (const item of items) {
│ ^^^
2 │ switch (item.type) {
3 │ case "hidden":

ℹ Add a label to the loop and use it in the continue statement, as in continue label;, to make it clear which loop it applies to.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

itemsLoop: for (const item of items) {
switch (item.type) {
case "hidden":
continue itemsLoop;
}
process(item);
}
  • The useEarlyContinue rule suggests early continue statements, which checkContinue reports inside nested loops.