@@ -79,20 +79,20 @@ namespace ts.refactor.convertToNamedParameters {
79
79
const checker = program . getTypeChecker ( ) ;
80
80
81
81
const references = flatMap ( names , /*mapfn*/ name => FindAllReferences . getReferenceEntriesForNode ( - 1 , name , program , program . getSourceFiles ( ) , cancellationToken ) ) ;
82
- const isConstructor = isConstructorDeclaration ( functionDeclaration ) ;
83
- const groupedReferences = groupReferences ( references , isConstructor ) ;
82
+ const groupedReferences = groupReferences ( references ) ;
84
83
85
84
if ( ! every ( groupedReferences . declarations , decl => contains ( names , decl ) ) ) {
86
85
groupedReferences . valid = false ;
87
86
}
88
87
89
88
return groupedReferences ;
90
89
91
- function groupReferences ( referenceEntries : ReadonlyArray < FindAllReferences . Entry > , isConstructor : boolean ) : GroupedReferences {
90
+ function groupReferences ( referenceEntries : ReadonlyArray < FindAllReferences . Entry > ) : GroupedReferences {
92
91
const classReferences : ClassReferences = { accessExpressions : [ ] , typeUsages : [ ] } ;
93
92
const groupedReferences : GroupedReferences = { functionCalls : [ ] , declarations : [ ] , classReferences, valid : true } ;
94
93
const functionSymbols = map ( functionNames , checker . getSymbolAtLocation ) ;
95
94
const classSymbols = map ( classNames , checker . getSymbolAtLocation ) ;
95
+ const isConstructor = isConstructorDeclaration ( functionDeclaration ) ;
96
96
97
97
for ( const entry of referenceEntries ) {
98
98
if ( entry . kind !== FindAllReferences . EntryKind . Node ) {
@@ -141,95 +141,93 @@ namespace ts.refactor.convertToNamedParameters {
141
141
142
142
return groupedReferences ;
143
143
}
144
+ }
144
145
145
- function symbolComparer ( a : Symbol , b : Symbol ) : boolean {
146
- return getSymbolTarget ( a ) === getSymbolTarget ( b ) ;
147
- }
146
+ function symbolComparer ( a : Symbol , b : Symbol ) : boolean {
147
+ return getSymbolTarget ( a ) === getSymbolTarget ( b ) ;
148
+ }
148
149
149
- function entryToDeclaration ( entry : FindAllReferences . NodeEntry ) : Node | undefined {
150
- if ( isDeclaration ( entry . node . parent ) ) {
151
- return entry . node ;
152
- }
153
- return undefined ;
150
+ function entryToDeclaration ( entry : FindAllReferences . NodeEntry ) : Node | undefined {
151
+ if ( isDeclaration ( entry . node . parent ) ) {
152
+ return entry . node ;
154
153
}
154
+ return undefined ;
155
+ }
155
156
156
- function entryToFunctionCall ( entry : FindAllReferences . NodeEntry ) : CallExpression | NewExpression | undefined {
157
- if ( entry . node . parent ) {
158
- const functionReference = entry . node ;
159
- const parent = functionReference . parent ;
160
- switch ( parent . kind ) {
161
- // Function call (foo(...) or super(...))
162
- case SyntaxKind . CallExpression :
163
- const callExpression = tryCast ( parent , isCallExpression ) ;
164
- if ( callExpression && callExpression . expression === functionReference ) {
157
+ function entryToFunctionCall ( entry : FindAllReferences . NodeEntry ) : CallExpression | NewExpression | undefined {
158
+ if ( entry . node . parent ) {
159
+ const functionReference = entry . node ;
160
+ const parent = functionReference . parent ;
161
+ switch ( parent . kind ) {
162
+ // Function call (foo(...) or super(...))
163
+ case SyntaxKind . CallExpression :
164
+ const callExpression = tryCast ( parent , isCallExpression ) ;
165
+ if ( callExpression && callExpression . expression === functionReference ) {
166
+ return callExpression ;
167
+ }
168
+ break ;
169
+ // Constructor call (new Foo(...))
170
+ case SyntaxKind . NewExpression :
171
+ const newExpression = tryCast ( parent , isNewExpression ) ;
172
+ if ( newExpression && newExpression . expression === functionReference ) {
173
+ return newExpression ;
174
+ }
175
+ break ;
176
+ // Method call (x.foo(...))
177
+ case SyntaxKind . PropertyAccessExpression :
178
+ const propertyAccessExpression = tryCast ( parent , isPropertyAccessExpression ) ;
179
+ if ( propertyAccessExpression && propertyAccessExpression . parent && propertyAccessExpression . name === functionReference ) {
180
+ const callExpression = tryCast ( propertyAccessExpression . parent , isCallExpression ) ;
181
+ if ( callExpression && callExpression . expression === propertyAccessExpression ) {
165
182
return callExpression ;
166
183
}
167
- break ;
168
- // Constructor call (new Foo(...))
169
- case SyntaxKind . NewExpression :
170
- const newExpression = tryCast ( parent , isNewExpression ) ;
171
- if ( newExpression && newExpression . expression === functionReference ) {
172
- return newExpression ;
173
- }
174
- break ;
175
- // Method call (x.foo(...))
176
- case SyntaxKind . PropertyAccessExpression :
177
- const propertyAccessExpression = tryCast ( parent , isPropertyAccessExpression ) ;
178
- if ( propertyAccessExpression && propertyAccessExpression . parent && propertyAccessExpression . name === functionReference ) {
179
- const callExpression = tryCast ( propertyAccessExpression . parent , isCallExpression ) ;
180
- if ( callExpression && callExpression . expression === propertyAccessExpression ) {
181
- return callExpression ;
182
- }
183
- }
184
- break ;
185
- // Method call (x["foo"](...))
186
- case SyntaxKind . ElementAccessExpression :
187
- const elementAccessExpression = tryCast ( parent , isElementAccessExpression ) ;
188
- if ( elementAccessExpression && elementAccessExpression . parent && elementAccessExpression . argumentExpression === functionReference ) {
189
- const callExpression = tryCast ( elementAccessExpression . parent , isCallExpression ) ;
190
- if ( callExpression && callExpression . expression === elementAccessExpression ) {
191
- return callExpression ;
192
- }
184
+ }
185
+ break ;
186
+ // Method call (x["foo"](...))
187
+ case SyntaxKind . ElementAccessExpression :
188
+ const elementAccessExpression = tryCast ( parent , isElementAccessExpression ) ;
189
+ if ( elementAccessExpression && elementAccessExpression . parent && elementAccessExpression . argumentExpression === functionReference ) {
190
+ const callExpression = tryCast ( elementAccessExpression . parent , isCallExpression ) ;
191
+ if ( callExpression && callExpression . expression === elementAccessExpression ) {
192
+ return callExpression ;
193
193
}
194
- break ;
195
- }
194
+ }
195
+ break ;
196
196
}
197
- return undefined ;
198
197
}
198
+ return undefined ;
199
+ }
199
200
200
- function entryToAccessExpression ( entry : FindAllReferences . NodeEntry ) : ElementAccessExpression | PropertyAccessExpression | undefined {
201
- if ( entry . node . parent ) {
202
- const reference = entry . node ;
203
- const parent = reference . parent ;
204
- switch ( parent . kind ) {
205
- // `C.foo`
206
- case SyntaxKind . PropertyAccessExpression :
207
- const propertyAccessExpression = tryCast ( parent , isPropertyAccessExpression ) ;
208
- if ( propertyAccessExpression && propertyAccessExpression . expression === reference ) {
209
- return propertyAccessExpression ;
210
- }
211
- break ;
212
- // `C["foo"]`
213
- case SyntaxKind . ElementAccessExpression :
214
- const elementAccessExpression = tryCast ( parent , isElementAccessExpression ) ;
215
- if ( elementAccessExpression && elementAccessExpression . expression === reference ) {
216
- return elementAccessExpression ;
217
- }
218
- break ;
219
- }
201
+ function entryToAccessExpression ( entry : FindAllReferences . NodeEntry ) : ElementAccessExpression | PropertyAccessExpression | undefined {
202
+ if ( entry . node . parent ) {
203
+ const reference = entry . node ;
204
+ const parent = reference . parent ;
205
+ switch ( parent . kind ) {
206
+ // `C.foo`
207
+ case SyntaxKind . PropertyAccessExpression :
208
+ const propertyAccessExpression = tryCast ( parent , isPropertyAccessExpression ) ;
209
+ if ( propertyAccessExpression && propertyAccessExpression . expression === reference ) {
210
+ return propertyAccessExpression ;
211
+ }
212
+ break ;
213
+ // `C["foo"]`
214
+ case SyntaxKind . ElementAccessExpression :
215
+ const elementAccessExpression = tryCast ( parent , isElementAccessExpression ) ;
216
+ if ( elementAccessExpression && elementAccessExpression . expression === reference ) {
217
+ return elementAccessExpression ;
218
+ }
219
+ break ;
220
220
}
221
- return undefined ;
222
221
}
222
+ return undefined ;
223
+ }
223
224
224
- function entryToType ( entry : FindAllReferences . Entry ) : Node | undefined {
225
- if ( entry . kind === FindAllReferences . EntryKind . Node ) {
226
- const reference = entry . node ;
227
- if ( getMeaningFromLocation ( reference ) === SemanticMeaning . Type || isExpressionWithTypeArgumentsInClassExtendsClause ( reference . parent ) ) {
228
- return reference ;
229
- }
230
- }
231
- return undefined ;
225
+ function entryToType ( entry : FindAllReferences . NodeEntry ) : Node | undefined {
226
+ const reference = entry . node ;
227
+ if ( getMeaningFromLocation ( reference ) === SemanticMeaning . Type || isExpressionWithTypeArgumentsInClassExtendsClause ( reference . parent ) ) {
228
+ return reference ;
232
229
}
230
+ return undefined ;
233
231
}
234
232
235
233
function getFunctionDeclarationAtPosition ( file : SourceFile , startPosition : number , checker : TypeChecker ) : ValidFunctionDeclaration | undefined {
@@ -261,18 +259,18 @@ namespace ts.refactor.convertToNamedParameters {
261
259
return isValidVariableDeclaration ( functionDeclaration . parent ) ;
262
260
}
263
261
return false ;
262
+ }
264
263
265
- function isValidParameterNodeArray ( parameters : NodeArray < ParameterDeclaration > ) : parameters is ValidParameterNodeArray {
266
- return getRefactorableParametersLength ( parameters ) >= minimumParameterLength && every ( parameters , isValidParameterDeclaration ) ;
267
- }
264
+ function isValidParameterNodeArray ( parameters : NodeArray < ParameterDeclaration > ) : parameters is ValidParameterNodeArray {
265
+ return getRefactorableParametersLength ( parameters ) >= minimumParameterLength && every ( parameters , isValidParameterDeclaration ) ;
266
+ }
268
267
269
- function isValidParameterDeclaration ( paramDeclaration : ParameterDeclaration ) : paramDeclaration is ValidParameterDeclaration {
270
- return ! paramDeclaration . modifiers && ! paramDeclaration . decorators && isIdentifier ( paramDeclaration . name ) ;
271
- }
268
+ function isValidParameterDeclaration ( paramDeclaration : ParameterDeclaration ) : paramDeclaration is ValidParameterDeclaration {
269
+ return ! paramDeclaration . modifiers && ! paramDeclaration . decorators && isIdentifier ( paramDeclaration . name ) ;
270
+ }
272
271
273
- function isValidVariableDeclaration ( node : Node ) : node is ValidVariableDeclaration {
274
- return isVariableDeclaration ( node ) && isVarConst ( node ) && isIdentifier ( node . name ) && ! node . type ; // TODO: GH#30113
275
- }
272
+ function isValidVariableDeclaration ( node : Node ) : node is ValidVariableDeclaration {
273
+ return isVariableDeclaration ( node ) && isVarConst ( node ) && isIdentifier ( node . name ) && ! node . type ; // TODO: GH#30113
276
274
}
277
275
278
276
function hasThisParameter ( parameters : NodeArray < ParameterDeclaration > ) : boolean {
@@ -357,20 +355,6 @@ namespace ts.refactor.convertToNamedParameters {
357
355
}
358
356
return createNodeArray ( [ objectParameter ] ) ;
359
357
360
- function createBindingElementFromParameterDeclaration ( parameterDeclaration : ValidParameterDeclaration ) : BindingElement {
361
- const element = createBindingElement (
362
- /*dotDotDotToken*/ undefined ,
363
- /*propertyName*/ undefined ,
364
- getParameterName ( parameterDeclaration ) ,
365
- isRestParameter ( parameterDeclaration ) ? createArrayLiteral ( ) : parameterDeclaration . initializer ) ;
366
-
367
- suppressLeadingAndTrailingTrivia ( element ) ;
368
- if ( parameterDeclaration . initializer && element . initializer ) {
369
- copyComments ( parameterDeclaration . initializer , element . initializer ) ;
370
- }
371
- return element ;
372
- }
373
-
374
358
function createParameterTypeNode ( parameters : NodeArray < ValidParameterDeclaration > ) : TypeLiteralNode {
375
359
const members = map ( parameters , createPropertySignatureFromParameterDeclaration ) ;
376
360
const typeNode = addEmitFlags ( createTypeLiteralNode ( members ) , EmitFlags . SingleLine ) ;
@@ -406,6 +390,20 @@ namespace ts.refactor.convertToNamedParameters {
406
390
}
407
391
}
408
392
393
+ function createBindingElementFromParameterDeclaration ( parameterDeclaration : ValidParameterDeclaration ) : BindingElement {
394
+ const element = createBindingElement (
395
+ /*dotDotDotToken*/ undefined ,
396
+ /*propertyName*/ undefined ,
397
+ getParameterName ( parameterDeclaration ) ,
398
+ isRestParameter ( parameterDeclaration ) ? createArrayLiteral ( ) : parameterDeclaration . initializer ) ;
399
+
400
+ suppressLeadingAndTrailingTrivia ( element ) ;
401
+ if ( parameterDeclaration . initializer && element . initializer ) {
402
+ copyComments ( parameterDeclaration . initializer , element . initializer ) ;
403
+ }
404
+ return element ;
405
+ }
406
+
409
407
function copyComments ( sourceNode : Node , targetNode : Node ) {
410
408
const sourceFile = sourceNode . getSourceFile ( ) ;
411
409
const text = sourceFile . text ;
@@ -416,15 +414,15 @@ namespace ts.refactor.convertToNamedParameters {
416
414
copyTrailingAsLeadingComments ( sourceNode , targetNode , sourceFile ) ;
417
415
}
418
416
copyTrailingComments ( sourceNode , targetNode , sourceFile ) ;
417
+ }
419
418
420
- function hasLeadingLineBreak ( node : Node , text : string ) {
421
- const start = node . getFullStart ( ) ;
422
- const end = node . getStart ( ) ;
423
- for ( let i = start ; i < end ; i ++ ) {
424
- if ( text . charCodeAt ( i ) === CharacterCodes . lineFeed ) return true ;
425
- }
426
- return false ;
419
+ function hasLeadingLineBreak ( node : Node , text : string ) {
420
+ const start = node . getFullStart ( ) ;
421
+ const end = node . getStart ( ) ;
422
+ for ( let i = start ; i < end ; i ++ ) {
423
+ if ( text . charCodeAt ( i ) === CharacterCodes . lineFeed ) return true ;
427
424
}
425
+ return false ;
428
426
}
429
427
430
428
function getParameterName ( paramDeclaration : ValidParameterDeclaration ) {
0 commit comments