规则详情

该规则旨在防止访问未初始化的词法绑定以及跨案例子句访问提升的函数。

This rule aims to prevent access to uninitialized lexical bindings as well as accessing hoisted functions across case clauses.

2k0BPo/0b79qZaJVj2D2z24takoo4vACmRt/zf0ULpF9g+41KfjcbEP3uBkPK/VtmtEsKquRi9wRuUad+kiqlxcJsNlLBAdlk0u/WzP+5tHWu3EaNalSxqb2NxwodXqSzt0nCqZKmtPN/dT9OsijRhw7O5fkdKz5hCVOH86AhsM=

/*eslint no-case-declarations: "error"*/
/*eslint-env es6*/

switch (foo) {
    case 1:
        let x = 1;
        break;
    case 2:
        const y = 2;
        break;
    case 3:
        function f() {}
        break;
    default:
        class C {}
}

QmuA/Biwj8uhm1HNHY6ojd1f/fHxoqUyE/DMwpSSw/l2D8VlfgoN0I64ky9k6fJ77D/YsYXrY0qyOYRWJorvxbjPUm/RLPHBV75ZqXEW4ZZ/Y2Xc/jVts/uGMWTAhwzlmzwkWNs4hqomBP5ZyPHhMQ==

/*eslint no-case-declarations: "error"*/
/*eslint-env es6*/

// Declarations outside switch-statements are valid
const a = 0;

switch (foo) {
    // The following case clauses are wrapped into blocks using brackets
    case 1: {
        let x = 1;
        break;
    }
    case 2: {
        const y = 2;
        break;
    }
    case 3: {
        function f() {}
        break;
    }
    case 4:
        // Declarations using var without brackets are valid due to function-scope hoisting
        var z = 4;
        break;
    default: {
        class C {}
    }
}