Skip to content

useDefaultSwitchClauseLast (since v1.0.0)

Diagnostic Category: lint/suspicious/useDefaultSwitchClauseLast

Sources:

Enforce default clauses in switch statements to be last

A switch statement can optionally have a default clause.

If present, it’s usually the last clause, but it doesn’t need to be. It is also allowed to put the default clause before all case clauses, or anywhere between. The behavior is mostly the same as if it was the last clause.

The default block will be still executed only if there is no match in the case clauses (including those defined after the default), but there is also the ability to “fall through” from the default clause to the following clause in the list. However, such flow is not common and it would be confusing to the readers.

Even if there is no “fall through” logic, it’s still unexpected to see the default clause before or between the case clauses. By convention, it is expected to be the last clause.

switch (foo) {
default:
break;
case 0:
break;
}
suspicious/useDefaultSwitchClauseLast.js:2:5 lint/suspicious/useDefaultSwitchClauseLast ━━━━━━━━━━━━

   The default clause should be the last switch clause.
  
    1 │ switch (foo) {
  > 2 │     default:
       ^^^^^^^^
  > 3 │         break;
           ^^^^^^
    4 │     case 0:
    5 │         break;
  
   The following case clause is here:
  
    2 │     default:
    3 │         break;
  > 4 │     case 0:
       ^^^^^^^
  > 5 │         break;
           ^^^^^^
    6 │ }
    7 │ 
  
   Regardless its position, the default clause is always executed when there is no match. To avoid confusion, the default clause should be the last switch clause.
  
switch (foo) {
default:
f();
case 0:
break;
}
suspicious/useDefaultSwitchClauseLast.js:2:5 lint/suspicious/useDefaultSwitchClauseLast ━━━━━━━━━━━━

   The default clause should be the last switch clause.
  
    1 │ switch (foo) {
  > 2 │     default:
       ^^^^^^^^
  > 3 │         f();
           ^^^^
    4 │     case 0:
    5 │         break;
  
   The following case clause is here:
  
    2 │     default:
    3 │         f();
  > 4 │     case 0:
       ^^^^^^^
  > 5 │         break;
           ^^^^^^
    6 │ }
    7 │ 
  
   Regardless its position, the default clause is always executed when there is no match. To avoid confusion, the default clause should be the last switch clause.
  
switch (foo) {
case 0:
break;
default:
case 1:
break;
}
suspicious/useDefaultSwitchClauseLast.js:4:5 lint/suspicious/useDefaultSwitchClauseLast ━━━━━━━━━━━━

   The default clause should be the last switch clause.
  
    2 │     case 0:
    3 │         break;
  > 4 │     default:
       ^^^^^^^^
    5 │     case 1:
    6 │         break;
  
   The following case clause is here:
  
    3 │         break;
    4 │     default:
  > 5 │     case 1:
       ^^^^^^^
  > 6 │         break;
           ^^^^^^
    7 │ }
    8 │ 
  
   Regardless its position, the default clause is always executed when there is no match. To avoid confusion, the default clause should be the last switch clause.
  
switch (foo) {
case 0:
break;
case 1:
default:
break;
}
switch (foo) {
case 0:
break;
}