|
1
|
import ts from "typescript" |
|
2
|
import DefaultKindGenerator, { GetDocBlockOptions } from "./default.js" |
|
3
|
import { DOCBLOCK_END_LINE, DOCBLOCK_START } from "../../constants.js" |
|
4
|
import { camelToWords } from "utils" |
|
5
|
import { normalizeName } from "../../utils/str-formatting.js" |
|
6
|
|
|
7
|
/** |
|
8
|
* A class that generates doc blocks for properties in a DTO interface/type. |
|
9
|
*/ |
|
10
|
class DTOPropertyGenerator extends DefaultKindGenerator<ts.PropertySignature> { |
|
11
|
protected allowedKinds: ts.SyntaxKind[] = [ts.SyntaxKind.PropertySignature] |
|
12
|
public name = "dto-property" |
|
13
|
|
|
14
|
/** |
|
15
|
* Check that the generator can handle generating for the node. |
|
16
|
* |
|
17
|
* @param {ts.Node} node - The node to check. |
|
18
|
* @returns {boolean} Whether the generator can handle generating for the node. |
|
19
|
*/ |
|
20
|
isAllowed(node: ts.Node): node is ts.PropertySignature { |
|
21
|
if (!super.isAllowed(node)) { |
|
22
|
return false |
|
23
|
} |
|
24
|
|
|
25
|
return ( |
|
26
|
this.getParentName((node as ts.PropertySignature).parent).endsWith( |
|
27
|
"DTO" |
|
28
|
) || false |
|
29
|
) |
|
30
|
} |
|
31
|
|
|
32
|
async getDocBlock( |
|
33
|
node: ts.PropertyDeclaration | ts.Node, |
|
34
|
options?: GetDocBlockOptions |
|
35
|
): Promise<string> { |
|
36
|
if (!this.isAllowed(node)) { |
|
37
|
return await super.getDocBlock(node, options) |
|
38
|
} |
|
39
|
|
|
40
|
let str = DOCBLOCK_START |
|
41
|
const rawParentName = this.getParentName(node.parent) |
|
42
|
const parentName = this.formatInterfaceName(rawParentName) |
|
43
|
// check if the property's type is interface/type/class |
|
44
|
const propertyType = this.checker.getTypeAtLocation(node) |
|
45
|
const isPropertyClassOrInterface = propertyType.isClassOrInterface() |
|
46
|
|
|
47
|
// try first to retrieve the summary from the knowledge base if it exists. |
|
48
|
const summary = this.knowledgeBaseFactory.tryToGetObjectPropertySummary({ |
|
49
|
retrieveOptions: { |
|
50
|
str: node.name.getText(), |
|
51
|
kind: node.kind, |
|
52
|
templateOptions: { |
|
53
|
rawParentName, |
|
54
|
parentName, |
|
55
|
}, |
|
56
|
}, |
|
57
|
propertyDetails: { |
|
58
|
isClassOrInterface: isPropertyClassOrInterface, |
|
59
|
isBoolean: |
|
60
|
"intrinsicName" in propertyType && |
|
61
|
propertyType.intrinsicName === "boolean", |
|
62
|
classOrInterfaceName: isPropertyClassOrInterface |
|
63
|
? this.formatInterfaceName(this.checker.typeToString(propertyType)) |
|
64
|
: undefined, |
|
65
|
}, |
|
66
|
}) |
|
67
|
|
|
68
|
str += summary |
|
69
|
|
|
70
|
return `${str}${DOCBLOCK_END_LINE}` |
|
71
|
} |
|
72
|
|
|
73
|
/** |
|
74
|
* Format the name of the interface/type. |
|
75
|
* |
|
76
|
* @param {string} name - The name to format. |
|
77
|
* @returns {string} The formatted name. |
|
78
|
*/ |
|
79
|
formatInterfaceName(name: string): string { |
|
80
|
return camelToWords(normalizeName(name)) |
|
81
|
} |
|
82
|
|
|
83
|
/** |
|
84
|
* Get the name of the parent interface/type. |
|
85
|
* |
|
86
|
* @param {ts.InterfaceDeclaration | ts.TypeLiteralNode} parent - The parent node. |
|
87
|
* @returns {string} The name of the parent. |
|
88
|
*/ |
|
89
|
getParentName(parent: ts.InterfaceDeclaration | ts.TypeLiteralNode): string { |
|
90
|
if (ts.isInterfaceDeclaration(parent)) { |
|
91
|
return parent.name.getText() |
|
92
|
} |
|
93
|
|
|
94
|
return this.checker.typeToString(this.checker.getTypeFromTypeNode(parent)) |
|
95
|
} |
|
96
|
} |
|
97
|
|
|
98
|
export default DTOPropertyGenerator |