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: incorrect logic for required parens in no-extra-boolean-cast fixer #13061

Merged
merged 2 commits into from Mar 25, 2020

Conversation

mdjermanovic
Copy link
Member

Prerequisites checklist

  • I have read the contributing guidelines.
  • The team has reached consensus on the changes proposed in this pull request. If not, I understand that the evaluation process will begin with this pull request and won't be merged until the team has reached consensus.

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

[X] Bug fix

Tell us about your environment

  • ESLint Version: v7.0.0-alpha.2
  • Node Version: v12.14.0
  • npm Version: v6.13.4

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

Please show your full configuration:

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

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

Online Demo Link (v6.8.0)

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

if (Boolean(a ? b : c) ? d : e);

What did you expect to happen?

Autofix:

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

if ((a ? b : c) ? d : e);

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

Autofix:

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

if (a ? b : c ? d : e); // equivalent to: if (a ? b : (c ? d : e));

What changes did you make? (Give an overview)

Fixed the precedence check for required parens in the no-extra-boolean-cast fixer.

This would also avoid often unnecessary parens, e.g.:

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

if (Boolean(f(a)));

used to be auto-fixed to:

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

if ((f(a)));

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

@mdjermanovic mdjermanovic added bug ESLint is working incorrectly rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion autofix This change is related to ESLint's autofixing capabilities labels Mar 19, 2020
case "IfStatement":
case "DoWhileStatement":
case "WhileStatement":
case "ForStatement":
Copy link
Member

Choose a reason for hiding this comment

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

Just double checking, should ForOfStatement and ForInStatement be included here?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, this switch is supposed to cover only possible parents for the reported node in this rule (otherwise, this function could grow to the size of no-extra-parens rule).

It also assumes the node's role in its parent. For example, case "ConditionalExpression" knows that it is the test node at this point (precedence check would be different for consequent or alternate node).

Copy link
Member

Choose a reason for hiding this comment

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

Gotcha, thanks. Can you add a comment explaining that?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added to the function's description, thanks for the suggestion!

Copy link
Member

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

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. Thank you!

@kaicataldo kaicataldo merged commit 3e4e7f8 into master Mar 25, 2020
@kaicataldo kaicataldo deleted the noextraboolcast-parens branch March 25, 2020 18:50
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Sep 23, 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 Sep 23, 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 autofix This change is related to ESLint's autofixing capabilities 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

3 participants