Skip to content

useDefaultSwitchClauseLast

诊断类别:lint/suspicious/useDefaultSwitchClauseLast

¥Diagnostic Category: lint/suspicious/useDefaultSwitchClauseLast

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

强制原始类型的一致名称

¥Enforce default clauses in switch statements to be last

switch 语句可以选择具有默认子句。

¥A switch statement can optionally have a default clause.

如果存在,它通常是最后一个子句,但不一定如此。它还可以将默认子句放在所有 case 子句之前或两者之间的任何位置。行为与最后一条子句大致相同。

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

仅当 case 子句(包括 default 之后定义的子句)中没有匹配项时,才会执行 default 块,但也可以“从 default 子句”“落入”列表中的下一个子句。但是,这种流程并不常见,会让读者感到困惑。

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

即使没有 “失败” 逻辑,在 case 子句之前或之间看到默认子句仍然是意料之外的。按照惯例,它应该是最后一个子句。

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

¥Examples

¥Invalid

switch (foo) {
default:
break;
case 0:
break;
}
code-block.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;
}
code-block.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;
}
code-block.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.

¥Valid

switch (foo) {
case 0:
break;
case 1:
default:
break;
}
switch (foo) {
case 0:
break;
}

¥Related links