Versions

no-case-declarations

Disallow lexical declarations in case clauses

Recommended

The "extends": "eslint:recommended" property in a configuration file enables this rule

这条规则不允许在 case/default 子句中进行 lexical 声明(letconstfunctionclass)。因为 lexical 声明在整个开关块中是可见的。但只有在被分配时才会被初始化,而这只有在达到它被定义的情况下才会发生。

为了确保词法声明只适用于当前的 case 子句,应将你的子句包裹在块中。

规则细节

这条规则的目的是防止访问未初始化的词性绑定,以及访问跨例句的悬挂函数。

使用此规则的错误示例:

Open in Playground
/*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 {}
}

使用此规则的正确示例:

Open in Playground
/*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 {}
    }
}

何时不用

如果你依赖跌落行为,并希望访问案例块中引入的绑定。

Version

This rule was introduced in ESLint v1.9.0.

Resources

更改语言