Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(comments): export parameters comments per comment #9

Merged
merged 3 commits into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 39 additions & 25 deletions components/convert-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -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:
Expand All @@ -100,22 +100,22 @@ 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:
case ReflectionKind.SetSignature:
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;
}
Expand All @@ -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 => {
Expand All @@ -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;
}

Expand Down
7 changes: 5 additions & 2 deletions components/render-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ 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 {
fileOperations: FileOperations;
data: JSON;
mainDirOfJsons: string;
globalFuncsData;
parser: Parser;

public initialize() {
this.listenTo(this.owner, {
[RendererEvent.BEGIN]: this.onRenderBegin,
});

this.fileOperations = new FileOperations(this.application.logger);
this.parser = new Parser();
}

private onRenderBegin(event: RendererEvent) {
Expand Down Expand Up @@ -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;
}
}
Expand Down
1 change: 1 addition & 0 deletions utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export class Constants {
static readonly PARAMS = 'parameters';
static readonly TAGS = 'tags';
static readonly TAG_NAME = 'tagName';
static readonly COMMENT = 'comment';
Expand Down
17 changes: 14 additions & 3 deletions utils/parser.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down