Skip to content

Commit 6101fbc

Browse files
authored
fix(40150): use parameter name for a Promise callback function (#40191)
1 parent 1508446 commit 6101fbc

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/services/codefixes/convertToAsyncFunction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ namespace ts.codefix {
171171
// will eventually become
172172
// const response = await fetch('...')
173173
// so we push an entry for 'response'.
174-
if (lastCallSignature && !isFunctionLikeDeclaration(node.parent) && !synthNamesMap.has(symbolIdString)) {
174+
if (lastCallSignature && !isParameter(node.parent) && !isFunctionLikeDeclaration(node.parent) && !synthNamesMap.has(symbolIdString)) {
175175
const firstParameter = firstOrUndefined(lastCallSignature.parameters);
176176
const ident = firstParameter && isParameter(firstParameter.valueDeclaration) && tryCast(firstParameter.valueDeclaration.name, isIdentifier) || factory.createUniqueName("result", GeneratedIdentifierFlags.Optimistic);
177177
const synthName = getNewNameIfConflict(ident, collidingSymbolMap);

src/testRunner/unittests/services/convertToAsyncFunction.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,5 +1441,19 @@ function [#|get|]() {
14411441
function [#|f|]() {
14421442
return Promise.resolve().then(undefined, undefined, () => 1);
14431443
}`);
1444+
1445+
_testConvertToAsyncFunction("convertToAsyncFunction_callbackArgument", `
1446+
function foo(props: any): void {
1447+
return props;
1448+
}
1449+
1450+
const fn = (): Promise<(message: string) => void> =>
1451+
new Promise(resolve => resolve((message: string) => foo(message)));
1452+
1453+
function [#|f|]() {
1454+
return fn().then(res => res("test"));
1455+
}
1456+
`);
1457+
14441458
});
14451459
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// ==ORIGINAL==
2+
3+
function foo(props: any): void {
4+
return props;
5+
}
6+
7+
const fn = (): Promise<(message: string) => void> =>
8+
new Promise(resolve => resolve((message: string) => foo(message)));
9+
10+
function /*[#|*/f/*|]*/() {
11+
return fn().then(res => res("test"));
12+
}
13+
14+
// ==ASYNC FUNCTION::Convert to async function==
15+
16+
function foo(props: any): void {
17+
return props;
18+
}
19+
20+
const fn = (): Promise<(message: string) => void> =>
21+
new Promise(resolve => resolve((message: string) => foo(message)));
22+
23+
async function f() {
24+
const res = await fn();
25+
return res("test");
26+
}

0 commit comments

Comments
 (0)