mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 04:43:37 -05:00
fix(40150): use parameter name for a Promise callback function (#40191)
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
`);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
Reference in New Issue
Block a user