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
|
import type {
ClassDeclaration,
ConstructorDeclaration,
FunctionDeclaration,
InterfaceDeclaration,
MethodDeclaration,
MethodSignature,
Project,
} from 'ts-morph';
import { SyntaxKind } from 'ts-morph';
import { groupBy } from '../../../src/internal/group-by';
import { newProcessingError } from './error';
import type {
RawApiDocsSignature,
SignatureLikeDeclaration,
} from './signature';
import { processSignatures } from './signature';
import type { RawApiDocsSource } from './source';
import { getSourcePath as getSource } from './source';
/**
* Represents a method in the raw API docs.
*/
export interface RawApiDocsMethod {
/**
* The name of the method.
*/
name: string;
/**
* The signatures of the method.
*/
signatures: RawApiDocsSignature[];
/**
* The source of the method.
*/
source: RawApiDocsSource;
}
// Constructors
export function processClassConstructors(
clazz: ClassDeclaration
): RawApiDocsMethod[] {
return processConstructors(clazz.getConstructors());
}
function processConstructors(
constructors: ConstructorDeclaration[]
): RawApiDocsMethod[] {
return processMethodLikes(constructors, () => 'constructor');
}
// Class Methods
export function processClassMethods(
clazz: ClassDeclaration
): RawApiDocsMethod[] {
return processMethods(getAllMethods(clazz));
}
function getAllMethods(clazz: ClassDeclaration): MethodDeclaration[] {
const parents: ClassDeclaration[] = [clazz];
let parent: ClassDeclaration | undefined = clazz;
while ((parent = parent.getBaseClass()) != null) {
parents.unshift(parent);
}
const methods: Record<string, MethodDeclaration> = {};
for (const parent of parents) {
for (const method of parent.getMethods()) {
methods[method.getName()] = method;
}
}
return Object.values(methods).sort((a, b) =>
a.getName().localeCompare(b.getName())
);
}
type NamedMethodLikeDeclaration = MethodLikeDeclaration &
Pick<MethodDeclaration, 'getName'>;
function processMethods(
methods: NamedMethodLikeDeclaration[]
): RawApiDocsMethod[] {
return processMethodLikes(methods, (v) => v.getName());
}
// Interface Methods
export function processInterfaceMethods(
iface: InterfaceDeclaration
): RawApiDocsMethod[] {
return processMethodSignatures(iface.getMethods());
}
function processMethodSignatures(
methods: MethodSignature[]
): RawApiDocsMethod[] {
const groupedSignatures = groupBy(methods, (v) => v.getName());
const methodLikes: NamedMethodLikeDeclaration[] = Object.values(
groupedSignatures
).map((signatures) => {
const signature = signatures[0];
return {
getName: () => signature.getName(),
hasModifier: () => false,
getOverloads: () => signatures,
getTypeParameters: () => signature.getTypeParameters(),
getParameters: () => signature.getParameters(),
getReturnType: () => signature.getReturnType(),
getJsDocs: () => signature.getJsDocs(),
getSourceFile: () => signature.getSourceFile(),
getStart: () => signature.getStart(),
getText: () => signature.getText(),
};
});
return processMethods(methodLikes);
}
// Functions
function getAllFunctions(
project: Project
): Record<string, FunctionDeclaration> {
return Object.fromEntries(
project
.getSourceFiles()
.flatMap((file) => file.getFunctions())
.map((fn) => [fn.getNameOrThrow(), fn] as const)
);
}
export function processUtilityFunctions(project: Project): RawApiDocsMethod[] {
return processMethodLikes(
Object.values(getAllFunctions(project)).filter((fn) =>
fn.getSourceFile().getFilePath().includes('/src/utils/')
),
(f) => f.getNameOrThrow()
);
}
// Method-likes
type MethodLikeDeclaration = SignatureLikeDeclaration &
Pick<MethodDeclaration, 'hasModifier'> & {
getOverloads(): SignatureLikeDeclaration[];
};
function processMethodLikes<T extends MethodLikeDeclaration>(
methods: T[],
nameResolver: (value: T) => string
): RawApiDocsMethod[] {
return methods
.filter((method) => !method.hasModifier(SyntaxKind.PrivateKeyword))
.map((method) => {
const name = nameResolver(method);
try {
return processMethodLike(name, method);
} catch (error) {
throw newProcessingError({
type: 'method',
name,
source: method,
cause: error,
});
}
})
.sort((a, b) => a.name.localeCompare(b.name));
}
export function processMethodLike(
name: string,
method: MethodLikeDeclaration
): RawApiDocsMethod {
console.log(` - ${name}`);
const overloads = method.getOverloads();
const signatureData: SignatureLikeDeclaration[] =
overloads.length > 0 ? overloads : [method];
const signatures = processSignatures(name, signatureData, method);
const source = getSource(method);
return {
name,
signatures,
source,
};
}
|