Skip to content

Unify nodeKind implementations for navigationBar and navigateTo #9984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
3 commits merged into from
Jul 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 2 additions & 53 deletions src/services/navigationBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ namespace ts.NavigationBar {
function convertToTopLevelItem(n: NavigationBarNode): NavigationBarItem {
return {
text: getItemName(n.node),
kind: nodeKind(n.node),
kind: getNodeKind(n.node),
kindModifiers: getNodeModifiers(n.node),
spans: getSpans(n),
childItems: map(n.children, convertToChildItem) || emptyChildItemArray,
Expand All @@ -518,7 +518,7 @@ namespace ts.NavigationBar {
function convertToChildItem(n: NavigationBarNode): NavigationBarItem {
return {
text: getItemName(n.node),
kind: nodeKind(n.node),
kind: getNodeKind(n.node),
kindModifiers: getNodeModifiers(n.node),
spans: getSpans(n),
childItems: emptyChildItemArray,
Expand All @@ -539,57 +539,6 @@ namespace ts.NavigationBar {
}
}

// TODO: GH#9145: We should just use getNodeKind. No reason why navigationBar and navigateTo should have different behaviors.
function nodeKind(node: Node): string {
switch (node.kind) {
case SyntaxKind.SourceFile:
return ScriptElementKind.moduleElement;

case SyntaxKind.EnumMember:
return ScriptElementKind.memberVariableElement;

case SyntaxKind.VariableDeclaration:
case SyntaxKind.BindingElement:
let variableDeclarationNode: Node;
let name: Node;

if (node.kind === SyntaxKind.BindingElement) {
name = (<BindingElement>node).name;
variableDeclarationNode = node;
// binding elements are added only for variable declarations
// bubble up to the containing variable declaration
while (variableDeclarationNode && variableDeclarationNode.kind !== SyntaxKind.VariableDeclaration) {
variableDeclarationNode = variableDeclarationNode.parent;
}
Debug.assert(!!variableDeclarationNode);
}
else {
Debug.assert(!isBindingPattern((<VariableDeclaration>node).name));
variableDeclarationNode = node;
name = (<VariableDeclaration>node).name;
}

if (isConst(variableDeclarationNode)) {
return ts.ScriptElementKind.constElement;
}
else if (isLet(variableDeclarationNode)) {
return ts.ScriptElementKind.letElement;
}
else {
return ts.ScriptElementKind.variableElement;
}

case SyntaxKind.ArrowFunction:
return ts.ScriptElementKind.functionElement;

case SyntaxKind.JSDocTypedefTag:
return ScriptElementKind.typeElement;

default:
return getNodeKind(node);
}
}

function getModuleName(moduleDeclaration: ModuleDeclaration): string {
// We want to maintain quotation marks.
if (isAmbientModule(moduleDeclaration)) {
Expand Down
31 changes: 23 additions & 8 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,8 @@ namespace ts {

/** enum E */
export const enumElement = "enum";
// TODO: GH#9983
export const enumMemberElement = "const";

/**
* Inside module and script only
Expand Down Expand Up @@ -2947,19 +2949,21 @@ namespace ts {

/* @internal */ export function getNodeKind(node: Node): string {
switch (node.kind) {
case SyntaxKind.ModuleDeclaration: return ScriptElementKind.moduleElement;
case SyntaxKind.SourceFile:
return isExternalModule(<SourceFile>node) ? ScriptElementKind.moduleElement : ScriptElementKind.scriptElement;
case SyntaxKind.ModuleDeclaration:
return ScriptElementKind.moduleElement;
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
return ScriptElementKind.classElement;
case SyntaxKind.InterfaceDeclaration: return ScriptElementKind.interfaceElement;
case SyntaxKind.TypeAliasDeclaration: return ScriptElementKind.typeElement;
case SyntaxKind.EnumDeclaration: return ScriptElementKind.enumElement;
case SyntaxKind.VariableDeclaration:
return isConst(node)
? ScriptElementKind.constElement
: isLet(node)
? ScriptElementKind.letElement
: ScriptElementKind.variableElement;
return getKindOfVariableDeclaration(<VariableDeclaration>node);
case SyntaxKind.BindingElement:
return getKindOfVariableDeclaration(<VariableDeclaration>getRootDeclaration(node));
case SyntaxKind.ArrowFunction:
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
return ScriptElementKind.functionElement;
Expand All @@ -2976,16 +2980,27 @@ namespace ts {
case SyntaxKind.CallSignature: return ScriptElementKind.callSignatureElement;
case SyntaxKind.Constructor: return ScriptElementKind.constructorImplementationElement;
case SyntaxKind.TypeParameter: return ScriptElementKind.typeParameterElement;
case SyntaxKind.EnumMember: return ScriptElementKind.variableElement;
case SyntaxKind.EnumMember: return ScriptElementKind.enumMemberElement;
case SyntaxKind.Parameter: return (node.flags & NodeFlags.ParameterPropertyModifier) ? ScriptElementKind.memberVariableElement : ScriptElementKind.parameterElement;
case SyntaxKind.ImportEqualsDeclaration:
case SyntaxKind.ImportSpecifier:
case SyntaxKind.ImportClause:
case SyntaxKind.ExportSpecifier:
case SyntaxKind.NamespaceImport:
return ScriptElementKind.alias;
case SyntaxKind.JSDocTypedefTag:
return ScriptElementKind.typeElement;
default:
return ScriptElementKind.unknown;
}

function getKindOfVariableDeclaration(v: VariableDeclaration): string {
return isConst(v)
? ScriptElementKind.constElement
: isLet(v)
? ScriptElementKind.letElement
: ScriptElementKind.variableElement;
}
return ScriptElementKind.unknown;
}

class CancellationTokenObject implements CancellationToken {
Expand Down
8 changes: 4 additions & 4 deletions tests/cases/fourslash/deleteClassWithEnumPresent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edit.deleteAtCaret('class Bar { }'.length);
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "Foo",
Expand All @@ -22,15 +22,15 @@ verify.navigationBar([
"childItems": [
{
"text": "a",
"kind": "property"
"kind": "const"
},
{
"text": "b",
"kind": "property"
"kind": "const"
},
{
"text": "c",
"kind": "property"
"kind": "const"
}
],
"indent": 1
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/getNavigationBarItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "C",
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/navbar_const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "c",
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/navbar_contains-no-duplicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "ABC",
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/navbar_let.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "c",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "<function>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "A",
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/navigationBarGetterAndSetter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "X",
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/navigationBarItemsBindingPatterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "a",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "A",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "Test",
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/navigationBarItemsFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "baz",
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/navigationBarItemsFunctionsBroken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "f",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "<function>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "Class",
Expand Down Expand Up @@ -73,7 +73,7 @@ verify.navigationBar([
"childItems": [
{
"text": "LocalEnumMemberInConstructor",
"kind": "property"
"kind": "const"
}
],
"indent": 3
Expand Down Expand Up @@ -113,7 +113,7 @@ verify.navigationBar([
"childItems": [
{
"text": "LocalEnumMemberInMethod",
"kind": "property"
"kind": "const"
}
],
"indent": 3
Expand Down
8 changes: 4 additions & 4 deletions tests/cases/fourslash/navigationBarItemsItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "dist",
Expand Down Expand Up @@ -155,15 +155,15 @@ verify.navigationBar([
"childItems": [
{
"text": "value1",
"kind": "property"
"kind": "const"
},
{
"text": "value2",
"kind": "property"
"kind": "const"
},
{
"text": "value3",
"kind": "property"
"kind": "const"
}
],
"indent": 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ goTo.marker("file1");
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "Module1",
Expand All @@ -49,7 +49,7 @@ goTo.marker("file2");
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "Module1.SubModule",
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/navigationBarItemsMissingName2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "<class>",
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/navigationBarItemsModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "'X2.Y2.Z2'",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "\"Multiline\\\nMadness\"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
verify.navigationBar([
{
"text": "<global>",
"kind": "module",
"kind": "script",
"childItems": [
{
"text": "List",
Expand Down
Loading