-
Notifications
You must be signed in to change notification settings - Fork 692
/
kind.ts
177 lines (161 loc) · 5.28 KB
/
kind.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
import type { EnumKeys } from "../../utils";
/**
* Defines the available reflection kinds.
* @category Reflections
*/
export enum ReflectionKind {
Project = 0x1,
Module = 0x2,
Namespace = 0x4,
Enum = 0x8,
EnumMember = 0x10,
Variable = 0x20,
Function = 0x40,
Class = 0x80,
Interface = 0x100,
Constructor = 0x200,
Property = 0x400,
Method = 0x800,
CallSignature = 0x1000,
IndexSignature = 0x2000,
ConstructorSignature = 0x4000,
Parameter = 0x8000,
TypeLiteral = 0x10000,
TypeParameter = 0x20000,
Accessor = 0x40000,
GetSignature = 0x80000,
SetSignature = 0x100000,
TypeAlias = 0x200000,
Reference = 0x400000,
}
/** @category Reflections */
export namespace ReflectionKind {
export type KindString = EnumKeys<typeof ReflectionKind>;
export const All = ReflectionKind.Reference * 2 - 1;
/** @internal */
export const ClassOrInterface =
ReflectionKind.Class | ReflectionKind.Interface;
/** @internal */
export const VariableOrProperty =
ReflectionKind.Variable | ReflectionKind.Property;
/** @internal */
export const FunctionOrMethod =
ReflectionKind.Function | ReflectionKind.Method;
/** @internal */
export const ClassMember =
ReflectionKind.Accessor |
ReflectionKind.Constructor |
ReflectionKind.Method |
ReflectionKind.Property;
/** @internal */
export const SomeSignature =
ReflectionKind.CallSignature |
ReflectionKind.IndexSignature |
ReflectionKind.ConstructorSignature |
ReflectionKind.GetSignature |
ReflectionKind.SetSignature;
/** @internal */
export const SomeModule = ReflectionKind.Namespace | ReflectionKind.Module;
/** @internal */
export const SomeType =
ReflectionKind.Interface |
ReflectionKind.TypeLiteral |
ReflectionKind.TypeParameter |
ReflectionKind.TypeAlias;
/** @internal */
export const SomeValue = ReflectionKind.Variable | ReflectionKind.Function;
/** @internal */
export const SomeMember =
ReflectionKind.EnumMember |
ReflectionKind.Property |
ReflectionKind.Method |
ReflectionKind.Accessor;
/** @internal */
export const SomeExport =
ReflectionKind.Module |
ReflectionKind.Namespace |
ReflectionKind.Enum |
ReflectionKind.Variable |
ReflectionKind.Function |
ReflectionKind.Class |
ReflectionKind.Interface |
ReflectionKind.TypeAlias |
ReflectionKind.Reference;
/** @internal */
export const ExportContainer =
ReflectionKind.SomeModule | ReflectionKind.Project;
/** @internal */
export const Inheritable =
ReflectionKind.Accessor |
ReflectionKind.IndexSignature |
ReflectionKind.Property |
ReflectionKind.Method |
ReflectionKind.Constructor;
/** @internal */
export const ContainsCallSignatures =
ReflectionKind.Constructor |
ReflectionKind.Function |
ReflectionKind.Method;
// The differences between Type/Value here only really matter for
// possibly merged declarations where we have multiple reflections.
/** @internal */
export const TypeReferenceTarget =
ReflectionKind.Interface |
ReflectionKind.TypeAlias |
ReflectionKind.Class |
ReflectionKind.Enum;
/** @internal */
export const ValueReferenceTarget =
ReflectionKind.Module |
ReflectionKind.Namespace |
ReflectionKind.Variable |
ReflectionKind.Function;
/**
* Note: This does not include Class/Interface, even though they technically could contain index signatures
* @internal
*/
export const SignatureContainer =
ContainsCallSignatures | ReflectionKind.Accessor;
export const VariableContainer = SomeModule | ReflectionKind.Project;
export const MethodContainer =
ClassOrInterface |
VariableOrProperty |
FunctionOrMethod |
ReflectionKind.TypeLiteral;
const SINGULARS = {
[ReflectionKind.Enum]: "Enumeration",
[ReflectionKind.EnumMember]: "Enumeration Member",
};
const PLURALS = {
[ReflectionKind.Class]: "Classes",
[ReflectionKind.Property]: "Properties",
[ReflectionKind.Enum]: "Enumerations",
[ReflectionKind.EnumMember]: "Enumeration Members",
[ReflectionKind.TypeAlias]: "Type Aliases",
};
export function singularString(kind: ReflectionKind): string {
if (kind in SINGULARS) {
return SINGULARS[kind as keyof typeof SINGULARS];
} else {
return getKindString(kind);
}
}
export function pluralString(kind: ReflectionKind): string {
if (kind in PLURALS) {
return PLURALS[kind as keyof typeof PLURALS];
} else {
return getKindString(kind) + "s";
}
}
export function classString(kind: ReflectionKind): string {
return `tsd-kind-${ReflectionKind[kind]
.replace(/(.)([A-Z])/g, (_m, a, b) => `${a}-${b}`)
.toLowerCase()}`;
}
}
function getKindString(kind: ReflectionKind): string {
return ReflectionKind[kind].replace(
/(.)([A-Z])/g,
(_m, a, b) => a + " " + b.toLowerCase(),
);
}