禁止 case 穿透
注意:此规则是
recommended规则集的一部分。在
deno.json 中启用完整集合{
"lint": {
"rules": {
"tags": ["recommended"]
}
}
}使用 Deno CLI 启用完整集合
deno lint --rules-tags=recommended
通过将其添加到
deno.json文件中的include或exclude数组,可以将此规则显式地包含或排除在当前标签中存在的规则之外。{
"lint": {
"rules": {
"include": ["no-fallthrough"],
"exclude": ["no-fallthrough"]
}
}
}禁止 case 语句的隐式穿透。
没有break的 case 语句将执行其主体,然后穿透到下一个 case 或 default 块并执行该块。虽然这有时是故意的,但很多时候开发人员忘记添加 break 语句,只打算执行单个 case 语句。此规则强制要求您要么以 break 语句结束每个 case 语句,要么使用显式注释表明穿透是故意的。穿透注释必须包含fallthrough、falls through或fall through中的一个。
无效示例
switch (myVar) {
case 1:
console.log("1");
case 2:
console.log("2");
}
// If myVar = 1, outputs both `1` and `2`. Was this intentional?
有效示例
switch (myVar) {
case 1:
console.log("1");
break;
case 2:
console.log("2");
break;
}
// If myVar = 1, outputs only `1`
switch (myVar) {
case 1:
console.log("1");
/* falls through */
case 2:
console.log("2");
}
// If myVar = 1, intentionally outputs both `1` and `2`