noNestedSwitch (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/noNestedSwitch - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
sonarjs/no-nested-switch
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noNestedSwitch": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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; }}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.