-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Add quick fix to add missing 'await' #32356
Add quick fix to add missing 'await' #32356
Conversation
87e017e
to
355469e
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.
Code looks good, but it looks like this fix shows for any incorrect addition, like number + HTMLElement
, and then proceeds to do nothing when invoked. Did you think about making specific errors for +
et al when one of the sides is a Promise? That would make the codefix suggestion quite a bit more accurate. What do you think?
const errorCodes = [ | ||
Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_bigint_or_an_enum_type.code, | ||
Diagnostics.The_left_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type.code, | ||
Diagnostics.The_right_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_bigint_or_an_enum_type.code, |
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 will show for any old type combination, like number + Event
, right? What happens in that case? Does it just fail to do anything when you try to "add missing 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.
It's supposed to be filtered out further in getCodeActions
(it doesn’t show anything if you return undefined / empty array in there). This is back to the tradeoffs we were discussing about using related info as a “bonus error message” vs. copying and pasting all these into new, unique messages. I wrote this down as a larger LS design/API change to consider at some later point.
Hum, yes, I did think about it, but it should be handled by |
Co-Authored-By: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
Ah, I thought you were saying you observed this happening, but I think you were just looking at the error codes? I believe it’s working correctly, but I just pushed a test for that negative case specifically. It’s a good thing to assert in the tests. I gave some more context about what’s happening and why I took this approach in the inline comments, but TL;DR, the error codes are necessary but not sufficient to surface a quick fix. Once |
Nope, I didn't run it, just read the code. I think I was thinking of the refactor API instead of the codefix API? IIRC the refactor API is the one with two calls, and the first one has to be fast because it runs a lot. |
Fixes #30646
Most of this PR is not terribly interesting, so let me point out the interesting parts:
await
inside a non-async function, we’ll immediately surface a subsequent code fix to addasync
to the function, which I predict will be a common and convenient sequence. So right now, I’ve limited the quick fix to appearing when theawait
would go inside a function/arrow/method body (something that can takeasync
).await
to the initializer of the variable at the declaration site. The second is the standard insertion ofawait
at the use site, as is available everywhere else. When applying “Fix all expressions possibly missing 'await',” the initializer fix is preferred if available.