no-fallthrough
禁止 case 语句的失败
在 配置文件 中使用来自 @eslint/js 的 recommended 配置可以启用此规则
JavaScript 中的 switch 语句是该语言中更容易出错的结构之一,这在一定程度上是由于能够从一个 case “贯穿”到下一个的能力。例如:
🌐 The switch statement in JavaScript is one of the more error-prone constructs of the language thanks in part to the ability to “fall through” from one case to the next. For example:
switch(foo) {
case 1:
doSomething();
case 2:
doSomethingElse();
}
在这个例子中,如果 foo 是 1,那么执行将会流过两个 case,因为第一个会贯穿到第二个。你可以通过使用 break 来防止这种情况,如下例所示:
🌐 In this example, if foo is 1, then execution will flow through both cases, as the first falls through to the second. You can prevent this by using break, as in this example:
switch(foo) {
case 1:
doSomething();
break;
case 2:
doSomethingElse();
}
当你不想出现贯穿时,这样做很好,但如果贯穿是有意的,语言中没有方法来表示这一点。最佳实践是始终使用与 /falls?\s?through/i 正则表达式匹配但不是指令的注释来表明贯穿是有意的:
🌐 That works fine when you don’t want a fallthrough, but what if the fallthrough is intentional, there is no way to indicate that in the language. It’s considered a best practice to always indicate when a fallthrough is intentional using a comment which matches the /falls?\s?through/i regular expression but isn’t a directive:
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();
}
}
在这个例子中,对于预期的行为没有混淆。显然,第一个情况是要继续执行到第二个情况。
🌐 In this example, there is no confusion as to the expected behavior. It is clear that the first case is meant to fall through to the second case.
规则详情
🌐 Rule Details
此规则旨在消除一种情况无意间进入另一种情况的情况。因此,它会标记所有未用注释标记的穿透情景。
🌐 This rule is aimed at eliminating unintentional fallthrough of one case to the other. As such, it flags any fallthrough scenarios that are not marked by a comment.
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/*eslint no-fallthrough: "error"*/
switch(foo) {
case 1:
doSomething();
case 2:
doSomething();
}
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*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 语句不会引发警告,因为没有内容可以继续执行。
🌐 Note that the last case statement in these examples does not cause a warning because there is nothing to fall through into.
选项
🌐 Options
此规则有一个对象选项:
🌐 This rule has an object option:
- 将
commentPattern选项设置为正则表达式字符串,以更改对有意穿透注释的测试。如果穿透注释匹配某个指令,则该指令优先于commentPattern。 - 将
allowEmptyCase选项设置为true,以允许空情况,而不管布局如何。默认情况下,只有当空的case与下一个case在同一行或连续行时,该规则才不要求在空的case之后添加 fallthrough 注释。 - 将
reportUnusedFallthroughComment选项设置为true,以禁止在 case 由于不可达而无法贯穿时出现贯穿注释。这主要是为了帮助避免因重构而产生误导性注释。
commentPattern
适用于 { "commentPattern": "break[\\s\\w]*omitted" } 选项的正确代码示例:
🌐 Examples of correct code for the { "commentPattern": "break[\\s\\w]*omitted" } option:
/*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 } 选项的正确代码示例:
🌐 Examples of correct code for the { "allowEmptyCase": true } option:
/* 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 } 选项的错误代码示例:
🌐 Examples of incorrect code for the { "reportUnusedFallthroughComment": true } option:
/* eslint no-fallthrough: ["error", { "reportUnusedFallthroughComment": true }] */
switch(foo){
case 1:
doSomething();
break;
// falls through
case 2: doSomething();
}
function f() {
switch(foo){
case 1:
if (a) {
throw new Error();
} else if (b) {
break;
} else {
return;
}
// falls through
case 2:
break;
}
}
适用于 { "reportUnusedFallthroughComment": true } 选项的正确代码示例:
🌐 Examples of correct code for the { "reportUnusedFallthroughComment": true } option:
/* eslint no-fallthrough: ["error", { "reportUnusedFallthroughComment": true }] */
switch(foo){
case 1:
doSomething();
break;
// just a comment
case 2: doSomething();
}
何时不使用
🌐 When Not To Use It
如果你不想强制每个 case 语句都以 throw、return、break 或注释结尾,那么你可以安全地关闭此规则。
🌐 If you don’t want to enforce that each case statement should end with a throw, return, break, or comment, then you can safely turn this rule off.
相关规则
版本
此规则是在 ESLint v0.0.7 中引入。