Skip to content

Commit 2187ba1

Browse files
authored
Fix variable name collisions (#37761)
1 parent 527f467 commit 2187ba1

File tree

5 files changed

+35
-24
lines changed

5 files changed

+35
-24
lines changed

src/services/codefixes/convertToAsyncFunction.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ namespace ts.codefix {
3636
hasBeenDeclared: boolean;
3737
}
3838

39-
interface SymbolAndIdentifier {
40-
readonly identifier: Identifier;
41-
readonly symbol: Symbol;
42-
}
43-
4439
interface Transformer {
4540
readonly checker: TypeChecker;
4641
readonly synthNamesMap: Map<SynthIdentifier>; // keys are the symbol id of the identifier
@@ -139,12 +134,6 @@ namespace ts.codefix {
139134
return !!(nodeType && checker.getPromisedTypeOfPromise(nodeType));
140135
}
141136

142-
function isParameterOfPromiseCallback(node: Node, checker: TypeChecker): node is ParameterDeclaration {
143-
return isParameter(node) && (
144-
isPromiseReturningCallExpression(node.parent.parent, checker, "then") ||
145-
isPromiseReturningCallExpression(node.parent.parent, checker, "catch"));
146-
}
147-
148137
function isPromiseTypedExpression(node: Node, checker: TypeChecker): node is Expression {
149138
if (!isExpression(node)) return false;
150139
return !!checker.getPromisedTypeOfPromise(checker.getTypeAtLocation(node));
@@ -160,7 +149,6 @@ namespace ts.codefix {
160149
It then checks for any collisions and renames them through getSynthesizedDeepClone
161150
*/
162151
function renameCollidingVarNames(nodeToRename: FunctionLikeDeclaration, checker: TypeChecker, synthNamesMap: Map<SynthIdentifier>, sourceFile: SourceFile): FunctionLikeDeclaration {
163-
const variableNames: SymbolAndIdentifier[] = [];
164152
const identsToRenameMap = createMap<Identifier>(); // key is the symbol id
165153
const collidingSymbolMap = createMultiMap<Symbol>();
166154
forEachChild(nodeToRename, function visit(node: Node) {
@@ -188,7 +176,6 @@ namespace ts.codefix {
188176
const ident = firstParameter && isParameter(firstParameter.valueDeclaration) && tryCast(firstParameter.valueDeclaration.name, isIdentifier) || createOptimisticUniqueName("result");
189177
const synthName = getNewNameIfConflict(ident, collidingSymbolMap);
190178
synthNamesMap.set(symbolIdString, synthName);
191-
variableNames.push({ identifier: synthName.identifier, symbol });
192179
collidingSymbolMap.add(ident.text, symbol);
193180
}
194181
// We only care about identifiers that are parameters, variable declarations, or binding elements
@@ -201,17 +188,12 @@ namespace ts.codefix {
201188
const newName = getNewNameIfConflict(node, collidingSymbolMap);
202189
identsToRenameMap.set(symbolIdString, newName.identifier);
203190
synthNamesMap.set(symbolIdString, newName);
204-
variableNames.push({ identifier: newName.identifier, symbol });
205191
collidingSymbolMap.add(originalName, symbol);
206192
}
207193
else {
208194
const identifier = getSynthesizedDeepClone(node);
209-
identsToRenameMap.set(symbolIdString, identifier);
210195
synthNamesMap.set(symbolIdString, createSynthIdentifier(identifier));
211-
if (isParameterOfPromiseCallback(node.parent, checker) || isVariableDeclaration(node.parent)) {
212-
variableNames.push({ identifier, symbol });
213-
collidingSymbolMap.add(originalName, symbol);
214-
}
196+
collidingSymbolMap.add(originalName, symbol);
215197
}
216198
}
217199
}

src/testRunner/unittests/services/convertToAsyncFunction.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,16 @@ function rej(reject): a{
11131113
`
11141114
);
11151115

1116+
_testConvertToAsyncFunction("convertToAsyncFunction_ParameterNameCollision", `
1117+
async function foo<T>(x: T): Promise<T> {
1118+
return x;
1119+
}
11161120
1121+
function [#|bar|]<T>(x: T): Promise<T> {
1122+
return foo(x).then(foo)
1123+
}
1124+
`
1125+
);
11171126

11181127

11191128
_testConvertToAsyncFunction("convertToAsyncFunction_LocalReturn", `

tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_InnerPromiseRetBinding1.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ function /*[#|*/innerPromise/*|]*/(): Promise<string> {
1212

1313
async function innerPromise(): Promise<string> {
1414
const resp = await fetch("https://typescriptlang.org");
15-
let blob: any;
15+
let blob_1: any;
1616
try {
1717
const { blob } = await resp.blob();
18-
blob = blob.byteOffset;
18+
blob_1 = blob.byteOffset;
1919
}
2020
catch ({ message }) {
21-
blob = 'Error ' + message;
21+
blob_1 = 'Error ' + message;
2222
}
23-
return blob.toString();
23+
return blob_1.toString();
2424
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ==ORIGINAL==
2+
3+
async function foo<T>(x: T): Promise<T> {
4+
return x;
5+
}
6+
7+
function /*[#|*/bar/*|]*/<T>(x: T): Promise<T> {
8+
return foo(x).then(foo)
9+
}
10+
11+
// ==ASYNC FUNCTION::Convert to async function==
12+
13+
async function foo<T>(x: T): Promise<T> {
14+
return x;
15+
}
16+
17+
async function bar<T>(x: T): Promise<T> {
18+
const x_1 = await foo(x);
19+
return foo(x_1);
20+
}

tests/baselines/reference/convertToAsyncFunction/convertToAsyncFunction_basicWithComments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ async function f(): Promise<void>{
1414

1515
// a
1616
/*b*/ const result /*g*/ = await fetch(/*d*/ 'https://typescriptlang.org' /*e*/);
17-
console.log(result); /*k*/
17+
console.log(/*i*/ result /*j*/); /*k*/
1818
// m
1919
}

0 commit comments

Comments
 (0)