Skip to content

Commit 92edc2d

Browse files
author
Benjamin Lichtman
committed
[WIP] Record original name of renamed variable
1 parent a4c87df commit 92edc2d

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

src/services/codefixes/convertToAsyncFunction.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ namespace ts.codefix {
2525
numberOfAssignmentsOriginal: number;
2626
}
2727

28-
interface SymbolAndIdentifier {
28+
interface SymbolAndIdentifierAndOriginalName {
2929
identifier: Identifier;
3030
symbol: Symbol;
31+
originalName: string;
3132
}
3233

3334
interface Transformer {
3435
checker: TypeChecker;
3536
synthNamesMap: Map<SynthIdentifier>; // keys are the symbol id of the identifier
36-
allVarNames: SymbolAndIdentifier[];
37+
allVarNames: SymbolAndIdentifierAndOriginalName[];
3738
setOfExpressionsToReturn: Map<true>; // keys are the node ids of the expressions
3839
constIdentifiers: Identifier[];
3940
originalTypeMap: Map<Type>; // keys are the node id of the identifier
@@ -60,7 +61,7 @@ namespace ts.codefix {
6061

6162
const synthNamesMap: Map<SynthIdentifier> = createMap();
6263
const originalTypeMap: Map<Type> = createMap();
63-
const allVarNames: SymbolAndIdentifier[] = [];
64+
const allVarNames: SymbolAndIdentifierAndOriginalName[] = [];
6465
const isInJSFile = isInJavaScriptFile(functionToConvert);
6566
const setOfExpressionsToReturn = getAllPromiseExpressionsToReturn(functionToConvert, checker);
6667
const functionToConvertRenamed: FunctionLikeDeclaration = renameCollidingVarNames(functionToConvert, checker, synthNamesMap, context, setOfExpressionsToReturn, originalTypeMap, allVarNames);
@@ -157,7 +158,7 @@ namespace ts.codefix {
157158
This function collects all existing identifier names and names of identifiers that will be created in the refactor.
158159
It then checks for any collisions and renames them through getSynthesizedDeepClone
159160
*/
160-
function renameCollidingVarNames(nodeToRename: FunctionLikeDeclaration, checker: TypeChecker, synthNamesMap: Map<SynthIdentifier>, context: CodeFixContextBase, setOfAllExpressionsToReturn: Map<true>, originalType: Map<Type>, allVarNames: SymbolAndIdentifier[]): FunctionLikeDeclaration {
161+
function renameCollidingVarNames(nodeToRename: FunctionLikeDeclaration, checker: TypeChecker, synthNamesMap: Map<SynthIdentifier>, context: CodeFixContextBase, setOfAllExpressionsToReturn: Map<true>, originalType: Map<Type>, allVarNames: SymbolAndIdentifierAndOriginalName[]): FunctionLikeDeclaration {
161162

162163
const identsToRenameMap: Map<Identifier> = createMap(); // key is the symbol id
163164
forEachChild(nodeToRename, function visit(node: Node) {
@@ -177,26 +178,27 @@ namespace ts.codefix {
177178
// 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))
178179
// Note - the choice of the last call signature is arbitrary
179180
if (lastCallSignature && lastCallSignature.parameters.length && !synthNamesMap.has(symbolIdString)) {
181+
const name = lastCallSignature.parameters[0].name;
180182
const synthName = getNewNameIfConflict(createIdentifier(lastCallSignature.parameters[0].name), allVarNames);
181183
synthNamesMap.set(symbolIdString, synthName);
182-
allVarNames.push({ identifier: synthName.identifier, symbol });
184+
allVarNames.push({ identifier: synthName.identifier, symbol, originalName: name });
183185
}
184186
// we only care about identifiers that are parameters and declarations (don't care about other uses)
185187
else if (node.parent && (isParameter(node.parent) || isVariableDeclaration(node.parent))) {
186188

187189
// if the identifier name conflicts with a different identifier that we've already seen
188-
if (allVarNames.some(ident => ident.identifier.text === node.text && ident.symbol !== symbol)) {
190+
if (allVarNames.some(ident => ident.originalName === node.text && ident.symbol !== symbol)) {
189191
const newName = getNewNameIfConflict(node, allVarNames);
190192
identsToRenameMap.set(symbolIdString, newName.identifier);
191193
synthNamesMap.set(symbolIdString, newName);
192-
allVarNames.push({ identifier: newName.identifier, symbol });
194+
allVarNames.push({ identifier: newName.identifier, symbol, originalName: node.text });
193195
}
194196
else {
195197
const identifier = getSynthesizedDeepClone(node);
196198
identsToRenameMap.set(symbolIdString, identifier);
197199
synthNamesMap.set(symbolIdString, { identifier, types: [], numberOfAssignmentsOriginal: allVarNames.filter(elem => elem.identifier.text === node.text).length/*, numberOfAssignmentsSynthesized: 0*/ });
198200
if ((isParameter(node.parent) && isExpressionOrCallOnTypePromise(node.parent.parent)) || isVariableDeclaration(node.parent)) {
199-
allVarNames.push({ identifier, symbol });
201+
allVarNames.push({ identifier, symbol, originalName: node.text });
200202
}
201203
}
202204
}
@@ -239,8 +241,8 @@ namespace ts.codefix {
239241

240242
}
241243

242-
function getNewNameIfConflict(name: Identifier, allVarNames: SymbolAndIdentifier[]): SynthIdentifier {
243-
const numVarsSameName = allVarNames.filter(elem => elem.symbol.name === name.text).length;
244+
function getNewNameIfConflict(name: Identifier, allVarNames: SymbolAndIdentifierAndOriginalName[]): SynthIdentifier {
245+
const numVarsSameName = allVarNames.filter(elem => elem.originalName === name.text).length;
244246
const numberOfAssignmentsOriginal = 0;
245247
const identifier = numVarsSameName === 0 ? name : createIdentifier(name.text + "_" + numVarsSameName);
246248
return { identifier, types: [], numberOfAssignmentsOriginal };
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// ==ORIGINAL==
2+
3+
function /*[#|*/f/*|]*/() {
4+
return Promise.resolve().then(x => 1).catch(x => "a").then(x => !!x);
5+
}
6+
7+
// ==ASYNC FUNCTION::Convert to async function==
8+
9+
async function f() {
10+
let x_2;
11+
try {
12+
const x = await Promise.resolve();
13+
x_2 = 1;
14+
}
15+
catch (x_1) {
16+
x_2 = "a";
17+
}
18+
return !!x_2;
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// ==ORIGINAL==
2+
3+
function /*[#|*/f/*|]*/() {
4+
return Promise.resolve().then(x => 1).catch(x => "a").then(x => !!x);
5+
}
6+
7+
// ==ASYNC FUNCTION::Convert to async function==
8+
9+
async function f() {
10+
let x_2: string | number;
11+
try {
12+
const x = await Promise.resolve();
13+
x_2 = 1;
14+
}
15+
catch (x_1) {
16+
x_2 = "a";
17+
}
18+
return !!x_2;
19+
}

0 commit comments

Comments
 (0)