Skip to content

noNestedSwitch (JavaScript)

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

Disallow switch statements inside other switch statements.

A switch statement inside another switch statement is hard to read, because it is easy to mix up which case belongs to which switch. Move the inner switch into a separate function, written outside the outer switch, instead.

A switch statement inside a function is still reported when that function is written inside another switch statement.

function foo(n, m) {
switch (n) {
case 0:
switch (m) {
case 1:
break;
}
break;
default:
break;
}
}
code-block.js:4:13 lint/nursery/noNestedSwitch ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This switch statement is inside another switch statement.

2 │ switch (n) {
3 │ case 0:
> 4 │ switch (m) {
│ ^^^^^^
5 │ case 1:
6 │ break;

ℹ The outer switch statement starts here.

1 │ function foo(n, m) {
> 2 │ switch (n) {
│ ^^^^^^
3 │ case 0:
4 │ switch (m) {

ℹ Nested switch statements are hard to read, because it is easy to mix up which case belongs to which switch.

ℹ Move the inner switch statement into a separate function written outside the outer switch statement.

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

switch (n) {
case 0: {
const handle = (m) => {
switch (m) {
case 1:
break;
}
};
handle(n);
break;
}
}
code-block.js:4:13 lint/nursery/noNestedSwitch ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This switch statement is inside another switch statement.

2 │ case 0: {
3 │ const handle = (m) => {
> 4 │ switch (m) {
│ ^^^^^^
5 │ case 1:
6 │ break;

ℹ The outer switch statement starts here.

> 1 │ switch (n) {
│ ^^^^^^
2 │ case 0: {
3 │ const handle = (m) => {

ℹ Nested switch statements are hard to read, because it is easy to mix up which case belongs to which switch.

ℹ Move the inner switch statement into a separate function written outside the outer switch statement.

ℹ 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(n, m) {
switch (n) {
case 0:
bar(m);
break;
default:
break;
}
}
function bar(m) {
switch (m) {
case 1:
break;
}
}