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(eslint-plugin): [no-unnecessary-boolean-literal-compare] fixer should handle parentheses #6569

Merged

Conversation

armano2
Copy link
Member

@armano2 armano2 commented Mar 4, 2023

PR Checklist

Overview

Add missing handling for removal and adding parenthesis in rule fixer

new cases covered:

if ((x instanceof Error) === false) {}
if (false === (x instanceof Error)) {}
if (x instanceof Error === false) {}

old fixer result

if (!(x instanceof Error) { }
if (!x instanceof Error)) { }
if (!x instanceof Error) { }

new fixer result

if (!(x instanceof Error)) { }
if (!(x instanceof Error)) { }
if (!(x instanceof Error)) { }

initially i was planning to extend range of fixer to include )( but after further investigation this started generating to many edge cases

eg. (false) === x instanceof Error

@nx-cloud
Copy link

nx-cloud bot commented Mar 4, 2023

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 79de5c2. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 46 targets

Sent with 💌 from NxCloud.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @armano2!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint.

@netlify
Copy link

netlify bot commented Mar 4, 2023

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 79de5c2
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/6403a01a1f961b0008db9ab8
😎 Deploy Preview https://deploy-preview-6569--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@armano2 armano2 marked this pull request as draft March 4, 2023 19:04
@codecov
Copy link

codecov bot commented Mar 4, 2023

Codecov Report

Merging #6569 (79de5c2) into main (9427b7c) will increase coverage by 0.04%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6569      +/-   ##
==========================================
+ Coverage   90.66%   90.71%   +0.04%     
==========================================
  Files         376      376              
  Lines       12851    12855       +4     
  Branches     3783     3783              
==========================================
+ Hits        11651    11661      +10     
+ Misses        856      850       -6     
  Partials      344      344              
Flag Coverage Δ
unittest 90.71% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...rc/rules/no-unnecessary-boolean-literal-compare.ts 97.67% <100.00%> (+7.31%) ⬆️
...ackages/eslint-plugin/src/util/getWrappingFixer.ts 100.00% <100.00%> (ø)

@armano2 armano2 marked this pull request as ready for review March 4, 2023 19:49
yield fixer.replaceText(
node,
sourceCode.getText(comparison.expression),
);

// if the expression `exp` isn't nullable, or we're comparing to `true`,
// we can just replace the entire comparison with `exp` or `!exp`
Copy link
Member Author

@armano2 armano2 Mar 5, 2023

Choose a reason for hiding this comment

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

after further investigation we could simplify this fixer to

          fix: function* (fixer) {
            const isUnaryNegation =
              node.parent != null && nodeIsUnaryNegation(node.parent);

            const mutatedNode = isUnaryNegation ? node.parent! : node;

            yield fixer.replaceText(
              mutatedNode,
              sourceCode.getText(comparison.expression),
            );

            const shouldNegate = comparison.literalBooleanInComparison
              ? !comparison.negated
              : comparison.negated;

            if (isUnaryNegation === shouldNegate) {
              yield fixer.insertTextBefore(mutatedNode, '!');

              if (!util.isStrongPrecedenceNode(comparison.expression)) {
                yield fixer.insertTextBefore(mutatedNode, '(');
                yield fixer.insertTextAfter(mutatedNode, ')');
              }
            }

            // if the expression `exp` is nullable, and we're not comparing to `true`,
            // we can just replace the entire comparison with `exp` or `!exp`
            if (
              comparison.expressionIsNullableBoolean &&
              !comparison.literalBooleanInComparison
            ) {
              // provide the default `true`
              yield fixer.insertTextBefore(mutatedNode, '(');
              yield fixer.insertTextAfter(mutatedNode, ' ?? true)');
            }
          }

that should allow us to provide better fixer for

// source
if (!(varBoolean === false)) {}
// current
if (!(!varBoolean)) {}
// new
if (varBoolean) {}

i'm not sure if we should push this with this fix

Copy link
Member

Choose a reason for hiding this comment

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

I like it 😄 +1 to including it here. Also a +1 to filing a followup if you want to get this in sooner.

Copy link
Member

Choose a reason for hiding this comment

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

I'm going to land this as-is for now, just so we can get this fix out with the next release.
We can follow-up next week with the changes to simplify this fixer later

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

💯

yield fixer.replaceText(
node,
sourceCode.getText(comparison.expression),
);

// if the expression `exp` isn't nullable, or we're comparing to `true`,
// we can just replace the entire comparison with `exp` or `!exp`
Copy link
Member

Choose a reason for hiding this comment

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

I like it 😄 +1 to including it here. Also a +1 to filing a followup if you want to get this in sooner.

@JoshuaKGoldberg JoshuaKGoldberg added the 1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready label Mar 8, 2023
@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party and removed awaiting response Issues waiting for a reply from the OP or another party labels Mar 13, 2023
@bradzacher bradzacher added this pull request to the merge queue Mar 13, 2023
@bradzacher bradzacher added the bug Something isn't working label Mar 13, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Mar 13, 2023
@JoshuaKGoldberg JoshuaKGoldberg merged commit 2d8c196 into main Mar 13, 2023
@JoshuaKGoldberg JoshuaKGoldberg deleted the fix/no-unnecessary-boolean-literal-compare-braces branch March 13, 2023 04:25
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval PR that a maintainer has LGTM'd - any maintainer can merge this when ready bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [no-unnecessary-boolean-literal-compare] Quick Fix generates bug with instanceof
3 participants