Skip to content

fix(40042): Convert to async function with decorator #40050

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
merged 1 commit into from
Nov 2, 2020
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
8 changes: 5 additions & 3 deletions src/services/codefixes/convertToAsyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ namespace ts.codefix {
const functionToConvertRenamed = renameCollidingVarNames(functionToConvert, checker, synthNamesMap, context.sourceFile);
const returnStatements = functionToConvertRenamed.body && isBlock(functionToConvertRenamed.body) ? getReturnStatementsWithPromiseHandlers(functionToConvertRenamed.body) : emptyArray;
const transformer: Transformer = { checker, synthNamesMap, setOfExpressionsToReturn, isInJSFile: isInJavascript };

if (!returnStatements.length) {
return;
}

// add the async keyword
changes.insertLastModifierBefore(sourceFile, SyntaxKind.AsyncKeyword, functionToConvert);
const pos = functionToConvert.modifiers ? functionToConvert.modifiers.end :
functionToConvert.decorators ? skipTrivia(sourceFile.text, functionToConvert.decorators.end) :
functionToConvert.getStart(sourceFile);
const options = functionToConvert.modifiers ? { prefix: " " } : { suffix: " " };
changes.insertModifierAt(sourceFile, pos, SyntaxKind.AsyncKeyword, options);

for (const returnStatement of returnStatements) {
forEachChild(returnStatement, function visit(node) {
Expand Down
15 changes: 4 additions & 11 deletions src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,19 +388,12 @@ namespace ts.textChanges {
this.insertNodeAt(sourceFile, getAdjustedStartPosition(sourceFile, before, options), newNode, this.getOptionsForInsertNodeBefore(before, newNode, blankLineBetween));
}

public insertModifierBefore(sourceFile: SourceFile, modifier: SyntaxKind, before: Node): void {
const pos = before.getStart(sourceFile);
this.insertNodeAt(sourceFile, pos, factory.createToken(modifier), { suffix: " " });
public insertModifierAt(sourceFile: SourceFile, pos: number, modifier: SyntaxKind, options: InsertNodeOptions = {}): void {
this.insertNodeAt(sourceFile, pos, factory.createToken(modifier), options);
}

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

const pos = before.modifiers.end;
this.insertNodeAt(sourceFile, pos, factory.createToken(modifier), { prefix: " " });
public insertModifierBefore(sourceFile: SourceFile, modifier: SyntaxKind, before: Node): void {
return this.insertModifierAt(sourceFile, before.getStart(sourceFile), modifier, { suffix: " " });
}

public insertCommentBeforeLine(sourceFile: SourceFile, lineNumber: number, position: number, commentText: string): void {
Expand Down
51 changes: 51 additions & 0 deletions src/testRunner/unittests/services/convertToAsyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,57 @@ function [#|foo|]() {
return fetch('b').then(() => 'c');
})
}
`);
_testConvertToAsyncFunction("convertToAsyncFunction_decoratedMethod", `
function decorator() {
return (target: any, key: any, descriptor: PropertyDescriptor) => descriptor;
}
class Foo {
@decorator()
[#|method|]() {
return fetch('a').then(x => x);
}
}
`);

_testConvertToAsyncFunction("convertToAsyncFunction_decoratedMethodWithSingleLineComment", `
function decorator() {
return (target: any, key: any, descriptor: PropertyDescriptor) => descriptor;
}
class Foo {
@decorator()
// comment
[#|method|]() {
return fetch('a').then(x => x);
}
}
`);

_testConvertToAsyncFunction("convertToAsyncFunction_decoratedMethodWithMultipleLineComment", `
function decorator() {
return (target: any, key: any, descriptor: PropertyDescriptor) => descriptor;
}
class Foo {
@decorator()
/**
* comment
*/
[#|method|]() {
return fetch('a').then(x => x);
}
}
`);

_testConvertToAsyncFunction("convertToAsyncFunction_decoratedMethodWithModifier", `
function decorator() {
return (target: any, key: any, descriptor: PropertyDescriptor) => descriptor;
}
class Foo {
@decorator()
public [#|method|]() {
return fetch('a').then(x => x);
}
}
`);

_testConvertToAsyncFunctionFailedSuggestion("convertToAsyncFunction_OutermostOnlyFailure", `
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ==ORIGINAL==

function decorator() {
return (target: any, key: any, descriptor: PropertyDescriptor) => descriptor;
}
class Foo {
@decorator()
/*[#|*/method/*|]*/() {
return fetch('a').then(x => x);
}
}

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

function decorator() {
return (target: any, key: any, descriptor: PropertyDescriptor) => descriptor;
}
class Foo {
@decorator()
async method() {
const x = await fetch('a');
return x;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ==ORIGINAL==

function decorator() {
return (target: any, key: any, descriptor: PropertyDescriptor) => descriptor;
}
class Foo {
@decorator()
public /*[#|*/method/*|]*/() {
return fetch('a').then(x => x);
}
}

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

function decorator() {
return (target: any, key: any, descriptor: PropertyDescriptor) => descriptor;
}
class Foo {
@decorator()
public async method() {
const x = await fetch('a');
return x;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// ==ORIGINAL==

function decorator() {
return (target: any, key: any, descriptor: PropertyDescriptor) => descriptor;
}
class Foo {
@decorator()
/**
* comment
*/
/*[#|*/method/*|]*/() {
return fetch('a').then(x => x);
}
}

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

function decorator() {
return (target: any, key: any, descriptor: PropertyDescriptor) => descriptor;
}
class Foo {
@decorator()
/**
* comment
*/
async method() {
const x = await fetch('a');
return x;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ==ORIGINAL==

function decorator() {
return (target: any, key: any, descriptor: PropertyDescriptor) => descriptor;
}
class Foo {
@decorator()
// comment
/*[#|*/method/*|]*/() {
return fetch('a').then(x => x);
}
}

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

function decorator() {
return (target: any, key: any, descriptor: PropertyDescriptor) => descriptor;
}
class Foo {
@decorator()
// comment
async method() {
const x = await fetch('a');
return x;
}
}