-
Notifications
You must be signed in to change notification settings - Fork 677
/
TypePlugin.ts
163 lines (144 loc) · 6.16 KB
/
TypePlugin.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { Reflection, ReflectionKind, Decorator, DeclarationReflection, DeclarationHierarchy } from '../../models/reflections/index';
import { Type, ReferenceType, TupleType, UnionType, IntersectionType, ArrayType, TypeOperatorType } from '../../models/types/index';
import { Component, ConverterComponent } from '../components';
import { Converter } from '../converter';
import { Context } from '../context';
/**
* A handler that converts all instances of [[LateResolvingType]] to their renderable equivalents.
*/
@Component({name: 'type'})
export class TypePlugin extends ConverterComponent {
reflections: DeclarationReflection[] = [];
/**
* Create a new TypeHandler instance.
*/
initialize() {
this.listenTo(this.owner, {
[Converter.EVENT_RESOLVE]: this.onResolve,
[Converter.EVENT_RESOLVE_END]: this.onResolveEnd
});
}
/**
* Triggered when the converter resolves a reflection.
*
* @param context The context object describing the current state the converter is in.
* @param reflection The reflection that is currently resolved.
*/
private onResolve(context: Context, reflection: DeclarationReflection) {
const project = context.project;
resolveType(reflection, <ReferenceType> reflection.type);
resolveType(reflection, <ReferenceType> reflection.inheritedFrom);
resolveType(reflection, <ReferenceType> reflection.overwrites);
resolveTypes(reflection, reflection.extendedTypes);
resolveTypes(reflection, reflection.extendedBy);
resolveTypes(reflection, reflection.implementedTypes);
if (reflection.decorators) {
reflection.decorators.forEach((decorator: Decorator) => {
if (decorator.type) {
resolveType(reflection, decorator.type);
}
});
}
if (reflection.kindOf(ReflectionKind.ClassOrInterface)) {
this.postpone(reflection);
walk(reflection.implementedTypes, (target) => {
this.postpone(target);
if (!target.implementedBy) {
target.implementedBy = [];
}
target.implementedBy.push(new ReferenceType(reflection.name, ReferenceType.SYMBOL_FQN_RESOLVED, reflection));
});
walk(reflection.extendedTypes, (target) => {
this.postpone(target);
if (!target.extendedBy) {
target.extendedBy = [];
}
target.extendedBy.push(new ReferenceType(reflection.name, ReferenceType.SYMBOL_FQN_RESOLVED, reflection));
});
}
function walk(types: Type[] | undefined, callback: {(declaration: DeclarationReflection): void}) {
if (!types) {
return;
}
types.forEach(type => {
if (!(type instanceof ReferenceType)) {
return;
}
if (!type.reflection || !(type.reflection instanceof DeclarationReflection)) {
return;
}
callback(type.reflection);
});
}
function resolveTypes(reflection: Reflection, types?: Type[]) {
if (!types) {
return;
}
for (let i = 0, c = types.length; i < c; i++) {
resolveType(reflection, <ReferenceType> types[i]);
}
}
function resolveType(reflection: Reflection, type: Type) {
if (type instanceof ReferenceType) {
if (type.symbolFullyQualifiedName === ReferenceType.SYMBOL_FQN_RESOLVE_BY_NAME) {
type.reflection = reflection.findReflectionByName(type.name);
} else if (!type.reflection && type.symbolFullyQualifiedName !== ReferenceType.SYMBOL_FQN_RESOLVED) {
type.reflection = project.getReflectionFromFQN(type.symbolFullyQualifiedName);
}
if (type.typeArguments) {
resolveTypes(reflection, type.typeArguments);
}
} else if (type instanceof TupleType) {
resolveTypes(reflection, type.elements);
} else if (type instanceof UnionType || type instanceof IntersectionType) {
resolveTypes(reflection, type.types);
} else if (type instanceof ArrayType) {
resolveType(reflection, type.elementType);
} else if (type instanceof TypeOperatorType) {
resolveType(reflection, type.target);
}
}
}
private postpone(reflection: DeclarationReflection) {
if (!this.reflections.includes(reflection)) {
this.reflections.push(reflection);
}
}
/**
* Triggered when the converter has finished resolving a project.
*
* @param context The context object describing the current state the converter is in.
*/
private onResolveEnd(context: Context) {
this.reflections.forEach((reflection) => {
if (reflection.implementedBy) {
reflection.implementedBy.sort((a: Type, b: Type): number => {
if (a['name'] === b['name']) {
return 0;
}
return a['name'] > b['name'] ? 1 : -1;
});
}
let root!: DeclarationHierarchy;
let hierarchy!: DeclarationHierarchy;
function push(types: Type[]) {
const level: DeclarationHierarchy = {types: types};
if (hierarchy) {
hierarchy.next = level;
hierarchy = level;
} else {
root = hierarchy = level;
}
}
if (reflection.extendedTypes) {
push(reflection.extendedTypes);
}
push([new ReferenceType(reflection.name, ReferenceType.SYMBOL_FQN_RESOLVED, reflection)]);
hierarchy.isTarget = true;
if (reflection.extendedBy) {
push(reflection.extendedBy);
}
reflection.typeHierarchy = root;
});
}
}