Skip to content

enable rule no-case-declarations to prevent usage of undeclared variables #1211

@nifgraup

Description

@nifgraup

What version of standard?
12.0.1

What operating system, Node.js, and npm version?
N/A

What did you expect to happen?
This code should be considered an error:

switch (foo) {
  case 1:
    let x = 1
    // some code using x
    break
  case 2:
    x = 2 // ReferenceError on runtime but no lint error
    // some code using x
    break
}

What actually happened?
Code was ported to ES6 & Standard at the same time. Originally it looked like this (simplified):

var x
switch (foo) {
  case 1:
    x = 1
    // some code using x
    break
  case 2:
    x = 2
    // some code using x
    break
}

During refactoring, var x was removed which triggers two no-undef errors. First error is solved with let x = 1 but that makes the second error not to report although x is not declared in case 2.

Adding rule https://eslint.org/docs/rules/no-case-declarations prevents this situation from happening.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions