Skip to content

eslint/no-case-declarations Pedantic ​

What it does ​

Disallow lexical declarations in case clauses.

Why is this bad? ​

The reason is that the lexical declaration is visible in the entire switch block but it only gets initialized when it is assigned, which will only happen if the case where it is defined is reached.

Example ​

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

References ​

Released under the MIT License.