-
Notifications
You must be signed in to change notification settings - Fork 679
/
declaration.ts
248 lines (213 loc) · 6.55 KB
/
declaration.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import type * as ts from "typescript";
import type { SomeType } from "..";
import { ReferenceType, ReflectionType, Type } from "../types";
import { TraverseCallback, TraverseProperty } from "./abstract";
import { ContainerReflection } from "./container";
import type { SignatureReflection } from "./signature";
import type { TypeParameterReflection } from "./type-parameter";
/**
* Stores hierarchical type data.
*
* @see {@link DeclarationReflection.typeHierarchy}
*/
export interface DeclarationHierarchy {
/**
* The types represented by this node in the hierarchy.
*/
types: Type[];
/**
* The next hierarchy level.
*/
next?: DeclarationHierarchy;
/**
* Is this the entry containing the target type?
*/
isTarget?: boolean;
}
/**
* A reflection that represents a single declaration emitted by the TypeScript compiler.
*
* All parts of a project are represented by DeclarationReflection instances. The actual
* kind of a reflection is stored in its ´kind´ member.
*/
export class DeclarationReflection extends ContainerReflection {
/**
* The escaped name of this declaration assigned by the TS compiler if there is an associated symbol.
* This is used to retrieve properties for analyzing inherited members.
* @internal
*/
escapedName?: ts.__String;
/**
* The type of the reflection.
*
* If the reflection represents a variable or a property, this is the value type.<br />
* If the reflection represents a signature, this is the return type.
*/
type?: SomeType;
typeParameters?: TypeParameterReflection[];
/**
* A list of call signatures attached to this declaration.
*
* TypeDoc creates one declaration per function that may contain one or more
* signature reflections.
*/
signatures?: SignatureReflection[];
/**
* The index signature of this declaration.
*/
indexSignature?: SignatureReflection;
/**
* The get signature of this declaration.
*/
getSignature?: SignatureReflection;
/**
* The set signature of this declaration.
*/
setSignature?: SignatureReflection;
/**
* The default value of this reflection.
*
* Applies to function parameters.
*/
defaultValue?: string;
/**
* A type that points to the reflection that has been overwritten by this reflection.
*
* Applies to interface and class members.
*/
overwrites?: ReferenceType;
/**
* A type that points to the reflection this reflection has been inherited from.
*
* Applies to interface and class members.
*/
inheritedFrom?: ReferenceType;
/**
* A type that points to the reflection this reflection is the implementation of.
*
* Applies to class members.
*/
implementationOf?: ReferenceType;
/**
* A list of all types this reflection extends (e.g. the parent classes).
*/
extendedTypes?: Type[];
/**
* A list of all types that extend this reflection (e.g. the subclasses).
*/
extendedBy?: ReferenceType[];
/**
* A list of all types this reflection implements.
*/
implementedTypes?: Type[];
/**
* A list of all types that implement this reflection.
*/
implementedBy?: ReferenceType[];
/**
* Contains a simplified representation of the type hierarchy suitable for being
* rendered in templates.
*/
typeHierarchy?: DeclarationHierarchy;
override hasGetterOrSetter(): boolean {
return !!this.getSignature || !!this.setSignature;
}
getAllSignatures(): SignatureReflection[] {
let result: SignatureReflection[] = [];
if (this.signatures) {
result = result.concat(this.signatures);
}
if (this.indexSignature) {
result.push(this.indexSignature);
}
if (this.getSignature) {
result.push(this.getSignature);
}
if (this.setSignature) {
result.push(this.setSignature);
}
return result;
}
/** @internal */
getNonIndexSignatures(): SignatureReflection[] {
return ([] as SignatureReflection[]).concat(
this.signatures ?? [],
this.setSignature ?? [],
this.getSignature ?? []
);
}
/**
* Traverse all potential child reflections of this reflection.
*
* The given callback will be invoked for all children, signatures and type parameters
* attached to this reflection.
*
* @param callback The callback function that should be applied for each child reflection.
*/
override traverse(callback: TraverseCallback) {
for (const parameter of this.typeParameters?.slice() || []) {
if (callback(parameter, TraverseProperty.TypeParameter) === false) {
return;
}
}
if (this.type instanceof ReflectionType) {
if (
callback(
this.type.declaration,
TraverseProperty.TypeLiteral
) === false
) {
return;
}
}
for (const signature of this.signatures?.slice() || []) {
if (callback(signature, TraverseProperty.Signatures) === false) {
return;
}
}
if (this.indexSignature) {
if (
callback(
this.indexSignature,
TraverseProperty.IndexSignature
) === false
) {
return;
}
}
if (this.getSignature) {
if (
callback(this.getSignature, TraverseProperty.GetSignature) ===
false
) {
return;
}
}
if (this.setSignature) {
if (
callback(this.setSignature, TraverseProperty.SetSignature) ===
false
) {
return;
}
}
super.traverse(callback);
}
/**
* Return a string representation of this reflection.
*/
override toString(): string {
let result = super.toString();
if (this.typeParameters) {
const parameters: string[] = [];
this.typeParameters.forEach((parameter) => {
parameters.push(parameter.name);
});
result += "<" + parameters.join(", ") + ">";
}
if (this.type) {
result += ":" + this.type.toString();
}
return result;
}
}