Skip to content
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 fixAwaitInSyncFunction code fix #21069

Merged
merged 7 commits into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions src/services/codefixes/fixAwaitInSyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,42 @@ namespace ts.codefix {
errorCodes,
getCodeActions(context) {
const { sourceFile, span } = context;
const node = getNodeToInsertBefore(sourceFile, span.start);
if (!node) return undefined;
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, node));
const token = getTokenAtPosition(sourceFile, span.start, /*includeJsDocComment*/ false);
const containingFunction = getContainingFunction(token);
const insertBefore = getNodeToInsertBefore(sourceFile, containingFunction);
const returnType = getReturnTypeNode(containingFunction);
if (!insertBefore) return undefined;
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, insertBefore, returnType));
return [{ description: getLocaleSpecificMessage(Diagnostics.Convert_to_async), changes, fixId }];
},
fixIds: [fixId],
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

Choose a reason for hiding this comment

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

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.

const containingFunction = getContainingFunction(token);
const insertBefore = getNodeToInsertBefore(diag.file, containingFunction);
const returnType = getReturnTypeNode(containingFunction);
if (insertBefore) {
doChange(changes, context.sourceFile, insertBefore, returnType);
}
}),
});

function getNodeToInsertBefore(sourceFile: SourceFile, pos: number): Node | undefined {
const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
const containingFunction = getContainingFunction(token);
function getReturnTypeNode(containingFunction: FunctionLike): TypeNode | undefined {
switch (containingFunction.kind) {
case SyntaxKind.MethodDeclaration:
case SyntaxKind.FunctionDeclaration:
return containingFunction.type;
case SyntaxKind.FunctionExpression:
case SyntaxKind.ArrowFunction:
if (isVariableDeclaration(containingFunction.parent) &&
containingFunction.parent.type &&
isFunctionTypeNode(containingFunction.parent.type)) {
return containingFunction.parent.type.type;
Copy link

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
Contributor Author

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 refactored getReturnType to not use a switch statement at all since with the added condition the two if statements handle all cases.

}
}
}

function getNodeToInsertBefore(sourceFile: SourceFile, containingFunction: FunctionLike): Node | undefined {
switch (containingFunction.kind) {
case SyntaxKind.MethodDeclaration:
return containingFunction.name;
Expand All @@ -35,7 +58,13 @@ namespace ts.codefix {
}
}

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, insertBefore: Node): void {
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

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")

changes.replaceNode(sourceFile, returnType, createTypeReferenceNode("Promise", createNodeArray([returnType])));
}
}
changes.insertModifierBefore(sourceFile, SyntaxKind.AsyncKeyword, insertBefore);
}
}
13 changes: 13 additions & 0 deletions tests/cases/fourslash/codeFixAwaitInSyncFunction10.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />

////const f: () => number | string = () => {
//// await Promise.resolve('foo');
////}

verify.codeFix({
description: "Convert to async",
newFileContent:
`const f: () => Promise<number | string> = async () => {
await Promise.resolve('foo');
}`,
});
14 changes: 14 additions & 0 deletions tests/cases/fourslash/codeFixAwaitInSyncFunction11.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />

////const f: string = () => {
//// await Promise.resolve('foo');
////}

// should not change type if it's incorrectly set
verify.codeFix({
description: "Convert to async",
newFileContent:
`const f: string = async () => {
await Promise.resolve('foo');
}`,
});
13 changes: 13 additions & 0 deletions tests/cases/fourslash/codeFixAwaitInSyncFunction12.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />

////const f: () => Array<number | string> = function() {
//// await Promise.resolve([]);
////}

verify.codeFix({
description: "Convert to async",
newFileContent:
`const f: () => Promise<Array<number | string>> = async function() {
await Promise.resolve([]);
}`,
});
13 changes: 13 additions & 0 deletions tests/cases/fourslash/codeFixAwaitInSyncFunction13.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />

////const f: () => Promise<number | string> = () => {
//// await Promise.resolve('foo');
////}

verify.codeFix({
description: "Convert to async",
newFileContent:
`const f: () => Promise<number | string> = async () => {
await Promise.resolve('foo');
}`,
});
13 changes: 13 additions & 0 deletions tests/cases/fourslash/codeFixAwaitInSyncFunction8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />

////function f(): number | string {
//// await Promise.resolve(8);
////}

verify.codeFix({
description: "Convert to async",
newFileContent:
`async function f(): Promise<number | string> {
await Promise.resolve(8);
}`,
});
17 changes: 17 additions & 0 deletions tests/cases/fourslash/codeFixAwaitInSyncFunction9.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />

////class Foo {
//// bar(): string {
//// await Promise.resolve('baz');
//// }
////}

verify.codeFix({
description: "Convert to async",
newFileContent:
`class Foo {
async bar(): Promise<string> {
await Promise.resolve('baz');
}
}`,
});