Skip to content

Commit

Permalink
feat: update test and optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
lit26 committed Feb 7, 2024
1 parent d3e7e1c commit 7e6b64f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
37 changes: 15 additions & 22 deletions lib/plugin/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,17 @@ import {
DocBlock
} from '@microsoft/tsdoc';

export class Formatter {
public static renderDocNode(docNode: DocNode): string {
let result: string = '';
if (docNode) {
if (docNode instanceof DocExcerpt) {
result += docNode.content.toString();
}
for (const childNode of docNode.getChildNodes()) {
result += Formatter.renderDocNode(childNode);
}
export function renderDocNode(docNode: DocNode) {
let result: string = '';
if (docNode) {
if (docNode instanceof DocExcerpt) {
result += docNode.content.toString();
}
return result;
}

public static renderDocNodes(docNodes: ReadonlyArray<DocNode>): string {
let result: string = '';
for (const docNode of docNodes) {
result += Formatter.renderDocNode(docNode);
for (const childNode of docNode.getChildNodes()) {
result += renderDocNode(childNode);
}
return result;
}
return result;
}

export function isArray(type: Type) {
Expand Down Expand Up @@ -157,7 +147,7 @@ export function getMainCommentOfNode(
node.getFullText()
);
const docComment: DocComment = parserContext.docComment;
return Formatter.renderDocNode(docComment.summarySection).trim();
return renderDocNode(docComment.summarySection).trim();
}

export function parseCommentDocValue(docValue: string, type: ts.Type) {
Expand Down Expand Up @@ -209,9 +199,7 @@ export function getTsDocTagsOfNode(node: Node, typeChecker: TypeChecker) {
const type = typeChecker.getTypeAtLocation(node);
if (hasProperties) {
blocks.forEach((block) => {
const docValue = Formatter.renderDocNode(block.content).split(
'\n'
)[0];
const docValue = renderDocNode(block.content).split('\n')[0];
const value = parseCommentDocValue(docValue, type);

if (value !== null) {
Expand All @@ -226,6 +214,11 @@ export function getTsDocTagsOfNode(node: Node, typeChecker: TypeChecker) {
tagResults[tag] = true;
}
}
if (docComment.remarksBlock) {
tagResults['remarks'] = renderDocNode(
docComment.remarksBlock.content
).trim();
}
if (docComment.deprecatedBlock) {
tagResults['deprecated'] = true;
}
Expand Down
12 changes: 12 additions & 0 deletions lib/plugin/visitors/controller-class.visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ export class ControllerClassVisitor extends AbstractFileVisitor {
...(apiOperationExistingProps ?? factory.createNodeArray())
];

const hasRemarksKey = hasPropertyKey(
'description',
factory.createNodeArray(apiOperationExistingProps)
);
if (!hasRemarksKey && tags.remarks) {
const remarksPropertyAssignment = factory.createPropertyAssignment(
'description',
createLiteralFromAnyValue(factory, tags.remarks)
);
properties.push(remarksPropertyAssignment);
}

const hasDeprecatedKey = hasPropertyKey(
'deprecated',
factory.createNodeArray(apiOperationExistingProps)
Expand Down
6 changes: 5 additions & 1 deletion test/plugin/fixtures/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export class AppController {
/**
* create a Cat
*
* @remarks Creating a test cat
*
* @returns {Promise<Cat>}
* @memberof AppController
Expand Down Expand Up @@ -71,6 +73,8 @@ let AppController = exports.AppController = class AppController {
/**
* create a Cat
*
* @remarks Creating a test cat
*
* @returns {Promise<Cat>}
* @memberof AppController
*/
Expand Down Expand Up @@ -104,7 +108,7 @@ let AppController = exports.AppController = class AppController {
async findAll() { }
};
__decorate([
openapi.ApiOperation({ summary: \"create a Cat\" }),
openapi.ApiOperation({ summary: \"create a Cat\", description: \"Creating a test cat\" }),
(0, common_1.Post)(),
openapi.ApiResponse({ status: 201, type: Cat })
], AppController.prototype, \"create\", null);
Expand Down

0 comments on commit 7e6b64f

Please sign in to comment.