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

Comments

@nifgraup
Copy link

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

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Author

commented Oct 10, 2018

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

@LinusU

This comment has been minimized.

Copy link
Member

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 Oct 10, 2018

@nifgraup

This comment has been minimized.

Copy link
Author

commented Oct 10, 2018

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

@nifgraup

This comment has been minimized.

Copy link
Author

commented Oct 10, 2018

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

This comment has been minimized.

Copy link
Contributor

commented Oct 10, 2018

@LinusU

This comment has been minimized.

Copy link
Member

commented Oct 11, 2018

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

@LinusU LinusU reopened this Oct 11, 2018

@justinkalland justinkalland referenced this issue Dec 20, 2018
1 of 1 task complete
@stale

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Contributor

commented Jan 9, 2019

Can we pin this one to avoid auto-closing?

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

@stale

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Contributor

commented Apr 9, 2019

please?

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

@stale

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Contributor

commented Jul 9, 2019

Bump

@stale stale bot removed the stale label Jul 9, 2019

@feross feross added the accepted label Jul 10, 2019

@feross

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Member

commented Aug 13, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
4 participants
You can’t perform that action at this time.