Skip to content

Commit 39bcb10

Browse files
Merge pull request #804 from Microsoft/overwhelmedByOverloads
Only show first overload in a series of consecutive overload signatures
2 parents 3e594ea + 4069e0a commit 39bcb10

File tree

5 files changed

+124
-12
lines changed

5 files changed

+124
-12
lines changed

src/services/services.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -504,47 +504,65 @@ module ts {
504504
if (!this.namedDeclarations) {
505505
var sourceFile = this;
506506
var namedDeclarations: Declaration[] = [];
507-
var isExternalModule = ts.isExternalModule(sourceFile);
508507

509-
forEachChild(sourceFile, function visit(node: Node): boolean {
508+
forEachChild(sourceFile, function visit(node: Node): void {
510509
switch (node.kind) {
510+
case SyntaxKind.FunctionDeclaration:
511+
case SyntaxKind.Method:
512+
var functionDeclaration = <FunctionDeclaration>node;
513+
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);
529+
}
530+
531+
forEachChild(node, visit);
532+
}
533+
break;
534+
511535
case SyntaxKind.ClassDeclaration:
512536
case SyntaxKind.InterfaceDeclaration:
513537
case SyntaxKind.EnumDeclaration:
514538
case SyntaxKind.ModuleDeclaration:
515539
case SyntaxKind.ImportDeclaration:
516-
case SyntaxKind.Method:
517-
case SyntaxKind.FunctionDeclaration:
518-
case SyntaxKind.Constructor:
519540
case SyntaxKind.GetAccessor:
520541
case SyntaxKind.SetAccessor:
521542
case SyntaxKind.TypeLiteral:
522543
if ((<Declaration>node).name) {
523544
namedDeclarations.push(<Declaration>node);
524545
}
525-
forEachChild(node, visit);
526-
break;
527-
546+
// fall through
547+
case SyntaxKind.Constructor:
528548
case SyntaxKind.VariableStatement:
529549
case SyntaxKind.ModuleBlock:
530550
case SyntaxKind.FunctionBlock:
531551
forEachChild(node, visit);
532552
break;
533553

534554
case SyntaxKind.Parameter:
555+
// Only consider properties defined as constructor parameters
535556
if (!(node.flags & NodeFlags.AccessibilityModifier)) {
536-
// Only consider properties defined as constructor parameters
537557
break;
538558
}
559+
// fall through
539560
case SyntaxKind.VariableDeclaration:
540561
case SyntaxKind.EnumMember:
541562
case SyntaxKind.Property:
542563
namedDeclarations.push(<Declaration>node);
543564
break;
544565
}
545-
546-
// do not go any deeper
547-
return undefined;
548566
});
549567

550568
this.namedDeclarations = namedDeclarations;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////function overload(a: string): boolean;
4+
////function overload(b: boolean): boolean;
5+
////function overload(b: number): boolean;
6+
////function overload(f: typeof overload): boolean;
7+
////function overload(x: any, b = (function overload() { return false })): boolean {
8+
//// throw overload;
9+
////}
10+
////
11+
////interface I {
12+
//// interfaceMethodSignature(a: string): boolean;
13+
//// interfaceMethodSignature(b: boolean): boolean;
14+
//// interfaceMethodSignature(b: number): boolean;
15+
//// interfaceMethodSignature(f: I): boolean;
16+
////}
17+
////
18+
////class C {
19+
//// methodOverload(a: string): boolean;
20+
//// methodOverload(b: boolean): boolean;
21+
//// methodOverload(b: number): boolean;
22+
//// methodOverload(f: I): boolean;
23+
//// methodOverload(x: any, b = (function overload() { return false })): boolean {
24+
//// throw C;
25+
//// }
26+
////}
27+
28+
verify.navigationItemsListCount(1, "overload", "exact");
29+
verify.navigationItemsListCount(1, "interfaceMethodSignature", "exact");
30+
verify.navigationItemsListCount(1, "methodOverload", "exact");
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////interface I {
4+
//// interfaceMethodSignature(a: string): boolean;
5+
//// interfaceMethodSignature(b: number): boolean;
6+
//// interfaceMethodSignature(f: I): boolean;
7+
////}
8+
////interface I {
9+
//// interfaceMethodSignature(b: boolean): boolean;
10+
////}
11+
12+
verify.navigationItemsListCount(2, "interfaceMethodSignature", "exact");
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////function overload1(a: string): boolean;
4+
////function overload1(b: boolean): boolean;
5+
////function overload1(b: number): boolean;
6+
////
7+
////var heyImNotInterruptingAnythingAmI = '?';
8+
////
9+
////function overload1(f: typeof overload): boolean;
10+
////function overload1(x: any, b = (function overload() { return false })): boolean {
11+
//// throw overload;
12+
////}
13+
14+
////function overload2(a: string): boolean;
15+
////function overload2(b: boolean): boolean;
16+
////function overload2(b: number): boolean;
17+
////
18+
////function iJustRuinEverything(x: any, b = (function overload() { return false })): boolean {
19+
//// throw overload;
20+
////}
21+
////
22+
////function overload2(f: typeof overload): boolean;
23+
////function overload2(x: any, b = (function overload() { return false })): boolean {
24+
//// throw overload;
25+
////}
26+
27+
verify.navigationItemsListCount(2, "overload1", "exact");
28+
verify.navigationItemsListCount(2, "overload2", "exact");
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)