-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [no-unnecessary-type-conversion] add rule #10182
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
Conversation
Thanks for the PR, @skondrashov! 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 6822a69.
☁️ Nx Cloud last updated this comment at |
Sorry for force-push, wasn't sure how else to resolve the merge conflicts other than redoing the commit cause they didn't show up locally. Doesn't seem like it did anything to the merge conflict though, so I'm out of ideas. I see I have problems from the perfectionist plugin in my files as well, but the plugin doesn't run locally when I use Would be happy to address the 2 missing lines of coverage at some point if everything else gets figured out. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #10182 +/- ##
==========================================
+ Coverage 90.84% 90.90% +0.06%
==========================================
Files 497 498 +1
Lines 50320 50655 +335
Branches 8311 8335 +24
==========================================
+ Hits 45714 46049 +335
Misses 4591 4591
Partials 15 15
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
(hint) - We often use // setup
declare let str: string | number;
declare const num: number | string;
// etc It's a useful pattern for testing sometimes too, since you may run into surprising behavior when
Makes sense 👍
Yep, also makes sense. And, even if TS did allow them, we don't need to stress about identifying every possible way for a value to be coerced, just the common patterns, and others can always be added in the future, too. Appreciate your close attention to detail on all these, though!
This is an astute observation. Is there anything this rule would flag that that rule doesn't flag? If not, I'd lean towards letting that separate rule handle this case. If you want you could even put a note in the docs that tells the user that template expressions intentionally aren't handled because they're already dealt with more thoroughly in the
Hmm, I hear you. I personally think either one is fine. Maybe someone else will care more strongly 🤷
I think for these reports, reporting the whole node is totally fine declare const s: string;
s + '';
~~~~~~
!!(longExpressionThatsABoolean);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ But, I really like what you've done with the more granular approach. !!(longExpressionThatsABoolean);
~~ So I'd say go for it! But if it becomes an implementation hassle, no stress either if you end up falling back to the typical strategy of letting the |
No worries! The real pain is just force pushing between reviews, not before a reviewer has looked at it at all 🙂.
Oh, so I think the trouble that you're having is that you've used the
But you're in a slightly awkward scenario since your branch is on main. What I personally do (which differs from the above) is to set a second remote on your local repo, i.e. LMK if this helps or if you'd like some more help. (I can also push things directly to your branch to bail you out if needed 🤣). And no stress if you end up doing a force push again this time to get yourself in a clean state here! |
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.
This is great work! And especially impressive considering it's your first PR to typescript-eslint. Well done!
Requesting a few changes, but nothing major.
🙂
packages/eslint-plugin/tests/rules/no-unnecessary-coercion.test.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/tests/rules/no-unnecessary-coercion.test.ts
Outdated
Show resolved
Hide resolved
Agreed; let's go with |
…th more than just string types
👋 Just checking in @skondrashov - is this still something you have time and energy for? |
I'll have time to finish up the changes probably early in the new year! |
Awesome! I'll move this to draft then in the meantime, so we don't bug you. 🚀 |
Excited about this rule! I just saw a lot of |
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 I'm happy with this! Thanks for your very detailed work on this. Outstanding first PR!
packages/eslint-plugin/docs/rules/no-unnecessary-type-conversion.mdx
Outdated
Show resolved
Hide resolved
I am salivating at how many existing violations were removed in this PR. 🤤 |
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.
Awesome, this is looking very good! A stellar first PR indeed 🏆. Thanks for pushing on this!
Just a few small things from me - nothing we can't just do before merge if you don't have the time.
packages/eslint-plugin/docs/rules/no-unnecessary-type-conversion.mdx
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/no-unnecessary-type-conversion.ts
Outdated
Show resolved
Hide resolved
@@ -4,7 +4,6 @@ import rule from '../../src/rules/await-thenable'; | |||
import { getFixturesRootDir } from '../RuleTester'; | |||
|
|||
const rootDir = getFixturesRootDir(); | |||
const messageId = 'await'; |
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.
Nit: it would be nice to have these split out into their own PR. I'm not going to block on this though.
6822a69
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.
PR Checklist
Overview
Shows autofixable errors for:
with the following error messaging:
Does not (and should not) show errors for:
Does not do anything with Symbol() because everything weird I tried to do with symbols was already a typescript error.
Does not do anything for some conversions to number which are already forbidden by typescript ("The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type."), including:
Does not cover
${"123"}
, because that overlaps with theno-unnecessary-template-expression
rule. Need feedback on what to do about that.I went with the word "coercion" because it was chosen by @Josh-Cena in the original issue but I think it's a poor choice. I think "conversion" or even "type conversion idiom" might be the most precise, because
.toString()
for example doesn't rely on coercion, and while!!value
and!value
both coerce the type ofvalue
to a boolean, it's the property of the idiom!!
that it can be used for type conversion which puts it under the umbrella of this rule. So I thinkno-unnecessary-type-conversion
is a better name for this rule, given the things that I'm currently having it check for.Catches the following problems in this repository (separate commit):
Might not have the best placement for the error scope, and might not have the best formatting for autofixes, could use feedback on those too.