Skip to content

Improve “Add missing await” fix-all#32922

Merged
andrewbranch merged 3 commits into
microsoft:masterfrom
andrewbranch:enhancement/await-all-improvements
Aug 20, 2019
Merged

Improve “Add missing await” fix-all#32922
andrewbranch merged 3 commits into
microsoft:masterfrom
andrewbranch:enhancement/await-all-improvements

Conversation

@andrewbranch
Copy link
Copy Markdown
Member

I saw @mjbvz’s tweet showing a screen cap of turning on checking for an existing JS file where a Promise value had been misused multiple times throughout the file, and doing the fix-all to add missing awaits. The result was an await added at every use site of the Promise, when it could have been added at the initializer. Someone even pointed out that the resulting code had unnecessarily many awaits in it.

The original codefix operated under the assumption that if a symbol from a variable declaration is referenced more than once, we shouldn’t change its initializer, to avoid a situation where something like

const result = fetch(url);
result.json(); // Error because `result` is a Promise<Response>, not a Response
result.then(() => {}); // Fine

would turn into

const result = await fetch(url);
result.json(); // Fine
result.then(() => {}); // Error because `result` is a Response, not a Promise<Response>

However, the enabling of checking for existing JavaScript shows a great example of why a Promise might be referenced many times, but wrongly every time, in such a way that the initializer can be awaited.

This PR fixes that example, along with a couple other scenarios where we should have been offering a fix for a variable initializer but weren’t.

better-await

@andrewbranch andrewbranch force-pushed the enhancement/await-all-improvements branch from 88d35f8 to 92a8466 Compare August 15, 2019 21:57
Copy link
Copy Markdown
Contributor

@orta orta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good!

!variableName ||
!isInsideAwaitableBody(declaration.initializer)) {
isCompleteFix = false;
continue;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, this is quite the if statement

@andrewbranch andrewbranch force-pushed the enhancement/await-all-improvements branch from c9a9e8f to 1242019 Compare August 20, 2019 20:51
@andrewbranch andrewbranch merged commit 5d04250 into microsoft:master Aug 20, 2019
timsuchanek pushed a commit to timsuchanek/TypeScript that referenced this pull request Sep 11, 2019
* Improve codeFixAll for add missing await

* Improve add missing await for initializers and fix-all

* Fix when only one side of a binary expression can have its initializer fixed
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants