no-fallthrough
注意: 此规则是
recommended
规则集的一部分。在
deno.json
中启用完整规则集{ "lint": { "tags": ["recommended"] } }
使用 Deno CLI 启用完整规则集
deno lint --tags=recommended
禁止 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`