/
api-doc.ts
368 lines (310 loc) · 11.7 KB
/
api-doc.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// tslint:disable:no-bitwise
import {
createProgram,
displayPartsToString,
getCombinedModifierFlags,
ModifierFlags,
Program,
SyntaxKind,
TypeChecker
} from 'typescript';
import * as marked from 'marked';
function displayPartsToHtml(displayParts: any): string {
return marked(displayPartsToString(displayParts), {gfm: true}).trim();
}
function getNamesCompareFn(name = 'name') {
return (a, b) => a[name].localeCompare(b[name]);
}
const ANGULAR_LIFECYCLE_METHODS = [
'ngOnInit', 'ngOnChanges', 'ngDoCheck', 'ngOnDestroy', 'ngAfterContentInit', 'ngAfterContentChecked',
'ngAfterViewInit', 'ngAfterViewChecked', 'writeValue', 'registerOnChange', 'registerOnTouched', 'setDisabledState'
];
function hasNoJSDoc(member, typeChecker) {
if (!member.symbol) {
return true;
}
const jsDoc = displayPartsToHtml(member.symbol.getDocumentationComment(typeChecker));
return jsDoc.trim().length === 0;
}
function isInternalMember(member) {
if (member.jsDoc && member.jsDoc.length > 0) {
for (let i = 0; i < member.jsDoc.length; i++) {
if (member.jsDoc[i].tags && member.jsDoc[i].tags.length > 0) {
for (let j = 0; j < member.jsDoc[i].tags.length; j++) {
if (member.jsDoc[i].tags[j].tagName.text === 'internal') {
return true;
}
}
}
}
}
return false;
}
function isAngularLifecycleHook(methodName) {
return ANGULAR_LIFECYCLE_METHODS.includes(methodName);
}
function isPrivate(member) {
return (getCombinedModifierFlags(member) & ModifierFlags.Private) !== 0;
}
function isPrivateOrInternal(member, typeChecker) {
return isPrivate(member) || hasNoJSDoc(member, typeChecker) || isInternalMember(member);
}
function getJsDocTags(symbol) {
// clang-format off
return !symbol ? {}
: symbol.getJsDocTags()
.filter(el => ['deprecated', 'since'].includes(el.name))
.reduce(
(obj, el) => {
const[version, ...rest] = el.text.split(' ');
obj[el.name] = {version, description: rest.join(' ').trim()};
return obj;
}, {}
);
// clang-format on
}
function getTypeParameter(program, declaration) {
// getting type of 'Class <T = number>'
const type = program.getTypeChecker().getTypeAtLocation(declaration);
// checking if '<...>' part is present
if (type.typeParameters) {
// getting 'T' parameter from declaration
const parameter = type.typeParameters[0].symbol;
let parameterString = parameter.getName();
// checking if there is a default type value (ex. '= number')
const defaultType = program.getTypeChecker().getTypeAtLocation(parameter.getDeclarations()[0].default);
// default type can be 'unknown', 'error', base type (ex. 'number') or another symbol
if (defaultType && !['error', 'unknown'].includes(defaultType.intrinsicName)) {
parameterString +=
defaultType.intrinsicName ? ` = ${defaultType.intrinsicName}` : ` = ${defaultType.symbol.getName()}`;
}
return parameterString;
} else {
return undefined;
}
}
class APIDocVisitor {
private readonly program: Program;
private readonly typeChecker: TypeChecker;
constructor(fileNames: string[]) {
this.program = createProgram(fileNames, {lib: ['lib.es6.d.ts']});
this.typeChecker = this.program.getTypeChecker();
}
visitSourceFile(fileName: string) {
const sourceFile = this.program.getSourceFile(fileName);
if (!sourceFile) {
throw new Error(`File doesn't exist: ${fileName}.`);
}
return sourceFile.statements.reduce((directivesSoFar, statement) => {
if (statement.kind === SyntaxKind.ClassDeclaration) {
return directivesSoFar.concat(this.visitClassDeclaration(fileName, statement));
} else if (statement.kind === SyntaxKind.InterfaceDeclaration) {
return directivesSoFar.concat(this.visitInterfaceDeclaration(fileName, statement));
}
return directivesSoFar;
}, []);
}
visitInterfaceDeclaration(fileName, interfaceDeclaration) {
const symbol = this.typeChecker.getSymbolAtLocation(interfaceDeclaration.name);
const description = displayPartsToHtml(symbol.getDocumentationComment(this.typeChecker));
const {deprecated, since} = getJsDocTags(symbol);
const className = interfaceDeclaration.name.text;
const typeParameter = getTypeParameter(this.program, interfaceDeclaration.name);
const members = this.visitMembers(interfaceDeclaration.members);
return [{
fileName,
className,
description,
deprecated,
since,
typeParameter,
type: 'Interface',
methods: members.methods,
properties: members.properties
}];
}
visitClassDeclaration(fileName, classDeclaration) {
const symbol = this.typeChecker.getSymbolAtLocation(classDeclaration.name);
const description = displayPartsToHtml(symbol.getDocumentationComment(this.typeChecker));
const {deprecated, since} = getJsDocTags(symbol);
const className = classDeclaration.name.text;
const typeParameter = getTypeParameter(this.program, classDeclaration.name);
const decorators = classDeclaration.decorators;
let directiveInfo;
let members;
// If there is no top documentation comment, consider it private, we skip it.
if (!description) {
return [];
}
if (decorators) {
for (let i = 0; i < decorators.length; i++) {
if (this.isDirectiveDecorator(decorators[i])) {
directiveInfo = this.visitDirectiveDecorator(decorators[i]);
members = this.visitMembers(classDeclaration.members);
return [{
fileName,
className,
description,
deprecated,
since,
typeParameter,
type: directiveInfo.type,
selector: directiveInfo.selector,
exportAs: directiveInfo.exportAs,
inputs: members.inputs,
outputs: members.outputs,
properties: members.properties,
methods: members.methods
}];
} else if (this.isServiceDecorator(decorators[i])) {
members = this.visitMembers(classDeclaration.members);
return [{
fileName,
className,
description,
deprecated,
since,
typeParameter,
type: 'Service',
methods: members.methods,
properties: members.properties
}];
}
}
} else if (description) {
members = this.visitMembers(classDeclaration.members);
return [{
fileName,
className,
description,
deprecated,
since,
type: 'Class',
methods: members.methods,
properties: members.properties
}];
}
// a class that is not a directive or a service, not documented for now
return [];
}
visitDirectiveDecorator(decorator) {
let selector;
let exportAs;
const properties = decorator.expression.arguments[0].properties;
const type = decorator.expression.expression.text;
for (let i = 0; i < properties.length; i++) {
if (properties[i].name.text === 'selector') {
// TODO: this will only work if selector is initialized as a string literal
selector = properties[i].initializer.text;
}
if (properties[i].name.text === 'exportAs') {
// TODO: this will only work if selector is initialized as a string literal
exportAs = properties[i].initializer.text;
}
}
return {selector, exportAs, type};
}
visitMembers(members) {
const inputs = [];
const outputs = [];
const methods = [];
const properties = [];
let inputDecorator, outDecorator;
for (let i = 0; i < members.length; i++) {
inputDecorator = this.getDecoratorOfType(members[i], 'Input');
outDecorator = this.getDecoratorOfType(members[i], 'Output');
const {deprecated, since} = getJsDocTags(members[i].symbol);
const releaseInfo = {deprecated, since};
if (inputDecorator) {
inputs.push(Object.assign(this.visitInput(members[i], inputDecorator), releaseInfo));
} else if (outDecorator) {
outputs.push(Object.assign(this.visitOutput(members[i], outDecorator), releaseInfo));
} else if (
(members[i].kind === SyntaxKind.MethodDeclaration || members[i].kind === SyntaxKind.MethodSignature) &&
!isAngularLifecycleHook(members[i].name.text) && !isPrivateOrInternal(members[i], this.typeChecker)) {
methods.push(Object.assign(this.visitMethodDeclaration(members[i]), releaseInfo));
} else if (
(members[i].kind === SyntaxKind.PropertyDeclaration || members[i].kind === SyntaxKind.PropertySignature ||
members[i].kind === SyntaxKind.GetAccessor) &&
!isPrivate(members[i]) && !isInternalMember(members[i])) {
properties.push(Object.assign(this.visitProperty(members[i]), releaseInfo));
}
}
inputs.sort(getNamesCompareFn());
outputs.sort(getNamesCompareFn());
properties.sort(getNamesCompareFn());
return {inputs, outputs, methods, properties};
}
visitMethodDeclaration(method) {
return {
name: method.name.text,
description: displayPartsToHtml(method.symbol.getDocumentationComment(this.typeChecker)),
args: method.parameters ? method.parameters.map((prop) => this.visitArgument(prop)) : [],
returnType: this.visitType(method.type)
};
}
visitArgument(arg) { return {name: arg.name.text, type: this.visitType(arg)}; }
visitInput(property, inDecorator) {
const inArgs = inDecorator.expression.arguments;
return {
name: inArgs.length ? inArgs[0].text : property.name.text,
defaultValue: property.initializer ? this.stringifyDefaultValue(property.initializer) : undefined,
type: this.visitType(property),
description: displayPartsToHtml(property.symbol.getDocumentationComment(this.typeChecker))
};
}
stringifyDefaultValue(node) {
if (node.text) {
return node.text;
} else if (node.kind === SyntaxKind.FalseKeyword) {
return 'false';
} else if (node.kind === SyntaxKind.TrueKeyword) {
return 'true';
}
}
visitOutput(property, outDecorator) {
const outArgs = outDecorator.expression.arguments;
return {
name: outArgs.length ? outArgs[0].text : property.name.text,
description: displayPartsToHtml(property.symbol.getDocumentationComment(this.typeChecker))
};
}
visitProperty(property) {
return {
name: property.name.text,
defaultValue: property.initializer ? this.stringifyDefaultValue(property.initializer) : undefined,
type: this.visitType(property),
description: displayPartsToHtml(property.symbol.getDocumentationComment(this.typeChecker))
};
}
visitType(node) {
if (node && node.type) {
return node.type.getText();
}
return node ? this.typeChecker.typeToString(this.typeChecker.getTypeAtLocation(node)) : 'void';
}
isDirectiveDecorator(decorator) {
const decoratorIdentifierText = decorator.expression.expression.text;
return decoratorIdentifierText === 'Directive' || decoratorIdentifierText === 'Component';
}
isServiceDecorator(decorator) { return decorator.expression.expression.text === 'Injectable'; }
getDecoratorOfType(node, decoratorType) {
const decorators = node.decorators || [];
for (let i = 0; i < decorators.length; i++) {
if (decorators[i].expression.expression.text === decoratorType) {
return decorators[i];
}
}
return null;
}
}
export function parseOutApiDocs(programFiles: string[]): any {
const apiDocVisitor = new APIDocVisitor(programFiles);
return programFiles.reduce(
(soFar, file) => {
const directivesInFile = apiDocVisitor.visitSourceFile(file);
directivesInFile.forEach((directive) => { soFar[directive.className] = directive; });
return soFar;
},
{});
}