Skip to content

Commit 64c3236

Browse files
committed
fix: visualize loadChildren in corner cases
Fix #15 Fix #16
1 parent 70d4a30 commit 64c3236

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

src/background/states/module-tree.state.ts

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { State } from './state';
21
import { StaticSymbol, CompileNgModuleMetadata } from '@angular/compiler';
2+
import { ProjectSymbols, ModuleSymbol } from 'ngast';
33
import { DataSet } from 'vis';
4+
import { isAbsolute, normalize, join, sep } from 'path';
5+
6+
import { State } from './state';
47
import { ModuleState } from './module.state';
58
import { VisualizationConfig, Layout, Node, Metadata, Graph, getId, Direction, isAngularSymbol, SymbolTypes, SymbolType } from '../../shared/data-format';
6-
import { ProjectSymbols, ModuleSymbol } from 'ngast';
79
import { getModuleMetadata } from '../formatters/model-formatter';
810
import { Trie } from '../utils/trie';
9-
import { isAbsolute, normalize, join, sep } from 'path';
1011

1112
interface NodeMap {
1213
[id: string]: ModuleSymbol;
@@ -26,9 +27,7 @@ export class ModuleTreeState extends State {
2627

2728
if (!ModuleIndex.size) {
2829
rootContext.getModules()
29-
.forEach(m => {
30-
ModuleIndex.insert(getId(m.symbol), m);
31-
});
30+
.forEach(m => ModuleIndex.insert(getId(m.symbol), m));
3231
}
3332

3433
const graph = this._getModuleGraph(module);
@@ -124,9 +123,32 @@ export class ModuleTreeState extends State {
124123
moduleUriParts[0] = moduleUriParts[0] + '.ts';
125124
}
126125
if (!isAbsolute(moduleUriParts[0])) {
127-
const parent = currentPath.split(sep);
128-
parent.pop();
129-
moduleUriParts[0] = normalize(join(parent.join(sep), moduleUriParts[0]));
126+
const parentParts = currentPath.split(sep);
127+
parentParts.pop();
128+
const childParts = moduleUriParts[0].split(sep);
129+
let longestMatch = 0;
130+
const findLongestPrefix = (a: string[], b: string[], astart: number, bstart: number) => {
131+
const max = Math.min(a.length - astart, b.length - bstart);
132+
let matchLen = 0;
133+
for (let i = 0; i < max; i += 1) {
134+
if (a[i + astart] === b[i + bstart]) {
135+
matchLen += 1;
136+
} else {
137+
return matchLen;
138+
}
139+
}
140+
return matchLen;
141+
};
142+
for (let i = 0; i < parentParts.length; i += 1) {
143+
for (let j = 0; j < childParts.length; j += 1) {
144+
const currentPrefix = findLongestPrefix(parentParts, childParts, i, j);
145+
if (currentPrefix > longestMatch) {
146+
longestMatch = currentPrefix;
147+
}
148+
}
149+
}
150+
let parentPath = parentParts.slice(0, parentParts.length - longestMatch).join(sep);
151+
moduleUriParts[0] = normalize(join(parentPath, moduleUriParts[0]));
130152
}
131153
return getId({
132154
name: moduleUriParts[1],
@@ -154,9 +176,8 @@ export class ModuleTreeState extends State {
154176
return [];
155177
} else {
156178
const result: ModuleSymbol[] = [];
157-
declarations
158-
.filter(d => !!d.loadChildren)
159-
.map(d => this._loadChildrenToSymbolId(d.loadChildren))
179+
_collectLoadChildren(declarations)
180+
.map(loadChildren => this._loadChildrenToSymbolId(loadChildren))
160181
.map(id => ModuleIndex.get(id))
161182
.forEach(d => d && result.push(d));
162183
return result;
@@ -165,3 +186,17 @@ export class ModuleTreeState extends State {
165186
}
166187
}
167188
}
189+
190+
function _collectLoadChildren(routes: any[]): string[] {
191+
return routes.reduce((m, r) => {
192+
if (r.loadChildren && typeof r.loadChildren === 'string') {
193+
return m.concat(r.loadChildren);
194+
} else if (Array.isArray(r)) {
195+
return m.concat(_collectLoadChildren(r));
196+
} else if (r.children) {
197+
return m.concat(_collectLoadChildren(r.children));
198+
} else {
199+
return m;
200+
}
201+
}, []);
202+
}

0 commit comments

Comments
 (0)