Skip to content

Commit 5dfb0c5

Browse files
Andy Hansonsrolel
Andy Hanson
authored andcommitted
Just insert the keyword
1 parent 626f399 commit 5dfb0c5

10 files changed

+52
-48
lines changed

src/compiler/diagnosticMessages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3845,7 +3845,7 @@
38453845
},
38463846
"Convert to async": {
38473847
"category": "Message",
3848-
"code": 90028
3848+
"code": 90029
38493849
},
38503850
"Convert function to an ES2015 class": {
38513851
"category": "Message",

src/services/codefixes/fixAwaitInSyncFunction.ts

+13-24
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,33 @@ namespace ts.codefix {
99
errorCodes,
1010
getCodeActions(context) {
1111
const { sourceFile, span } = context;
12-
const node = getNode(sourceFile, span.start);
12+
const node = getNodeToInsertBefore(sourceFile, span.start);
1313
if (!node) return undefined;
1414
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, node));
1515
return [{ description: getLocaleSpecificMessage(Diagnostics.Convert_to_async), changes, fixId }];
1616
},
1717
fixIds: [fixId],
1818
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) =>
19-
doChange(changes, context.sourceFile, getNode(diag.file, diag.start!))),
19+
doChange(changes, context.sourceFile, getNodeToInsertBefore(diag.file, diag.start!))),
2020
});
2121

22-
function getNode(sourceFile: SourceFile, pos: number): FunctionLikeDeclaration {
22+
function getNodeToInsertBefore(sourceFile: SourceFile, pos: number): Node | undefined {//name
2323
const token = getTokenAtPosition(sourceFile, pos, /*includeJsDocComment*/ false);
2424
const containingFunction = getContainingFunction(token);
25-
if (!isFunctionLikeDeclaration(containingFunction) ||
26-
isConstructorDeclaration(containingFunction) ||
27-
isGetAccessorDeclaration(containingFunction) ||
28-
isSetAccessorDeclaration(containingFunction)) return;
29-
return containingFunction;
30-
}
31-
32-
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, decl: FunctionLikeDeclaration) {
33-
const asyncToken = createToken(SyntaxKind.AsyncKeyword);
34-
const modifiers = decl.modifiers ? decl.modifiers.concat(asyncToken) : createNodeArray([asyncToken]);
35-
let changed;
36-
switch (decl.kind) {
25+
switch (containingFunction.kind) {
3726
case SyntaxKind.MethodDeclaration:
38-
changed = createMethod(decl.decorators, modifiers, decl.asteriskToken, decl.name, decl.questionToken, decl.typeParameters, decl.parameters, decl.type, decl.body);
39-
break;
27+
return containingFunction.name;
4028
case SyntaxKind.FunctionExpression:
41-
changed = createFunctionExpression(modifiers, decl.asteriskToken, decl.name, decl.typeParameters, decl.parameters, decl.type, decl.body);
42-
break;
4329
case SyntaxKind.FunctionDeclaration:
44-
changed = createFunctionDeclaration(decl.decorators, modifiers, decl.asteriskToken, decl.name, decl.typeParameters, decl.parameters, decl.type, decl.body);
45-
break;
30+
return findChildOfKind(containingFunction, SyntaxKind.FunctionKeyword, sourceFile);
4631
case SyntaxKind.ArrowFunction:
47-
changed = createArrowFunction(modifiers, decl.typeParameters, decl.parameters, decl.type, decl.equalsGreaterThanToken, decl.body);
48-
break;
32+
return findChildOfKind(containingFunction, SyntaxKind.OpenParenToken, sourceFile) || first(containingFunction.parameters);
33+
default:
34+
return undefined;
4935
}
50-
changes.replaceNode(sourceFile, decl, changed);
36+
}
37+
38+
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, insertBefore: Node): void {
39+
changes.insertModifierBefore(sourceFile, SyntaxKind.AsyncKeyword, insertBefore);
5140
}
5241
}

src/services/textChanges.ts

+5
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ namespace ts.textChanges {
345345
return this.replaceWithSingle(sourceFile, startPosition, startPosition, newNode, this.getOptionsForInsertNodeBefore(before, blankLineBetween));
346346
}
347347

348+
public insertModifierBefore(sourceFile: SourceFile, modifier: SyntaxKind, before: Node): void {
349+
const pos = before.getStart(sourceFile);
350+
this.replaceWithSingle(sourceFile, pos, pos, createToken(modifier), { suffix: " " });
351+
}
352+
348353
public changeIdentifierToPropertyAccess(sourceFile: SourceFile, prefix: string, node: Identifier): void {
349354
const startPosition = getAdjustedStartPosition(sourceFile, node, {}, Position.Start);
350355
this.replaceWithSingle(sourceFile, startPosition, startPosition, createPropertyAccess(createIdentifier(prefix), ""), {});

tests/cases/fourslash/codeFixAwaitInSyncFunction1.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

77
verify.codeFix({
88
description: "Convert to async",
9-
index: 0,
109
newFileContent:
11-
`async function f() {\r
12-
await Promise.resolve();\r
10+
`async function f() {
11+
await Promise.resolve();
1312
}`,
1413
});

tests/cases/fourslash/codeFixAwaitInSyncFunction2.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77

88
verify.codeFix({
99
description: "Convert to async",
10-
index: 0,
1110
newFileContent:
12-
`const f = async function() {\r
13-
await Promise.resolve();\r
14-
await Promise.resolve();\r
11+
`const f = async function() {
12+
await Promise.resolve();
13+
await Promise.resolve();
1514
}`,
1615
});

tests/cases/fourslash/codeFixAwaitInSyncFunction5.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
verify.codeFix({
1010
description: "Convert to async",
11-
index: 0,
1211
newFileContent:
1312
`class Foo {
14-
async bar() {\r
15-
await Promise.resolve();\r
16-
}}`,
13+
async bar() {
14+
await Promise.resolve();
15+
}
16+
}`,
1717
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////const f = promise => {
4+
//// await promise;
5+
////}
6+
7+
verify.codeFix({
8+
description: "Convert to async",
9+
newFileContent:
10+
`const f = async promise => {
11+
await promise;
12+
}`,
13+
});

tests/cases/fourslash/codeFixAwaitInSyncFunction6.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

77
verify.codeFix({
88
description: "Convert to async",
9-
index: 0,
109
newFileContent:
11-
`const f = async (promise) => {\r
12-
await promise;\r
10+
`const f = async (promise) => {
11+
await promise;
1312
}`,
1413
});

tests/cases/fourslash/codeFixAwaitInSyncFunction7.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88

99
verify.codeFix({
1010
description: "Convert to async",
11-
index: 0,
1211
newFileContent:
13-
`function f() => {\r
14-
for await (const x of g()) {\r
15-
console.log(x);\r
16-
}\r
12+
`async function f() {
13+
for await (const x of g()) {
14+
console.log(x);
15+
}
1716
}`,
1817
});

tests/cases/fourslash/codeFixAwaitInSyncFunction_all.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
verify.codeFixAll({
1212
fixId: "fixAwaitInSyncFunction",
1313
newFileContent:
14-
`async function f() {\r
15-
await Promise.resolve();\r
14+
`async function f() {
15+
await Promise.resolve();
1616
}
17-
const g = async () => {\r
18-
await f();\r
17+
18+
const g = async () => {
19+
await f();
1920
}`,
2021
});

0 commit comments

Comments
 (0)