Skip to content

useWhileLoopCondition (JavaScript)

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

Require the condition that ends a loop to be written in the while statement.

A loop such as while (true) or for (;;) that starts its body with if (condition) { break; } splits its stopping condition across two places. Writing the condition in the while statement shows when the loop stops without having to read its body.

The rule reports while (true), do ... while (true), for (;;), and for (; true;) loops whose body starts with an if statement that contains only a break. The rule ignores loops that have a label, and loops that can also be stopped by another break in their body.

The fix isn’t offered when comments in the loop header or in the if statement would be lost.

while (true) {
if (!hasMore()) {
break;
}
processNext();
}
code-block.js:2:5 lint/nursery/useWhileLoopCondition  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ The condition that ends this loop is checked by an if statement at the start of its body.

1 │ while (true) {
> 2 │ if (!hasMore()) {
│ ^^^^^^^^^^^^^^^^^
> 3 │ break;
> 4 │ }
│ ^
5 │ processNext();
6 │ }

ℹ Writing the condition in the while statement shows when the loop stops without reading its body.

ℹ 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.

ℹ Safe fix: Move the condition into the while statement.

1 │ - while·(true)·{
2 │ - ····if·(!hasMore())·{
3 │ - ········break;
4 │ - ····}
1 │ + while·(hasMore())·{
5 2 │ processNext();
6 3 │ }

for (;;) {
if (done) break;
processNext();
}
code-block.js:2:5 lint/nursery/useWhileLoopCondition  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ The condition that ends this loop is checked by an if statement at the start of its body.

1 │ for (;;) {
> 2 │ if (done) break;
│ ^^^^^^^^^^^^^^^^
3 │ processNext();
4 │ }

ℹ Writing the condition in the while statement shows when the loop stops without reading its body.

ℹ 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.

ℹ Safe fix: Move the condition into the while statement.

1 │ - for·(;;)·{
2 │ - ····if·(done)·break;
1 │ + while·(!done)·{
3 2 │ processNext();
4 3 │ }

do {
if (!hasMore()) {
break;
}
processNext();
} while (true);
code-block.js:2:5 lint/nursery/useWhileLoopCondition  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ The condition that ends this loop is checked by an if statement at the start of its body.

1 │ do {
> 2 │ if (!hasMore()) {
│ ^^^^^^^^^^^^^^^^^
> 3 │ break;
> 4 │ }
│ ^
5 │ processNext();
6 │ } while (true);

ℹ Writing the condition in the while statement shows when the loop stops without reading its body.

ℹ 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.

ℹ Safe fix: Move the condition into the while statement.

1 │ - do·{
2 │ - ····if·(!hasMore())·{
3 │ - ········break;
4 │ - ····}
1 │ + while·(hasMore())·{
5 2 │ processNext();
6 │ - }·while·(true);
3 │ + }
7 4 │

while (hasMore()) {
processNext();
}
while (!done) {
processNext();
}
while (true) {
prepare();
if (!hasMore()) {
break;
}
}
while (true) {
if (!hasMore()) {
break;
}
if (cancelled) {
break;
}
}