-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Implement codefix for adding missing await in return statements enclosed by try statements
#60642
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
base: main
Are you sure you want to change the base?
Implement codefix for adding missing await in return statements enclosed by try statements
#60642
Conversation
…nclosed by try statements
|
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
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.
not gonna lie, those messages could use some improvement
| if (canBeConvertedToAsync(node)) { | ||
| addConvertToAsyncFunctionDiagnostics(node, checker, diags); | ||
| } | ||
| if (isFunctionLikeDeclaration(node) && isAsyncFunction(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.
TODO: recheck the desired behavior for async generators and add related tests
| async function outer() { | ||
| try { | ||
| return await (Math.random() > 0.5 ? inner() : 5); |
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 should actually produce this:
| return await (Math.random() > 0.5 ? inner() : 5); | |
| return Math.random() > 0.5 ? await inner() : 5; |
The current implementation is a draft and it's not yet fully implemented for more complex cases. The problem with it is that for the code like this:
return Math.random() > 0.5 ? await inner1() : inner2();the compiler shouldn't produce smth like this
return await (Math.random() > 0.5 ? await inner1() : inner2());but rather:
return Math.random() > 0.5 ? await inner1() : await inner2();
Given that async functions flatten returned promises it's a fairly easy mistake to make to assume that
try/catch/finallyenclosing a returned promise can handle that as ifreturn await pwould be written:For that reason, I think, it would be great if TS could suggest adding that "missing"
awaitI'm opening this PR as a draft proposal for this feature. It still requires some work to be finalized but it already shows nicely the idea. I can finish this after receiving positive feedback.
I understand this is on the verge of being a lint rule so I'd be fine with closing this for that reason