- Notifications
You must be signed in to change notification settings - Fork13.2k
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
ghost commentedJan 8, 2018
srolel commentedJan 8, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@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 |
| }); | ||
| functiongetNodeToInsertBefore(sourceFile:SourceFile,pos:number):Node|undefined{//name | ||
| functiongetNodeToInsertBefore(sourceFile:SourceFile,pos:number):Node|undefined{//name |
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.
Whoops, I should have removed the comment before pushing.
ghost commentedJan 9, 2018 • edited by ghost
Loading Uh oh!
There was an error while loading.Please reload this page.
edited by ghost
Uh oh!
There was an error while loading.Please reload this page.
We could also consider changing an explicit return type |
src/compiler/diagnosticMessages.json Outdated
| "category":"Message", | ||
| "code":90028 | ||
| }, | ||
| "Convert to async": { |
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.
nit.Add async modifier to containing function
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.
@DanielRosenwasser any better suggestions for the message?
| if(isVariableDeclaration(containingFunction.parent)&& | ||
| containingFunction.parent.type&& | ||
| isFunctionTypeNode(containingFunction.parent.type)){ | ||
| returncontainingFunction.parent.type.type; |
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.
Also check for a.type immediately on either of these, i.e.function(): number { ... } or(): number => { ... }.
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.
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"){ |
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.
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); |
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.
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.
ghost left a comment• edited by ghost
Loading Uh oh!
There was an error while loading.Please reload this page.
edited by ghost
Uh oh!
There was an error while loading.Please reload this page.
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.
(but note@mhegazy's comment about the message)
Uh oh!
There was an error while loading.Please reload this page.
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:
Becomes:
This happens both in the unit tests, and in manual e2e testing with vscode.
2.
codeFixAwaitInSyncFunction7, the test case of anfor-await-ofloop, fails -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.