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): [prefer-optional-chain] properly disambiguate between boolean and false #8685

Conversation

abrahamguo
Copy link
Contributor

PR Checklist

Overview

In #8659, we see that the example in the docs for prefer-optional-chain: allowPotentiallyUnsafeFixesThatModifyTheReturnTypeIKnowWhatImDoing is not reporting an error.

The root cause is actually unrelated to allowPotentiallyUnsafeFixesThatModifyTheReturnTypeIKnowWhatImDoing — it is because foo.bar is of type boolean.

In the rule, there is a bypass to avoid flagging unions with falsy members, such as

declare const foo: { bar: boolean } | boolean | null | undefined;
foo && foo.bar;

since changing that to an optional chain would cause a TS error.

However, if the falsy literal is the last operand, then we should report it. This is the change made by this PR.

declare const foo: { bar: boolean } | null | undefined;
foo && foo.bar;

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @abrahamguo!

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.

Copy link

netlify bot commented Mar 15, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 43a2514
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/65f4d071d458a700080081b9
😎 Deploy Preview https://deploy-preview-8685--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 99 (🟢 up 6 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 98 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

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

Copy link

codecov bot commented Mar 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.23%. Comparing base (609a000) to head (43a2514).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #8685   +/-   ##
=======================================
  Coverage   87.23%   87.23%           
=======================================
  Files         251      251           
  Lines       12319    12321    +2     
  Branches     3884     3885    +1     
=======================================
+ Hits        10746    10748    +2     
  Misses       1303     1303           
  Partials      270      270           
Flag Coverage Δ
unittest 87.23% <100.00%> (+<0.01%) ⬆️

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

Files Coverage Δ
...efer-optional-chain-utils/gatherLogicalOperands.ts 100.00% <100.00%> (ø)

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.

Clever - I like the changes. Very minimal and clean. Nicely done! 🙌

@JoshuaKGoldberg JoshuaKGoldberg merged commit 2018f91 into typescript-eslint:main Mar 25, 2024
60 checks passed
peanutenthusiast pushed a commit to peanutenthusiast/typescript-eslint that referenced this pull request Mar 27, 2024
…ween `boolean` and `false` (typescript-eslint#8685)

* make the example from the docs work

* revert

* allow falsey literals on last operand
yeonjuan pushed a commit to yeonjuan/typescript-eslint that referenced this pull request Mar 31, 2024
…ween `boolean` and `false` (typescript-eslint#8685)

* make the example from the docs work

* revert

* allow falsey literals on last operand
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants