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: enforceForLogicalOperands no-extra-boolean-cast (fixes #12137) #12734

Merged
merged 15 commits into from Feb 14, 2020
Merged

Update: enforceForLogicalOperands no-extra-boolean-cast (fixes #12137) #12734

merged 15 commits into from Feb 14, 2020

Conversation

jmoore914
Copy link
Contributor

@jmoore914 jmoore914 commented Jan 2, 2020

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

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[X ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

fixes #12137

What changes did you make? (Give an overview)

  • Changed lib/rules/no-extra-boolean-cast.js to allow an option for to enforce the rule in logical operands
  • Changed tests/lib/rules/no-extra-boolean-cast.js to add tests for the new option
  • Changed docs/rules/no-extra-boolean-cast.md to document the new option

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

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Jan 2, 2020
@jmoore914 jmoore914 changed the title no-extra-boolean-cast option to enforce for logical operands (#12137) New no-extra-boolean-cast option to enforce for logical operands (#12137) Jan 2, 2020
@jmoore914 jmoore914 closed this Jan 2, 2020
@jmoore914 jmoore914 reopened this Jan 2, 2020
@jmoore914 jmoore914 changed the title New no-extra-boolean-cast option to enforce for logical operands (#12137) New: Added enforceForLogicalOperands option to no-extra-boolean-cast (fixes #12137) Jan 2, 2020
@jmoore914 jmoore914 closed this Jan 2, 2020
@jmoore914 jmoore914 reopened this Jan 2, 2020
@mdjermanovic
Copy link
Member

@jmoore914 the title can be just:

Update: enforceForLogicalOperands no-extra-boolean-cast (fixes #12137)

@jmoore914 jmoore914 changed the title New: Added enforceForLogicalOperands option to no-extra-boolean-cast (fixes #12137) Update: enforceForLogicalOperands no-extra-boolean-cast (fixes #12137) Jan 2, 2020
@mdjermanovic mdjermanovic added accepted There is consensus among the team that this change meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Jan 2, 2020
@jmoore914
Copy link
Contributor Author

jmoore914 commented Jan 2, 2020

@mdjermanovic thanks, title has been updated!

@mdjermanovic
Copy link
Member

Re-run the checks and it's passing now. It happens sometimes on macOS-latest for an unknown reason.

Copy link
Member

@mdjermanovic mdjermanovic 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 very good in general!

lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
lib/rules/no-extra-boolean-cast.js Show resolved Hide resolved
lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
…wer parameters to functions; check precedence in fixer
@jmoore914
Copy link
Contributor Author

Nice suggestions! Let me know what you think now :)

lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
tests/lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
docs/rules/no-extra-boolean-cast.md Outdated Show resolved Hide resolved
tests/lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
docs/rules/no-extra-boolean-cast.md Show resolved Hide resolved
lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
Copy link
Member

@mdjermanovic mdjermanovic 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 changes! Looks great, just left several minor suggestions.

docs/rules/no-extra-boolean-cast.md Outdated Show resolved Hide resolved
docs/rules/no-extra-boolean-cast.md Outdated Show resolved Hide resolved
lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
tests/lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
tests/lib/rules/no-extra-boolean-cast.js Show resolved Hide resolved
docs/rules/no-extra-boolean-cast.md Outdated Show resolved Hide resolved
docs/rules/no-extra-boolean-cast.md Outdated Show resolved Hide resolved
tests/lib/rules/no-extra-boolean-cast.js Show resolved Hide resolved
Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

Thanks! Just a couple of minor suggestions and I think this is finished.

docs/rules/no-extra-boolean-cast.md Outdated Show resolved Hide resolved
lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
lib/rules/no-extra-boolean-cast.js Outdated Show resolved Hide resolved
tests/lib/rules/no-extra-boolean-cast.js Show resolved Hide resolved
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.

One small question about checking if Boolean is an actual global, but otherwise this LGTM!

@@ -1,6 +1,6 @@
/**
* @fileoverview Tests for no-extra-boolean-cast rule.
* @author Brandon Mills
*@fileoverview Tests for no-extra-boolean-cast rule.
Copy link
Member

Choose a reason for hiding this comment

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

Mind reverting these two line changes? Seems like an accidental change.

function isBooleanFunctionOrConstructorCall(node) {

// Boolean(<bool>) and new Boolean(<bool>)
return (node.type === "CallExpression" || node.type === "NewExpression") &&
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to ensure that Boolean isn't a shadowed global variable?

Copy link
Member

Choose a reason for hiding this comment

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

That should be also done in CallExpression(node).

Though I think that both global Boolean fixes can be a separate PR because it's an existing issue (this function was extracted from UnaryExpression(node)).

Copy link
Member

Choose a reason for hiding this comment

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

Separating it out into a separate PR sounds good to me!

Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

Looks great, thanks!

This PR also fixes 3 bugs in the default behavior:

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

// these were false negatives
Boolean(Boolean(a));
new Boolean(Boolean(a));

// these were false positives
Boolean(a, !!b);
new Boolean(a, !!b);

// this was invalid autofix to: a = b ? c : d;
!!(a = b) ? c : d;

Online Demo

Side note: the fixer can be improved to avoid some unnecessary parens. This can be a separate PR as it's already happening in the actual master version for extra Boolean() so I believe it isn't directly related to this enhancement.

Also, it's questionable should the fixer add parens around b || c for if (a || Boolean(b || c)) {} since a || b || c has different semantics. I think this isn't a big issue as it doesn't change behavior, it can be evaluated and fixed if necessary in the same PR for the extra parens.

@mdjermanovic
Copy link
Member

I edited the description just to link the issue. Not sure why that issue doesn't appear under "Linked issues" in the list of issues to choose.

@jmoore914 I think that the accidental change noticed in this comment is the only unaddressed concern at the moment.

@jmoore914
Copy link
Contributor Author

Could've sworn I fixed that! Well, should be all good now

Copy link
Member

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

@kaicataldo
Copy link
Member

Thanks for contributing!

@kaicataldo kaicataldo merged commit 41de9df into eslint:master Feb 14, 2020
@Standard8
Copy link
Contributor

@jmoore914 Thank you for taking this on and getting it landed. I look forward to being able to use it soon.

@jmoore914 jmoore914 deleted the no-extra-boolean-cast-enforce-for-logical-operands branch February 15, 2020 12:47
montmanu pushed a commit to montmanu/eslint that referenced this pull request Mar 4, 2020
…#12137) (eslint#12734)

* Working rule, updated documentation

* New: Added enforceForLogicalOperands option to no-extra-boolean-cast
(fixes eslint#12137)

* Add default for schema option; renamed functions for clarity; pass fewer parameters to functions; check precedence in fixer

* Removed extra variable from function; fixed function name; fixed precedence check; added additional example

* Updated documentation

* Removed outdated legacy code method

* Added additional tests

* Renamed function

* Added additional test; updated documentation

* Switched docs examples order; fixed comments in rule; added test for inserting space

* Fixed accidentally edited lines
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Aug 14, 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 Aug 14, 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 enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule change: no-extra-boolean-cast should catch more cases
4 participants