Skip to content

Commit f356cd6

Browse files
uniqueiniquityAndy
authored and
Andy
committed
Insert async keyword as last modifier (microsoft#27491)
1 parent 70e26fc commit f356cd6

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

src/services/codefixes/convertToAsyncFunction.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ namespace ts.codefix {
6868
}
6969

7070
// add the async keyword
71-
changes.insertModifierBefore(sourceFile, SyntaxKind.AsyncKeyword, functionToConvert);
71+
changes.insertLastModifierBefore(sourceFile, SyntaxKind.AsyncKeyword, functionToConvert);
7272

7373
function startTransformation(node: CallExpression, nodeToReplace: Node) {
7474
const newNodes = transformExpression(node, transformer, node);

src/services/textChanges.ts

+10
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,16 @@ namespace ts.textChanges {
315315
this.replaceRange(sourceFile, { pos, end: pos }, createToken(modifier), { suffix: " " });
316316
}
317317

318+
public insertLastModifierBefore(sourceFile: SourceFile, modifier: SyntaxKind, before: Node): void {
319+
if (!before.modifiers) {
320+
this.insertModifierBefore(sourceFile, modifier, before);
321+
return;
322+
}
323+
324+
const pos = before.modifiers.end;
325+
this.replaceRange(sourceFile, { pos, end: pos }, createToken(modifier), { prefix: " " });
326+
}
327+
318328
public insertCommentBeforeLine(sourceFile: SourceFile, lineNumber: number, position: number, commentText: string): void {
319329
const lineStartPosition = getStartPositionOfLine(lineNumber, sourceFile);
320330
const startPosition = getFirstNonSpaceCharacterPosition(sourceFile.text, lineStartPosition);

src/testRunner/unittests/convertToAsyncFunction.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,11 @@ function [#|main2|]() {
12541254
.then(() => { console.log("."); return delay(500); })
12551255
.then(() => { console.log("."); return delay(500); })
12561256
}
1257+
`);
1258+
_testConvertToAsyncFunction("convertToAsyncFunction_exportModifier", `
1259+
export function [#|foo|]() {
1260+
return fetch('https://typescriptlang.org').then(s => console.log(s));
1261+
}
12571262
`);
12581263
});
12591264

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ==ORIGINAL==
2+
3+
export function /*[#|*/foo/*|]*/() {
4+
return fetch('https://typescriptlang.org').then(s => console.log(s));
5+
}
6+
7+
// ==ASYNC FUNCTION::Convert to async function==
8+
9+
export async function foo() {
10+
const s = await fetch('https://typescriptlang.org');
11+
return console.log(s);
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ==ORIGINAL==
2+
3+
export function /*[#|*/foo/*|]*/() {
4+
return fetch('https://typescriptlang.org').then(s => console.log(s));
5+
}
6+
7+
// ==ASYNC FUNCTION::Convert to async function==
8+
9+
export async function foo() {
10+
const s = await fetch('https://typescriptlang.org');
11+
return console.log(s);
12+
}

0 commit comments

Comments
 (0)