Index

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 {};
    }
}

选项

🌐 Options

此规则没有选项。

🌐 This rule has no options.

由 TypeScript 处理

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

TypeScript must be configured with allowUnreachableCode: false for it to consider unreachable code an error.

版本

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

资源