Skip to content
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
Next Next commit
add fixAwaitInSyncFunction code fix
  • Loading branch information
srolel committed Jan 8, 2018
commit 626f3991e7d2ba3db03304c7033728c948d59c3d
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3843,6 +3843,10 @@
"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.

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.

@DanielRosenwasser any better suggestions for the message?

"category": "Message",
"code": 90028
},
"Convert function to an ES2015 class": {
"category": "Message",
"code": 95001
Expand Down
52 changes: 52 additions & 0 deletions src/services/codefixes/fixAwaitInSyncFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* @internal */
namespace ts.codefix {
const fixId = "fixAwaitInSyncFunction";
const errorCodes = [
Diagnostics.await_expression_is_only_allowed_within_an_async_function.code,
Diagnostics.A_for_await_of_statement_is_only_allowed_within_an_async_function_or_async_generator.code,
];
registerCodeFix({
errorCodes,
getCodeActions(context) {
const { sourceFile, span } = context;
const node = getNode(sourceFile, span.start);
if (!node) return undefined;
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, node));
return [{ description: getLocaleSpecificMessage(Diagnostics.Convert_to_async), changes, fixId }];
},
fixIds: [fixId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) =>
doChange(changes, context.sourceFile, getNode(diag.file, diag.start!))),
});

function getNode(sourceFile: SourceFile, pos: number): FunctionLikeDeclaration {
const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
const containingFunction = getContainingFunction(token);
if (!isFunctionLikeDeclaration(containingFunction) ||
isConstructorDeclaration(containingFunction) ||
isGetAccessorDeclaration(containingFunction) ||
isSetAccessorDeclaration(containingFunction)) return;
return containingFunction;
}

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, decl: FunctionLikeDeclaration) {
const asyncToken = createToken(SyntaxKind.AsyncKeyword);
const modifiers = decl.modifiers ? decl.modifiers.concat(asyncToken) : createNodeArray([asyncToken]);
let changed;
switch (decl.kind) {
case SyntaxKind.MethodDeclaration:
changed = createMethod(decl.decorators, modifiers, decl.asteriskToken, decl.name, decl.questionToken, decl.typeParameters, decl.parameters, decl.type, decl.body);
break;
case SyntaxKind.FunctionExpression:
changed = createFunctionExpression(modifiers, decl.asteriskToken, decl.name, decl.typeParameters, decl.parameters, decl.type, decl.body);
break;
case SyntaxKind.FunctionDeclaration:
changed = createFunctionDeclaration(decl.decorators, modifiers, decl.asteriskToken, decl.name, decl.typeParameters, decl.parameters, decl.type, decl.body);
break;
case SyntaxKind.ArrowFunction:
changed = createArrowFunction(modifiers, decl.typeParameters, decl.parameters, decl.type, decl.equalsGreaterThanToken, decl.body);
break;
}
changes.replaceNode(sourceFile, decl, changed);
}
}
1 change: 1 addition & 0 deletions src/services/codefixes/fixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/// <reference path="fixForgottenThisPropertyAccess.ts" />
/// <reference path='fixUnusedIdentifier.ts' />
/// <reference path='fixJSDocTypes.ts' />
/// <reference path='fixAwaitInSyncFunction.ts' />
/// <reference path='importFixes.ts' />
/// <reference path='disableJsDiagnostics.ts' />
/// <reference path='helpers.ts' />
Expand Down
14 changes: 14 additions & 0 deletions tests/cases/fourslash/codeFixAwaitInSyncFunction1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />

////function f() {
//// await Promise.resolve();
////}

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

////const f = function() {
//// await Promise.resolve();
//// await Promise.resolve();
////}

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

////const f = {
//// get a() {
//// return await Promise.resolve();
//// },
//// get a() {
//// await Promise.resolve();
//// },
////}

verify.not.codeFixAvailable();
9 changes: 9 additions & 0 deletions tests/cases/fourslash/codeFixAwaitInSyncFunction4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path='fourslash.ts' />

////class Foo {
//// constructor {
//// await Promise.resolve();
//// }
////}

verify.not.codeFixAvailable();
17 changes: 17 additions & 0 deletions tests/cases/fourslash/codeFixAwaitInSyncFunction5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />

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

verify.codeFix({
description: "Convert to async",
index: 0,
newFileContent:
`class Foo {
async bar() {\r
await Promise.resolve();\r
}}`,
});
14 changes: 14 additions & 0 deletions tests/cases/fourslash/codeFixAwaitInSyncFunction6.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />

////const f = (promise) => {
//// await promise;
////}

verify.codeFix({
description: "Convert to async",
index: 0,
newFileContent:
`const f = async (promise) => {\r
await promise;\r
}`,
});
18 changes: 18 additions & 0 deletions tests/cases/fourslash/codeFixAwaitInSyncFunction7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference path='fourslash.ts' />

////function f() {
//// for await (const x of g()) {
//// console.log(x);
//// }
////}

verify.codeFix({
description: "Convert to async",
index: 0,
newFileContent:
`function f() => {\r
for await (const x of g()) {\r
console.log(x);\r
}\r
}`,
});
20 changes: 20 additions & 0 deletions tests/cases/fourslash/codeFixAwaitInSyncFunction_all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference path='fourslash.ts' />

////function f() {
//// await Promise.resolve();
////}
////
////const g = () => {
//// await f();
////}

verify.codeFixAll({
fixId: "fixAwaitInSyncFunction",
newFileContent:
`async function f() {\r
await Promise.resolve();\r
}
const g = async () => {\r
await f();\r
}`,
});