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

Update: make no-var fixable (fixes #6639) #6644

Merged
merged 3 commits into from Jul 15, 2016
Merged

Update: make no-var fixable (fixes #6639) #6644

merged 3 commits into from Jul 15, 2016

Conversation

mysticatea
Copy link
Member

Fixes #6639.

@mention-bot
Copy link

@mysticatea, thanks for your PR! By analyzing the annotation information on this pull request, we identified @vitorbal, @nzakas and @IanVS to be potential reviewers

@eslintbot
Copy link

LGTM

@ilyavolodin
Copy link
Member

Need to update index.md as well.

@eslintbot
Copy link

LGTM

@mysticatea
Copy link
Member Author

@ilyavolodin Thank you!

@mysticatea
Copy link
Member Author

BTW, when I applied this rule to our codebase, this fix generated many no-case-declarations's warnings. Though I think that the behavior of this fix is no problem.

@ilyavolodin
Copy link
Member

Hmm.. That's interesting. So by fixing one rule, you might introduce a bunch of errors in another rule. While it's a valid fix, I'm not 100% sure we want to create code that fails some other rule.

@mysticatea
Copy link
Member Author

Probably there are people which are turning no-case-declarations off. This fixing has worth for those people.
But eslint:recommended includes no-case-declarations, so it may be worth not modifying var declarations when the declaration is a child of a SwitchCase node.

I don't have strong opinions here.

@platinumazure
Copy link
Member

Couldn't we just special-case var declarations in switches and let them be manually fixed? That way users can choose whether to hoist the declaration or ignore/disable the no-case-declarations error. We probably don't want to fix into questionable/lint-polluted code, in the same way that we choose to avoid fixing into incorrect code with hoisted var declarations.

@nzakas
Copy link
Member

nzakas commented Jul 12, 2016

My vote is to not autofix in a case.

@ilyavolodin
Copy link
Member

As far as I remember, no-var doesn't currently check for case. That's part of another rule. If we were to add the code to prevent fixes inside case we would more or less will have to duplicate the logic from no-case-declarations rule.

@nzakas
Copy link
Member

nzakas commented Jul 12, 2016

@ilyavolodin What I'm suggesting is that before applying a fix, we just check to see if the parent is a switch case (it's literally node.parent.type !== "SwitchCase"). That doesn't change what the rule does or duplicate any complicated logic.

@ilyavolodin
Copy link
Member

It sort of feels like we are coupling rules here. But I guess I'm not opposed to a simple check like that.

@platinumazure
Copy link
Member

@nzakas Just to make sure, you wouldn't see any problems with BlockStatements being interposed due to those creating new lexical scope (unlike cases), correct?

switch (foo) {
    case 1:
        var a = 1;      // Should be reported but not fixed?
        if (true) {
            var b = 1;  // Should be reported and fixed?
        }
}

@nzakas
Copy link
Member

nzakas commented Jul 13, 2016

@ilyavolodin it's not really coupling rules -- we are just not "fixing" something to avoid a possible error. This doesn't add any additional warning, it just doesn't fix something that could be problematic.

@platinumazure yup, that's the intended behavior.

@eslintbot
Copy link

LGTM

@mysticatea
Copy link
Member Author

Thank you, everyone.

I modified no-var rule to not fix var declarations on case chunks.

var scopeNode = getScopeNode(node);

return !(
node.parent.type === "SwitchCase" ||
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 a comment to this line explaining why we are skipping SwitchCase. Otherwise next person who fixes the rule might think it was mistake.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for the suggestion.
I added some notes to the JSDoc comment of this function.

@eslintbot
Copy link

LGTM

@nzakas
Copy link
Member

nzakas commented Jul 15, 2016

Lgtm. Awesome work!

@nzakas nzakas merged commit 38639bf into master Jul 15, 2016
@mysticatea mysticatea deleted the issue6639 branch July 15, 2016 15:48
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 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 Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants