Skip to content

noExcessiveNestedBlocks (JavaScript)

biome.json
{
"linter": {
"rules": {
"nursery": {
"noExcessiveNestedBlocks": "error"
}
}
}
}

Enforce a maximum depth that blocks can be nested.

Deeply nested code is difficult to read and follow, because every level adds another condition the reader has to keep in mind. This rule reports if, switch, try, and loop statements nested beyond the configured limit.

An else if doesn’t add a level of nesting. Each function, method, and static {} block in a class starts counting from zero again.

Only the outermost statement exceeding the limit is reported: statements nested inside it aren’t reported again.

function foo() {
for (const item of items) {
if (item.enabled) {
while (item.next()) {
try {
if (item.done) {
break;
}
} catch {}
}
}
}
}
code-block.js:6:21 lint/nursery/noExcessiveNestedBlocks ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This block is nested too deeply.

4 │ while (item.next()) {
5 │ try {
> 6 │ if (item.done) {
│ ^^
7 │ break;
8 │ }

ℹ Blocks nested 5 levels deep are hard to read and maintain. The configured maximum is 4.

ℹ Return early or extract the nested code into a separate function to reduce nesting.

ℹ 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 foo() {
for (const item of items) {
if (!item.enabled) {
continue;
}
processItem(item);
}
}
function processItem(item) {
while (item.next()) {
try {
if (item.done) {
break;
}
} catch {}
}
}
if (a) {
if (b) {
if (c) {
if (d) {
} else if (e) {
} else if (f) {
}
}
}
}

Default: 4

The maximum block nesting depth allowed.

biome.json
{
"linter": {
"rules": {
"nursery": {
"noExcessiveNestedBlocks": {
"level": "on",
"options": {
"max": 2
}
}
}
}
}
}
if (a) {
if (b) {
if (c) {}
}
}
code-block.js:3:9 lint/nursery/noExcessiveNestedBlocks ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This block is nested too deeply.

1 │ if (a) {
2 │ if (b) {
> 3 │ if (c) {}
│ ^^
4 │ }
5 │ }

ℹ Blocks nested 3 levels deep are hard to read and maintain. The configured maximum is 2.

ℹ Return early or extract the nested code into a separate function to reduce nesting.

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

if (a) {
if (b) {}
}