Skip to content

Commit 95c46a1

Browse files
committedFeb 15, 2021
chore(): Use tsdoc to generate enhanced introspect comments
1 parent cd75f5d commit 95c46a1

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed
 

‎lib/plugin/utils/ast-utils.ts

+25
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
TypeFormatFlags
1919
} from 'typescript';
2020
import { isDynamicallyAdded } from './plugin-utils';
21+
import { DocComment, DocExcerpt, DocNode, ParserContext, TSDocParser } from '@microsoft/tsdoc';
2122

2223
export function isArray(type: Type) {
2324
const symbol = type.getSymbol();
@@ -102,6 +103,30 @@ export function getDefaultTypeFormatFlags(enclosingNode: Node) {
102103
return formatFlags;
103104
}
104105

106+
export function getNodeDocs(
107+
node: Node
108+
): DocComment {
109+
const tsdocParser: TSDocParser = new TSDocParser();
110+
const parserContext: ParserContext = tsdocParser.parseString(node.getFullText());
111+
return parserContext.docComment;
112+
}
113+
114+
export function docNodeToString(docNode: DocNode): string {
115+
let result = '';
116+
117+
if (docNode) {
118+
if (docNode instanceof DocExcerpt) {
119+
result += docNode.content.toString();
120+
}
121+
122+
for(const childNode of docNode.getChildNodes()) {
123+
result += docNodeToString(childNode);
124+
}
125+
}
126+
127+
return result.trim();
128+
}
129+
105130
export function getMainCommentAndExamplesOfNode(
106131
node: Node,
107132
sourceFile: SourceFile,

‎lib/plugin/visitors/controller-class.visitor.ts

+39-17
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { ApiOperation, ApiResponse } from '../../decorators';
44
import { PluginOptions } from '../merge-options';
55
import { OPENAPI_NAMESPACE } from '../plugin-constants';
66
import {
7+
docNodeToString,
78
getDecoratorArguments,
8-
getMainCommentAndExamplesOfNode
9+
getMainCommentAndExamplesOfNode, getNodeDocs
910
} from '../utils/ast-utils';
1011
import {
1112
getDecoratorOrUndefinedByNames,
@@ -116,25 +117,46 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
116117
!apiOperationExprProperties ||
117118
!hasPropertyKey(keyToGenerate, apiOperationExprProperties)
118119
) {
119-
const [extractedComments] = getMainCommentAndExamplesOfNode(
120-
node,
121-
sourceFile,
122-
typeChecker
123-
);
124-
if (!extractedComments) {
125-
// Node does not have any comments
126-
return [];
120+
const properties = [];
121+
122+
if (keyToGenerate) {
123+
const [extractedComments] = getMainCommentAndExamplesOfNode(
124+
node,
125+
sourceFile,
126+
typeChecker
127+
);
128+
129+
if (!extractedComments) {
130+
// Node does not have any comments
131+
return [];
132+
}
133+
134+
properties.push(ts.createPropertyAssignment(keyToGenerate, ts.createLiteral(extractedComments)));
135+
} else {
136+
const docs = getNodeDocs(node);
137+
138+
if (!docs) {
139+
return [];
140+
}
141+
142+
const summary = docNodeToString(docs.summarySection);
143+
if (summary && (!apiOperationExprProperties || !hasPropertyKey("summary", apiOperationExprProperties))) {
144+
properties.push(ts.createPropertyAssignment("summary", ts.createLiteral(summary)));
145+
}
146+
147+
const remarks = docNodeToString(docs.remarksBlock.content);
148+
if (remarks && (!apiOperationExprProperties || !hasPropertyKey("description", apiOperationExprProperties))) {
149+
properties.push(ts.createPropertyAssignment("description", ts.createLiteral(remarks)));
150+
}
127151
}
128-
const properties = [
129-
ts.createPropertyAssignment(
130-
keyToGenerate,
131-
ts.createLiteral(extractedComments)
132-
),
133-
...(apiOperationExprProperties ?? ts.createNodeArray())
134-
];
152+
135153
const apiOperationDecoratorArguments: ts.NodeArray<ts.Expression> = ts.createNodeArray(
136-
[ts.createObjectLiteral(compact(properties))]
154+
[ts.createObjectLiteral(compact([
155+
...properties,
156+
...(apiOperationExprProperties ?? ts.createNodeArray())
157+
]))]
137158
);
159+
138160
if (apiOperationDecorator) {
139161
((apiOperationDecorator.expression as ts.CallExpression) as any).arguments = apiOperationDecoratorArguments;
140162
} else {

0 commit comments

Comments
 (0)
Please sign in to comment.