diff --git a/src/services/codefixes/convertToAsyncFunction.ts b/src/services/codefixes/convertToAsyncFunction.ts index a01d3613198f0..6474c84df9b08 100644 --- a/src/services/codefixes/convertToAsyncFunction.ts +++ b/src/services/codefixes/convertToAsyncFunction.ts @@ -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); diff --git a/src/testRunner/unittests/services/convertToAsyncFunction.ts b/src/testRunner/unittests/services/convertToAsyncFunction.ts index 78a2f43f6c3d6..952f6bf4d68e7 100644 --- a/src/testRunner/unittests/services/convertToAsyncFunction.ts +++ b/src/testRunner/unittests/services/convertToAsyncFunction.ts @@ -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")); +} +`); + }); } diff --git a/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackArgument.ts b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackArgument.ts new file mode 100644 index 0000000000000..7f46b8f564a8a --- /dev/null +++ b/tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_callbackArgument.ts @@ -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"); +}