|
1
|
import ts from "typescript" |
|
2
|
import { |
|
3
|
DOCBLOCK_START, |
|
4
|
DOCBLOCK_END_LINE, |
|
5
|
DOCBLOCK_DOUBLE_LINES, |
|
6
|
DOCBLOCK_NEW_LINE, |
|
7
|
SUMMARY_PLACEHOLDER, |
|
8
|
} from "../../constants.js" |
|
9
|
import getSymbol from "../../utils/get-symbol.js" |
|
10
|
import KnowledgeBaseFactory, { |
|
11
|
RetrieveOptions, |
|
12
|
} from "../helpers/knowledge-base-factory.js" |
|
13
|
import { |
|
14
|
getCustomNamespaceTag, |
|
15
|
shouldHaveCustomNamespace, |
|
16
|
} from "../../utils/medusa-react-utils.js" |
|
17
|
import GeneratorEventManager from "../helpers/generator-event-manager.js" |
|
18
|
import { CommonCliOptions } from "../../types/index.js" |
|
19
|
import { camelToWords, capitalize } from "utils" |
|
20
|
import { normalizeName } from "../../utils/str-formatting.js" |
|
21
|
|
|
22
|
export type GeneratorOptions = { |
|
23
|
checker: ts.TypeChecker |
|
24
|
kinds?: ts.SyntaxKind[] |
|
25
|
generatorEventManager: GeneratorEventManager |
|
26
|
additionalOptions?: Pick<CommonCliOptions, "generateExamples"> |
|
27
|
} |
|
28
|
|
|
29
|
export type GetDocBlockOptions = { |
|
30
|
addEnd?: boolean |
|
31
|
summaryPrefix?: string |
|
32
|
} |
|
33
|
|
|
34
|
type CommonDocsOptions = { |
|
35
|
addDefaultSummary?: boolean |
|
36
|
prefixWithLineBreaks?: boolean |
|
37
|
} |
|
38
|
|
|
39
|
/** |
|
40
|
* Class used to generate docblocks for basic kinds. It can be |
|
41
|
* extended for kinds requiring more elaborate TSDocs. |
|
42
|
*/ |
|
43
|
class DefaultKindGenerator<T extends ts.Node = ts.Node> { |
|
44
|
static DEFAULT_ALLOWED_NODE_KINDS = [ |
|
45
|
ts.SyntaxKind.SourceFile, |
|
46
|
ts.SyntaxKind.ClassDeclaration, |
|
47
|
ts.SyntaxKind.EnumDeclaration, |
|
48
|
ts.SyntaxKind.EnumMember, |
|
49
|
ts.SyntaxKind.ModuleDeclaration, |
|
50
|
ts.SyntaxKind.PropertyDeclaration, |
|
51
|
ts.SyntaxKind.InterfaceDeclaration, |
|
52
|
ts.SyntaxKind.TypeAliasDeclaration, |
|
53
|
ts.SyntaxKind.PropertySignature, |
|
54
|
] |
|
55
|
public name = "default" |
|
56
|
protected allowedKinds: ts.SyntaxKind[] |
|
57
|
protected checker: ts.TypeChecker |
|
58
|
protected knowledgeBaseFactory: KnowledgeBaseFactory |
|
59
|
protected generatorEventManager: GeneratorEventManager |
|
60
|
protected options: Pick<CommonCliOptions, "generateExamples"> |
|
61
|
|
|
62
|
constructor({ |
|
63
|
checker, |
|
64
|
kinds, |
|
65
|
generatorEventManager, |
|
66
|
additionalOptions = {}, |
|
67
|
}: GeneratorOptions) { |
|
68
|
this.allowedKinds = kinds || DefaultKindGenerator.DEFAULT_ALLOWED_NODE_KINDS |
|
69
|
this.checker = checker |
|
70
|
this.knowledgeBaseFactory = new KnowledgeBaseFactory() |
|
71
|
this.generatorEventManager = generatorEventManager |
|
72
|
this.options = additionalOptions |
|
73
|
} |
|
74
|
|
|
75
|
/** |
|
76
|
* @returns the kinds that are handled by this generator. |
|
77
|
*/ |
|
78
|
getAllowedKinds(): ts.SyntaxKind[] { |
|
79
|
return this.allowedKinds |
|
80
|
} |
|
81
|
|
|
82
|
/** |
|
83
|
* Check whether this generator can be used for a node based on the node's kind. |
|
84
|
* |
|
85
|
* @param {ts.Node} node - The node to check for. |
|
86
|
* @returns {boolean} Whether this generator can be used with the specified node. |
|
87
|
*/ |
|
88
|
isAllowed(node: ts.Node): node is T { |
|
89
|
return !this.isIgnored(node) && this.allowedKinds.includes(node.kind) |
|
90
|
} |
|
91
|
|
|
92
|
/** |
|
93
|
* Retrieve the doc block for the passed node. |
|
94
|
* |
|
95
|
* @param {T | ts.Node} node - The node to retrieve the docblock for. |
|
96
|
* @param {GetDocBlockOptions} options - Options useful for children classes of this class to specify the formatting of the docblock. |
|
97
|
* @returns {string} The node's docblock. |
|
98
|
*/ |
|
99
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
|
100
|
async getDocBlock( |
|
101
|
node: T | ts.Node, |
|
102
|
options: GetDocBlockOptions = { addEnd: true } |
|
103
|
): Promise<string> { |
|
104
|
let str = DOCBLOCK_START |
|
105
|
const summary = this.getNodeSummary({ node }) |
|
106
|
|
|
107
|
switch (node.kind) { |
|
108
|
case ts.SyntaxKind.EnumDeclaration: |
|
109
|
str += `@enum${DOCBLOCK_DOUBLE_LINES}${summary}` |
|
110
|
break |
|
111
|
case ts.SyntaxKind.TypeAliasDeclaration: |
|
112
|
str += `@interface${DOCBLOCK_DOUBLE_LINES}${summary}` |
|
113
|
break |
|
114
|
default: |
|
115
|
str += summary |
|
116
|
} |
|
117
|
|
|
118
|
str += this.getCommonDocs(node, { |
|
119
|
prefixWithLineBreaks: true, |
|
120
|
}) |
|
121
|
|
|
122
|
return `${str}${options.addEnd ? DOCBLOCK_END_LINE : ""}` |
|
123
|
} |
|
124
|
|
|
125
|
/** |
|
126
|
* Retrieves the summary comment of a node. It gives precedense to the node's symbol if it's provided/retrieved and if it's available using the {@link getSymbolDocBlock}. |
|
127
|
* Otherwise, it retrieves the comments of the type using the {@link getTypeDocBlock} |
|
128
|
* @returns {string} The summary comment. |
|
129
|
*/ |
|
130
|
getNodeSummary({ |
|
131
|
node, |
|
132
|
symbol, |
|
133
|
nodeType, |
|
134
|
knowledgeBaseOptions: overrideOptions, |
|
135
|
}: { |
|
136
|
/** |
|
137
|
* The node to retrieve the summary comment for. |
|
138
|
*/ |
|
139
|
node: T | ts.Node |
|
140
|
/** |
|
141
|
* Optionally provide the node's symbol. If not provided, the |
|
142
|
* method will try to retrieve it. |
|
143
|
*/ |
|
144
|
symbol?: ts.Symbol |
|
145
|
/** |
|
146
|
* Optionally provide the node's type. If not provided, the method |
|
147
|
* will try to retrieve it. |
|
148
|
*/ |
|
149
|
nodeType?: ts.Type |
|
150
|
/** |
|
151
|
* Override any of the default knowledge base options |
|
152
|
* inferred using the {@link getKnowledgeBaseOptions} method |
|
153
|
*/ |
|
154
|
knowledgeBaseOptions?: Partial<RetrieveOptions> |
|
155
|
}): string { |
|
156
|
const syntheticComments = ts.getSyntheticLeadingComments(node) |
|
157
|
if (syntheticComments?.length) { |
|
158
|
return syntheticComments.map((comment) => comment.text).join(" ") |
|
159
|
} |
|
160
|
const knowledgeBaseOptions = { |
|
161
|
...this.getKnowledgeBaseOptions(node), |
|
162
|
...overrideOptions, |
|
163
|
} |
|
164
|
if (!nodeType) { |
|
165
|
nodeType = |
|
166
|
"type" in node && node.type && ts.isTypeNode(node.type as ts.Node) |
|
167
|
? this.checker.getTypeFromTypeNode(node.type as ts.TypeNode) |
|
168
|
: symbol |
|
169
|
? this.checker.getTypeOfSymbolAtLocation(symbol, node) |
|
170
|
: this.checker.getTypeAtLocation(node) |
|
171
|
} |
|
172
|
|
|
173
|
if (!symbol) { |
|
174
|
symbol = getSymbol(node, this.checker) |
|
175
|
} |
|
176
|
|
|
177
|
let summary = "" |
|
178
|
|
|
179
|
if (symbol) { |
|
180
|
summary = this.getSymbolDocBlock(symbol, knowledgeBaseOptions) |
|
181
|
} |
|
182
|
|
|
183
|
if (!summary.length) { |
|
184
|
summary = this.getTypeDocBlock(nodeType, knowledgeBaseOptions) |
|
185
|
} |
|
186
|
|
|
187
|
return summary.length > 0 ? summary : SUMMARY_PLACEHOLDER |
|
188
|
} |
|
189
|
|
|
190
|
/** |
|
191
|
* Retrieves the summary comment of a type. It tries to retrieve from the alias symbol, type arguments, or {@link KnowledgeBaseFactory}. |
|
192
|
* If no summary comments are found, the {@link defaultSummary} is used. |
|
193
|
* |
|
194
|
* @param {ts.Type} nodeType - The type of a node. |
|
195
|
* @returns {string} The summary comment. |
|
196
|
*/ |
|
197
|
protected getTypeDocBlock( |
|
198
|
nodeType: ts.Type, |
|
199
|
knowledgeBaseOptions?: Partial<RetrieveOptions> |
|
200
|
): string { |
|
201
|
if (nodeType.aliasSymbol || nodeType.symbol) { |
|
202
|
const symbolDoc = this.getSymbolDocBlock( |
|
203
|
nodeType.aliasSymbol || nodeType.symbol |
|
204
|
) |
|
205
|
|
|
206
|
if (symbolDoc.length) { |
|
207
|
return symbolDoc |
|
208
|
} |
|
209
|
} |
|
210
|
|
|
211
|
const typeArguments = this.checker.getTypeArguments( |
|
212
|
nodeType as ts.TypeReference |
|
213
|
) |
|
214
|
|
|
215
|
if (typeArguments.length) { |
|
216
|
// take only the first type argument to account |
|
217
|
const typeArgumentDoc = this.getTypeDocBlock(typeArguments[0]) |
|
218
|
|
|
219
|
if (!typeArgumentDoc.length) { |
|
220
|
const tryKnowledgeSummary = this.knowledgeBaseFactory.tryToGetSummary({ |
|
221
|
...knowledgeBaseOptions, |
|
222
|
str: this.checker.typeToString(nodeType), |
|
223
|
}) |
|
224
|
|
|
225
|
if (tryKnowledgeSummary?.length) { |
|
226
|
return tryKnowledgeSummary |
|
227
|
} |
|
228
|
} |
|
229
|
|
|
230
|
if (!this.checker.isArrayType(nodeType)) { |
|
231
|
return typeArgumentDoc |
|
232
|
} |
|
233
|
|
|
234
|
// do some formatting if the encapsulating type is an array |
|
235
|
return `The list of ${capitalize(typeArgumentDoc) || SUMMARY_PLACEHOLDER}` |
|
236
|
} |
|
237
|
|
|
238
|
return ( |
|
239
|
this.knowledgeBaseFactory.tryToGetSummary({ |
|
240
|
...knowledgeBaseOptions, |
|
241
|
str: this.checker.typeToString(nodeType), |
|
242
|
}) || "" |
|
243
|
) |
|
244
|
} |
|
245
|
|
|
246
|
/** |
|
247
|
* Retrieves the docblock of a symbol. It tries to retrieve it using the symbol's `getDocumentationComment` and `getJsDocTags` |
|
248
|
* methods. If both methods don't return any comments, it tries to get the comments from the {@link KnowledgeBaseFactory}. |
|
249
|
* |
|
250
|
* @param {ts.Symbol} symbol - The symbol to retrieve its docblock. |
|
251
|
* @returns {string} The symbol's docblock. |
|
252
|
*/ |
|
253
|
protected getSymbolDocBlock( |
|
254
|
symbol: ts.Symbol, |
|
255
|
knowledgeBaseOptions?: Partial<RetrieveOptions> |
|
256
|
): string { |
|
257
|
const commentDisplayParts = symbol.getDocumentationComment(this.checker) |
|
258
|
if (!commentDisplayParts.length) { |
|
259
|
// try to get description from the first JSDoc comment |
|
260
|
const jsdocComments = symbol.getJsDocTags(this.checker) |
|
261
|
|
|
262
|
if (jsdocComments.length) { |
|
263
|
jsdocComments |
|
264
|
.find((tag) => (tag.text?.length || 0) > 0) |
|
265
|
?.text!.forEach((tagText) => { |
|
266
|
commentDisplayParts.push(tagText) |
|
267
|
}) |
|
268
|
} |
|
269
|
} |
|
270
|
|
|
271
|
if (!commentDisplayParts.length) { |
|
272
|
return ( |
|
273
|
this.knowledgeBaseFactory.tryToGetSummary({ |
|
274
|
...knowledgeBaseOptions, |
|
275
|
str: this.checker.typeToString(this.checker.getTypeOfSymbol(symbol)), |
|
276
|
}) || |
|
277
|
this.knowledgeBaseFactory.tryToGetSummary({ |
|
278
|
...knowledgeBaseOptions, |
|
279
|
str: symbol.name, |
|
280
|
}) || |
|
281
|
"" |
|
282
|
) |
|
283
|
} |
|
284
|
|
|
285
|
return ts |
|
286
|
.displayPartsToString(commentDisplayParts) |
|
287
|
.replaceAll("\n", DOCBLOCK_NEW_LINE) |
|
288
|
} |
|
289
|
|
|
290
|
/** |
|
291
|
* Retrieves docblocks based on decorators used on a symbol. |
|
292
|
* |
|
293
|
* @param {ts.Symbol} symbol - The symbol to retrieve its decorators docblock. |
|
294
|
* @returns {string} The symbol's decorators docblock. |
|
295
|
*/ |
|
296
|
getDecoratorDocs(symbol: ts.Symbol): string { |
|
297
|
let str = "" |
|
298
|
|
|
299
|
symbol.declarations?.forEach((declaration) => { |
|
300
|
const modifiers = |
|
301
|
"modifiers" in declaration && declaration.modifiers |
|
302
|
? (declaration.modifiers as ts.NodeArray<ts.Modifier>) |
|
303
|
: [] |
|
304
|
|
|
305
|
modifiers.forEach((modifier) => { |
|
306
|
if (!ts.isDecorator(modifier)) { |
|
307
|
return |
|
308
|
} |
|
309
|
|
|
310
|
// check for decorator text |
|
311
|
;(modifier as ts.Decorator).forEachChild((childNode) => { |
|
312
|
if (ts.isCallExpression(childNode)) { |
|
313
|
const childNodeExpression = (childNode as ts.CallExpression) |
|
314
|
.expression |
|
315
|
if (ts.isIdentifier(childNodeExpression)) { |
|
316
|
switch (childNodeExpression.escapedText) { |
|
317
|
case "FeatureFlagEntity": |
|
318
|
// add the `@featureFlag` tag. |
|
319
|
str += `${DOCBLOCK_DOUBLE_LINES}@featureFlag [flag_name]` |
|
320
|
break |
|
321
|
case "BeforeInsert": |
|
322
|
case "BeforeLoad": |
|
323
|
case "AfterLoad": |
|
324
|
// add `@apiIgnore` tag |
|
325
|
str += `${DOCBLOCK_DOUBLE_LINES}@apiIgnore` |
|
326
|
} |
|
327
|
} |
|
328
|
} |
|
329
|
}) |
|
330
|
}) |
|
331
|
}) |
|
332
|
|
|
333
|
return str |
|
334
|
} |
|
335
|
|
|
336
|
/** |
|
337
|
* Retrieve docblocks that are common to all nodes, despite their kind. |
|
338
|
* |
|
339
|
* @param {T | ts.Node} node - The node to retrieve its common doc blocks. |
|
340
|
* @param {CommonDocsOptions} options - Formatting options. |
|
341
|
* @returns {string} The common docblocks. |
|
342
|
*/ |
|
343
|
getCommonDocs( |
|
344
|
node: T | ts.Node, |
|
345
|
options: CommonDocsOptions = { addDefaultSummary: false } |
|
346
|
): string { |
|
347
|
const tags = new Set<string>() |
|
348
|
|
|
349
|
const symbol = getSymbol(node, this.checker) |
|
350
|
|
|
351
|
if (!symbol) { |
|
352
|
return "" |
|
353
|
} |
|
354
|
|
|
355
|
if (ts.isSourceFile(node)) { |
|
356
|
// comments for source files must start with this tag |
|
357
|
tags.add(`@packageDocumentation`) |
|
358
|
} |
|
359
|
|
|
360
|
if (options.addDefaultSummary) { |
|
361
|
tags.add(SUMMARY_PLACEHOLDER) |
|
362
|
} |
|
363
|
|
|
364
|
// check for private or protected modifiers |
|
365
|
// and if found, add the `@ignore` tag. |
|
366
|
symbol.declarations?.some((declaration) => { |
|
367
|
if (!("modifiers" in declaration) || !declaration.modifiers) { |
|
368
|
return false |
|
369
|
} |
|
370
|
|
|
371
|
const hasPrivateOrProtected = ( |
|
372
|
declaration.modifiers as ts.NodeArray<ts.Modifier> |
|
373
|
).find((modifier) => { |
|
374
|
modifier.kind === ts.SyntaxKind.PrivateKeyword || |
|
375
|
modifier.kind === ts.SyntaxKind.ProtectedKeyword |
|
376
|
}) |
|
377
|
|
|
378
|
if (!hasPrivateOrProtected) { |
|
379
|
return false |
|
380
|
} |
|
381
|
|
|
382
|
tags.add("@ignore") |
|
383
|
return true |
|
384
|
}) |
|
385
|
|
|
386
|
// if a symbol's name starts with `_` then we |
|
387
|
// should add the `@ignore` tag |
|
388
|
if (symbol.getName().startsWith("_")) { |
|
389
|
tags.add("@ignore") |
|
390
|
} |
|
391
|
|
|
392
|
// check if any docs can be added for the symbol's |
|
393
|
// decorators |
|
394
|
this.getDecoratorDocs(symbol) |
|
395
|
.split(`${DOCBLOCK_DOUBLE_LINES}`) |
|
396
|
.filter((docItem) => docItem.length > 0) |
|
397
|
.forEach((docItem) => tags.add(docItem)) |
|
398
|
|
|
399
|
// add `@expandable` tag if the resource is |
|
400
|
if (ts.isPropertyDeclaration(node)) { |
|
401
|
const symbolType = this.checker.getTypeOfSymbol(symbol) |
|
402
|
if ( |
|
403
|
symbolType.symbol?.declarations?.length && |
|
404
|
ts.isClassDeclaration(symbolType.symbol?.declarations[0]) && |
|
405
|
this.isEntity({ |
|
406
|
heritageClauses: ( |
|
407
|
symbolType.symbol?.declarations[0] as ts.ClassDeclaration |
|
408
|
).heritageClauses, |
|
409
|
node: symbolType.symbol?.declarations[0], |
|
410
|
}) |
|
411
|
) { |
|
412
|
tags.add(`@expandable`) |
|
413
|
} |
|
414
|
} |
|
415
|
|
|
416
|
// check if custom namespace should be added |
|
417
|
if (shouldHaveCustomNamespace(node)) { |
|
418
|
tags.add(getCustomNamespaceTag(node)) |
|
419
|
} |
|
420
|
|
|
421
|
// check for default value |
|
422
|
const defaultValue = this.getDefaultValue(node) |
|
423
|
if (defaultValue?.length) { |
|
424
|
tags.add(`@defaultValue ${defaultValue}`) |
|
425
|
} |
|
426
|
|
|
427
|
let str = "" |
|
428
|
tags.forEach((tag) => { |
|
429
|
if (str.length > 0) { |
|
430
|
str += `${DOCBLOCK_DOUBLE_LINES}` |
|
431
|
} |
|
432
|
str += `${tag}` |
|
433
|
}) |
|
434
|
|
|
435
|
if (str.length && options.prefixWithLineBreaks) { |
|
436
|
str = `${DOCBLOCK_DOUBLE_LINES}${str}` |
|
437
|
} |
|
438
|
|
|
439
|
return str |
|
440
|
} |
|
441
|
|
|
442
|
/** |
|
443
|
* Check if a node is a Medusa entity. |
|
444
|
* @returns {boolean} Whether the node is a Medusa entity. |
|
445
|
*/ |
|
446
|
isEntity({ |
|
447
|
/** |
|
448
|
* The inherit/extend keywords of the node. |
|
449
|
*/ |
|
450
|
heritageClauses, |
|
451
|
/** |
|
452
|
* Optionally provide the node to accurately retrieve its type name. |
|
453
|
*/ |
|
454
|
node, |
|
455
|
}: { |
|
456
|
heritageClauses?: ts.NodeArray<ts.HeritageClause> |
|
457
|
node?: ts.Node |
|
458
|
}): boolean { |
|
459
|
return ( |
|
460
|
heritageClauses?.some((heritageClause) => { |
|
461
|
return heritageClause.types.some((heritageClauseType) => { |
|
462
|
const symbolType = this.checker.getTypeAtLocation( |
|
463
|
heritageClauseType.expression |
|
464
|
) |
|
465
|
|
|
466
|
if ( |
|
467
|
this.checker |
|
468
|
.typeToString(symbolType, node, undefined) |
|
469
|
.includes("BaseEntity") |
|
470
|
) { |
|
471
|
return true |
|
472
|
} |
|
473
|
|
|
474
|
if ( |
|
475
|
symbolType.symbol?.valueDeclaration && |
|
476
|
"heritageClauses" in symbolType.symbol.valueDeclaration |
|
477
|
) { |
|
478
|
return this.isEntity({ |
|
479
|
heritageClauses: symbolType.symbol.valueDeclaration |
|
480
|
.heritageClauses as ts.NodeArray<ts.HeritageClause>, |
|
481
|
node, |
|
482
|
}) |
|
483
|
} |
|
484
|
|
|
485
|
return false |
|
486
|
}) |
|
487
|
}) || false |
|
488
|
) |
|
489
|
} |
|
490
|
|
|
491
|
/** |
|
492
|
* Get knowledge base options for a specified node. |
|
493
|
* |
|
494
|
* @param node - The node to retrieve its knowledge base options. |
|
495
|
* @returns The knowledge base options. |
|
496
|
*/ |
|
497
|
getKnowledgeBaseOptions(node: ts.Node): Partial<RetrieveOptions> { |
|
498
|
const rawParentName = |
|
499
|
"name" in node.parent && |
|
500
|
node.parent.name && |
|
501
|
ts.isIdentifier(node.parent.name as ts.Node) |
|
502
|
? (node.parent.name as ts.Identifier).getText() |
|
503
|
: undefined |
|
504
|
return { |
|
505
|
kind: node.kind, |
|
506
|
templateOptions: { |
|
507
|
rawParentName, |
|
508
|
parentName: rawParentName |
|
509
|
? camelToWords(normalizeName(rawParentName)) |
|
510
|
: undefined, |
|
511
|
}, |
|
512
|
} |
|
513
|
} |
|
514
|
|
|
515
|
/** |
|
516
|
* Get the default value of a node. |
|
517
|
* |
|
518
|
* @param node - The node to get its default value. |
|
519
|
* @returns The default value, if any. |
|
520
|
*/ |
|
521
|
getDefaultValue(node: ts.Node): string | undefined { |
|
522
|
if ( |
|
523
|
"initializer" in node && |
|
524
|
node.initializer && |
|
525
|
ts.isExpression(node.initializer as ts.Node) |
|
526
|
) { |
|
527
|
const initializer = node.initializer as ts.Expression |
|
528
|
|
|
529
|
// retrieve default value only if the value is numeric, string, or boolean |
|
530
|
const defaultValue = |
|
531
|
ts.isNumericLiteral(initializer) || ts.isStringLiteral(initializer) |
|
532
|
? initializer.getText() |
|
533
|
: initializer.kind === ts.SyntaxKind.FalseKeyword |
|
534
|
? "false" |
|
535
|
: initializer.kind === ts.SyntaxKind.TrueKeyword |
|
536
|
? "true" |
|
537
|
: "" |
|
538
|
|
|
539
|
if (defaultValue.length) { |
|
540
|
return defaultValue |
|
541
|
} |
|
542
|
} |
|
543
|
} |
|
544
|
|
|
545
|
/** |
|
546
|
* Checks whether a node can be documented. |
|
547
|
* |
|
548
|
* @param {ts.Node} node - The node to check for. |
|
549
|
* @returns {boolean} Whether the node can be documented. |
|
550
|
*/ |
|
551
|
canDocumentNode(node: ts.Node): boolean { |
|
552
|
// check if node already has docblock |
|
553
|
return !this.nodeHasComments(node) |
|
554
|
} |
|
555
|
|
|
556
|
/** |
|
557
|
* Get the comments range of a node. |
|
558
|
* @param node - The node to get its comment range. |
|
559
|
* @returns The comment range of the node if available. |
|
560
|
*/ |
|
561
|
getNodeCommentsRange(node: ts.Node): ts.CommentRange[] | undefined { |
|
562
|
return ts.getLeadingCommentRanges( |
|
563
|
node.getSourceFile().getFullText(), |
|
564
|
node.getFullStart() |
|
565
|
) |
|
566
|
} |
|
567
|
|
|
568
|
/** |
|
569
|
* Get a node's comment from its range. |
|
570
|
* |
|
571
|
* @param node - The node to get its comment range. |
|
572
|
* @returns The comment if available. |
|
573
|
*/ |
|
574
|
getNodeCommentsFromRange(node: ts.Node): string | undefined { |
|
575
|
const commentRange = this.getNodeCommentsRange(node) |
|
576
|
|
|
577
|
if (!commentRange?.length) { |
|
578
|
return |
|
579
|
} |
|
580
|
|
|
581
|
return node |
|
582
|
.getSourceFile() |
|
583
|
.getFullText() |
|
584
|
.slice(commentRange[0].pos, commentRange[0].end) |
|
585
|
} |
|
586
|
|
|
587
|
/** |
|
588
|
* Check whether a node has comments. |
|
589
|
* |
|
590
|
* @param node - The node to check. |
|
591
|
* @returns Whether the node has comments. |
|
592
|
*/ |
|
593
|
nodeHasComments(node: ts.Node): boolean { |
|
594
|
return this.getNodeCommentsFromRange(node) !== undefined |
|
595
|
} |
|
596
|
|
|
597
|
/** |
|
598
|
* Retrieve information from the tags of a node. |
|
599
|
* |
|
600
|
* @param node - The node to retrieve the information from. |
|
601
|
* @returns An object containing the deprecated and since tags, if available. |
|
602
|
*/ |
|
603
|
getInformationFromTags(node: ts.Node): { |
|
604
|
deprecatedTag: ts.JSDocTag | undefined |
|
605
|
sinceTag: ts.JSDocTag | undefined |
|
606
|
featureFlagTag: ts.JSDocTag | undefined |
|
607
|
summary: string | undefined |
|
608
|
example: ts.JSDocTag | undefined |
|
609
|
} { |
|
610
|
const nodeComments = ts.getJSDocCommentsAndTags(node) |
|
611
|
let deprecatedTag: ts.JSDocTag | undefined |
|
612
|
let sinceTag: ts.JSDocTag | undefined |
|
613
|
let featureFlagTag: ts.JSDocTag | undefined |
|
614
|
let summary: string | undefined |
|
615
|
let example: ts.JSDocTag | undefined |
|
616
|
|
|
617
|
nodeComments.forEach((comment) => { |
|
618
|
if (!("tags" in comment)) { |
|
619
|
if (ts.isJSDoc(comment) && comment.comment) { |
|
620
|
summary = |
|
621
|
typeof comment.comment === "string" |
|
622
|
? comment.comment |
|
623
|
: comment.comment.map((part) => part.text).join(" ") |
|
624
|
} |
|
625
|
return |
|
626
|
} |
|
627
|
|
|
628
|
comment.tags?.forEach((tag) => { |
|
629
|
if (tag.tagName.getText() === "deprecated") { |
|
630
|
deprecatedTag = tag |
|
631
|
} |
|
632
|
|
|
633
|
if (tag.tagName.getText() === "since") { |
|
634
|
sinceTag = tag |
|
635
|
} |
|
636
|
|
|
637
|
if (tag.tagName.getText() === "featureFlag") { |
|
638
|
featureFlagTag = tag |
|
639
|
} |
|
640
|
|
|
641
|
if (tag.tagName.getText() === "example") { |
|
642
|
example = tag |
|
643
|
} |
|
644
|
}) |
|
645
|
}) |
|
646
|
|
|
647
|
return { |
|
648
|
deprecatedTag, |
|
649
|
sinceTag, |
|
650
|
featureFlagTag, |
|
651
|
summary, |
|
652
|
example, |
|
653
|
} |
|
654
|
} |
|
655
|
|
|
656
|
formatJSDocTag(tag: ts.JSDocTag | undefined): string | undefined { |
|
657
|
if (!tag) { |
|
658
|
return undefined |
|
659
|
} |
|
660
|
|
|
661
|
if (typeof tag.comment === "string") { |
|
662
|
return tag.comment |
|
663
|
} |
|
664
|
|
|
665
|
return tag.comment?.map((part) => part.text).join(" ") |
|
666
|
} |
|
667
|
|
|
668
|
/** |
|
669
|
* Check if a node is ignored. |
|
670
|
* |
|
671
|
* @param node - The node to check. |
|
672
|
* @returns Whether the node is ignored. |
|
673
|
*/ |
|
674
|
isIgnored(node: ts.Node): boolean { |
|
675
|
return ts |
|
676
|
.getJSDocTags(node) |
|
677
|
.some((tag) => tag.tagName.getText() === "ignore") |
|
678
|
} |
|
679
|
} |
|
680
|
|
|
681
|
export default DefaultKindGenerator |