@@ -286,7 +286,7 @@ function getArgumentOrParameterListInfo(node: Node, position: number, sourceFile
286
286
if ( ! info ) return undefined ;
287
287
const { list, argumentIndex } = info ;
288
288
289
- const argumentCount = getArgumentCount ( list , checker ) ;
289
+ const argumentCount = getArgumentIndexOrCount ( checker , list , /*node*/ undefined ) ;
290
290
if ( argumentIndex !== 0 ) {
291
291
Debug . assertLessThan ( argumentIndex , argumentCount ) ;
292
292
}
@@ -307,7 +307,7 @@ function getArgumentOrParameterListAndIndex(node: Node, sourceFile: SourceFile,
307
307
// - On the target of the call (parent.func)
308
308
// - On the 'new' keyword in a 'new' expression
309
309
const list = findContainingList ( node ) ;
310
- return list && { list, argumentIndex : getArgumentIndex ( list , node , checker ) } ;
310
+ return list && { list, argumentIndex : getArgumentIndexOrCount ( checker , list , node ) } ;
311
311
}
312
312
}
313
313
@@ -479,46 +479,6 @@ function chooseBetterSymbol(s: Symbol): Symbol {
479
479
: s ;
480
480
}
481
481
482
- function getArgumentIndex ( argumentsList : Node , node : Node , checker : TypeChecker ) {
483
- // The list we got back can include commas. In the presence of errors it may
484
- // also just have nodes without commas. For example "Foo(a b c)" will have 3
485
- // args without commas. We want to find what index we're at. So we count
486
- // forward until we hit ourselves.
487
- //
488
- // Note: the subtlety around trailing commas (in getArgumentCount) does not apply
489
- // here. That's because we're only walking forward until we hit the node we're
490
- // on. In that case, even if we're after the trailing comma, we'll still see
491
- // that trailing comma in the list, and we'll have generated the appropriate
492
- // arg index.
493
- const args = argumentsList . getChildren ( ) ;
494
- let argumentIndex = 0 ;
495
- let skipComma = false ;
496
- for ( const child of args ) {
497
- if ( child === node ) {
498
- if ( ! skipComma && child . kind === SyntaxKind . CommaToken ) {
499
- argumentIndex ++ ;
500
- }
501
- break ;
502
- }
503
- if ( isSpreadElement ( child ) ) {
504
- argumentIndex += getSpreadElementCount ( child , checker ) ;
505
- skipComma = true ;
506
- continue ;
507
- }
508
- if ( child . kind !== SyntaxKind . CommaToken ) {
509
- argumentIndex ++ ;
510
- skipComma = true ;
511
- continue ;
512
- }
513
- if ( skipComma ) {
514
- skipComma = false ;
515
- continue ;
516
- }
517
- argumentIndex ++ ;
518
- }
519
- return argumentIndex ;
520
- }
521
-
522
482
function getSpreadElementCount ( node : SpreadElement , checker : TypeChecker ) {
523
483
const spreadType = checker . getTypeAtLocation ( node . expression ) ;
524
484
if ( checker . isTupleType ( spreadType ) ) {
@@ -532,34 +492,46 @@ function getSpreadElementCount(node: SpreadElement, checker: TypeChecker) {
532
492
return 0 ;
533
493
}
534
494
535
- function getArgumentCount ( argumentsList : Node , checker : TypeChecker ) {
536
- // The argument count for a list is normally the number of non-comma children it has.
537
- // For example, if you have "Foo(a,b)" then there will be three children of the arg
538
- // list 'a' '<comma>' 'b'. So, in this case the arg count will be 2. However, there
539
- // is a small subtlety. If you have "Foo(a,)", then the child list will just have
540
- // 'a' '<comma>'. So, in the case where the last child is a comma, we increase the
541
- // arg count by one to compensate.
495
+ function getArgumentIndexOrCount ( checker : TypeChecker , argumentsList : Node , node : Node | undefined ) {
496
+ // The list we got back can include commas. In the presence of errors it may
497
+ // also just have nodes without commas. For example "Foo(a b c)" will have 3
498
+ // args without commas.
542
499
const args = argumentsList . getChildren ( ) ;
543
- let argumentCount = 0 ;
500
+ let argumentIndex = 0 ;
544
501
let skipComma = false ;
545
502
for ( const child of args ) {
503
+ if ( node && child === node ) {
504
+ if ( ! skipComma && child . kind === SyntaxKind . CommaToken ) {
505
+ argumentIndex ++ ;
506
+ }
507
+ return argumentIndex ;
508
+ }
546
509
if ( isSpreadElement ( child ) ) {
547
- argumentCount += getSpreadElementCount ( child , checker ) ;
510
+ argumentIndex += getSpreadElementCount ( child , checker ) ;
548
511
skipComma = true ;
549
512
continue ;
550
513
}
551
514
if ( child . kind !== SyntaxKind . CommaToken ) {
552
- argumentCount ++ ;
515
+ argumentIndex ++ ;
553
516
skipComma = true ;
554
517
continue ;
555
518
}
556
519
if ( skipComma ) {
557
520
skipComma = false ;
558
521
continue ;
559
522
}
560
- argumentCount ++ ;
523
+ argumentIndex ++ ;
524
+ }
525
+ if ( node ) {
526
+ return argumentIndex ;
561
527
}
562
- return args . length && last ( args ) . kind === SyntaxKind . CommaToken ? argumentCount + 1 : argumentCount ;
528
+ // The argument count for a list is normally the number of non-comma children it has.
529
+ // For example, if you have "Foo(a,b)" then there will be three children of the arg
530
+ // list 'a' '<comma>' 'b'. So, in this case the arg count will be 2. However, there
531
+ // is a small subtlety. If you have "Foo(a,)", then the child list will just have
532
+ // 'a' '<comma>'. So, in the case where the last child is a comma, we increase the
533
+ // arg count by one to compensate.
534
+ return args . length && last ( args ) . kind === SyntaxKind . CommaToken ? argumentIndex + 1 : argumentIndex ;
563
535
}
564
536
565
537
// spanIndex is either the index for a given template span.
0 commit comments