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 4 commits
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
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
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?

"category": "Message",
"code": 90029
},
"Convert function to an ES2015 class": {
"category": "Message",
"code": 95001
Expand Down
41 changes: 41 additions & 0 deletions src/services/codefixes/fixAwaitInSyncFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* @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 = getNodeToInsertBefore(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, getNodeToInsertBefore(diag.file, diag.start!))),
});

function getNodeToInsertBefore(sourceFile: SourceFile, pos: number): Node | undefined {
const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
const containingFunction = getContainingFunction(token);
switch (containingFunction.kind) {
case SyntaxKind.MethodDeclaration:
return containingFunction.name;
case SyntaxKind.FunctionExpression:
case SyntaxKind.FunctionDeclaration:
return findChildOfKind(containingFunction, SyntaxKind.FunctionKeyword, sourceFile);
case SyntaxKind.ArrowFunction:
return findChildOfKind(containingFunction, SyntaxKind.OpenParenToken, sourceFile) || first(containingFunction.parameters);
default:
return undefined;
}
}

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, insertBefore: Node): void {
changes.insertModifierBefore(sourceFile, SyntaxKind.AsyncKeyword, insertBefore);
}
}
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
5 changes: 5 additions & 0 deletions src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ namespace ts.textChanges {
return this.replaceWithSingle(sourceFile, startPosition, startPosition, newNode, this.getOptionsForInsertNodeBefore(before, blankLineBetween));
}

public insertModifierBefore(sourceFile: SourceFile, modifier: SyntaxKind, before: Node): void {
const pos = before.getStart(sourceFile);
this.replaceWithSingle(sourceFile, pos, pos, createToken(modifier), { suffix: " " });
}

public changeIdentifierToPropertyAccess(sourceFile: SourceFile, prefix: string, node: Identifier): void {
const startPosition = getAdjustedStartPosition(sourceFile, node, {}, Position.Start);
this.replaceWithSingle(sourceFile, startPosition, startPosition, createPropertyAccess(createIdentifier(prefix), ""), {});
Expand Down
13 changes: 13 additions & 0 deletions tests/cases/fourslash/codeFixAwaitInSyncFunction1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />

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

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

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

verify.codeFix({
description: "Convert to async",
newFileContent:
`const f = async function() {
await Promise.resolve();
}`,
});
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",
newFileContent:
`class Foo {
async bar() {
await Promise.resolve();
}
}`,
});
13 changes: 13 additions & 0 deletions tests/cases/fourslash/codeFixAwaitInSyncFunction6.5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />

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

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

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

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

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

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

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

verify.codeFixAll({
fixId: "fixAwaitInSyncFunction",
newFileContent:
`async function f() {
await Promise.resolve();
}

const g = async () => {
await f();
}`,
});