Skip to content

Commit 905578c

Browse files
author
Benjamin Lichtman
committed
Use existing identifier when possible for renaming functions
1 parent 95e5f7d commit 905578c

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/services/codefixes/convertToAsyncFunction.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,11 @@ namespace ts.codefix {
178178
// if the identifier refers to a function we want to add the new synthesized variable for the declaration (ex. blob in let blob = res(arg))
179179
// Note - the choice of the last call signature is arbitrary
180180
if (lastCallSignature && lastCallSignature.parameters.length && !synthNamesMap.has(symbolIdString)) {
181-
const name = lastCallSignature.parameters[0].name;
182-
const synthName = getNewNameIfConflict(createIdentifier(name), allVarNames);
181+
const firstParameter = lastCallSignature.parameters[0];
182+
const ident = isParameter(firstParameter.valueDeclaration) && tryCast(firstParameter.valueDeclaration.name, isIdentifier) || createOptimisticUniqueName("result");
183+
const synthName = getNewNameIfConflict(ident, allVarNames);
183184
synthNamesMap.set(symbolIdString, synthName);
184-
allVarNames.push({ identifier: synthName.identifier, symbol, originalName: name });
185+
allVarNames.push({ identifier: synthName.identifier, symbol, originalName: ident.text });
185186
}
186187
// we only care about identifiers that are parameters and declarations (don't care about other uses)
187188
else if (node.parent && (isParameter(node.parent) || isVariableDeclaration(node.parent))) {
@@ -449,7 +450,7 @@ namespace ts.codefix {
449450

450451
function getLastCallSignature(type: Type, checker: TypeChecker): Signature | undefined {
451452
const callSignatures = checker.getSignaturesOfType(type, SignatureKind.Call);
452-
return callSignatures && callSignatures[callSignatures.length - 1];
453+
return lastOrUndefined(callSignatures);
453454
}
454455

455456

0 commit comments

Comments
 (0)