-
Notifications
You must be signed in to change notification settings - Fork 679
/
reference.ts
117 lines (104 loc) · 3.23 KB
/
reference.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
import type * as ts from "typescript";
import type { ProjectReflection } from "../reflections";
import { Reflection } from "../reflections/abstract";
import { Type } from "./abstract";
/**
* Represents a type that refers to another reflection like a class, interface or enum.
*
* ~~~
* let value: MyClass;
* ~~~
*/
export class ReferenceType extends Type {
/**
* The type name identifier.
*/
readonly type = "reference";
/**
* The name of the referenced type.
*
* If the symbol cannot be found cause it's not part of the documentation this
* can be used to represent the type.
*/
name: string;
/**
* The type arguments of this reference.
*/
typeArguments?: Type[];
/**
* The resolved reflection.
*/
get reflection() {
if (typeof this._target === "number") {
return this._project.getReflectionById(this._target);
}
const resolved = this._project.getReflectionFromSymbol(this._target);
if (resolved) this._target = resolved.id;
return resolved;
}
/**
* Horrible hacky solution to get around Handlebars messing with `this` in bad ways.
* Don't use this if possible, it will go away once we use something besides handlebars for themes.
*/
getReflection = () => this.reflection;
private _target: ts.Symbol | number;
private _project: ProjectReflection;
/**
* Create a new instance of ReferenceType.
*/
constructor(
name: string,
target: ts.Symbol | Reflection | number,
project: ProjectReflection
) {
super();
this.name = name;
this._target = target instanceof Reflection ? target.id : target;
this._project = project;
}
/** @internal this is used for type parameters, which don't actually point to something */
static createBrokenReference(name: string, project: ProjectReflection) {
return new ReferenceType(name, -1, project);
}
/**
* Clone this type.
*
* @return A clone of this type.
*/
clone(): Type {
const clone = new ReferenceType(this.name, this._target, this._project);
clone.typeArguments = this.typeArguments;
return clone;
}
/**
* Test whether this type equals the given type.
*
* @param other The type that should be checked for equality.
* @returns TRUE if the given type equals this type, FALSE otherwise.
*/
equals(other: ReferenceType): boolean {
if (other instanceof ReferenceType) {
if (this.reflection != null) {
return this.reflection === other.reflection;
}
return this._target === other._target;
}
return false;
}
/**
* Return a string representation of this type.
* @example EventEmitter<any>
*/
toString() {
const name = this.reflection ? this.reflection.name : this.name;
let typeArgs = "";
if (this.typeArguments && this.typeArguments.length > 0) {
typeArgs += "<";
typeArgs += this.typeArguments
.map((arg) => arg.toString())
.join(", ");
typeArgs += ">";
}
return name + typeArgs;
}
}