Skip to content

Commit 4069e0a

Browse files
Finished up "overload collapsing" for navigate-to.
Current semantics: - If an overload lacks an implementation, go to the first implementation. - If an overload has any implementation, go to the first one. - If there are any declarations between an implementation and any overload, this will split the series of overloads (note that this is invalid code).
1 parent 7822759 commit 4069e0a

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

src/services/services.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -504,28 +504,31 @@ module ts {
504504
if (!this.namedDeclarations) {
505505
var sourceFile = this;
506506
var namedDeclarations: Declaration[] = [];
507-
508-
// This keeps track of the last encountered function/method/method signature
509-
// so that we may ignore all but the first overload.
510-
var overloadDeclaration: FunctionDeclaration;
511507

512-
forEachChild(sourceFile, function visit(node: Node): boolean {
508+
forEachChild(sourceFile, function visit(node: Node): void {
513509
switch (node.kind) {
514510
case SyntaxKind.FunctionDeclaration:
515511
case SyntaxKind.Method:
516512
var functionDeclaration = <FunctionDeclaration>node;
517513

518-
// We can assume that overloadDeclaration will never be "trampled"
519-
// between consecutive overloads because we never dive into parameter initializers.
520-
if (functionDeclaration.name) {
521-
if (overloadDeclaration &&
522-
functionDeclaration.name.text === overloadDeclaration.name.text &&
523-
node.parent === overloadDeclaration.parent) {
524-
break;
514+
if (functionDeclaration.name && functionDeclaration.name.kind !== SyntaxKind.Missing) {
515+
var lastDeclaration = namedDeclarations.length > 0 ?
516+
namedDeclarations[namedDeclarations.length - 1] :
517+
undefined;
518+
519+
// Check whether this declaration belongs to an "overload group".
520+
if (lastDeclaration && functionDeclaration.symbol === lastDeclaration.symbol) {
521+
// Overwrite the last declaration if it was an overload
522+
// and this one is an implementation.
523+
if (functionDeclaration.body && !(<FunctionDeclaration>lastDeclaration).body) {
524+
namedDeclarations[namedDeclarations.length - 1] = functionDeclaration;
525+
}
526+
}
527+
else {
528+
namedDeclarations.push(node);
525529
}
526530

527-
namedDeclarations.push(functionDeclaration);
528-
overloadDeclaration = functionDeclaration;
531+
forEachChild(node, visit);
529532
}
530533
break;
531534

@@ -534,14 +537,14 @@ module ts {
534537
case SyntaxKind.EnumDeclaration:
535538
case SyntaxKind.ModuleDeclaration:
536539
case SyntaxKind.ImportDeclaration:
537-
case SyntaxKind.Constructor:
538540
case SyntaxKind.GetAccessor:
539541
case SyntaxKind.SetAccessor:
540542
case SyntaxKind.TypeLiteral:
541543
if ((<Declaration>node).name) {
542544
namedDeclarations.push(<Declaration>node);
543545
}
544546
// fall through
547+
case SyntaxKind.Constructor:
545548
case SyntaxKind.VariableStatement:
546549
case SyntaxKind.ModuleBlock:
547550
case SyntaxKind.FunctionBlock:
@@ -560,9 +563,6 @@ module ts {
560563
namedDeclarations.push(<Declaration>node);
561564
break;
562565
}
563-
564-
// do not go any deeper
565-
return undefined;
566566
});
567567

568568
this.namedDeclarations = namedDeclarations;

tests/cases/fourslash/navigationItemsOverloadsBroken.ts renamed to tests/cases/fourslash/navigationItemsOverloadsBroken1.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@
2424
//// throw overload;
2525
////}
2626

27-
verify.navigationItemsListCount(1, "overload1", "exact");
27+
verify.navigationItemsListCount(2, "overload1", "exact");
2828
verify.navigationItemsListCount(2, "overload2", "exact");
29-
verify.navigationItemsListCount(3, "overload", "prefix");
29+
verify.navigationItemsListCount(4, "overload", "prefix");
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////function overload1(a: string): boolean;
4+
////function overload1(b: boolean): boolean;
5+
////function overload1(x: any, b = (function overload() { return false })): boolean {
6+
//// throw overload1;
7+
////}
8+
////function overload1(b: number): boolean;
9+
////function overload1(f: typeof overload): boolean;
10+
11+
////function overload2(a: string): boolean;
12+
////function overload2(b: boolean): boolean;
13+
////function overload2(x: any, b = (function overload() { return false })): boolean {
14+
//// function overload2(): boolean;
15+
//// function overload2(x: any): boolean;
16+
//// throw overload2;
17+
////}
18+
////function overload2(b: number): boolean;
19+
////function overload2(f: typeof overload): boolean;
20+
21+
verify.navigationItemsListCount(1, "overload1", "exact");
22+
verify.navigationItemsListCount(3, "overload2", "exact");
23+
verify.navigationItemsListCount(4, "overload", "prefix");

0 commit comments

Comments
 (0)