Disallow unreachable code

Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

Metadata

ID: javascript-best-practices/no-unreachable

Language: JavaScript

Severity: Warning

Category: Best Practices

Description

In JavaScript, any code following a return, throw, continue, or break statement is unreachable and will never be executed. The only exception here is declarations, which are valid because of JavaScript Hoisting.

Non-Compliant Code Examples

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

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

    constructor() {
        return {};
    }
}

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

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

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

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

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

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

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

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

    constructor() {
        return {};
    }
}

Compliant Code Examples

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

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

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

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 {};
    }
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: may/unit-testing