diff --git a/components/convert-component.ts b/components/convert-component.ts index b4db766..ae04cac 100644 --- a/components/convert-component.ts +++ b/components/convert-component.ts @@ -15,7 +15,7 @@ import { FunctionFactory } from '../utils/factories/function-factory'; @Component({ name: 'convert-component' }) export class ConvertComponent extends ConverterComponent { jsonObjectName: string; - factory: BaseFactory; + factoryInstance: BaseFactory; fileOperations: FileOperations; reflection; parser: Parser; @@ -60,9 +60,9 @@ export class ConvertComponent extends ConverterComponent { private onResolveEnd(...rest) { // Add the last resolved object - if (this.factory && !this.factory.isEmpty()) { + if (this.factoryInstance && !this.factoryInstance.isEmpty()) { const filePath = this.reflection.sources[0].fileName; - this.fileOperations.appendFileData(this.mainDirToExport, filePath, this.jsonObjectName, 'json', this.factory.getJsonContent()); + this.fileOperations.appendFileData(this.mainDirToExport, filePath, this.jsonObjectName, 'json', this.factoryInstance.getJsonContent()); } /** @@ -80,17 +80,17 @@ export class ConvertComponent extends ConverterComponent { case ReflectionKind.Class: case ReflectionKind.Interface: if (this.jsonObjectName !== reflection.name && this.jsonObjectName !== undefined) { - if (!this.factory.isEmpty()) { + if (!this.factoryInstance.isEmpty()) { const filePath = this.reflection.sources[0].fileName - this.fileOperations.appendFileData(this.mainDirToExport, filePath, this.jsonObjectName, 'json', this.factory.getJsonContent()); + this.fileOperations.appendFileData(this.mainDirToExport, filePath, this.jsonObjectName, 'json', this.factoryInstance.getJsonContent()); } } const data = this.getCommentInfo(reflection); this.jsonObjectName = reflection.name; this.reflection = reflection; - this.factory = this.instanceBuilder(reflection.kind, reflection.name); - this.factory.buildObjectStructure(data); + this.factoryInstance = this.instanceBuilder(reflection.kind, reflection.name); + this.factoryInstance.buildObjectStructure(data); break; case ReflectionKind.Property: case ReflectionKind.CallSignature: @@ -100,14 +100,14 @@ export class ConvertComponent extends ConverterComponent { } const getData = this.getCommentInfo(reflection); - this.factory.appendAttribute(this.jsonObjectName, reflection.kind, reflection.name, getData); + this.factoryInstance.appendAttribute(this.jsonObjectName, reflection.kind, reflection.name, getData); break; case ReflectionKind.Function: const funcData = this.getCommentInfo(reflection.signatures[0]); - const funcFactory = this.instanceBuilder(reflection.kind, reflection.name); - funcFactory.buildObjectStructure(funcData); - if (!funcFactory.isEmpty()) { - this.globalFuncsJson = Object.assign(funcFactory.getJsonContent(), this.globalFuncsJson); + const funcInstance = this.instanceBuilder(reflection.kind, reflection.name); + funcInstance.buildObjectStructure(funcData); + if (!funcInstance.isEmpty()) { + this.globalFuncsJson = Object.assign(funcInstance.getJsonContent(), this.globalFuncsJson); } break; case ReflectionKind.GetSignature: @@ -115,7 +115,7 @@ export class ConvertComponent extends ConverterComponent { const accessorName = reflection.parent.name; const accessorType = reflection.kind; const accessorData = this.getCommentInfo(reflection); - this.factory.appendAccessorAttributes(this.jsonObjectName, reflection.parent.kind, accessorName, accessorType, accessorData); + this.factoryInstance.appendAccessorAttributes(this.jsonObjectName, reflection.parent.kind, accessorName, accessorType, accessorData); default: return; } @@ -130,13 +130,35 @@ export class ConvertComponent extends ConverterComponent { let comment = this.getCommentData(reflection.comment); if (reflection.comment.tags) { - comment[Constants.COMMENT] = Object.assign(this.getTagComments(reflection.comment), comment[Constants.COMMENT]); + comment[Constants.COMMENT] = Object.assign(this.getTagsComments(reflection.comment), comment[Constants.COMMENT]); + } + + if (reflection.parameters) { + comment[Constants.COMMENT] = Object.assign(this.getParamsComments(reflection), comment[Constants.COMMENT]); } return comment; } - private getTagComments(comment) { + private getParamsComments(reflection) { + let params = {}; + params[Constants.PARAMS] = {}; + reflection.parameters.forEach(param => { + if (!param.comment) { + return; + } + + const paramComment = this.getCommentData(param.comment); + const paramCommentKeys = Object.keys(paramComment[Constants.COMMENT]); + if (paramCommentKeys.length) { + params[Constants.PARAMS][param.name] = paramComment; + } + }); + + return Object.keys(params[Constants.PARAMS]).length ? params : {}; + } + + private getTagsComments(comment) { let tags = {}; tags[Constants.TAGS] = {}; comment.tags.forEach(tag => { @@ -155,21 +177,13 @@ export class ConvertComponent extends ConverterComponent { let splittedObj; - if(obj.text) { + if(obj.text && obj.text.trim().length) { splittedObj = this.parser.splitByCharacter(obj.text, '\n'); - if (splittedObj.length === 1) { - splittedObj = splittedObj[0]; - } - comment[Constants.COMMENT][Constants.TEXT] = splittedObj; } - if(obj.shortText) { + if(obj.shortText && obj.shortText.trim().length) { splittedObj = this.parser.splitByCharacter(obj.shortText, '\n'); - if (splittedObj.length === 1) { - splittedObj = splittedObj[0]; - } - comment[Constants.COMMENT][Constants.SHORT_TEXT] = splittedObj; } diff --git a/components/render-component.ts b/components/render-component.ts index 1ba4b26..177b523 100644 --- a/components/render-component.ts +++ b/components/render-component.ts @@ -5,6 +5,7 @@ import { FileOperations } from '../utils/file-operations'; import { AttributeType } from '../utils/enums/json-obj-kind'; import { Constants } from '../utils/constants'; import { RendererEvent } from 'typedoc/dist/lib/output/events'; +import { Parser } from '../utils/parser'; @Component({ name: 'render-component'}) export class RenderComponenet extends RendererComponent { @@ -12,6 +13,7 @@ export class RenderComponenet extends RendererComponent { data: JSON; mainDirOfJsons: string; globalFuncsData; + parser: Parser; public initialize() { this.listenTo(this.owner, { @@ -19,6 +21,7 @@ export class RenderComponenet extends RendererComponent { }); this.fileOperations = new FileOperations(this.application.logger); + this.parser = new Parser(); } private onRenderBegin(event: RendererEvent) { @@ -122,12 +125,12 @@ export class RenderComponenet extends RendererComponent { let parsed; if(reflection.comment.text) { - parsed = dataObj[Constants.COMMENT][Constants.TEXT].join('\n'); + parsed = this.parser.joinByCharacter(dataObj[Constants.COMMENT][Constants.TEXT], '\n'); reflection.comment.text = parsed; } if(reflection.comment.shortText) { - parsed = dataObj[Constants.COMMENT][Constants.SHORT_TEXT].join('\n'); + parsed = this.parser.joinByCharacter(dataObj[Constants.COMMENT][Constants.SHORT_TEXT], '\n'); reflection.comment.shortText = parsed; } } diff --git a/utils/constants.ts b/utils/constants.ts index 7c56c3f..5e0caf8 100644 --- a/utils/constants.ts +++ b/utils/constants.ts @@ -1,4 +1,5 @@ export class Constants { + static readonly PARAMS = 'parameters'; static readonly TAGS = 'tags'; static readonly TAG_NAME = 'tagName'; static readonly COMMENT = 'comment'; diff --git a/utils/parser.ts b/utils/parser.ts index 9056515..d965cd5 100644 --- a/utils/parser.ts +++ b/utils/parser.ts @@ -1,10 +1,21 @@ +import { isArray } from "util"; + export class Parser { public splitByCharacter(text, char) { - return this.removeEmptyStrings(text.split(char)); + const res = this.removeEmptyStrings(text.split(char)); + if (res.length === 1) { + return res[0] + } + + return res; } - public joinByCharacter(array, char) { - return this.removeEmptyStrings(array.join(char)); + public joinByCharacter(obj, char) { + if (!isArray(obj)) { + return obj; + } + + return obj.join(char); } private removeEmptyStrings(splittedText) {