fix(40150): use parameter name for a Promise callback function (#40191)

This commit is contained in:
Alex T
2020-09-08 23:44:38 +03:00
committed by GitHub
parent 15084465b7
commit 6101fbca39
3 changed files with 41 additions and 1 deletions

View File

@@ -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);

View File

@@ -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"));
}
`);
});
}

View File

@@ -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");
}