Skip to content

Commit 93dca00

Browse files
author
Andy
authored
In navigation tree, don't merge static with non-static (microsoft#20347)
1 parent 711d30c commit 93dca00

2 files changed

Lines changed: 97 additions & 34 deletions

File tree

src/services/navigationBar.ts

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -336,46 +336,53 @@ namespace ts.NavigationBar {
336336
nameToItems.set(name, [itemWithSameName, child]);
337337
return true;
338338
}
339-
340-
function tryMerge(a: NavigationBarNode, b: NavigationBarNode): boolean {
341-
if (shouldReallyMerge(a.node, b.node)) {
342-
merge(a, b);
343-
return true;
344-
}
345-
return false;
346-
}
347339
});
340+
}
348341

349-
/** a and b have the same name, but they may not be mergeable. */
350-
function shouldReallyMerge(a: Node, b: Node): boolean {
351-
return a.kind === b.kind && (a.kind !== SyntaxKind.ModuleDeclaration || areSameModule(<ModuleDeclaration>a, <ModuleDeclaration>b));
342+
function tryMerge(a: NavigationBarNode, b: NavigationBarNode): boolean {
343+
if (shouldReallyMerge(a.node, b.node)) {
344+
merge(a, b);
345+
return true;
346+
}
347+
return false;
348+
}
352349

353-
// We use 1 NavNode to represent 'A.B.C', but there are multiple source nodes.
354-
// Only merge module nodes that have the same chain. Don't merge 'A.B.C' with 'A'!
355-
function areSameModule(a: ModuleDeclaration, b: ModuleDeclaration): boolean {
356-
if (a.body.kind !== b.body.kind) {
357-
return false;
358-
}
359-
if (a.body.kind !== SyntaxKind.ModuleDeclaration) {
360-
return true;
361-
}
362-
return areSameModule(<ModuleDeclaration>a.body, <ModuleDeclaration>b.body);
363-
}
350+
/** a and b have the same name, but they may not be mergeable. */
351+
function shouldReallyMerge(a: Node, b: Node): boolean {
352+
if (a.kind !== b.kind) {
353+
return false;
364354
}
355+
switch (a.kind) {
356+
case SyntaxKind.PropertyDeclaration:
357+
case SyntaxKind.MethodDeclaration:
358+
case SyntaxKind.GetAccessor:
359+
case SyntaxKind.SetAccessor:
360+
return hasModifier(a, ModifierFlags.Static) === hasModifier(b, ModifierFlags.Static);
361+
case SyntaxKind.ModuleDeclaration:
362+
return areSameModule(<ModuleDeclaration>a, <ModuleDeclaration>b);
363+
default:
364+
return true;
365+
}
366+
}
365367

366-
/** Merge source into target. Source should be thrown away after this is called. */
367-
function merge(target: NavigationBarNode, source: NavigationBarNode): void {
368-
target.additionalNodes = target.additionalNodes || [];
369-
target.additionalNodes.push(source.node);
370-
if (source.additionalNodes) {
371-
target.additionalNodes.push(...source.additionalNodes);
372-
}
368+
// We use 1 NavNode to represent 'A.B.C', but there are multiple source nodes.
369+
// Only merge module nodes that have the same chain. Don't merge 'A.B.C' with 'A'!
370+
function areSameModule(a: ModuleDeclaration, b: ModuleDeclaration): boolean {
371+
return a.body.kind === b.body.kind && (a.body.kind !== SyntaxKind.ModuleDeclaration || areSameModule(<ModuleDeclaration>a.body, <ModuleDeclaration>b.body));
372+
}
373373

374-
target.children = concatenate(target.children, source.children);
375-
if (target.children) {
376-
mergeChildren(target.children);
377-
sortChildren(target.children);
378-
}
374+
/** Merge source into target. Source should be thrown away after this is called. */
375+
function merge(target: NavigationBarNode, source: NavigationBarNode): void {
376+
target.additionalNodes = target.additionalNodes || [];
377+
target.additionalNodes.push(source.node);
378+
if (source.additionalNodes) {
379+
target.additionalNodes.push(...source.additionalNodes);
380+
}
381+
382+
target.children = concatenate(target.children, source.children);
383+
if (target.children) {
384+
mergeChildren(target.children);
385+
sortChildren(target.children);
379386
}
380387
}
381388

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
////class C {
2+
//// static x;
3+
//// x;
4+
////}
5+
6+
// Anonymous classes are still included.
7+
verify.navigationTree({
8+
"text": "<global>",
9+
"kind": "script",
10+
"childItems": [
11+
{
12+
"text": "C",
13+
"kind": "class",
14+
"childItems": [
15+
{
16+
"text": "x",
17+
"kind": "property",
18+
"kindModifiers": "static"
19+
},
20+
{
21+
"text": "x",
22+
"kind": "property"
23+
}
24+
]
25+
}
26+
]
27+
});
28+
29+
verify.navigationBar([
30+
{
31+
"text": "<global>",
32+
"kind": "script",
33+
"childItems": [
34+
{
35+
"text": "C",
36+
"kind": "class"
37+
}
38+
]
39+
},
40+
{
41+
"text": "C",
42+
"kind": "class",
43+
"childItems": [
44+
{
45+
"text": "x",
46+
"kind": "property",
47+
"kindModifiers": "static"
48+
},
49+
{
50+
"text": "x",
51+
"kind": "property"
52+
}
53+
],
54+
"indent": 1
55+
}
56+
]);

0 commit comments

Comments
 (0)