no-fallthrough
禁止 case
语句的意外穿透
在 配置文件中使用来自 @eslint/js
的 recommended
配置启用此规则
JavaScript 中的 switch
语句是该语言中更易出错的结构之一,部分原因是能够从一个 case
“穿透”到下一个 case
。例如
switch(foo) {
case 1:
doSomething();
case 2:
doSomethingElse();
}
在这个例子中,如果 foo
是 1
,那么执行将穿透两个 case,因为第一个 case 穿透到第二个 case。您可以使用 break
来防止这种情况,如下例所示
switch(foo) {
case 1:
doSomething();
break;
case 2:
doSomethingElse();
}
当您不想要穿透时,这很好用,但是如果穿透是故意的,则无法在语言中指示这一点。最佳实践是始终使用与 /falls?\s?through/i
正则表达式匹配但不属于指令的注释来指示穿透是故意的
switch(foo) {
case 1:
doSomething();
// falls through
case 2:
doSomethingElse();
}
switch(foo) {
case 1:
doSomething();
// fall through
case 2:
doSomethingElse();
}
switch(foo) {
case 1:
doSomething();
// fallsthrough
case 2:
doSomethingElse();
}
switch(foo) {
case 1: {
doSomething();
// falls through
}
case 2: {
doSomethingElse();
}
}
在这个例子中,对于预期的行为没有混淆。很明显,第一个 case 旨在穿透到第二个 case。
规则详情
此规则旨在消除一个 case 到另一个 case 的意外穿透。因此,它标记任何未用注释标记的穿透场景。
此规则的错误代码示例
/*eslint no-fallthrough: "error"*/
switch(foo) {
case 1:
doSomething();
}
此规则的正确代码示例
/*eslint no-fallthrough: "error"*/
switch(foo) {
case 1:
doSomething();
break;
case 2:
doSomething();
}
function bar(foo) {
switch(foo) {
case 1:
doSomething();
return;
case 2:
doSomething();
}
}
switch(foo) {
case 1:
doSomething();
throw new Error("Boo!");
case 2:
doSomething();
}
switch(foo) {
case 1:
case 2:
doSomething();
}
switch(foo) {
case 1: case 2:
doSomething();
}
switch(foo) {
case 1:
doSomething();
// falls through
case 2:
doSomething();
}
switch(foo) {
case 1: {
doSomething();
// falls through
}
case 2: {
doSomethingElse();
}
}
请注意,这些示例中的最后一个 case
语句不会导致警告,因为没有要穿透到的内容。
选项
此规则有一个对象选项
-
将
commentPattern
选项设置为正则表达式字符串,以更改对故意穿透注释的测试。如果穿透注释与指令匹配,则优先于commentPattern
。 -
将
allowEmptyCase
选项设置为true
以允许空 case,而不管布局如何。默认情况下,仅当空case
和下一个case
在同一行或连续行上时,此规则才不需要在空case
之后添加穿透注释。 -
将
reportUnusedFallthroughComment
选项设置为true
以禁止在 case 由于不可达而无法穿透时存在穿透注释。这主要旨在帮助避免由于重构而导致误导性注释。
commentPattern
{ "commentPattern": "break[\\s\\w]*omitted" }
选项的正确代码示例
/*eslint no-fallthrough: ["error", { "commentPattern": "break[\\s\\w]*omitted" }]*/
switch(foo) {
case 1:
doSomething();
// break omitted
case 2:
doSomething();
}
switch(foo) {
case 1:
doSomething();
// caution: break is omitted intentionally
default:
doSomething();
}
allowEmptyCase
{ "allowEmptyCase": true }
选项的正确代码示例
/* eslint no-fallthrough: ["error", { "allowEmptyCase": true }] */
switch(foo){
case 1:
case 2: doSomething();
}
switch(foo){
case 1:
/*
Put a message here
*/
case 2: doSomething();
}
reportUnusedFallthroughComment
{ "reportUnusedFallthroughComment": true }
选项的错误代码示例
/* eslint no-fallthrough: ["error", { "reportUnusedFallthroughComment": true }] */
switch(foo){
case 1:
doSomething();
break;
case 2: doSomething();
}
function f() {
switch(foo){
case 1:
if (a) {
throw new Error();
} else if (b) {
break;
} else {
return;
}
case 2:
break;
}
}
{ "reportUnusedFallthroughComment": true }
选项的正确代码示例
/* eslint no-fallthrough: ["error", { "reportUnusedFallthroughComment": true }] */
switch(foo){
case 1:
doSomething();
break;
// just a comment
case 2: doSomething();
}
何时不使用
如果您不想强制每个 case
语句都以 throw
、return
、break
或注释结尾,那么您可以安全地关闭此规则。
相关规则
版本
此规则在 ESLint v0.0.7 中引入。