@@ -25,15 +25,16 @@ namespace ts.codefix {
25
25
numberOfAssignmentsOriginal : number ;
26
26
}
27
27
28
- interface SymbolAndIdentifier {
28
+ interface SymbolAndIdentifierAndOriginalName {
29
29
identifier : Identifier ;
30
30
symbol : Symbol ;
31
+ originalName : string ;
31
32
}
32
33
33
34
interface Transformer {
34
35
checker : TypeChecker ;
35
36
synthNamesMap : Map < SynthIdentifier > ; // keys are the symbol id of the identifier
36
- allVarNames : SymbolAndIdentifier [ ] ;
37
+ allVarNames : SymbolAndIdentifierAndOriginalName [ ] ;
37
38
setOfExpressionsToReturn : Map < true > ; // keys are the node ids of the expressions
38
39
constIdentifiers : Identifier [ ] ;
39
40
originalTypeMap : Map < Type > ; // keys are the node id of the identifier
@@ -60,7 +61,7 @@ namespace ts.codefix {
60
61
61
62
const synthNamesMap : Map < SynthIdentifier > = createMap ( ) ;
62
63
const originalTypeMap : Map < Type > = createMap ( ) ;
63
- const allVarNames : SymbolAndIdentifier [ ] = [ ] ;
64
+ const allVarNames : SymbolAndIdentifierAndOriginalName [ ] = [ ] ;
64
65
const isInJSFile = isInJavaScriptFile ( functionToConvert ) ;
65
66
const setOfExpressionsToReturn = getAllPromiseExpressionsToReturn ( functionToConvert , checker ) ;
66
67
const functionToConvertRenamed : FunctionLikeDeclaration = renameCollidingVarNames ( functionToConvert , checker , synthNamesMap , context , setOfExpressionsToReturn , originalTypeMap , allVarNames ) ;
@@ -157,7 +158,7 @@ namespace ts.codefix {
157
158
This function collects all existing identifier names and names of identifiers that will be created in the refactor.
158
159
It then checks for any collisions and renames them through getSynthesizedDeepClone
159
160
*/
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 {
161
162
162
163
const identsToRenameMap : Map < Identifier > = createMap ( ) ; // key is the symbol id
163
164
forEachChild ( nodeToRename , function visit ( node : Node ) {
@@ -177,26 +178,27 @@ namespace ts.codefix {
177
178
// 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))
178
179
// Note - the choice of the last call signature is arbitrary
179
180
if ( lastCallSignature && lastCallSignature . parameters . length && ! synthNamesMap . has ( symbolIdString ) ) {
181
+ const name = lastCallSignature . parameters [ 0 ] . name ;
180
182
const synthName = getNewNameIfConflict ( createIdentifier ( lastCallSignature . parameters [ 0 ] . name ) , allVarNames ) ;
181
183
synthNamesMap . set ( symbolIdString , synthName ) ;
182
- allVarNames . push ( { identifier : synthName . identifier , symbol } ) ;
184
+ allVarNames . push ( { identifier : synthName . identifier , symbol, originalName : name } ) ;
183
185
}
184
186
// we only care about identifiers that are parameters and declarations (don't care about other uses)
185
187
else if ( node . parent && ( isParameter ( node . parent ) || isVariableDeclaration ( node . parent ) ) ) {
186
188
187
189
// 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 ) ) {
189
191
const newName = getNewNameIfConflict ( node , allVarNames ) ;
190
192
identsToRenameMap . set ( symbolIdString , newName . identifier ) ;
191
193
synthNamesMap . set ( symbolIdString , newName ) ;
192
- allVarNames . push ( { identifier : newName . identifier , symbol } ) ;
194
+ allVarNames . push ( { identifier : newName . identifier , symbol, originalName : node . text } ) ;
193
195
}
194
196
else {
195
197
const identifier = getSynthesizedDeepClone ( node ) ;
196
198
identsToRenameMap . set ( symbolIdString , identifier ) ;
197
199
synthNamesMap . set ( symbolIdString , { identifier, types : [ ] , numberOfAssignmentsOriginal : allVarNames . filter ( elem => elem . identifier . text === node . text ) . length /*, numberOfAssignmentsSynthesized: 0*/ } ) ;
198
200
if ( ( isParameter ( node . parent ) && isExpressionOrCallOnTypePromise ( node . parent . parent ) ) || isVariableDeclaration ( node . parent ) ) {
199
- allVarNames . push ( { identifier, symbol } ) ;
201
+ allVarNames . push ( { identifier, symbol, originalName : node . text } ) ;
200
202
}
201
203
}
202
204
}
@@ -239,8 +241,8 @@ namespace ts.codefix {
239
241
240
242
}
241
243
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 ;
244
246
const numberOfAssignmentsOriginal = 0 ;
245
247
const identifier = numVarsSameName === 0 ? name : createIdentifier ( name . text + "_" + numVarsSameName ) ;
246
248
return { identifier, types : [ ] , numberOfAssignmentsOriginal } ;
0 commit comments