noExcessiveNestedBlocks (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/noExcessiveNestedBlocks - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
- Same as
max-depth
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noExcessiveNestedBlocks": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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) { } } }}Options
Section titled “Options”Default: 4
The maximum block nesting depth allowed.
{ "linter": { "rules": { "nursery": { "noExcessiveNestedBlocks": { "level": "on", "options": { "max": 2 } } } } }}Invalid
Section titled “Invalid”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) {}}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.