-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): add rule use-unknown-in-catch-callback-variables
#8383
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): add rule use-unknown-in-catch-callback-variables
#8383
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. |
packages/eslint-plugin/src/rules/use-unknown-in-catch-callback-variable.ts
Show resolved
Hide resolved
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #8383 +/- ##
==========================================
+ Coverage 88.02% 88.10% +0.08%
==========================================
Files 401 402 +1
Lines 13934 14028 +94
Branches 4087 4116 +29
==========================================
+ Hits 12265 12359 +94
Misses 1370 1370
Partials 299 299
Flags with carried forward coverage won't be shown. Click here to find out more.
|
packages/eslint-plugin/src/rules/use-unknown-in-catch-callback-variable.ts
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/use-unknown-in-catch-callback-variable.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/docs/rules/use-unknown-in-catch-callback-variable.md
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.
This looks great, awesome job on all the edge cases & whatnot!
Left a few comments inline, requesting changes. But really solid all-up - no major notes! 🙌
packages/eslint-plugin/src/rules/use-unknown-in-catch-callback-variable.ts
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/use-unknown-in-catch-callback-variable.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/docs/rules/use-unknown-in-catch-callback-variable.md
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/docs/rules/use-unknown-in-catch-callback-variable.md
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/docs/rules/use-unknown-in-catch-callback-variable.md
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/use-unknown-in-catch-callback-variable.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/use-unknown-in-catch-callback-variable.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/use-unknown-in-catch-callback-variable.ts
Outdated
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/use-unknown-in-catch-callback-variable.ts
Show resolved
Hide resolved
packages/eslint-plugin/src/rules/use-unknown-in-catch-callback-variable.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
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.
packages/eslint-plugin/src/rules/use-unknown-in-catch-callback-variable.ts
Outdated
Show resolved
Hide resolved
6dbd99a
to
ef47613
Compare
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.
use-unknown-in-catch-callback-variables
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.
quick eyeball - haven't got time for a full review at this second.
@@ -76,5 +76,6 @@ export = { | |||
'@typescript-eslint/triple-slash-reference': 'error', | |||
'@typescript-eslint/unbound-method': 'error', | |||
'@typescript-eslint/unified-signatures': 'error', | |||
'@typescript-eslint/use-unknown-in-catch-callback-variable': 'error', |
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.
just double checking/confirming we want to add this to the strict config before the next major?
i don't think we've ever expressly discussed our strategy for the strict configs.
they're not technically included in our versioning policy for a major release if they're changed - but I wonder if adding a brand new rule in a minor might be a bit disruptive for users?
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.
In some of my projects, I already set the type of the exception to |
To be honest, I didn't test this, since the purpose of this rule existing (per the issue) is for the people who don't modify the lib, and I don't really remember since I worked on this a while back. I did put a note in the docs that that's not the intended use case, though. https://main--typescript-eslint.netlify.app/rules/use-unknown-in-catch-callback-variable#when-not-to-use-it Feel free to share what you find if you test it! |
Yeah if there's a real use case for overriding global types & this rule, then I'd love to see that. In a new issue please. 🙂 |
PR Checklist
(error: unknown) => void
with promise.catch #7526Overview
This change implements the rule proposal requiring variables in
.catch()
callbacks to have typeunknown
. The rule fundamentally works by flagging on the condition.catch()
call (on a Thenable) is a functionunknown
type (according to the type checker, not syntax analysis).Note that the backing issue mentions the return type, but any return type is legitimate in a
.catch
callback, so this rule does not check the return type.Fixes for cases that are analyzable syntactically have been implemented as editor suggestions.
Limitations
err: unknown
if you have modified the gobal type declarations for.catch()
.