Skip to content

useEarlyContinue (JavaScript)

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

Require an early continue instead of an if statement that wraps the rest of a loop body.

When a loop body ends with an if statement that holds most of the work, the main code of the loop sits one level deeper than it needs to. Skipping the iterations you don’t care about first, with if (...) { continue; }, keeps that code at the top level of the loop and makes it easier to follow.

The rule reports an if statement without an else branch when it’s the last statement of a loop body wrapped in braces, and it contains more statements than the maximumStatements option allows. Other statements can come before the if statement. The rule doesn’t report an if statement whose last statement already leaves the iteration with return, break, continue, or throw, because an early continue wouldn’t remove any nesting.

The fix reverses the condition, adds a continue statement, and moves the content of the if statement after it. The fix isn’t offered when:

  • the if statement contains a function, a class, a using declaration, or a TypeScript declaration such as a type or an interface;
  • the if statement contains a let or const declaration, and either other statements come before the if statement in the loop body, or the condition uses a variable with the same name;
  • comments are attached to the braces of the if statement.
for (const item of items) {
if (item.isActive) {
process(item);
save(item);
}
}
code-block.js:2:5 lint/nursery/useEarlyContinue  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This if statement wraps the rest of the loop body.

1 │ for (const item of items) {
> 2 │ if (item.isActive) {
│ ^^^^^^^^^^^^^^^^^^
3 │ process(item);
4 │ save(item);

ℹ The main code of the loop is nested one level deeper than it needs to be, which makes it harder to follow.

ℹ Skip the iteration early with continue when the condition isn't met, then write the rest of the code after it.

ℹ 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: Use an early continue instead.

1 1 │ for (const item of items) {
2 │ - ····if·(item.isActive)·{
3 │ - ········process(item);
2 │ + ····if·(!item.isActive)·{
3 │ + ········continue;
4 │ + ····}
5 │ + ········process(item);
4 6 │ save(item);
5 │ - ····}
6 7 │ }
7 8 │

while (queue.length > 0) {
const item = queue.shift();
if (item.isActive) {
process(item);
save(item);
}
}
code-block.js:3:5 lint/nursery/useEarlyContinue  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This if statement wraps the rest of the loop body.

1 │ while (queue.length > 0) {
2 │ const item = queue.shift();
> 3 │ if (item.isActive) {
│ ^^^^^^^^^^^^^^^^^^
4 │ process(item);
5 │ save(item);

ℹ The main code of the loop is nested one level deeper than it needs to be, which makes it harder to follow.

ℹ Skip the iteration early with continue when the condition isn't met, then write the rest of the code after it.

ℹ 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: Use an early continue instead.

1 1 │ while (queue.length > 0) {
2 2 │ const item = queue.shift();
3 │ - ····if·(item.isActive)·{
4 │ - ········process(item);
3 │ + ····if·(!item.isActive)·{
4 │ + ········continue;
5 │ + ····}
6 │ + ········process(item);
5 7 │ save(item);
6 │ - ····}
7 8 │ }
8 9 │

for (const item of items) {
if (!item.isActive) {
continue;
}
process(item);
save(item);
}
for (const item of items) {
if (item.isActive) {
process(item);
}
}
for (const item of items) {
if (item.isActive) {
process(item);
save(item);
}
finish(item);
}
for (const item of items) {
if (item.isActive) {
process(item);
save(item);
} else {
skip(item);
}
}
function findActive(items) {
for (const item of items) {
if (item.isActive) {
process(item);
return item;
}
}
}

Type: number

Default: 1

The largest number of statements that an if statement at the end of a loop body can contain before the rule reports it. Empty statements (a lone ;) aren’t counted. Set it to 0 to report any if statement at the end of a loop body that contains at least one statement.

biome.json
{
"linter": {
"rules": {
"nursery": {
"useEarlyContinue": {
"level": "on",
"options": {
"maximumStatements": 0
}
}
}
}
}
}
for (const item of items) {
if (item.isActive) {
process(item);
}
}
code-block.js:2:5 lint/nursery/useEarlyContinue  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This if statement wraps the rest of the loop body.

1 │ for (const item of items) {
> 2 │ if (item.isActive) {
│ ^^^^^^^^^^^^^^^^^^
3 │ process(item);
4 │ }

ℹ The main code of the loop is nested one level deeper than it needs to be, which makes it harder to follow.

ℹ Skip the iteration early with continue when the condition isn't met, then write the rest of the code after it.

ℹ 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: Use an early continue instead.

1 1 │ for (const item of items) {
2 │ - ····if·(item.isActive)·{
3 │ - ········process(item);
4 │ - ····}
2 │ + ····if·(!item.isActive)·{
3 │ + ········continue;
4 │ + ····}
5 │ + ········process(item);
5 6 │ }
6 7 │

for (const item of items) {
if (!item.isActive) {
continue;
}
process(item);
}
  • The noContinue rule enforces the opposite style. Don’t enable both rules.