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: no-extra-boolean-cast invalid autofix for Boolean() without args #12076

Merged
merged 3 commits into from Aug 13, 2019
Merged

Fix: no-extra-boolean-cast invalid autofix for Boolean() without args #12076

merged 3 commits into from Aug 13, 2019

Conversation

mdjermanovic
Copy link
Member

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

[X] Bug fix

Tell us about your environment

  • ESLint Version: 6.1.0
  • Node Version: 10.16.0
  • npm Version: 6.9.0

What parser (default, Babel-ESLint, etc.) are you using?

default

Please show your full configuration:

Configuration
module.exports = {
  parserOptions: {
    ecmaVersion: 2015,
  },
};

What did you do? Please include the actual source code causing the issue.

/*eslint no-extra-boolean-cast: "error"*/

// Bug #1

var foo = Boolean() ? bar() : baz();

if (Boolean()) {
    foo();
}

while (Boolean()) {
  foo();
}

// Bug #2

var foo = typeof!Boolean();

Demo link

What did you expect to happen?

Not to change behavior and/or remove the code.

What actually happened? Please include the actual, raw output from ESLint.

/*eslint no-extra-boolean-cast: "error"*/

// Bug #1

var foo = true;

true

true

// Bug #2

var foo = typeoftrue;

What changes did you make? (Give an overview)

Fixed bugs:

  • Bug#1 - code was assuming it's always !Boolean() and the fix was replacing parent with true.
  • Bug#2 - didn't check can the tokens be adjacent.

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

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Aug 7, 2019
@kaicataldo
Copy link
Member

kaicataldo commented Aug 8, 2019

This change makes sense to me. I know that this is the current behavior, but the fixer changing

if (Boolean()) {
    foo();
}

while (Boolean()) {
    foo();
}

to

true;

true;

feels like the autofixer overreaching to me. Now that we're fixing

var foo = Boolean() ? bar() : baz();

to

var foo = false ? bar() : baz();

, I wonder if we should be fixing the first example above to

if (false) {
    foo();
}

while (false) {
    foo();
}

as well? Additionally, seems like it should fix the following:

if (!Boolean()) {
    foo();
}

while (!Boolean()) {
    foo();
}

to

if (true) {
    foo();
}

while (true) {
    foo();
}

I'm curious what the rest of the team has to say about this - there might be some historical context around this that I'm not aware of.

@kaicataldo kaicataldo 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 and removed triage An ESLint team member will look at this issue soon labels Aug 8, 2019
@mdjermanovic
Copy link
Member Author

mdjermanovic commented Aug 8, 2019

This change makes sense to me. I know that this is the current behavior, but the fixer changing

if (Boolean()) {
    foo();
}

while (Boolean()) {
    foo();
}

to

true;

true;

feels like the autofixer overreaching to me.

I think this is a bug from #7977. It was assumed that this rule warns on Boolean() only when it's in !, but it can appear in other 'boolean contexts'.

While these two don't change behavior (just delete a lot of unreachable code, which I guess is also not okay), the first example does:

var foo = Boolean() ? bar() : baz();

to

var foo = true;

This is certainly a bug, foo = baz() is changed to foo = true.

Now that we're fixing

var foo = Boolean() ? bar() : baz();

to

var foo = false ? bar() : baz();

, I wonder if we should be fixing the first example above to

if (false) {
    foo();
}

while (false) {
    foo();
}

as well?

It's working exactly like that with this fix:

{
    code: "if (Boolean()) {}",
    output: "if (false) {}",
    errors: [{
        messageId: "unexpectedCall",
        type: "CallExpression"
    }]
},

Additionally, seems like it should fix the following:

if (!Boolean()) {
    foo();
}

while (!Boolean()) {
    foo();
}

to

if (true) {
    foo();
}

while (true) {
    foo();
}

It was already working like this, and this shouldn't be changed by this fix. But to be sure, I'll add these test cases.

@mdjermanovic
Copy link
Member Author

Added 3 test cases and modified 1 to match the example, and everything works as you described :)

Copy link
Member

@kaicataldo kaicataldo 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!

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.

Curious to know what happens in this case: void ! /* Comment */ Boolean(). I'm thinking sourceCode.getTokenBefore should find the correct token since you didn't specify { comments: true }, but I think we should make sure the comment is preserved. That might mean no autofix in that case (we have other rules that choose not to fix if it is difficult to preserve comments).

Otherwise, changes look good to me. Thanks for contributing!

@mdjermanovic
Copy link
Member Author

Can't verify in the PR version at the moment, but I'm pretty sure that this comment will be removed, as it is within the node which is supposed to be replaced.

It was already working like that (Demo link). I'll fix that in this PR, autofix shouldn't delete user's comments.

I've already noticed that many rules are deleting comments (which should never happen I guess) and was thinking about an additional method in the FixTracker which would set a flag to check for comments within the range before the fix. I'll open an issue with that idea if it makes sense.

@platinumazure
Copy link
Member

I've already noticed that many rules are deleting comments (which should never happen I guess) and was thinking about an additional method in the FixTracker which would set a flag to check for comments within the range before the fix. I'll open an issue with that idea if it makes sense.

That sounds like a great idea!

@mdjermanovic
Copy link
Member Author

Comments should be safe now in these and other autofix cases in this rule, please check is it ok.

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 adding all the extra tests!

Copy link
Member

@kaicataldo kaicataldo left a comment

Choose a reason for hiding this comment

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

Great call on handling comments 👍 This autofixer is in much better shape than it was previously!

Copy link
Member

@aladdin-add aladdin-add 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 the good tests!!

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 10, 2020
@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 10, 2020
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

4 participants