FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

add fixAwaitInSyncFunction code fix by srolel · Pull Request #21069 · microsoft/TypeScript · GitHub

add fixAwaitInSyncFunction code fix - #21069

Merged
Mohamed Hegazy (mhegazy) merged 7 commits into
microsoft:masterfrom
srolel:codefix-async
Jan 10, 2018
Merged

add fixAwaitInSyncFunction code fix#21069
Mohamed Hegazy (mhegazy) merged 7 commits into
microsoft:masterfrom
srolel:codefix-async

Conversation

Sharon (Sean) Rolel (srolel) commented Jan 8, 2018
edited
Loading

Copy link
Copy Markdown
Contributor

Fixes #21034

This is pretty straightforward, replacing function expressions/declarations, method declarations and arrow functions with corresponding ones with the async modifier.

There are 2 issues that I need some help with:

1. Replacing the entire node seems to omit the line break at the end of the replaced node. So that this:

class Foo {
    bar() {
        await Promise.resolve();
    }
}

Becomes:

class Foo {
    async bar() {
        await Promise.resolve();
    }}

This happens both in the unit tests, and in manual e2e testing with vscode.

2. codeFixAwaitInSyncFunction7, the test case of an for-await-of loop, fails -

  tests/cases/fourslash/codeFixAwaitInSyncFunction7.ts
         fourslash test codeFixAwaitInSyncFunction7.ts runs correctly:
     Error: Debug Failure. False expression: Token end is child end
      at processChildNode (src\services\formatting\formatting.ts:704:27)
      at src\services\formatting\formatting.ts:629:21
      at visitNode (src\compiler\parser.ts:38:24)
      at Object.forEachChild (src\compiler\parser.ts:285:24)
      at processNode (src\services\formatting\formatting.ts:626:13)
      at processChildNode (src\services\formatting\formatting.ts:712:17)
      at processChildNodes (src\services\formatting\formatting.ts:767:44)
      at src\services\formatting\formatting.ts:632:21
      at visitNodes (src\compiler\parser.ts:44:24)
      at Object.forEachChild (src\compiler\parser.ts:253:24)
      at processNode (src\services\formatting\formatting.ts:626:13)
      at processChildNode (src\services\formatting\formatting.ts:712:17)
      at src\services\formatting\formatting.ts:629:21
      at visitNode (src\compiler\parser.ts:38:24)
      at Object.forEachChild (src\compiler\parser.ts:160:21)
      at processNode (src\services\formatting\formatting.ts:626:13)
      at formatSpanWorker (src\services\formatting\formatting.ts:416:13)
      at src\services\formatting\formatting.ts:346:108
      at Object.getFormattingScanner (src\services\formatting\formattingScanner.ts:41:21)

I tried to debug this for a while, but nothing conclusive. This does not happen when I use the compiled tsserver.js in vscode to test the code fix.

Copy link
Copy Markdown

Hi MOSHOOD OJUKO (@mosho1), I've created a branch at 1319519 that would just add the keyword to the front of the function instead of replacing the node. That seems to fix the formatting problems since we're only inserting text now.

Sharon (Sean) Rolel (srolel) commented Jan 8, 2018
edited
Loading

Copy link
Copy Markdown
Contributor Author

Andy (Andrewkraft) (@Andy-MS) Thanks, that is a lot better. I picked only the commit with the change, let me know if I should do it differently.

I'm also wondering about those CRs in the tests, I saw them in some tests but not in others. Are they also an outcome of replaceNode or something to do with my editor?

});

function getNodeToInsertBefore(sourceFile: SourceFile, pos: number): Node | undefined {//name
function getNodeToInsertBefore(sourceFile: SourceFile, pos: number): Node | undefined {// name

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Whoops, I should have removed the comment before pushing.

Deleted user (ghost) commented Jan 9, 2018
edited by ghost
Loading

Copy link
Copy Markdown

We could also consider changing an explicit return type T to Promise<T> (if it's not already a promise).

Comment thread src/compiler/diagnosticMessages.json Outdated
"category": "Message",
"code": 90028
},
"Convert to async": {

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

nit. Add async modifier to containing function

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Daniel Rosenwasser (@DanielRosenwasser) any better suggestions for the message?

if (isVariableDeclaration(containingFunction.parent) &&
containingFunction.parent.type &&
isFunctionTypeNode(containingFunction.parent.type)) {
return containingFunction.parent.type.type;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Also check for a .type immediately on either of these, i.e. function(): number { ... } or (): number => { ... }.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

After adding this change I refactored getReturnType to not use a switch statement at all since with the added condition the two if statements handle all cases.

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, insertBefore: Node, returnType: TypeNode | undefined): void {
if (returnType) {
const entityName = getEntityNameFromTypeNode(returnType);
if (!entityName || entityName.getText() !== "Promise") {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

if (!entityName || entityName.kind !== SyntaxKind.Identifier || entityName.text !== "Promise")

getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) =>
doChange(changes, context.sourceFile, getNodeToInsertBefore(diag.file, diag.start!))),
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
const token = getTokenAtPosition(diag.file, diag.start!, /*includeJsDocComment*/ false);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Use a function getNodes(...): { insertBefore: Node, returnType: Type | undefined } | undefined to avoid repeating this. That would also let you combine the two switch statements from getNodeToInsertBefore and getReturnTypeNode.

Deleted user (ghost) left a comment
edited by ghost
Loading

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

(but note Mohamed Hegazy (@mhegazy)'s comment about the message)

Mohamed Hegazy (mhegazy) merged commit c0bdd12 into microsoft:master Jan 10, 2018
Microsoft (microsoft) locked and limited conversation to collaborators Jul 3, 2018
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

Quick fix at await to make current function async

2 participants


Back | FazBrowse Home | New Git URL