no-unreachable

禁止在 returnthrowcontinuebreak 语句后出现无法访问的代码

Recommended

配置文件 中使用来自 @eslint/jsrecommended 配置可以启用此规则

因为 returnthrowcontinuebreak 语句无条件退出代码块,所以后面的任何语句都无法执行。无法访问的语句通常是一个错误。

¥Because the return, throw, continue, and break statements unconditionally exit a block of code, any statements after them cannot be executed. Unreachable statements are usually a mistake.

function fn() {
    x = 1;
    return x;
    x = 3; // this will never execute
}

另一种错误是在其构造函数没有调用 super() 的子类中定义实例字段。子类的实例字段只添加到 super() 之后的实例中。如果没有 super() 调用,则它们的定义永远不会应用,因此是无法访问的代码。

¥Another kind of mistake is defining instance fields in a subclass whose constructor doesn’t call super(). Instance fields of a subclass are only added to the instance after super(). If there are no super() calls, their definitions are never applied and therefore are unreachable code.

class C extends B {
    #x; // this will never be added to instances

    constructor() {
        return {};
    }
}

规则详情

¥Rule Details

此规则不允许在 returnthrowcontinuebreak 语句之后出现无法访问的代码。此规则还标记其构造函​​数没有 super() 调用的子类中的实例字段定义。

¥This rule disallows unreachable code after return, throw, continue, and break statements. This rule also flags definitions of instance fields in subclasses whose constructors don’t have super() calls.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-unreachable: "error"*/

function foo() {
    return true;
    console.log("done");
}

function bar() {
    throw new Error("Oops!");
    console.log("done");
}

while(value) {
    break;
    console.log("done");
}

throw new Error("Oops!");
console.log("done");

function baz() {
    if (Math.random() < 0.5) {
        return;
    } else {
        throw new Error();
    }
    console.log("done");
}

for (;;) {}
console.log("done");

由于 JavaScript 函数和变量提升,此规则的正确代码示例:

¥Examples of correct code for this rule, because of JavaScript function and variable hoisting:

在线运行
/*eslint no-unreachable: "error"*/

function foo() {
    return bar();
    function bar() {
        return 1;
    }
}

function bar() {
    return x;
    var x;
}

switch (foo) {
    case 1:
        break;
        var x;
}

此规则的其他错误代码示例:

¥Examples of additional incorrect code for this rule:

在线运行
/*eslint no-unreachable: "error"*/

class C extends B {
    #x; // unreachable
    #y = 1; // unreachable
    a; // unreachable
    b = 1; // unreachable

    constructor() {
        return {};
    }
}

此规则的附加正确代码示例:

¥Examples of additional correct code for this rule:

在线运行
/*eslint no-unreachable: "error"*/

class D extends B {
    #x;
    #y = 1;
    a;
    b = 1;

    constructor() {
        super();
    }
}

class E extends B {
    #x;
    #y = 1;
    a;
    b = 1;

    // implicit constructor always calls `super()`
}

class F extends B {
    static #x;
    static #y = 1;
    static a;
    static b = 1;

    constructor() {
        return {};
    }
}

由 TypeScript 处理

使用 TypeScript 时禁用此规则是安全的,因为 TypeScript 的编译器强制执行此检查。

TypeScript 必须配置为 allowUnreachableCode: false,才能将无法访问的代码视为错误。

版本

此规则是在 ESLint v0.0.6 中引入。

资源

ESLint 中文网
粤ICP备13048890号