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

Fix: remove autofix for var undef inits (fixes #9231) #9288

Merged

Conversation

VictorHom
Copy link
Member

What is the purpose of this pull request? (put an "X" next to item)

[X ] Other, please explain: Remove autofixing to no-undef-init rule, particularly for var declarations

What changes did you make? (Give an overview)

Is there anything you'd like reviewers to focus on?

  1. To the point in no-undef-init incorrectly autofixes some var declarations #9231 about not using let: Using let looks like it still works, so I want to make sure I understand.
function fun() {
  for (var p in [0, 1, 2]) {
    let selected = undefined;// auto fix will remove `= undefined`

    for (var i in [0, 1, 2]) {
      if (!selected) {
        console.log('selected');
      }
      selected = i;
    }
  }
}

fun();
  1. Is it okay to keep as separate if? I didn't include in the same if as line 50 since the inner comment wouldn't make sense.
  2. I am checking node.parent.kind because it looks to distinguish between var and let

@eslintbot
Copy link

LGTM

@mention-bot
Copy link

@VictorHom, thanks for your PR! By analyzing the history of the files in this pull request, we identified @not-an-aardvark, @ilyavolodin and @vitorbal to be potential reviewers.

@gyandeeps
Copy link
Member

we need to avoid var fix only if its inside a loop. I think currently you are completely removing fix for var.

@not-an-aardvark
Copy link
Member

@gyandeeps While that might be a better fix, note that completely removing the autofix for var was the solution that I proposed in #9231 (comment).

@not-an-aardvark
Copy link
Member

Also, I don't think checking if the variable is in a loop fully solves the issue:

var foo = 5;
var foo = undefined;
foo; // => undefined
var foo = 5;
var foo;
foo; // => 5

@gyandeeps
Copy link
Member

you are right @not-an-aardvark ... forgot about that weird case of var... so current plan sounds good...

Copy link
Member

@not-an-aardvark not-an-aardvark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! This looks good, I just have a small suggestion.

errors: [{ message: "It's not necessary to initialize 'a' to undefined.", type: "VariableDeclarator" }]
},
{
code: "var a = 1, b = undefined, c = 5;",
output: "var a = 1, b, c = 5;",
output: null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add similar tests to the old versions, but with let? Previously, none of the tests used let because the rule worked the same way with var as it did with let. Now that there is a behavior change between the two types, we should make sure the normal autofixing behavior still works correctly with let. (For example, we should test that let a; gets fixed to let a, b = 1 gets autofixed to let a = undefined, b = 1, and we should test that let [a] = undefined still doesn't get autofixed.)

@not-an-aardvark not-an-aardvark added accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly rule Relates to ESLint's core rules labels Sep 12, 2017
@eslintbot
Copy link

LGTM

@eslintbot
Copy link

LGTM

Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for contributing!

@not-an-aardvark not-an-aardvark merged commit e220687 into eslint:master Sep 15, 2017
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Mar 15, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Mar 15, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants