Skip to content

Commit 5c10301

Browse files
committed
Hardened code and added export specifiers
1 parent 71f604a commit 5c10301

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/services/navigationBar.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,35 +63,31 @@ namespace ts.NavigationBar {
6363
case SyntaxKind.FunctionDeclaration:
6464
case SyntaxKind.ClassDeclaration:
6565
case SyntaxKind.Constructor:
66-
// TODO: Check behavior with names that are symbols, indexers, etc.
66+
case SyntaxKind.GetAccessor:
67+
case SyntaxKind.SetAccessor:
6768
const name = node.kind === SyntaxKind.Constructor ?
6869
"constructor" : declarationNameToString((node as (Declaration)).name);
6970

7071
const elementKind =
7172
node.kind === SyntaxKind.VariableDeclaration ? ScriptElementKind.variableElement :
7273
node.kind === SyntaxKind.FunctionDeclaration ? ScriptElementKind.functionElement :
73-
node.kind === SyntaxKind.ClassDeclaration ? ScriptElementKind.classElement : "constructor";
74+
node.kind === SyntaxKind.ClassDeclaration ? ScriptElementKind.classElement :
75+
node.kind === SyntaxKind.GetAccessor ? ScriptElementKind.memberGetAccessorElement :
76+
node.kind === SyntaxKind.SetAccessor ? ScriptElementKind.memberSetAccessorElement :
77+
"constructor";
7478

7579
return getNavBarItem(name, elementKind, [getNodeSpan(node)]);
7680
case SyntaxKind.FunctionExpression:
7781
case SyntaxKind.ArrowFunction:
7882
return getDefineModuleItem(node) || getFunctionExpressionItem(node);
7983
case SyntaxKind.MethodDeclaration:
8084
const methodDecl = node as MethodDeclaration;
85+
if (!methodDecl.name) {
86+
return undefined;
87+
}
8188
return getNavBarItem(declarationNameToString(methodDecl.name),
8289
ScriptElementKind.memberFunctionElement,
8390
[getNodeSpan(node)]);
84-
case SyntaxKind.GetAccessor:
85-
case SyntaxKind.SetAccessor:
86-
const accessor = node as GetAccessorDeclaration | SetAccessorDeclaration;
87-
let accessorName: string;
88-
if (accessor.name && getFullWidth(accessor.name) > 0) {
89-
accessorName = declarationNameToString(accessor.name);
90-
}
91-
else {
92-
accessorName = "<accessor>";
93-
}
94-
return getNavBarItem(accessorName, ScriptElementKind.memberGetAccessorElement, [getNodeSpan(node)]);
9591
case SyntaxKind.ExportAssignment:
9692
return getNavBarItem("default", ScriptElementKind.variableElement, [getNodeSpan(node)]);
9793
case SyntaxKind.ImportClause: // e.g. 'def' in: import def from 'mod' (in ImportDeclaration)
@@ -101,10 +97,17 @@ namespace ts.NavigationBar {
10197
}
10298
case SyntaxKind.ImportSpecifier: // e.g. 'id' in: import {id} from 'mod' (in NamedImports, in ImportClause)
10399
case SyntaxKind.NamespaceImport: // e.g. '* as ns' in: import * as ns from 'mod' (in ImportClause)
104-
// TODO: Should export specifiers add a navbar item?
105-
// They are often just references to items defined elsewhere in the file (unless renamed, or re-exported)
106-
// case SyntaxKind.ExportSpecifier: // e.g. 'a' or 'b' in: export {a, foo as b}
100+
case SyntaxKind.ExportSpecifier: // e.g. 'a' or 'b' in: export {a, foo as b} from 'mod'
101+
// Export specifiers are only interesting if they are reexports from another module, or renamed, else they are already globals
102+
if (node.kind === SyntaxKind.ExportSpecifier) {
103+
if (!(node.parent.parent as ExportDeclaration).moduleSpecifier && !(node as ExportSpecifier).propertyName) {
104+
return undefined;
105+
}
106+
}
107107
const decl = node as (ImportSpecifier | ImportClause | NamespaceImport | ExportSpecifier);
108+
if (!decl.name) {
109+
return undefined;
110+
}
108111
const declName = declarationNameToString(decl.name);
109112
return getNavBarItem(declName, ScriptElementKind.constElement, [getNodeSpan(node)]);
110113
default:

0 commit comments

Comments
 (0)