Skip to content

Commit 4cdc970

Browse files
Produce a map of named declarations instead of a flat list.
Produce a map of named declarations instead of a flat list.
1 parent d56f2f6 commit 4cdc970

13 files changed

Lines changed: 96 additions & 91 deletions

src/services/navigateTo.ts

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ module ts.NavigateTo {
99
forEach(program.getSourceFiles(), sourceFile => {
1010
cancellationToken.throwIfCancellationRequested();
1111

12-
let declarations = sourceFile.getNamedDeclarations();
13-
for (let declaration of declarations) {
14-
var name = getDeclarationName(declaration);
15-
if (name !== undefined) {
16-
12+
let nameToDeclarations = sourceFile.getNamedDeclarations();
13+
for (let name in nameToDeclarations) {
14+
let declarations = getProperty(nameToDeclarations, name);
15+
if (declarations) {
1716
// First do a quick check to see if the name of the declaration matches the
1817
// last portion of the (possibly) dotted name they're searching for.
1918
let matches = patternMatcher.getMatchesForLastSegmentOfPattern(name);
@@ -22,24 +21,26 @@ module ts.NavigateTo {
2221
continue;
2322
}
2423

25-
// It was a match! If the pattern has dots in it, then also see if the
26-
// declaration container matches as well.
27-
if (patternMatcher.patternContainsDots) {
28-
let containers = getContainers(declaration);
29-
if (!containers) {
30-
return undefined;
31-
}
24+
for (let declaration of declarations) {
25+
// It was a match! If the pattern has dots in it, then also see if the
26+
// declaration container matches as well.
27+
if (patternMatcher.patternContainsDots) {
28+
let containers = getContainers(declaration);
29+
if (!containers) {
30+
return undefined;
31+
}
3232

33-
matches = patternMatcher.getMatches(containers, name);
33+
matches = patternMatcher.getMatches(containers, name);
3434

35-
if (!matches) {
36-
continue;
35+
if (!matches) {
36+
continue;
37+
}
3738
}
38-
}
3939

40-
let fileName = sourceFile.fileName;
41-
let matchKind = bestMatchKind(matches);
42-
rawItems.push({ name, fileName, matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration });
40+
let fileName = sourceFile.fileName;
41+
let matchKind = bestMatchKind(matches);
42+
rawItems.push({ name, fileName, matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration });
43+
}
4344
}
4445
}
4546
});
@@ -66,30 +67,14 @@ module ts.NavigateTo {
6667
return true;
6768
}
6869

69-
function getDeclarationName(declaration: Declaration): string {
70-
let result = getTextOfIdentifierOrLiteral(declaration.name);
71-
if (result !== undefined) {
72-
return result;
73-
}
74-
75-
if (declaration.name.kind === SyntaxKind.ComputedPropertyName) {
76-
let expr = (<ComputedPropertyName>declaration.name).expression;
77-
if (expr.kind === SyntaxKind.PropertyAccessExpression) {
78-
return (<PropertyAccessExpression>expr).name.text;
79-
}
80-
81-
return getTextOfIdentifierOrLiteral(expr);
82-
}
83-
84-
return undefined;
85-
}
86-
8770
function getTextOfIdentifierOrLiteral(node: Node) {
88-
if (node.kind === SyntaxKind.Identifier ||
89-
node.kind === SyntaxKind.StringLiteral ||
90-
node.kind === SyntaxKind.NumericLiteral) {
71+
if (node) {
72+
if (node.kind === SyntaxKind.Identifier ||
73+
node.kind === SyntaxKind.StringLiteral ||
74+
node.kind === SyntaxKind.NumericLiteral) {
9175

92-
return (<Identifier | LiteralExpression>node).text;
76+
return (<Identifier | LiteralExpression>node).text;
77+
}
9378
}
9479

9580
return undefined;

src/services/services.ts

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ module ts {
6363
/* @internal */ scriptSnapshot: IScriptSnapshot;
6464
/* @internal */ nameTable: Map<string>;
6565

66-
getNamedDeclarations(): Declaration[];
66+
/* @internal */ getNamedDeclarations(): Map<Declaration[]>;
67+
6768
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
6869
getLineStarts(): number[];
6970
getPositionOfLineAndCharacter(line: number, character: number): number;
@@ -749,7 +750,7 @@ module ts {
749750
public identifiers: Map<string>;
750751
public nameTable: Map<string>;
751752

752-
private namedDeclarations: Declaration[];
753+
private namedDeclarations: Map<Declaration[]>;
753754

754755
public update(newText: string, textChangeRange: TextChangeRange): SourceFile {
755756
return updateSourceFile(this, newText, textChangeRange);
@@ -767,43 +768,88 @@ module ts {
767768
return ts.getPositionOfLineAndCharacter(this, line, character);
768769
}
769770

770-
public getNamedDeclarations() {
771+
public getNamedDeclarations(): Map<Declaration[]> {
771772
if (!this.namedDeclarations) {
772773
this.namedDeclarations = this.computeNamedDeclarations();
773774
}
774775

775776
return this.namedDeclarations;
776777
}
777778

778-
private computeNamedDeclarations() {
779-
let namedDeclarations: Declaration[] = [];
779+
private computeNamedDeclarations(): Map<Declaration[]> {
780+
let result: Map<Declaration[]> = {};
780781

781782
forEachChild(this, visit);
782783

783-
return namedDeclarations;
784+
return result;
785+
786+
function addDeclaration(declaration: Declaration) {
787+
let name = getDeclarationName(declaration);
788+
if (name) {
789+
let declarations = getDeclarations(name);
790+
declarations.push(declaration);
791+
}
792+
}
793+
794+
function getDeclarations(name: string) {
795+
return getProperty(result, name) || (result[name] = []);
796+
}
797+
798+
function getDeclarationName(declaration: Declaration) {
799+
if (declaration.name) {
800+
let result = getTextOfIdentifierOrLiteral(declaration.name);
801+
if (result !== undefined) {
802+
return result;
803+
}
804+
805+
if (declaration.name.kind === SyntaxKind.ComputedPropertyName) {
806+
let expr = (<ComputedPropertyName>declaration.name).expression;
807+
if (expr.kind === SyntaxKind.PropertyAccessExpression) {
808+
return (<PropertyAccessExpression>expr).name.text;
809+
}
810+
811+
return getTextOfIdentifierOrLiteral(expr);
812+
}
813+
}
814+
815+
return undefined;
816+
}
817+
818+
function getTextOfIdentifierOrLiteral(node: Node) {
819+
if (node) {
820+
if (node.kind === SyntaxKind.Identifier ||
821+
node.kind === SyntaxKind.StringLiteral ||
822+
node.kind === SyntaxKind.NumericLiteral) {
823+
824+
return (<Identifier | LiteralExpression>node).text;
825+
}
826+
}
827+
828+
return undefined;
829+
}
784830

785831
function visit(node: Node): void {
786832
switch (node.kind) {
787833
case SyntaxKind.FunctionDeclaration:
788834
case SyntaxKind.MethodDeclaration:
789835
case SyntaxKind.MethodSignature:
790836
let functionDeclaration = <FunctionLikeDeclaration>node;
837+
let declarationName = getDeclarationName(functionDeclaration);
791838

792-
if (functionDeclaration.name && functionDeclaration.name.getFullWidth() > 0) {
793-
let lastDeclaration = namedDeclarations.length > 0 ?
794-
namedDeclarations[namedDeclarations.length - 1] :
795-
undefined;
839+
if (declarationName) {
840+
let declarations = getDeclarations(declarationName);
841+
let lastDeclaration = lastOrUndefined(declarations);
796842

797843
// Check whether this declaration belongs to an "overload group".
798-
if (lastDeclaration && functionDeclaration.symbol === lastDeclaration.symbol) {
844+
if (lastDeclaration && functionDeclaration.parent === lastDeclaration.parent && functionDeclaration.symbol === lastDeclaration.symbol) {
799845
// Overwrite the last declaration if it was an overload
800846
// and this one is an implementation.
801847
if (functionDeclaration.body && !(<FunctionLikeDeclaration>lastDeclaration).body) {
802-
namedDeclarations[namedDeclarations.length - 1] = functionDeclaration;
848+
declarations[declarations.length - 1] = functionDeclaration;
803849
}
804850
}
805851
else {
806-
namedDeclarations.push(functionDeclaration);
852+
declarations.push(functionDeclaration);
807853
}
808854

809855
forEachChild(node, visit);
@@ -824,10 +870,8 @@ module ts {
824870
case SyntaxKind.GetAccessor:
825871
case SyntaxKind.SetAccessor:
826872
case SyntaxKind.TypeLiteral:
827-
if ((<Declaration>node).name) {
828-
namedDeclarations.push(<Declaration>node);
829-
}
830-
// fall through
873+
addDeclaration(<Declaration>node);
874+
// fall through
831875
case SyntaxKind.Constructor:
832876
case SyntaxKind.VariableStatement:
833877
case SyntaxKind.VariableDeclarationList:
@@ -858,7 +902,7 @@ module ts {
858902
case SyntaxKind.EnumMember:
859903
case SyntaxKind.PropertyDeclaration:
860904
case SyntaxKind.PropertySignature:
861-
namedDeclarations.push(<Declaration>node);
905+
addDeclaration(<Declaration>node);
862906
break;
863907

864908
case SyntaxKind.ExportDeclaration:
@@ -875,15 +919,15 @@ module ts {
875919
// Handle default import case e.g.:
876920
// import d from "mod";
877921
if (importClause.name) {
878-
namedDeclarations.push(importClause);
922+
addDeclaration(importClause);
879923
}
880924

881925
// Handle named bindings in imports e.g.:
882926
// import * as NS from "mod";
883927
// import {a, b as B} from "mod";
884928
if (importClause.namedBindings) {
885929
if (importClause.namedBindings.kind === SyntaxKind.NamespaceImport) {
886-
namedDeclarations.push(<NamespaceImport>importClause.namedBindings);
930+
addDeclaration(<NamespaceImport>importClause.namedBindings);
887931
}
888932
else {
889933
forEach((<NamedImports>importClause.namedBindings).elements, visit);

tests/baselines/reference/APISample_compile.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,6 @@ declare module "typescript" {
15621562
getDocumentationComment(): SymbolDisplayPart[];
15631563
}
15641564
interface SourceFile {
1565-
getNamedDeclarations(): Declaration[];
15661565
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
15671566
getLineStarts(): number[];
15681567
getPositionOfLineAndCharacter(line: number, character: number): number;

tests/baselines/reference/APISample_compile.types

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5018,10 +5018,6 @@ declare module "typescript" {
50185018
interface SourceFile {
50195019
>SourceFile : SourceFile
50205020

5021-
getNamedDeclarations(): Declaration[];
5022-
>getNamedDeclarations : () => Declaration[]
5023-
>Declaration : Declaration
5024-
50255021
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
50265022
>getLineAndCharacterOfPosition : (pos: number) => LineAndCharacter
50275023
>pos : number

tests/baselines/reference/APISample_linter.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,6 @@ declare module "typescript" {
15931593
getDocumentationComment(): SymbolDisplayPart[];
15941594
}
15951595
interface SourceFile {
1596-
getNamedDeclarations(): Declaration[];
15971596
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
15981597
getLineStarts(): number[];
15991598
getPositionOfLineAndCharacter(line: number, character: number): number;

tests/baselines/reference/APISample_linter.types

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5164,10 +5164,6 @@ declare module "typescript" {
51645164
interface SourceFile {
51655165
>SourceFile : SourceFile
51665166

5167-
getNamedDeclarations(): Declaration[];
5168-
>getNamedDeclarations : () => Declaration[]
5169-
>Declaration : Declaration
5170-
51715167
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
51725168
>getLineAndCharacterOfPosition : (pos: number) => LineAndCharacter
51735169
>pos : number

tests/baselines/reference/APISample_linter.types.pull

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5164,10 +5164,6 @@ declare module "typescript" {
51645164
interface SourceFile {
51655165
>SourceFile : SourceFile
51665166

5167-
getNamedDeclarations(): Declaration[];
5168-
>getNamedDeclarations : () => Declaration[]
5169-
>Declaration : Declaration
5170-
51715167
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
51725168
>getLineAndCharacterOfPosition : (pos: number) => LineAndCharacter
51735169
>pos : number

tests/baselines/reference/APISample_transform.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,6 @@ declare module "typescript" {
15941594
getDocumentationComment(): SymbolDisplayPart[];
15951595
}
15961596
interface SourceFile {
1597-
getNamedDeclarations(): Declaration[];
15981597
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
15991598
getLineStarts(): number[];
16001599
getPositionOfLineAndCharacter(line: number, character: number): number;

tests/baselines/reference/APISample_transform.types

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5114,10 +5114,6 @@ declare module "typescript" {
51145114
interface SourceFile {
51155115
>SourceFile : SourceFile
51165116

5117-
getNamedDeclarations(): Declaration[];
5118-
>getNamedDeclarations : () => Declaration[]
5119-
>Declaration : Declaration
5120-
51215117
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
51225118
>getLineAndCharacterOfPosition : (pos: number) => LineAndCharacter
51235119
>pos : number

tests/baselines/reference/APISample_watcher.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1631,7 +1631,6 @@ declare module "typescript" {
16311631
getDocumentationComment(): SymbolDisplayPart[];
16321632
}
16331633
interface SourceFile {
1634-
getNamedDeclarations(): Declaration[];
16351634
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
16361635
getLineStarts(): number[];
16371636
getPositionOfLineAndCharacter(line: number, character: number): number;

0 commit comments

Comments
 (0)