Skip to content

Insert async keyword as last modifier in async refactor #27491

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

Merged
1 commit merged into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/services/codefixes/convertToAsyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace ts.codefix {
}

// add the async keyword
changes.insertModifierBefore(sourceFile, SyntaxKind.AsyncKeyword, functionToConvert);
changes.insertLastModifierBefore(sourceFile, SyntaxKind.AsyncKeyword, functionToConvert);

function startTransformation(node: CallExpression, nodeToReplace: Node) {
const newNodes = transformExpression(node, transformer, node);
Expand Down
10 changes: 10 additions & 0 deletions src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,16 @@ namespace ts.textChanges {
this.replaceRange(sourceFile, { pos, end: pos }, createToken(modifier), { suffix: " " });
}

public insertLastModifierBefore(sourceFile: SourceFile, modifier: SyntaxKind, before: Node): void {
if (!before.modifiers) {
this.insertModifierBefore(sourceFile, modifier, before);
return;
}

const pos = before.modifiers.end;
this.replaceRange(sourceFile, { pos, end: pos }, createToken(modifier), { prefix: " " });
}

public insertCommentBeforeLine(sourceFile: SourceFile, lineNumber: number, position: number, commentText: string): void {
const lineStartPosition = getStartPositionOfLine(lineNumber, sourceFile);
const startPosition = getFirstNonSpaceCharacterPosition(sourceFile.text, lineStartPosition);
Expand Down
5 changes: 5 additions & 0 deletions src/testRunner/unittests/convertToAsyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,11 @@ function [#|main2|]() {
.then(() => { console.log("."); return delay(500); })
.then(() => { console.log("."); return delay(500); })
}
`);
_testConvertToAsyncFunction("convertToAsyncFunction_exportModifier", `
export function [#|foo|]() {
return fetch('https://typescriptlang.org').then(s => console.log(s));
}
`);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// ==ORIGINAL==

export function /*[#|*/foo/*|]*/() {
return fetch('https://typescriptlang.org').then(s => console.log(s));
}

// ==ASYNC FUNCTION::Convert to async function==

export async function foo() {
const s = await fetch('https://typescriptlang.org');
return console.log(s);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// ==ORIGINAL==

export function /*[#|*/foo/*|]*/() {
return fetch('https://typescriptlang.org').then(s => console.log(s));
}

// ==ASYNC FUNCTION::Convert to async function==

export async function foo() {
const s = await fetch('https://typescriptlang.org');
return console.log(s);
}