From f356cd6c8956ed274b2010b3003d237e0762816b Mon Sep 17 00:00:00 2001 From: Benjamin Lichtman Date: Mon, 1 Oct 2018 17:43:17 -0700 Subject: [PATCH] Insert async keyword as last modifier (#27491) --- src/services/codefixes/convertToAsyncFunction.ts | 2 +- src/services/textChanges.ts | 10 ++++++++++ src/testRunner/unittests/convertToAsyncFunction.ts | 5 +++++ .../convertToAsyncFunction_exportModifier.js | 12 ++++++++++++ .../convertToAsyncFunction_exportModifier.ts | 12 ++++++++++++ 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_exportModifier.js create mode 100644 tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_exportModifier.ts diff --git a/src/services/codefixes/convertToAsyncFunction.ts b/src/services/codefixes/convertToAsyncFunction.ts index fbaf0178d2..c02609e890 100644 --- a/src/services/codefixes/convertToAsyncFunction.ts +++ b/src/services/codefixes/convertToAsyncFunction.ts @@ -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); diff --git a/src/services/textChanges.ts b/src/services/textChanges.ts index 756d2799a2..3b6da401db 100644 --- a/src/services/textChanges.ts +++ b/src/services/textChanges.ts @@ -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); diff --git a/src/testRunner/unittests/convertToAsyncFunction.ts b/src/testRunner/unittests/convertToAsyncFunction.ts index 162b357847..3431603685 100644 --- a/src/testRunner/unittests/convertToAsyncFunction.ts +++ b/src/testRunner/unittests/convertToAsyncFunction.ts @@ -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)); +} `); }); diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_exportModifier.js b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_exportModifier.js new file mode 100644 index 0000000000..5ae3876f9e --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_exportModifier.js @@ -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); +} diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_exportModifier.ts b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_exportModifier.ts new file mode 100644 index 0000000000..5ae3876f9e --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_exportModifier.ts @@ -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); +}