@@ -187,24 +187,10 @@ namespace ts.codefix {
187
187
}
188
188
}
189
189
190
- function isApplicableFunctionForInference ( declaration : FunctionLike ) : declaration is MethodDeclaration | FunctionDeclaration | ConstructorDeclaration {
191
- switch ( declaration . kind ) {
192
- case SyntaxKind . FunctionDeclaration :
193
- case SyntaxKind . MethodDeclaration :
194
- case SyntaxKind . Constructor :
195
- return true ;
196
- case SyntaxKind . FunctionExpression :
197
- const parent = declaration . parent ;
198
- return isVariableDeclaration ( parent ) && isIdentifier ( parent . name ) || ! ! declaration . name ;
199
- }
200
- return false ;
201
- }
202
-
203
190
function annotateParameters ( changes : textChanges . ChangeTracker , sourceFile : SourceFile , parameterDeclaration : ParameterDeclaration , containingFunction : FunctionLike , program : Program , host : LanguageServiceHost , cancellationToken : CancellationToken ) : void {
204
- if ( ! isIdentifier ( parameterDeclaration . name ) || ! isApplicableFunctionForInference ( containingFunction ) ) {
191
+ if ( ! isIdentifier ( parameterDeclaration . name ) ) {
205
192
return ;
206
193
}
207
-
208
194
const parameterInferences = inferTypeForParametersFromUsage ( containingFunction , sourceFile , program , cancellationToken ) ||
209
195
containingFunction . parameters . map < ParameterInference > ( p => ( {
210
196
declaration : p ,
@@ -216,11 +202,14 @@ namespace ts.codefix {
216
202
annotateJSDocParameters ( changes , sourceFile , parameterInferences , program , host ) ;
217
203
}
218
204
else {
205
+ const needParens = isArrowFunction ( containingFunction ) && ! findChildOfKind ( containingFunction , SyntaxKind . OpenParenToken , sourceFile ) ;
206
+ if ( needParens ) changes . insertNodeBefore ( sourceFile , first ( containingFunction . parameters ) , createToken ( SyntaxKind . OpenParenToken ) ) ;
219
207
for ( const { declaration, type } of parameterInferences ) {
220
208
if ( declaration && ! declaration . type && ! declaration . initializer ) {
221
209
annotate ( changes , sourceFile , declaration , type , program , host ) ;
222
210
}
223
211
}
212
+ if ( needParens ) changes . insertNodeAfter ( sourceFile , last ( containingFunction . parameters ) , createToken ( SyntaxKind . CloseParenToken ) ) ;
224
213
}
225
214
}
226
215
@@ -342,12 +331,13 @@ namespace ts.codefix {
342
331
return InferFromReference . unifyFromContext ( types , checker ) ;
343
332
}
344
333
345
- function inferTypeForParametersFromUsage ( containingFunction : FunctionLikeDeclaration , sourceFile : SourceFile , program : Program , cancellationToken : CancellationToken ) : ParameterInference [ ] | undefined {
334
+ function inferTypeForParametersFromUsage ( containingFunction : FunctionLike , sourceFile : SourceFile , program : Program , cancellationToken : CancellationToken ) : ParameterInference [ ] | undefined {
346
335
let searchToken ;
347
336
switch ( containingFunction . kind ) {
348
337
case SyntaxKind . Constructor :
349
338
searchToken = findChildOfKind < Token < SyntaxKind . ConstructorKeyword > > ( containingFunction , SyntaxKind . ConstructorKeyword , sourceFile ) ;
350
339
break ;
340
+ case SyntaxKind . ArrowFunction :
351
341
case SyntaxKind . FunctionExpression :
352
342
const parent = containingFunction . parent ;
353
343
searchToken = isVariableDeclaration ( parent ) && isIdentifier ( parent . name ) ?
@@ -399,7 +389,7 @@ namespace ts.codefix {
399
389
return inferFromContext ( usageContext , checker ) ;
400
390
}
401
391
402
- export function inferTypeForParametersFromReferences ( references : ReadonlyArray < Identifier > , declaration : FunctionLikeDeclaration , program : Program , cancellationToken : CancellationToken ) : ParameterInference [ ] | undefined {
392
+ export function inferTypeForParametersFromReferences ( references : ReadonlyArray < Identifier > , declaration : FunctionLike , program : Program , cancellationToken : CancellationToken ) : ParameterInference [ ] | undefined {
403
393
const checker = program . getTypeChecker ( ) ;
404
394
if ( references . length === 0 ) {
405
395
return undefined ;
0 commit comments