Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

add fixAwaitInSyncFunction code fix#21069

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mhegazy merged 7 commits intomicrosoft:masterfromsrolel:codefix-async
Jan 10, 2018

Conversation

@srolel
Copy link
Contributor

@srolelsrolel commentedJan 8, 2018
edited
Loading

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 anfor-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.

@ghost
Copy link

Hi@mosho1, I've created a branch at1319519 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.

@srolel
Copy link
ContributorAuthor

srolel commentedJan 8, 2018
edited
Loading

@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 ofreplaceNode or something to do with my editor?

});

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

Choose a reason for hiding this comment

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

Whoops, I should have removed the comment before pushing.

@ghost
Copy link

ghost commentedJan 9, 2018
edited by ghost
Loading

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

srolel reacted with thumbs up emoji

"category":"Message",
"code":90028
},
"Convert to async": {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit.Add async modifier to containing function

Copy link
Contributor

Choose a reason for hiding this comment

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

@DanielRosenwasser any better suggestions for the message?

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

Choose a reason for hiding this comment

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

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

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

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

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

Choose a reason for hiding this comment

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

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)=>{
consttoken=getTokenAtPosition(diag.file,diag.start!,/*includeJsDocComment*/false);

Choose a reason for hiding this comment

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

Use a functiongetNodes(...): { insertBefore: Node, returnType: Type | undefined } | undefined to avoid repeating this. That would also let you combine the two switch statements fromgetNodeToInsertBefore andgetReturnTypeNode.

Copy link

@ghostghost left a comment
edited by ghost
Loading

Choose a reason for hiding this comment

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

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

@mhegazymhegazy merged commitc0bdd12 intomicrosoft:masterJan 10, 2018
@microsoftmicrosoft locked and limited conversation to collaboratorsJul 3, 2018
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

2 more reviewers

@mhegazymhegazymhegazy left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Quick fix at await to make current function async

2 participants

@srolel@mhegazy

[8]ページ先頭

©2009-2025 Movatter.jp