no-unreachable
NOTE: this rule is part of the
推荐
rule set.Enable full set in
deno.json
:{ "lint": { "rules": { "tags": ["推荐"] } } }
Enable full set using the Deno CLI:
deno lint --rules-tags=推荐
This rule can be explictly included to or excluded from the rules present in the current tag by adding it to the
include
or exclude
array in deno.json
:{ "lint": { "rules": { "include": ["no-unreachable"], "exclude": ["no-unreachable"] } } }
禁止在控制流语句之后写无法到达的代码。
因为控制流语句(return
、throw
、break
和 continue
)
会无条件地退出代码块,所以它们后面的任何语句都无法执行。
无效示例:
function foo() {
return true;
console.log("完成");
}
function bar() {
throw new Error("哎呀!");
console.log("完成");
}
while (value) {
break;
console.log("完成");
}
throw new Error("哎呀!");
console.log("完成");
function baz() {
if (Math.random() < 0.5) {
return;
} else {
throw new Error();
}
console.log("完成");
}
for (;;) {}
console.log("完成");
有效示例:
function foo() {
return bar();
function bar() {
return 1;
}
}