-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathPropTree.ts
81 lines (66 loc) · 1.93 KB
/
PropTree.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { Dictionary } from './Dictionary';
import { warn } from './logger';
export interface IPropDesc
{
prop: IDocletProp;
name: string;
children: IPropDesc[];
}
export class PropTree
{
roots: IPropDesc[] = [];
nodes: Dictionary<IPropDesc> = {};
constructor(props: IDocletProp[])
{
// create all node for each property
for (let i = 0; i < props.length; ++i)
{
const prop = props[i];
if (!prop || !prop.name)
{
warn('Encountered a property with no name, this is likely due to invalid JSDoc. Skipping.');
continue;
}
const parts = prop.name.split('.');
this.nodes[prop.name] = {
prop,
name: parts[parts.length - 1],
children: [],
};
}
// build the tree of props
for (let i = 0; i < props.length; ++i)
{
const prop = props[i];
if (!prop || !prop.name)
continue;
const parts = prop.name.split('.');
const obj = this.nodes[prop.name];
if (!obj)
{
warn('Failed to find dot-notation property in map. This is likely a bug.');
continue;
}
if (parts.length > 1)
{
parts.pop();
let parentName = parts.join('.');
if (parentName.endsWith('[]'))
{
parentName = parentName.substring(0, parentName.length - '[]'.length);
}
const parent = this.nodes[parentName];
if (!parent)
{
// TODO LOG WARNING
continue;
}
parent.children.push(obj);
}
else
{
this.roots.push(obj)
}
}
}
}