@@ -16,6 +16,7 @@ import {
16
16
emptyArray ,
17
17
Expression ,
18
18
factory ,
19
+ findAncestor ,
19
20
findContainingList ,
20
21
findIndex ,
21
22
findPrecedingToken ,
@@ -30,7 +31,9 @@ import {
30
31
Identifier ,
31
32
identity ,
32
33
InternalSymbolName ,
34
+ isArrayBindingPattern ,
33
35
isBinaryExpression ,
36
+ isBindingElement ,
34
37
isBlock ,
35
38
isCallOrNewExpression ,
36
39
isFunctionTypeNode ,
@@ -41,6 +44,8 @@ import {
41
44
isJsxOpeningLikeElement ,
42
45
isMethodDeclaration ,
43
46
isNoSubstitutionTemplateLiteral ,
47
+ isObjectBindingPattern ,
48
+ isParameter ,
44
49
isPropertyAccessExpression ,
45
50
isSourceFile ,
46
51
isSourceFileJS ,
@@ -387,8 +392,11 @@ function countBinaryExpressionParameters(b: BinaryExpression): number {
387
392
}
388
393
389
394
function tryGetParameterInfo ( startingToken : Node , position : number , sourceFile : SourceFile , checker : TypeChecker ) : ArgumentListInfo | undefined {
390
- const info = getContextualSignatureLocationInfo ( startingToken , sourceFile , position , checker ) ;
391
- if ( ! info ) return undefined ;
395
+ const node = getAdjustedNode ( startingToken ) ;
396
+ if ( node === undefined ) return undefined ;
397
+
398
+ const info = getContextualSignatureLocationInfo ( node , sourceFile , position , checker ) ;
399
+ if ( info === undefined ) return undefined ;
392
400
const { contextualType, argumentIndex, argumentCount, argumentsSpan } = info ;
393
401
394
402
// for optional function condition.
@@ -404,24 +412,34 @@ function tryGetParameterInfo(startingToken: Node, position: number, sourceFile:
404
412
return { isTypeParameterList : false , invocation, argumentsSpan, argumentIndex, argumentCount } ;
405
413
}
406
414
415
+ function getAdjustedNode ( node : Node ) {
416
+ switch ( node . kind ) {
417
+ case SyntaxKind . OpenParenToken :
418
+ case SyntaxKind . CommaToken :
419
+ return node ;
420
+ default :
421
+ return findAncestor ( node . parent , n =>
422
+ isParameter ( n ) ? true : isBindingElement ( n ) || isObjectBindingPattern ( n ) || isArrayBindingPattern ( n ) ? false : "quit" ) ;
423
+ }
424
+ }
425
+
407
426
interface ContextualSignatureLocationInfo { readonly contextualType : Type ; readonly argumentIndex : number ; readonly argumentCount : number ; readonly argumentsSpan : TextSpan ; }
408
- function getContextualSignatureLocationInfo ( startingToken : Node , sourceFile : SourceFile , position : number , checker : TypeChecker ) : ContextualSignatureLocationInfo | undefined {
409
- if ( startingToken . kind !== SyntaxKind . OpenParenToken && startingToken . kind !== SyntaxKind . CommaToken ) return undefined ;
410
- const { parent } = startingToken ;
427
+ function getContextualSignatureLocationInfo ( node : Node , sourceFile : SourceFile , position : number , checker : TypeChecker ) : ContextualSignatureLocationInfo | undefined {
428
+ const { parent } = node ;
411
429
switch ( parent . kind ) {
412
430
case SyntaxKind . ParenthesizedExpression :
413
431
case SyntaxKind . MethodDeclaration :
414
432
case SyntaxKind . FunctionExpression :
415
433
case SyntaxKind . ArrowFunction :
416
- const info = getArgumentOrParameterListInfo ( startingToken , position , sourceFile ) ;
434
+ const info = getArgumentOrParameterListInfo ( node , position , sourceFile ) ;
417
435
if ( ! info ) return undefined ;
418
436
const { argumentIndex, argumentCount, argumentsSpan } = info ;
419
437
const contextualType = isMethodDeclaration ( parent ) ? checker . getContextualTypeForObjectLiteralElement ( parent ) : checker . getContextualType ( parent as ParenthesizedExpression | FunctionExpression | ArrowFunction ) ;
420
438
return contextualType && { contextualType, argumentIndex, argumentCount, argumentsSpan } ;
421
439
case SyntaxKind . BinaryExpression : {
422
440
const highestBinary = getHighestBinary ( parent as BinaryExpression ) ;
423
441
const contextualType = checker . getContextualType ( highestBinary ) ;
424
- const argumentIndex = startingToken . kind === SyntaxKind . OpenParenToken ? 0 : countBinaryExpressionParameters ( parent as BinaryExpression ) - 1 ;
442
+ const argumentIndex = node . kind === SyntaxKind . OpenParenToken ? 0 : countBinaryExpressionParameters ( parent as BinaryExpression ) - 1 ;
425
443
const argumentCount = countBinaryExpressionParameters ( highestBinary ) ;
426
444
return contextualType && { contextualType, argumentIndex, argumentCount, argumentsSpan : createTextSpanFromNode ( parent ) } ;
427
445
}
0 commit comments