Skip to content

Commit

Permalink
chore(): Use tsdoc to generate enhanced introspect comments
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmiguelbesada committed Feb 15, 2021
1 parent cd75f5d commit 95c46a1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
25 changes: 25 additions & 0 deletions lib/plugin/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
TypeFormatFlags
} from 'typescript';
import { isDynamicallyAdded } from './plugin-utils';
import { DocComment, DocExcerpt, DocNode, ParserContext, TSDocParser } from '@microsoft/tsdoc';

export function isArray(type: Type) {
const symbol = type.getSymbol();
Expand Down Expand Up @@ -102,6 +103,30 @@ export function getDefaultTypeFormatFlags(enclosingNode: Node) {
return formatFlags;
}

export function getNodeDocs(
node: Node
): DocComment {
const tsdocParser: TSDocParser = new TSDocParser();
const parserContext: ParserContext = tsdocParser.parseString(node.getFullText());
return parserContext.docComment;
}

export function docNodeToString(docNode: DocNode): string {
let result = '';

if (docNode) {
if (docNode instanceof DocExcerpt) {
result += docNode.content.toString();
}

for(const childNode of docNode.getChildNodes()) {
result += docNodeToString(childNode);
}
}

return result.trim();
}

export function getMainCommentAndExamplesOfNode(
node: Node,
sourceFile: SourceFile,
Expand Down
56 changes: 39 additions & 17 deletions lib/plugin/visitors/controller-class.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { ApiOperation, ApiResponse } from '../../decorators';
import { PluginOptions } from '../merge-options';
import { OPENAPI_NAMESPACE } from '../plugin-constants';
import {
docNodeToString,
getDecoratorArguments,
getMainCommentAndExamplesOfNode
getMainCommentAndExamplesOfNode, getNodeDocs
} from '../utils/ast-utils';
import {
getDecoratorOrUndefinedByNames,
Expand Down Expand Up @@ -116,25 +117,46 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
!apiOperationExprProperties ||
!hasPropertyKey(keyToGenerate, apiOperationExprProperties)
) {
const [extractedComments] = getMainCommentAndExamplesOfNode(
node,
sourceFile,
typeChecker
);
if (!extractedComments) {
// Node does not have any comments
return [];
const properties = [];

if (keyToGenerate) {
const [extractedComments] = getMainCommentAndExamplesOfNode(
node,
sourceFile,
typeChecker
);

if (!extractedComments) {
// Node does not have any comments
return [];
}

properties.push(ts.createPropertyAssignment(keyToGenerate, ts.createLiteral(extractedComments)));
} else {
const docs = getNodeDocs(node);

if (!docs) {
return [];
}

const summary = docNodeToString(docs.summarySection);
if (summary && (!apiOperationExprProperties || !hasPropertyKey("summary", apiOperationExprProperties))) {
properties.push(ts.createPropertyAssignment("summary", ts.createLiteral(summary)));
}

const remarks = docNodeToString(docs.remarksBlock.content);
if (remarks && (!apiOperationExprProperties || !hasPropertyKey("description", apiOperationExprProperties))) {
properties.push(ts.createPropertyAssignment("description", ts.createLiteral(remarks)));
}
}
const properties = [
ts.createPropertyAssignment(
keyToGenerate,
ts.createLiteral(extractedComments)
),
...(apiOperationExprProperties ?? ts.createNodeArray())
];

const apiOperationDecoratorArguments: ts.NodeArray<ts.Expression> = ts.createNodeArray(
[ts.createObjectLiteral(compact(properties))]
[ts.createObjectLiteral(compact([
...properties,
...(apiOperationExprProperties ?? ts.createNodeArray())
]))]
);

if (apiOperationDecorator) {
((apiOperationDecorator.expression as ts.CallExpression) as any).arguments = apiOperationDecoratorArguments;
} else {
Expand Down

0 comments on commit 95c46a1

Please sign in to comment.