-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(eslint-plugin): [prefer-nullish-coalescing] doesn't report on ternary but on equivalent || #10517
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-nullish-coalescing] doesn't report on ternary but on equivalent || #10517
Conversation
Thanks for the PR, @OlivierZal! 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. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
View your CI Pipeline Execution ↗ for commit b771c7f.
☁️ Nx Cloud last updated this comment at |
db11672
to
586aa99
Compare
0360ad0
to
3f23cb4
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #10517 +/- ##
==========================================
+ Coverage 87.17% 87.18% +0.01%
==========================================
Files 448 450 +2
Lines 15598 15617 +19
Branches 4555 4562 +7
==========================================
+ Hits 13597 13616 +19
Misses 1645 1645
Partials 356 356
Flags with carried forward coverage won't be shown. Click here to find out more.
|
3f23cb4
to
ea899f8
Compare
@JoshuaKGoldberg, @kirkwaiblinger, this check seems to be stuck. However, tests passed so I guess the PR is ready for a first review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A good start - nice job working with such tricky existing logic!
The tests for valid
cases and primitives are very nice and thorough. Requesting changes on more thorough coverage of invalid
please. Thanks!
dfa9df1
to
74a4059
Compare
Hi @JoshuaKGoldberg, thanks for the review, I've made at least 2 of the requested changes:
Regarding the fix/suggestion logic tests following the
Do you see any other cases I’ve missed that I could add? Or do you have something else in mind? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is plenty thorough enough for me 😄 but I've also kind of burned out on reviewing prefer-nullish-coalescing
. We should get review from another @typescript-eslint/triage-team member.
@JoshuaKGoldberg, @kirkwaiblinger, waiting for this PR having a second review, I keep updating the branch each time a new PR is merged. Is it the right thing to do, or can I leave it as is until the review? |
Ready for review, but I remain open to feedback on #10517 (comment) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the current algorithm and factorization looks good! There's one minor complaint, where I think you've added a workaround which can be avoided - I made a little suggestion PR in order to illustrate this, see OlivierZal#2. Fortunately the test cases you've written validated this well when I was playing around 👍
I think if we make those few changes we'll be close to done iterating! 🙂
suggestion for prefer-nullish-coalescing PR
@kirkwaiblinger, I’m delighted I wasn’t too far off the mark 😅. Ultimately, I realized that the inconsistency mentioned in the corresponding issue went beyond the cited example, and since I’m still quite new to the project, I wasn’t entirely sure if taking a few liberties was appropriate. Thank you so much for your time, help, and guidance. The last commit only fixes a lint issue in this commit; all the other checks passed successfully. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few little nits but this is otherwise basically an approval! Great work on this!! It's delightful work on a real tricky part of the codebase 👍 Thanks for working on this!
(parent.consequent === node || | ||
parent.alternate === node || | ||
node.type === AST_NODE_TYPES.UnaryExpression) | ||
) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I'm realizing that these UnaryExpression
recursions are unnecessary as well, I think, right?
(parent.consequent === node || | |
parent.alternate === node || | |
node.type === AST_NODE_TYPES.UnaryExpression) | |
) { | |
(parent.consequent === node || parent.alternate === node) | |
) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed, it was the second part of my "workaround", that your suggestion fixed.
(parent.consequent === node || | ||
parent.alternate === node || | ||
node.type === AST_NODE_TYPES.UnaryExpression) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(parent.consequent === node || | |
parent.alternate === node || | |
node.type === AST_NODE_TYPES.UnaryExpression) | |
(parent.consequent === node || parent.alternate === node) |
packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.mdx
Outdated
Show resolved
Hide resolved
Co-authored-by: Kirk Waiblinger <53019676+kirkwaiblinger@users.noreply.github.com>
Fixed! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Praise] Really nice refactors here. Love to see a -62 line count on a rule 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 thanks for pushing through on this!
| datasource | package | from | to | | ---------- | -------------------------------- | ------ | ------ | | npm | @typescript-eslint/eslint-plugin | 8.21.0 | 8.22.0 | | npm | @typescript-eslint/parser | 8.21.0 | 8.22.0 | ## [v8.22.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8220-2025-01-27) ##### 🩹 Fixes - **eslint-plugin:** \[no-unnecessary-template-expression] handle template literal type ([#10612](typescript-eslint/typescript-eslint#10612)) - **eslint-plugin:** \[prefer-readonly] autofixer doesn't add type to property that is mutated in the constructor ([#10552](typescript-eslint/typescript-eslint#10552)) - **eslint-plugin:** \[no-extraneous-class] handle accessor keyword ([#10678](typescript-eslint/typescript-eslint#10678)) - **eslint-plugin:** \[no-shadow] don't report unnecessarily on valid ways of using module augmentation ([#10616](typescript-eslint/typescript-eslint#10616)) - **eslint-plugin:** \[no-duplicate-type-constituents] handle nested types ([#10638](typescript-eslint/typescript-eslint#10638)) - **eslint-plugin:** \[prefer-nullish-coalescing] doesn't report on ternary but on equivalent || ([#10517](typescript-eslint/typescript-eslint#10517)) ##### ❤️ Thank You - mdm317 - Olivier Zalmanski [@OlivierZal](https://github.com/OlivierZal) - Ronen Amiel - YeonJuan [@yeonjuan](https://github.com/yeonjuan) You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
| datasource | package | from | to | | ---------- | -------------------------------- | ------ | ------ | | npm | @typescript-eslint/eslint-plugin | 8.21.0 | 8.22.0 | | npm | @typescript-eslint/parser | 8.21.0 | 8.22.0 | ## [v8.22.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8220-2025-01-27) ##### 🩹 Fixes - **eslint-plugin:** \[no-unnecessary-template-expression] handle template literal type ([#10612](typescript-eslint/typescript-eslint#10612)) - **eslint-plugin:** \[prefer-readonly] autofixer doesn't add type to property that is mutated in the constructor ([#10552](typescript-eslint/typescript-eslint#10552)) - **eslint-plugin:** \[no-extraneous-class] handle accessor keyword ([#10678](typescript-eslint/typescript-eslint#10678)) - **eslint-plugin:** \[no-shadow] don't report unnecessarily on valid ways of using module augmentation ([#10616](typescript-eslint/typescript-eslint#10616)) - **eslint-plugin:** \[no-duplicate-type-constituents] handle nested types ([#10638](typescript-eslint/typescript-eslint#10638)) - **eslint-plugin:** \[prefer-nullish-coalescing] doesn't report on ternary but on equivalent || ([#10517](typescript-eslint/typescript-eslint#10517)) ##### ❤️ Thank You - mdm317 - Olivier Zalmanski [@OlivierZal](https://github.com/OlivierZal) - Ronen Amiel - YeonJuan [@yeonjuan](https://github.com/yeonjuan) You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
PR Checklist
Overview