Skip to content

Commit 8324dec

Browse files
authored
feat(33715): include methods of class defined in a property (#42164)
1 parent 22f8fa4 commit 8324dec

File tree

2 files changed

+317
-13
lines changed

2 files changed

+317
-13
lines changed

src/services/navigationBar.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,17 @@ namespace ts.NavigationBar {
183183
endNode();
184184
}
185185

186+
function addNodeWithRecursiveInitializer(node: VariableDeclaration | PropertyAssignment | BindingElement | PropertyDeclaration): void {
187+
if (node.initializer && isFunctionOrClassExpression(node.initializer)) {
188+
startNode(node);
189+
forEachChild(node.initializer, addChildrenRecursively);
190+
endNode();
191+
}
192+
else {
193+
addNodeWithRecursiveChild(node, node.initializer);
194+
}
195+
}
196+
186197
/** Look for navigation bar items in node's subtree, adding them to the current `parent`. */
187198
function addChildrenRecursively(node: Node | undefined): void {
188199
curCancellationToken.throwIfCancellationRequested();
@@ -215,8 +226,12 @@ namespace ts.NavigationBar {
215226
break;
216227

217228
case SyntaxKind.PropertyDeclaration:
229+
if (!hasDynamicName(<ClassElement>node)) {
230+
addNodeWithRecursiveInitializer(<PropertyDeclaration>node);
231+
}
232+
break;
218233
case SyntaxKind.PropertySignature:
219-
if (!hasDynamicName((<ClassElement | TypeElement>node))) {
234+
if (!hasDynamicName(<TypeElement>node)) {
220235
addLeafNode(node);
221236
}
222237
break;
@@ -255,22 +270,16 @@ namespace ts.NavigationBar {
255270
break;
256271
case SyntaxKind.BindingElement:
257272
case SyntaxKind.PropertyAssignment:
258-
case SyntaxKind.VariableDeclaration:
259-
const { name, initializer } = <VariableDeclaration | PropertyAssignment | BindingElement>node;
260-
if (isBindingPattern(name)) {
261-
addChildrenRecursively(name);
262-
}
263-
else if (initializer && isFunctionOrClassExpression(initializer)) {
264-
// Add a node for the VariableDeclaration, but not for the initializer.
265-
startNode(node);
266-
forEachChild(initializer, addChildrenRecursively);
267-
endNode();
273+
case SyntaxKind.VariableDeclaration: {
274+
const child = <VariableDeclaration | PropertyAssignment | BindingElement>node;
275+
if (isBindingPattern(child.name)) {
276+
addChildrenRecursively(child.name);
268277
}
269278
else {
270-
addNodeWithRecursiveChild(node, initializer);
279+
addNodeWithRecursiveInitializer(child);
271280
}
272281
break;
273-
282+
}
274283
case SyntaxKind.FunctionDeclaration:
275284
const nameNode = (<FunctionLikeDeclaration>node).name;
276285
// If we see a function declaration track as a possible ES5 class
Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////class A {
4+
//// public A1 = class {
5+
//// public x = 1;
6+
//// private y() {}
7+
//// protected z() {}
8+
//// }
9+
////
10+
//// public A2 = {
11+
//// x: 1,
12+
//// y() {},
13+
//// z() {}
14+
//// }
15+
////
16+
//// public A3 = function () {}
17+
//// public A4 = () => {}
18+
//// public A5 = 1;
19+
//// public A6 = "A6";
20+
////
21+
//// public ["A7"] = class {
22+
//// public x = 1;
23+
//// private y() {}
24+
//// protected z() {}
25+
//// }
26+
////
27+
//// public [1] = {
28+
//// x: 1,
29+
//// y() {},
30+
//// z() {}
31+
//// }
32+
////
33+
//// public [1 + 1] = 1;
34+
////}
35+
36+
verify.navigationTree({
37+
text: "<global>",
38+
kind: "script",
39+
childItems: [
40+
{
41+
text: "A",
42+
kind: "class",
43+
childItems: [
44+
{
45+
text: "[1]",
46+
kind: "property",
47+
kindModifiers: "public",
48+
childItems: [
49+
{
50+
text: "x",
51+
kind: "property"
52+
},
53+
{
54+
text: "y",
55+
kind: "method"
56+
},
57+
{
58+
text: "z",
59+
kind: "method"
60+
}
61+
]
62+
},
63+
{
64+
text: "A1",
65+
kind: "property",
66+
kindModifiers: "public",
67+
childItems: [
68+
{
69+
text: "x",
70+
kind: "property",
71+
kindModifiers: "public"
72+
},
73+
{
74+
text: "y",
75+
kind: "method",
76+
kindModifiers: "private"
77+
},
78+
{
79+
text: "z",
80+
kind: "method",
81+
kindModifiers: "protected"
82+
}
83+
]
84+
},
85+
{
86+
text: "A2",
87+
kind: "property",
88+
kindModifiers: "public",
89+
childItems: [
90+
{
91+
text: "x",
92+
kind: "property"
93+
},
94+
{
95+
text: "y",
96+
kind: "method"
97+
},
98+
{
99+
text: "z",
100+
kind: "method"
101+
}
102+
]
103+
},
104+
{
105+
text: "A3",
106+
kind: "property",
107+
kindModifiers: "public"
108+
},
109+
{
110+
text: "A4",
111+
kind: "property",
112+
kindModifiers: "public"
113+
},
114+
{
115+
text: "A5",
116+
kind: "property",
117+
kindModifiers: "public"
118+
},
119+
{
120+
text: "A6",
121+
kind: "property",
122+
kindModifiers: "public"
123+
},
124+
{
125+
text: "[\"A7\"]",
126+
kind: "property",
127+
kindModifiers: "public",
128+
childItems: [
129+
{
130+
text: "x",
131+
kind: "property",
132+
kindModifiers: "public"
133+
},
134+
{
135+
text: "y",
136+
kind: "method",
137+
kindModifiers: "private"
138+
},
139+
{
140+
text: "z",
141+
kind: "method",
142+
kindModifiers: "protected"
143+
}
144+
]
145+
}
146+
]
147+
}
148+
]
149+
});
150+
151+
verify.navigationBar([
152+
{
153+
text: "<global>",
154+
kind: "script",
155+
childItems: [
156+
{
157+
text: "A",
158+
kind: "class"
159+
}
160+
]
161+
},
162+
{
163+
text: "A",
164+
kind: "class",
165+
childItems: [
166+
{
167+
text: "[1]",
168+
kind: "property",
169+
kindModifiers: "public"
170+
},
171+
{
172+
text: "A1",
173+
kind: "property",
174+
kindModifiers: "public"
175+
},
176+
{
177+
text: "A2",
178+
kind: "property",
179+
kindModifiers: "public"
180+
},
181+
{
182+
text: "A3",
183+
kind: "property",
184+
kindModifiers: "public"
185+
},
186+
{
187+
text: "A4",
188+
kind: "property",
189+
kindModifiers: "public"
190+
},
191+
{
192+
text: "A5",
193+
kind: "property",
194+
kindModifiers: "public"
195+
},
196+
{
197+
text: "A6",
198+
kind: "property",
199+
kindModifiers: "public"
200+
},
201+
{
202+
text: "[\"A7\"]",
203+
kind: "property",
204+
kindModifiers: "public"
205+
}
206+
],
207+
indent: 1
208+
},
209+
{
210+
text: "[1]",
211+
kind: "property",
212+
kindModifiers: "public",
213+
childItems: [
214+
{
215+
text: "x",
216+
kind: "property"
217+
},
218+
{
219+
text: "y",
220+
kind: "method"
221+
},
222+
{
223+
text: "z",
224+
kind: "method"
225+
}
226+
],
227+
indent: 2
228+
},
229+
{
230+
text: "A1",
231+
kind: "property",
232+
kindModifiers: "public",
233+
childItems: [
234+
{
235+
text: "x",
236+
kind: "property",
237+
kindModifiers: "public"
238+
},
239+
{
240+
text: "y",
241+
kind: "method",
242+
kindModifiers: "private"
243+
},
244+
{
245+
text: "z",
246+
kind: "method",
247+
kindModifiers: "protected"
248+
}
249+
],
250+
indent: 2
251+
},
252+
{
253+
text: "A2",
254+
kind: "property",
255+
kindModifiers: "public",
256+
childItems: [
257+
{
258+
text: "x",
259+
kind: "property"
260+
},
261+
{
262+
text: "y",
263+
kind: "method"
264+
},
265+
{
266+
text: "z",
267+
kind: "method"
268+
}
269+
],
270+
indent: 2
271+
},
272+
{
273+
text: "[\"A7\"]",
274+
kind: "property",
275+
kindModifiers: "public",
276+
childItems: [
277+
{
278+
text: "x",
279+
kind: "property",
280+
kindModifiers: "public"
281+
},
282+
{
283+
text: "y",
284+
kind: "method",
285+
kindModifiers: "private"
286+
},
287+
{
288+
text: "z",
289+
kind: "method",
290+
kindModifiers: "protected"
291+
}
292+
],
293+
indent: 2
294+
}
295+
]);

0 commit comments

Comments
 (0)