Skip to content

fix(40150): "Convert to async function" refactor generates invalid code with a function returning Promise<function> #40191

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
Sep 8, 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
2 changes: 1 addition & 1 deletion src/services/codefixes/convertToAsyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ namespace ts.codefix {
// will eventually become
// const response = await fetch('...')
// so we push an entry for 'response'.
if (lastCallSignature && !isFunctionLikeDeclaration(node.parent) && !synthNamesMap.has(symbolIdString)) {
if (lastCallSignature && !isParameter(node.parent) && !isFunctionLikeDeclaration(node.parent) && !synthNamesMap.has(symbolIdString)) {
const firstParameter = firstOrUndefined(lastCallSignature.parameters);
const ident = firstParameter && isParameter(firstParameter.valueDeclaration) && tryCast(firstParameter.valueDeclaration.name, isIdentifier) || factory.createUniqueName("result", GeneratedIdentifierFlags.Optimistic);
const synthName = getNewNameIfConflict(ident, collidingSymbolMap);
Expand Down
14 changes: 14 additions & 0 deletions src/testRunner/unittests/services/convertToAsyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1441,5 +1441,19 @@ function [#|get|]() {
function [#|f|]() {
return Promise.resolve().then(undefined, undefined, () => 1);
}`);

_testConvertToAsyncFunction("convertToAsyncFunction_callbackArgument", `
function foo(props: any): void {
return props;
}

const fn = (): Promise<(message: string) => void> =>
new Promise(resolve => resolve((message: string) => foo(message)));

function [#|f|]() {
return fn().then(res => res("test"));
}
`);

});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ==ORIGINAL==

function foo(props: any): void {
return props;
}

const fn = (): Promise<(message: string) => void> =>
new Promise(resolve => resolve((message: string) => foo(message)));

function /*[#|*/f/*|]*/() {
return fn().then(res => res("test"));
}

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

function foo(props: any): void {
return props;
}

const fn = (): Promise<(message: string) => void> =>
new Promise(resolve => resolve((message: string) => foo(message)));

async function f() {
const res = await fn();
return res("test");
}