-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [no-unsafe-return] check promise any #8693
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, @yeonjuan! 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. |
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! Curious to see what ends up changing in order to support the added test cases.
And a thought - these are realllly tricky. I spent a lot of time myself getting confused over whether the code I was trying out actually should pass or fail 😆.
So, I wonder if we can help the user out by supplying a custom error message for the async cases that don't directly return any
, but whose type evaluates to any
after being Awaited
? Feel free to ignore this if it's a hassle to implement. But, my thought would be something like "Unsafe return of type Promise<any>
that is equivalent to simply returning any
within an async function." I'm curious for your thoughts. And, as said, it's completely ok if you would prefer to ignore this idea!
messageId: 'unsafeReturn', | ||
line: 4, | ||
column: 3, | ||
endColumn: 16, |
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, optional) If you're gonna include the endColumn
, might as well include the endLine
too for consistency. Here and elsewhere
@kirkwaiblinger I agree, this is great, but how about making the error message a little more concise? |
👍 👍 |
# Conflicts: # packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #8693 +/- ##
==========================================
- Coverage 88.45% 88.42% -0.03%
==========================================
Files 422 422
Lines 14695 14717 +22
Branches 4298 4308 +10
==========================================
+ Hits 12998 13014 +16
- Misses 1372 1378 +6
Partials 325 325
Flags with carried forward coverage won't be shown. Click here to find out more.
|
CI is failing, I created a separate PR to fix it. #9037 |
@kirkwaiblinger Thank you for your review. I added test cases and improved messsage. |
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.
Hi @yeonjuan!
First off, I want to say I am very sorry for how long it's taken me to get to another review pass on this!
This is great stuff. A few more tweaks and questions for iteration, but I feel like we're getting close 🙂
? 'any' | ||
: anyType === AnyType.AnyArray | ||
? 'any[]' | ||
: 'Promise<any>', |
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 - not your fault, but this now runs into an "a" vs "an" issue in the generated string. I get
Unsafe return of an `Promise<any>` typed value
Would you change the wording a bit so that it's grammatically consistent?
For example, Unsafe return of a value of type {{type}}.
@@ -123,24 +124,37 @@ export function isTypeUnknownArrayType( | |||
|
|||
export enum AnyType { | |||
Any, | |||
PromiseAny, | |||
AnyArray, | |||
Safe, | |||
} | |||
/** | |||
* @returns `AnyType.Any` if the type is `any`, `AnyType.AnyArray` if the type is `any[]` or `readonly any[]`, |
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 - comment will need updating
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.
Only blocker remaining is resolution of #8693 (comment), I think
@@ -22,6 +22,10 @@ declare module 'typescript' { | |||
* Return the type of the given property in the given type, or undefined if no such property exists | |||
*/ | |||
getTypeOfPropertyOfType(type: Type, propertyName: string): Type | undefined; | |||
/** | |||
* Return the awaited type of the given type. |
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.
Maybe add a TODO that this should no longer be necessary in a future TS version and link the TS issue?
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.
Sure! fcca5ec
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.
Yay! 🚀
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
{ | ||
code: ` | ||
declare const value: Promise<any>; | ||
function foo() { |
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 thought this is supposed to be valid, since it's not an async
function, according to #8674 (comment). Did that decision get changed?
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.
Note also that the error message for this case "Unsafe return of an Promise<any>
typed value." is problematic, same as #8693 (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've fixed! I had forgotten about those comments.
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.
Looks great!
UPD: There seems to be a merge conflict with #9540.
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.
Left a couple questions
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.
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.
c20bd2f
into
typescript-eslint:main
PR Checklist
Overview