-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [no-unnecessary-condition] add checkTypePredicates #10009
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
feat(eslint-plugin): [no-unnecessary-condition] add checkTypePredicates #10009
Conversation
Thanks for the PR, @kirkwaiblinger! 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. |
☁️ Nx Cloud ReportCI is running/has finished running commands for commit 42caf70. As they complete they will appear below. Click to see the status, the terminal output, and the build insights. 📂 See all runs for this CI Pipeline Execution ✅ Successfully ran 2 targetsSent with 💌 from NxCloud. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #10009 +/- ##
==========================================
+ Coverage 88.70% 88.72% +0.01%
==========================================
Files 426 427 +1
Lines 14835 14896 +61
Branches 4312 4329 +17
==========================================
+ Hits 13159 13216 +57
- Misses 1533 1538 +5
+ Partials 143 142 -1
Flags with carried forward coverage won't be shown. Click here to find out 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.
Seems very reasonable to me! The comments were quite helpful, thanks for that. 🙌
Requesting changes on added test coverage.
packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts
Outdated
Show resolved
Hide resolved
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.
FYI - I realized while working on this that we can use checker.getTypePredicateOfSignature(signature)
, which.... does all the heavy lifting for us 🤦♂️
So this is now much simpler. 🥳
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.
See 4ac6765 for just the diff of the algorithm change
@@ -516,29 +516,6 @@ declare const nullableString: string | null; | |||
assert(3 as any, nullableString); | |||
`, | |||
}, | |||
// Intentional TS error - A rest parameter must be last in a parameter list. |
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.
These tests were for coverage reasons that no longer make sense with the new algorithm.
None of them crashes. 2 of them now do report, but they match how TS thinks about the situation, so it's not really a false report 🤷♂️
Per discord conversation, we'll want to bring #10007 into this in order to put it behind the same option. Marking as draft until then |
This has changed a lot since its initial version. I would recommend reviewing it more or less from scratch |
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.
LGTM, very nice refactor!
Just one nit suggestion on test-utils.ts
, otherwise 🚀 from me.
@@ -139,6 +140,7 @@ export function omitDeep( | |||
value.push(visit(el, node)); | |||
} | |||
node[prop] = value; | |||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- I don't know if it's safe to change this. |
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.
Seems odd to me to leave an "I don't know if ..." in code. I tried removing them locally and some semanticInfo.test.ts
snapshots broke. I got around it by:
- Changing the type of
oNode
and the return types offunction visit
&function omitDeep
toUnknownObject | undefined
- Removing this
if (isObjectLike(child)) {
from theelse
WDYT?
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.
Hmm, well I'm scared of that since the one usage of omitDeep
is asserted to be not null
typescript-eslint/packages/typescript-estree/tests/test-utils/test-utils.ts
Lines 75 to 82 in d6c67fe
/** | |
* Returns a raw copy of the typescript AST | |
* @param ast the AST object | |
* @returns copy of the AST object | |
*/ | |
export function deeplyCopy<T extends NonNullable<unknown>>(ast: T): T { | |
return omitDeep(ast) as T; | |
} |
But I'm realizing now that this is "just" test code so I feel a little less scared of messing around with it
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.
An easier fix for now seems like just turning isObjectLike
into an ordinary boolean
-returning function, since its type predicate isn't actually needed in the two places it's used. 🤷 Plus, no runtime changes, yay!
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.
🤝 perfect!
PR Checklist
Overview
findAssertedArgument
in SBE to usechecker.getResolvedSignature()
andchecker.getTypePredicateOfSignature()
, and move it into shared location