Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nifgraup opened this issue Oct 10, 2018 · 15 comments · Fixed by standard/eslint-config-standard#137
Labels
Milestone

Comments

@nifgraup
Copy link

nifgraup commented Oct 10, 2018

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.

@LinusU
Copy link
Member

LinusU commented Oct 10, 2018

I think we should enable no-case-declarations, it seems like a nice rule, but are you sure that that would fix your problem?

It seems like no-case-declarations would not complain when you wrap the case body in { } (which creates a new lexical scope) 🤔

@nifgraup
Copy link
Author

nifgraup commented Oct 10, 2018

It will fix it because no-undef will start complaining about case 2 once case 1 is wrapped in {}.

@LinusU
Copy link
Member

LinusU commented Oct 10, 2018

I don't understand how enabling no-case-declaration could affect the behavior of no-undef 🤔 are you sure about that?

@LinusU LinusU closed this as completed Oct 10, 2018
@nifgraup
Copy link
Author

Wrapping the declaration, let x = 1, in {} is what makes no-undef think x is not defined in x = 2.

@nifgraup
Copy link
Author

I see now that the example case statements I wrote were wrong, they were not supposed to have {} in them. I fixed the description.

@nifgraup nifgraup changed the title enable rule no-case-declarations enable rule no-case-declarations to prevent usage of undeclared variables Oct 10, 2018
@brodybits
Copy link
Contributor

brodybits commented Oct 10, 2018 via email

@LinusU
Copy link
Member

LinusU commented Oct 11, 2018

Actually, I did not mean to close this, sorry about that, must have hit the wrong button

@stale
Copy link

stale bot commented Jan 9, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale label Jan 9, 2019
@brodybits
Copy link
Contributor

Can we pin this one to avoid auto-closing?

@stale stale bot removed the stale label Jan 9, 2019
@stale
Copy link

stale bot commented Apr 9, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale label Apr 9, 2019
@brodybits
Copy link
Contributor

please?

@stale stale bot removed the stale label Apr 9, 2019
@stale
Copy link

stale bot commented Jul 9, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale label Jul 9, 2019
@brodybits
Copy link
Contributor

Bump

@stale stale bot removed the stale label Jul 9, 2019
@feross
Copy link
Member

feross commented Jul 10, 2019

@brodybits Sorry this wasn't tagged to prevent closing.

I personally like this rule. I prefer to enclose case statements with braces to create a new lexical scope. It comes in handy when you want to reuse a variable name in multiple cases, like this:

switch (x) {
  case 1: {
    const y = x.y
    console.log(y)
    break
  }
  case 2: {
    const y = x.y // normally would be an error for redeclaring y. the braces make a new lexical scope
    console.log(y * 2)
  }
}

@feross
Copy link
Member

feross commented Aug 13, 2019

This rule will ship in standard v14. Thanks everyone for the feedback :)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants