Skip to content

feat(33715): navtree does not include methods of class defined in a property #42164

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
merged 1 commit into from
Jan 15, 2021
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
35 changes: 22 additions & 13 deletions src/services/navigationBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,17 @@ namespace ts.NavigationBar {
endNode();
}

function addNodeWithRecursiveInitializer(node: VariableDeclaration | PropertyAssignment | BindingElement | PropertyDeclaration): void {
if (node.initializer && isFunctionOrClassExpression(node.initializer)) {
startNode(node);
forEachChild(node.initializer, addChildrenRecursively);
endNode();
}
else {
addNodeWithRecursiveChild(node, node.initializer);
}
}

/** Look for navigation bar items in node's subtree, adding them to the current `parent`. */
function addChildrenRecursively(node: Node | undefined): void {
curCancellationToken.throwIfCancellationRequested();
Expand Down Expand Up @@ -215,8 +226,12 @@ namespace ts.NavigationBar {
break;

case SyntaxKind.PropertyDeclaration:
if (!hasDynamicName(<ClassElement>node)) {
addNodeWithRecursiveInitializer(<PropertyDeclaration>node);
}
break;
case SyntaxKind.PropertySignature:
if (!hasDynamicName((<ClassElement | TypeElement>node))) {
if (!hasDynamicName(<TypeElement>node)) {
addLeafNode(node);
}
break;
Expand Down Expand Up @@ -255,22 +270,16 @@ namespace ts.NavigationBar {
break;
case SyntaxKind.BindingElement:
case SyntaxKind.PropertyAssignment:
case SyntaxKind.VariableDeclaration:
const { name, initializer } = <VariableDeclaration | PropertyAssignment | BindingElement>node;
if (isBindingPattern(name)) {
addChildrenRecursively(name);
}
else if (initializer && isFunctionOrClassExpression(initializer)) {
// Add a node for the VariableDeclaration, but not for the initializer.
startNode(node);
forEachChild(initializer, addChildrenRecursively);
endNode();
case SyntaxKind.VariableDeclaration: {
const child = <VariableDeclaration | PropertyAssignment | BindingElement>node;
if (isBindingPattern(child.name)) {
addChildrenRecursively(child.name);
}
else {
addNodeWithRecursiveChild(node, initializer);
addNodeWithRecursiveInitializer(child);
}
break;

}
case SyntaxKind.FunctionDeclaration:
const nameNode = (<FunctionLikeDeclaration>node).name;
// If we see a function declaration track as a possible ES5 class
Expand Down
295 changes: 295 additions & 0 deletions tests/cases/fourslash/navigationBarPropertyDeclarations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
/// <reference path="fourslash.ts"/>

////class A {
//// public A1 = class {
//// public x = 1;
//// private y() {}
//// protected z() {}
//// }
////
//// public A2 = {
//// x: 1,
//// y() {},
//// z() {}
//// }
////
//// public A3 = function () {}
//// public A4 = () => {}
//// public A5 = 1;
//// public A6 = "A6";
////
//// public ["A7"] = class {
//// public x = 1;
//// private y() {}
//// protected z() {}
//// }
////
//// public [1] = {
//// x: 1,
//// y() {},
//// z() {}
//// }
////
//// public [1 + 1] = 1;
////}

verify.navigationTree({
text: "<global>",
kind: "script",
childItems: [
{
text: "A",
kind: "class",
childItems: [
{
text: "[1]",
kind: "property",
kindModifiers: "public",
childItems: [
{
text: "x",
kind: "property"
},
{
text: "y",
kind: "method"
},
{
text: "z",
kind: "method"
}
]
},
{
text: "A1",
kind: "property",
kindModifiers: "public",
childItems: [
{
text: "x",
kind: "property",
kindModifiers: "public"
},
{
text: "y",
kind: "method",
kindModifiers: "private"
},
{
text: "z",
kind: "method",
kindModifiers: "protected"
}
]
},
{
text: "A2",
kind: "property",
kindModifiers: "public",
childItems: [
{
text: "x",
kind: "property"
},
{
text: "y",
kind: "method"
},
{
text: "z",
kind: "method"
}
]
},
{
text: "A3",
kind: "property",
kindModifiers: "public"
},
{
text: "A4",
kind: "property",
kindModifiers: "public"
},
{
text: "A5",
kind: "property",
kindModifiers: "public"
},
{
text: "A6",
kind: "property",
kindModifiers: "public"
},
{
text: "[\"A7\"]",
kind: "property",
kindModifiers: "public",
childItems: [
{
text: "x",
kind: "property",
kindModifiers: "public"
},
{
text: "y",
kind: "method",
kindModifiers: "private"
},
{
text: "z",
kind: "method",
kindModifiers: "protected"
}
]
}
]
}
]
});

verify.navigationBar([
{
text: "<global>",
kind: "script",
childItems: [
{
text: "A",
kind: "class"
}
]
},
{
text: "A",
kind: "class",
childItems: [
{
text: "[1]",
kind: "property",
kindModifiers: "public"
},
{
text: "A1",
kind: "property",
kindModifiers: "public"
},
{
text: "A2",
kind: "property",
kindModifiers: "public"
},
{
text: "A3",
kind: "property",
kindModifiers: "public"
},
{
text: "A4",
kind: "property",
kindModifiers: "public"
},
{
text: "A5",
kind: "property",
kindModifiers: "public"
},
{
text: "A6",
kind: "property",
kindModifiers: "public"
},
{
text: "[\"A7\"]",
kind: "property",
kindModifiers: "public"
}
],
indent: 1
},
{
text: "[1]",
kind: "property",
kindModifiers: "public",
childItems: [
{
text: "x",
kind: "property"
},
{
text: "y",
kind: "method"
},
{
text: "z",
kind: "method"
}
],
indent: 2
},
{
text: "A1",
kind: "property",
kindModifiers: "public",
childItems: [
{
text: "x",
kind: "property",
kindModifiers: "public"
},
{
text: "y",
kind: "method",
kindModifiers: "private"
},
{
text: "z",
kind: "method",
kindModifiers: "protected"
}
],
indent: 2
},
{
text: "A2",
kind: "property",
kindModifiers: "public",
childItems: [
{
text: "x",
kind: "property"
},
{
text: "y",
kind: "method"
},
{
text: "z",
kind: "method"
}
],
indent: 2
},
{
text: "[\"A7\"]",
kind: "property",
kindModifiers: "public",
childItems: [
{
text: "x",
kind: "property",
kindModifiers: "public"
},
{
text: "y",
kind: "method",
kindModifiers: "private"
},
{
text: "z",
kind: "method",
kindModifiers: "protected"
}
],
indent: 2
}
]);