Skip to content

Commit 36cfea0

Browse files
committed
unify both functions
1 parent 6aadcd8 commit 36cfea0

File tree

1 file changed

+26
-54
lines changed

1 file changed

+26
-54
lines changed

src/services/signatureHelp.ts

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ function getArgumentOrParameterListInfo(node: Node, position: number, sourceFile
286286
if (!info) return undefined;
287287
const { list, argumentIndex } = info;
288288

289-
const argumentCount = getArgumentCount(list, checker);
289+
const argumentCount = getArgumentIndexOrCount(checker, list, /*node*/ undefined);
290290
if (argumentIndex !== 0) {
291291
Debug.assertLessThan(argumentIndex, argumentCount);
292292
}
@@ -307,7 +307,7 @@ function getArgumentOrParameterListAndIndex(node: Node, sourceFile: SourceFile,
307307
// - On the target of the call (parent.func)
308308
// - On the 'new' keyword in a 'new' expression
309309
const list = findContainingList(node);
310-
return list && { list, argumentIndex: getArgumentIndex(list, node, checker) };
310+
return list && { list, argumentIndex: getArgumentIndexOrCount(checker, list, node) };
311311
}
312312
}
313313

@@ -479,46 +479,6 @@ function chooseBetterSymbol(s: Symbol): Symbol {
479479
: s;
480480
}
481481

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-
522482
function getSpreadElementCount(node: SpreadElement, checker: TypeChecker) {
523483
const spreadType = checker.getTypeAtLocation(node.expression);
524484
if (checker.isTupleType(spreadType)) {
@@ -532,34 +492,46 @@ function getSpreadElementCount(node: SpreadElement, checker: TypeChecker) {
532492
return 0;
533493
}
534494

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.
542499
const args = argumentsList.getChildren();
543-
let argumentCount = 0;
500+
let argumentIndex = 0;
544501
let skipComma = false;
545502
for (const child of args) {
503+
if (node && child === node) {
504+
if (!skipComma && child.kind === SyntaxKind.CommaToken) {
505+
argumentIndex++;
506+
}
507+
return argumentIndex;
508+
}
546509
if (isSpreadElement(child)) {
547-
argumentCount += getSpreadElementCount(child, checker);
510+
argumentIndex += getSpreadElementCount(child, checker);
548511
skipComma = true;
549512
continue;
550513
}
551514
if (child.kind !== SyntaxKind.CommaToken) {
552-
argumentCount++;
515+
argumentIndex++;
553516
skipComma = true;
554517
continue;
555518
}
556519
if (skipComma) {
557520
skipComma = false;
558521
continue;
559522
}
560-
argumentCount++;
523+
argumentIndex++;
524+
}
525+
if (node) {
526+
return argumentIndex;
561527
}
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;
563535
}
564536

565537
// spanIndex is either the index for a given template span.

0 commit comments

Comments
 (0)