Skip to content

Commit c696030

Browse files
author
Andy
authored
Merge pull request #9984 from Microsoft/node_kind
Unify nodeKind implementations for navigationBar and navigateTo
2 parents 878cf85 + 958620b commit c696030

34 files changed

+73
-109
lines changed

src/services/navigationBar.ts

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ namespace ts.NavigationBar {
506506
function convertToTopLevelItem(n: NavigationBarNode): NavigationBarItem {
507507
return {
508508
text: getItemName(n.node),
509-
kind: nodeKind(n.node),
509+
kind: getNodeKind(n.node),
510510
kindModifiers: getNodeModifiers(n.node),
511511
spans: getSpans(n),
512512
childItems: map(n.children, convertToChildItem) || emptyChildItemArray,
@@ -518,7 +518,7 @@ namespace ts.NavigationBar {
518518
function convertToChildItem(n: NavigationBarNode): NavigationBarItem {
519519
return {
520520
text: getItemName(n.node),
521-
kind: nodeKind(n.node),
521+
kind: getNodeKind(n.node),
522522
kindModifiers: getNodeModifiers(n.node),
523523
spans: getSpans(n),
524524
childItems: emptyChildItemArray,
@@ -539,57 +539,6 @@ namespace ts.NavigationBar {
539539
}
540540
}
541541

542-
// TODO: GH#9145: We should just use getNodeKind. No reason why navigationBar and navigateTo should have different behaviors.
543-
function nodeKind(node: Node): string {
544-
switch (node.kind) {
545-
case SyntaxKind.SourceFile:
546-
return ScriptElementKind.moduleElement;
547-
548-
case SyntaxKind.EnumMember:
549-
return ScriptElementKind.memberVariableElement;
550-
551-
case SyntaxKind.VariableDeclaration:
552-
case SyntaxKind.BindingElement:
553-
let variableDeclarationNode: Node;
554-
let name: Node;
555-
556-
if (node.kind === SyntaxKind.BindingElement) {
557-
name = (<BindingElement>node).name;
558-
variableDeclarationNode = node;
559-
// binding elements are added only for variable declarations
560-
// bubble up to the containing variable declaration
561-
while (variableDeclarationNode && variableDeclarationNode.kind !== SyntaxKind.VariableDeclaration) {
562-
variableDeclarationNode = variableDeclarationNode.parent;
563-
}
564-
Debug.assert(!!variableDeclarationNode);
565-
}
566-
else {
567-
Debug.assert(!isBindingPattern((<VariableDeclaration>node).name));
568-
variableDeclarationNode = node;
569-
name = (<VariableDeclaration>node).name;
570-
}
571-
572-
if (isConst(variableDeclarationNode)) {
573-
return ts.ScriptElementKind.constElement;
574-
}
575-
else if (isLet(variableDeclarationNode)) {
576-
return ts.ScriptElementKind.letElement;
577-
}
578-
else {
579-
return ts.ScriptElementKind.variableElement;
580-
}
581-
582-
case SyntaxKind.ArrowFunction:
583-
return ts.ScriptElementKind.functionElement;
584-
585-
case SyntaxKind.JSDocTypedefTag:
586-
return ScriptElementKind.typeElement;
587-
588-
default:
589-
return getNodeKind(node);
590-
}
591-
}
592-
593542
function getModuleName(moduleDeclaration: ModuleDeclaration): string {
594543
// We want to maintain quotation marks.
595544
if (isAmbientModule(moduleDeclaration)) {

src/services/services.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,8 @@ namespace ts {
16901690

16911691
/** enum E */
16921692
export const enumElement = "enum";
1693+
// TODO: GH#9983
1694+
export const enumMemberElement = "const";
16931695

16941696
/**
16951697
* Inside module and script only
@@ -2947,19 +2949,21 @@ namespace ts {
29472949

29482950
/* @internal */ export function getNodeKind(node: Node): string {
29492951
switch (node.kind) {
2950-
case SyntaxKind.ModuleDeclaration: return ScriptElementKind.moduleElement;
2952+
case SyntaxKind.SourceFile:
2953+
return isExternalModule(<SourceFile>node) ? ScriptElementKind.moduleElement : ScriptElementKind.scriptElement;
2954+
case SyntaxKind.ModuleDeclaration:
2955+
return ScriptElementKind.moduleElement;
29512956
case SyntaxKind.ClassDeclaration:
29522957
case SyntaxKind.ClassExpression:
29532958
return ScriptElementKind.classElement;
29542959
case SyntaxKind.InterfaceDeclaration: return ScriptElementKind.interfaceElement;
29552960
case SyntaxKind.TypeAliasDeclaration: return ScriptElementKind.typeElement;
29562961
case SyntaxKind.EnumDeclaration: return ScriptElementKind.enumElement;
29572962
case SyntaxKind.VariableDeclaration:
2958-
return isConst(node)
2959-
? ScriptElementKind.constElement
2960-
: isLet(node)
2961-
? ScriptElementKind.letElement
2962-
: ScriptElementKind.variableElement;
2963+
return getKindOfVariableDeclaration(<VariableDeclaration>node);
2964+
case SyntaxKind.BindingElement:
2965+
return getKindOfVariableDeclaration(<VariableDeclaration>getRootDeclaration(node));
2966+
case SyntaxKind.ArrowFunction:
29632967
case SyntaxKind.FunctionDeclaration:
29642968
case SyntaxKind.FunctionExpression:
29652969
return ScriptElementKind.functionElement;
@@ -2976,16 +2980,27 @@ namespace ts {
29762980
case SyntaxKind.CallSignature: return ScriptElementKind.callSignatureElement;
29772981
case SyntaxKind.Constructor: return ScriptElementKind.constructorImplementationElement;
29782982
case SyntaxKind.TypeParameter: return ScriptElementKind.typeParameterElement;
2979-
case SyntaxKind.EnumMember: return ScriptElementKind.variableElement;
2983+
case SyntaxKind.EnumMember: return ScriptElementKind.enumMemberElement;
29802984
case SyntaxKind.Parameter: return (node.flags & NodeFlags.ParameterPropertyModifier) ? ScriptElementKind.memberVariableElement : ScriptElementKind.parameterElement;
29812985
case SyntaxKind.ImportEqualsDeclaration:
29822986
case SyntaxKind.ImportSpecifier:
29832987
case SyntaxKind.ImportClause:
29842988
case SyntaxKind.ExportSpecifier:
29852989
case SyntaxKind.NamespaceImport:
29862990
return ScriptElementKind.alias;
2991+
case SyntaxKind.JSDocTypedefTag:
2992+
return ScriptElementKind.typeElement;
2993+
default:
2994+
return ScriptElementKind.unknown;
2995+
}
2996+
2997+
function getKindOfVariableDeclaration(v: VariableDeclaration): string {
2998+
return isConst(v)
2999+
? ScriptElementKind.constElement
3000+
: isLet(v)
3001+
? ScriptElementKind.letElement
3002+
: ScriptElementKind.variableElement;
29873003
}
2988-
return ScriptElementKind.unknown;
29893004
}
29903005

29913006
class CancellationTokenObject implements CancellationToken {

tests/cases/fourslash/deleteClassWithEnumPresent.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edit.deleteAtCaret('class Bar { }'.length);
88
verify.navigationBar([
99
{
1010
"text": "<global>",
11-
"kind": "module",
11+
"kind": "script",
1212
"childItems": [
1313
{
1414
"text": "Foo",
@@ -22,15 +22,15 @@ verify.navigationBar([
2222
"childItems": [
2323
{
2424
"text": "a",
25-
"kind": "property"
25+
"kind": "const"
2626
},
2727
{
2828
"text": "b",
29-
"kind": "property"
29+
"kind": "const"
3030
},
3131
{
3232
"text": "c",
33-
"kind": "property"
33+
"kind": "const"
3434
}
3535
],
3636
"indent": 1

tests/cases/fourslash/getNavigationBarItems.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
verify.navigationBar([
99
{
1010
"text": "<global>",
11-
"kind": "module",
11+
"kind": "script",
1212
"childItems": [
1313
{
1414
"text": "C",

tests/cases/fourslash/navbar_const.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
verify.navigationBar([
66
{
77
"text": "<global>",
8-
"kind": "module",
8+
"kind": "script",
99
"childItems": [
1010
{
1111
"text": "c",

tests/cases/fourslash/navbar_contains-no-duplicates.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
verify.navigationBar([
3131
{
3232
"text": "<global>",
33-
"kind": "module",
33+
"kind": "script",
3434
"childItems": [
3535
{
3636
"text": "ABC",

tests/cases/fourslash/navbar_let.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
verify.navigationBar([
66
{
77
"text": "<global>",
8-
"kind": "module",
8+
"kind": "script",
99
"childItems": [
1010
{
1111
"text": "c",

tests/cases/fourslash/navigationBarAnonymousClassAndFunctionExpressions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
verify.navigationBar([
3030
{
3131
"text": "<global>",
32-
"kind": "module",
32+
"kind": "script",
3333
"childItems": [
3434
{
3535
"text": "<function>",

tests/cases/fourslash/navigationBarAnonymousClassAndFunctionExpressions2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
verify.navigationBar([
77
{
88
"text": "<global>",
9-
"kind": "module",
9+
"kind": "script",
1010
"childItems": [
1111
{
1212
"text": "A",

tests/cases/fourslash/navigationBarGetterAndSetter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
verify.navigationBar([
1212
{
1313
"text": "<global>",
14-
"kind": "module",
14+
"kind": "script",
1515
"childItems": [
1616
{
1717
"text": "X",

tests/cases/fourslash/navigationBarItemsBindingPatterns.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
verify.navigationBar([
1010
{
1111
"text": "<global>",
12-
"kind": "module",
12+
"kind": "script",
1313
"childItems": [
1414
{
1515
"text": "a",

tests/cases/fourslash/navigationBarItemsBindingPatternsInConstructor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
verify.navigationBar([
1515
{
1616
"text": "<global>",
17-
"kind": "module",
17+
"kind": "script",
1818
"childItems": [
1919
{
2020
"text": "A",

tests/cases/fourslash/navigationBarItemsEmptyConstructors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
verify.navigationBar([
99
{
1010
"text": "<global>",
11-
"kind": "module",
11+
"kind": "script",
1212
"childItems": [
1313
{
1414
"text": "Test",

tests/cases/fourslash/navigationBarItemsFunctions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
verify.navigationBar([
1818
{
1919
"text": "<global>",
20-
"kind": "module",
20+
"kind": "script",
2121
"childItems": [
2222
{
2323
"text": "baz",

tests/cases/fourslash/navigationBarItemsFunctionsBroken.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
verify.navigationBar([
88
{
99
"text": "<global>",
10-
"kind": "module",
10+
"kind": "script",
1111
"childItems": [
1212
{
1313
"text": "f",

tests/cases/fourslash/navigationBarItemsFunctionsBroken2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
verify.navigationBar([
99
{
1010
"text": "<global>",
11-
"kind": "module",
11+
"kind": "script",
1212
"childItems": [
1313
{
1414
"text": "<function>",

tests/cases/fourslash/navigationBarItemsInsideMethodsAndConstructors.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
verify.navigationBar([
2222
{
2323
"text": "<global>",
24-
"kind": "module",
24+
"kind": "script",
2525
"childItems": [
2626
{
2727
"text": "Class",
@@ -73,7 +73,7 @@ verify.navigationBar([
7373
"childItems": [
7474
{
7575
"text": "LocalEnumMemberInConstructor",
76-
"kind": "property"
76+
"kind": "const"
7777
}
7878
],
7979
"indent": 3
@@ -113,7 +113,7 @@ verify.navigationBar([
113113
"childItems": [
114114
{
115115
"text": "LocalEnumMemberInMethod",
116-
"kind": "property"
116+
"kind": "const"
117117
}
118118
],
119119
"indent": 3

tests/cases/fourslash/navigationBarItemsItems.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
verify.navigationBar([
4343
{
4444
"text": "<global>",
45-
"kind": "module",
45+
"kind": "script",
4646
"childItems": [
4747
{
4848
"text": "dist",
@@ -155,15 +155,15 @@ verify.navigationBar([
155155
"childItems": [
156156
{
157157
"text": "value1",
158-
"kind": "property"
158+
"kind": "const"
159159
},
160160
{
161161
"text": "value2",
162-
"kind": "property"
162+
"kind": "const"
163163
},
164164
{
165165
"text": "value3",
166-
"kind": "property"
166+
"kind": "const"
167167
}
168168
],
169169
"indent": 2

tests/cases/fourslash/navigationBarItemsItemsModuleVariables.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ goTo.marker("file1");
2323
verify.navigationBar([
2424
{
2525
"text": "<global>",
26-
"kind": "module",
26+
"kind": "script",
2727
"childItems": [
2828
{
2929
"text": "Module1",
@@ -49,7 +49,7 @@ goTo.marker("file2");
4949
verify.navigationBar([
5050
{
5151
"text": "<global>",
52-
"kind": "module",
52+
"kind": "script",
5353
"childItems": [
5454
{
5555
"text": "Module1.SubModule",

tests/cases/fourslash/navigationBarItemsMissingName2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
verify.navigationBar([
1010
{
1111
"text": "<global>",
12-
"kind": "module",
12+
"kind": "script",
1313
"childItems": [
1414
{
1515
"text": "<class>",

tests/cases/fourslash/navigationBarItemsModules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
verify.navigationBar([
3131
{
3232
"text": "<global>",
33-
"kind": "module",
33+
"kind": "script",
3434
"childItems": [
3535
{
3636
"text": "'X2.Y2.Z2'",

tests/cases/fourslash/navigationBarItemsMultilineStringIdentifiers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
verify.navigationBar([
2828
{
2929
"text": "<global>",
30-
"kind": "module",
30+
"kind": "script",
3131
"childItems": [
3232
{
3333
"text": "\"Multiline\\\nMadness\"",

tests/cases/fourslash/navigationBarItemsPropertiesDefinedInConstructors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
verify.navigationBar([
1010
{
1111
"text": "<global>",
12-
"kind": "module",
12+
"kind": "script",
1313
"childItems": [
1414
{
1515
"text": "List",

0 commit comments

Comments
 (0)