Skip to content

Commit

Permalink
feat(deps): function decorator arguments support / WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
vogloblinsky committed Oct 16, 2018
1 parent 77b24cd commit 4e7593b
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/app/compiler/angular/deps/helpers/class-helper.ts
Expand Up @@ -69,9 +69,9 @@ export class ClassHelper {
let info: any = {
name: decorator.expression.expression.text
};
if (decorator.expression.expression.arguments) {
if (decorator.expression.expression.arguments.length > 0) {
info.args = decorator.expression.expression.arguments;
if (decorator.expression.arguments) {
if (decorator.expression.arguments.length > 0) {
info.args = decorator.expression.arguments;
}
}
_decorators.push(info);
Expand Down
114 changes: 114 additions & 0 deletions src/app/engines/html-engine-helpers/function-decorator.helper.ts
@@ -0,0 +1,114 @@
import { IHtmlEngineHelper } from './html-engine-helper.interface';

import { DependenciesEngine } from '../dependencies.engine';
import { AngularVersionUtil, BasicTypeUtil } from '../../../utils';
import { StringifyObjectLiteralExpression } from '.././../../utils/object-literal-expression.util';
import { ConfigurationInterface } from '../../interfaces/configuration.interface';

import { ts, SyntaxKind } from 'ts-simple-ast';

export class FunctionDecoratorHelper implements IHtmlEngineHelper {
private angularVersionUtil = new AngularVersionUtil();
private basicTypeUtil = new BasicTypeUtil();

constructor(
private configuration: ConfigurationInterface,
private dependenciesEngine: DependenciesEngine
) {}

private handleFunction(arg): string {
if (arg.function.length === 0) {
return `${arg.name}${this.getOptionalString(arg)}: () => void`;
}

let argums = arg.function.map(argu => {
let _result = this.dependenciesEngine.find(argu.type);
if (_result) {
if (_result.source === 'internal') {
let path = _result.data.type;
if (_result.data.type === 'class') {
path = 'classe';
}
return `${argu.name}${this.getOptionalString(arg)}: <a href="../${path}s/${
_result.data.name
}.html">${argu.type}</a>`;
} else {
let path = this.angularVersionUtil.getApiLink(
_result.data,
this.configuration.mainData.angularVersion
);
return `${argu.name}${this.getOptionalString(
arg
)}: <a href="${path}" target="_blank">${argu.type}</a>`;
}
} else if (this.basicTypeUtil.isKnownType(argu.type)) {
let path = this.basicTypeUtil.getTypeUrl(argu.type);
return `${argu.name}${this.getOptionalString(
arg
)}: <a href="${path}" target="_blank">${argu.type}</a>`;
} else {
if (argu.name && argu.type) {
return `${argu.name}${this.getOptionalString(arg)}: ${argu.type}`;
} else {
if (argu.name) {
return `${argu.name.text}`;
} else {
return '';
}
}
}
});
return `${arg.name}${this.getOptionalString(arg)}: (${argums}) => void`;
}

private getOptionalString(arg): string {
return arg.optional ? '?' : '';
}

public helperFunc(context: any, decorator) {
let args = [];

if (decorator.args) {
args = decorator.args
.map(arg => {
let _result = this.dependenciesEngine.find(arg.type);
if (_result) {
if (_result.source === 'internal') {
let path = _result.data.type;
if (_result.data.type === 'class') {
path = 'classe';
}
return `${arg.name}${this.getOptionalString(arg)}: <a href="../${path}s/${_result.data.name}.html">${arg.type}</a>`;
} else {
let path = this.angularVersionUtil.getApiLink(_result.data, this.configuration.mainData.angularVersion);
return `${arg.name}${this.getOptionalString(arg)}: <a href="${path}" target="_blank">${arg.type}</a>`;
}
} else if (arg.dotDotDotToken) {
return `...${arg.name}: ${arg.type}`;
} else if (arg.function) {
return this.handleFunction(arg);
} else if (arg.expression && arg.name) {
return arg.expression.text + '.' + arg.name.text;
} else if (arg.kind && arg.kind === SyntaxKind.StringLiteral) {
return `'` + arg.text + `'`;
} else if (arg.kind && arg.kind === SyntaxKind.ObjectLiteralExpression) {
return StringifyObjectLiteralExpression(arg);
} else if (this.basicTypeUtil.isKnownType(arg.type)) {
let path = this.basicTypeUtil.getTypeUrl(arg.type);
return `${arg.name}${this.getOptionalString(arg)}: <a href="${path}" target="_blank">${arg.type}</a>`;
} else {
if (arg.type) {
return `${arg.name}${this.getOptionalString(arg)}: ${arg.type}`;
} else if (arg.text) {
return `${arg.text}`;
}else {
return `${arg.name}${this.getOptionalString(arg)}`;
}
}
})
.join(', ');
}

return `@${decorator.name}(${args}) `;
}
}
2 changes: 2 additions & 0 deletions src/app/engines/html.engine.helpers.ts
Expand Up @@ -37,6 +37,7 @@ import { ElementAloneHelper } from './html-engine-helpers/element-alone.helper';
import { HasOwnHelper } from './html-engine-helpers/has-own.helper';
import { ShortURLHelper } from './html-engine-helpers/short-url.helper';
import { I18nHelper } from './html-engine-helpers/i18n.helper';
import { FunctionDecoratorHelper } from './html-engine-helpers/function-decorator.helper';

export class HtmlEngineHelpers {
public registerHelpers(
Expand All @@ -51,6 +52,7 @@ export class HtmlEngineHelpers {
'functionSignature',
new FunctionSignatureHelper(configuration, dependenciesEngine)
);
this.registerHelper(bars, 'functionDecorator', new FunctionDecoratorHelper(configuration, dependenciesEngine));
this.registerHelper(bars, 'isNotToggle', new IsNotToggleHelper(configuration));
this.registerHelper(bars, 'isInitialTab', new IsInitialTabHelper());
this.registerHelper(bars, 'isTabEnabled', new IsTabEnabledHelper());
Expand Down
2 changes: 1 addition & 1 deletion src/templates/partials/block-method.hbs
Expand Up @@ -49,7 +49,7 @@
{{#if decorators}}
<tr>
<td class="col-md-4">
<b>{{t "decorators" }} : </b><code>{{#each decorators}}@{{name}}() {{/each}}</code>
<b>{{t "decorators" }} : </b><code>{{#each decorators}}{{{functionDecorator this}}}{{/each}}</code>
</td>
</tr>
{{/if}}
Expand Down
2 changes: 1 addition & 1 deletion src/templates/partials/block-property.hbs
Expand Up @@ -53,7 +53,7 @@
{{#if decorators}}
<tr>
<td class="col-md-4">
<i>{{t "decorators" }} : </i><code>{{#each decorators}}@{{name}}() {{/each}}</code>
<b>{{t "decorators" }} : </b><code>{{#each decorators}}@{{name}}() {{/each}}</code>
</td>
</tr>
{{/if}}
Expand Down
31 changes: 31 additions & 0 deletions src/utils/object-literal-expression.util.ts
@@ -0,0 +1,31 @@
const util = require('util');

import { SyntaxKind } from "ts-simple-ast";

export function StringifyObjectLiteralExpression(ole) {
let returnedString = '{';

// console.log(util.inspect(ole, { showHidden: true, depth: 10 }));

if (ole.properties && ole.properties.length > 0) {
ole.properties.forEach(property => {
// console.log(util.inspect(property, { showHidden: true, depth: 20 }));
if (property.name) {
returnedString += property.name.text + ':';
}
if (property.symbol) {
if (property.symbol.declarations && property.symbol.declarations.length > 0) {
property.symbol.declarations.forEach(declaration => {
if (declaration.kind === SyntaxKind.PropertyAssignment) {
returnedString += declaration.name.text + ':';
}
});
}
}
});
}

returnedString += '}';

return returnedString;
}
2 changes: 2 additions & 0 deletions test/src/cli/cli-nest.spec.ts
Expand Up @@ -33,6 +33,8 @@ describe('CLI nest projects support', () => {
it('it should contain a controller page with prefix', () => {
let file = read(`${distFolder}/controllers/AuthController.html`);
expect(file).to.contain('code>auth</code');
expect(file).to.contain('code>@Auth(Roles.User) @Post() </code');
expect(file).to.contain(`code>@Post('multiple') </code`);
});

it('it should contain a module page with controller referenced', () => {
Expand Down
19 changes: 14 additions & 5 deletions test/src/nest-app/src/app.controller.ts
Expand Up @@ -3,10 +3,19 @@ import { AppService } from './app.service';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
constructor(private readonly appService: AppService) {}

@Get()
root(): string {
return this.appService.root();
}
@Get()
root(): string {
return this.appService.root();
}

@Auth(Roles.User)
@Post()
async create(@Body() body: CreateTodoDto, @AuthUser() authUser: User) {}

@Post('multiple')
async createMultipleTodo(@Body() body: CreateMultipleTodoDto, @AuthUser() authUser: User) {

}
}

0 comments on commit 4e7593b

Please sign in to comment.