Skip to content

Commit 7546fd0

Browse files
author
Andy
authored
Simplify getContainers (microsoft#23323)
1 parent bd600cf commit 7546fd0

File tree

1 file changed

+12
-30
lines changed

1 file changed

+12
-30
lines changed

src/services/navigateTo.ts

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -67,41 +67,21 @@ namespace ts.NavigateTo {
6767
}
6868
}
6969

70-
function tryAddSingleDeclarationName(declaration: Declaration, containers: string[]): boolean {
70+
function tryAddSingleDeclarationName(declaration: Declaration, containers: Push<string>): boolean {
7171
const name = getNameOfDeclaration(declaration);
72-
if (name && isPropertyNameLiteral(name)) {
73-
containers.unshift(getTextOfIdentifierOrLiteral(name));
74-
return true;
75-
}
76-
else if (name && name.kind === SyntaxKind.ComputedPropertyName) {
77-
return tryAddComputedPropertyName(name.expression, containers, /*includeLastPortion*/ true);
78-
}
79-
else {
80-
// Don't know how to add this.
81-
return false;
82-
}
72+
return !!name && (pushLiteral(name, containers) || name.kind === SyntaxKind.ComputedPropertyName && tryAddComputedPropertyName(name.expression, containers));
8373
}
8474

8575
// Only added the names of computed properties if they're simple dotted expressions, like:
8676
//
8777
// [X.Y.Z]() { }
88-
function tryAddComputedPropertyName(expression: Expression, containers: string[], includeLastPortion: boolean): boolean {
89-
if (isPropertyNameLiteral(expression)) {
90-
const text = getTextOfIdentifierOrLiteral(expression);
91-
if (includeLastPortion) {
92-
containers.unshift(text);
93-
}
94-
return true;
95-
}
96-
if (isPropertyAccessExpression(expression)) {
97-
if (includeLastPortion) {
98-
containers.unshift(expression.name.text);
99-
}
100-
101-
return tryAddComputedPropertyName(expression.expression, containers, /*includeLastPortion*/ true);
102-
}
78+
function tryAddComputedPropertyName(expression: Expression, containers: Push<string>): boolean {
79+
return pushLiteral(expression, containers)
80+
|| isPropertyAccessExpression(expression) && (containers.push(expression.name.text), true) && tryAddComputedPropertyName(expression.expression, containers);
81+
}
10382

104-
return false;
83+
function pushLiteral(node: Node, containers: Push<string>): boolean {
84+
return isPropertyNameLiteral(node) && (containers.push(getTextOfIdentifierOrLiteral(node)), true);
10585
}
10686

10787
function getContainers(declaration: Declaration): ReadonlyArray<string> {
@@ -110,9 +90,11 @@ namespace ts.NavigateTo {
11090
// First, if we started with a computed property name, then add all but the last
11191
// portion into the container array.
11292
const name = getNameOfDeclaration(declaration);
113-
if (name && name.kind === SyntaxKind.ComputedPropertyName && !tryAddComputedPropertyName(name.expression, containers, /*includeLastPortion*/ false)) {
93+
if (name && name.kind === SyntaxKind.ComputedPropertyName && !tryAddComputedPropertyName(name.expression, containers)) {
11494
return emptyArray;
11595
}
96+
// Don't include the last portion.
97+
containers.shift();
11698

11799
// Now, walk up our containers, adding all their names to the container array.
118100
let container = getContainerNode(declaration);
@@ -125,7 +107,7 @@ namespace ts.NavigateTo {
125107
container = getContainerNode(container);
126108
}
127109

128-
return containers;
110+
return containers.reverse();
129111
}
130112

131113
function compareNavigateToItems(i1: RawNavigateToItem, i2: RawNavigateToItem) {

0 commit comments

Comments
 (0)