Skip to content

Commit

Permalink
feat(deps): optional support for parameter
Browse files Browse the repository at this point in the history
fix #288
  • Loading branch information
vogloblinsky committed Aug 31, 2017
1 parent c84c534 commit 5bcef12
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 67 deletions.
88 changes: 66 additions & 22 deletions dist/index-cli.js

Large diffs are not rendered by default.

88 changes: 66 additions & 22 deletions dist/index.js

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions src/app/compiler/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { compilerHost, detectIndent } from '../../utilities';
import { logger } from '../../logger';
import { RouterParser } from '../../utils/router.parser';
import { JSDocTagsParser } from '../../utils/jsdoc.parser';
import { markedtags } from '../../utils/utils';
import { markedtags, mergeTagsAndArgs } from '../../utils/utils';
import { kindToType } from '../../utils/kind-to-type';
import { generate } from './codegen';
import { cleanLifecycleHooksFromMethods, getNamesCompareFn } from '../../utils/utils';
Expand Down Expand Up @@ -1184,6 +1184,11 @@ export class Dependencies {
result.jsdoctags = markedtags(jsdoctags[0].tags);
}
}
if (result.jsdoctags && result.jsdoctags.length > 0) {
result.jsdoctags = mergeTagsAndArgs(result.args, result.jsdoctags);
} else if(result.args.length > 0) {
result.jsdoctags = mergeTagsAndArgs(result.args)
}
return result;
}

Expand Down Expand Up @@ -1285,6 +1290,11 @@ export class Dependencies {
result.jsdoctags = markedtags(jsdoctags[0].tags);
}
}
if (result.jsdoctags && result.jsdoctags.length > 0) {
result.jsdoctags = mergeTagsAndArgs(result.args, result.jsdoctags);
} else if(result.args.length > 0) {
result.jsdoctags = mergeTagsAndArgs(result.args)
}
return result;
}

Expand All @@ -1294,7 +1304,10 @@ export class Dependencies {
type: this.visitType(arg)
}
if (arg.dotDotDotToken) {
_result.dotDotDotToken = true
_result.dotDotDotToken = true;
}
if (arg.questionToken) {
_result.optional = true;
}
if (arg.type) {
if (arg.type.kind) {
Expand Down
51 changes: 34 additions & 17 deletions src/app/engines/html.engine.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,57 +287,65 @@ export let HtmlEngineHelpers = (function() {
angularDocPrefix = prefixOfficialDoc(configuration.mainData.angularVersion);
if (method.args) {
args = method.args.map(function(arg) {
var _result = $dependenciesEngine.find(arg.type);
var _result = $dependenciesEngine.find(arg.type),
_optional = '';
if (arg.optional) {
_optional = '?';
}
if (_result) {
if (_result.source === 'internal') {
let path = _result.data.type;
if (_result.data.type === 'class') path = 'classe';
return `${arg.name}: <a href="../${path}s/${_result.data.name}.html">${arg.type}</a>`;
return `${arg.name}${_optional}: <a href="../${path}s/${_result.data.name}.html">${arg.type}</a>`;
} else {
let path = `https://${angularDocPrefix}angular.io/docs/ts/latest/api/${_result.data.path}`;
return `${arg.name}: <a href="${path}" target="_blank">${arg.type}</a>`;
return `${arg.name}${_optional}: <a href="${path}" target="_blank">${arg.type}</a>`;
}
} else if (arg.dotDotDotToken) {
return `...${arg.name}: ${arg.type}`;
} else if (arg.function) {
if (arg.function.length > 0) {
let argums = arg.function.map(function(argu) {
var _result = $dependenciesEngine.find(argu.type);
var _result = $dependenciesEngine.find(argu.type),
_optional = '';
if (arg.optional) {
_optional = '?';
}
if (_result) {
if (_result.source === 'internal') {
let path = _result.data.type;
if (_result.data.type === 'class') path = 'classe';
return `${argu.name}: <a href="../${path}s/${_result.data.name}.html">${argu.type}</a>`;
return `${argu.name}${_optional}: <a href="../${path}s/${_result.data.name}.html">${argu.type}</a>`;
} else {
let path = `https://${angularDocPrefix}angular.io/docs/ts/latest/api/${_result.data.path}`;
return `${argu.name}: <a href="${path}" target="_blank">${argu.type}</a>`;
return `${argu.name}${_optional}: <a href="${path}" target="_blank">${argu.type}</a>`;
}
} else if (finderInBasicTypes(argu.type)) {
let path = `https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/${argu.type}`;
return `${argu.name}: <a href="${path}" target="_blank">${argu.type}</a>`;
return `${argu.name}${_optional}: <a href="${path}" target="_blank">${argu.type}</a>`;
} else if (finderInTypeScriptBasicTypes(argu.type)) {
let path = `https://www.typescriptlang.org/docs/handbook/basic-types.html`;
return `${argu.name}: <a href="${path}" target="_blank">${argu.type}</a>`;
return `${argu.name}${_optional}: <a href="${path}" target="_blank">${argu.type}</a>`;
} else {
if (argu.name && argu.type) {
return `${argu.name}: ${argu.type}`;
return `${argu.name}${_optional}: ${argu.type}`;
} else {
return `${argu.name.text}`;
}
}
});
return `${arg.name}: (${argums}) => void`;
return `${arg.name}${_optional}: (${argums}) => void`;
} else {
return `${arg.name}: () => void`;
return `${arg.name}${_optional}: () => void`;
}
} else if (finderInBasicTypes(arg.type)) {
let path = `https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/${arg.type}`;
return `${arg.name}: <a href="${path}" target="_blank">${arg.type}</a>`;
return `${arg.name}${_optional}: <a href="${path}" target="_blank">${arg.type}</a>`;
} else if (finderInTypeScriptBasicTypes(arg.type)) {
let path = `https://www.typescriptlang.org/docs/handbook/basic-types.html`;
return `${arg.name}: <a href="${path}" target="_blank">${arg.type}</a>`;
return `${arg.name}${_optional}: <a href="${path}" target="_blank">${arg.type}</a>`;
} else {
return `${arg.name}: ${arg.type}`;
return `${arg.name}${_optional}: ${arg.type}`;
}
}).join(', ');
}
Expand Down Expand Up @@ -445,16 +453,25 @@ export let HtmlEngineHelpers = (function() {
if (jsdocTags[i].tagName.text === 'param') {
var tag = {} as jsdocTagInterface;
if (jsdocTags[i].typeExpression && jsdocTags[i].typeExpression.type.kind) {
tag.type = kindToType(jsdocTags[i].typeExpression.type.kind);
tag.type = kindToType(jsdocTags[i].typeExpression.type.kind);
}
if (jsdocTags[i].typeExpression && jsdocTags[i].typeExpression.type.name) {
tag.type = jsdocTags[i].typeExpression.type.name.text
tag.type = jsdocTags[i].typeExpression.type.name.text
} else {
tag.type = jsdocTags[i].type;
}
if (jsdocTags[i].comment) {
tag.comment = jsdocTags[i].comment;
}
if (jsdocTags[i].name) {
tag.name = jsdocTags[i].name.text;
if (jsdocTags[i].name.text) {
tag.name = jsdocTags[i].name.text;
} else {
tag.name = jsdocTags[i].name;
}
}
if (jsdocTags[i].optional) {
tag.optional = true;
}
tags.push(tag);
}
Expand Down
8 changes: 7 additions & 1 deletion src/templates/partials/block-constructor.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{{/unless}}
{{/if}}

{{#or constructor.description constructor.jsdoctags}}
{{#or constructor.jsdoctags}}
<tr>
<td class="col-md-4">
{{#if constructor.description}}
Expand All @@ -33,6 +33,7 @@
<tr>
<td>Name</td>
<td>Type</td>
<td>Optional</td>
<td>Description</td>
</tr>
</thead>
Expand All @@ -45,6 +46,11 @@
{{> link-type type=type }}
{{/if}}
</td>
<td>
{{#if optional}}
true
{{/if}}
</td>
<td>{{{comment}}}</td>
</tr>
{{/each}}
Expand Down
8 changes: 7 additions & 1 deletion src/templates/partials/block-method.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
{{/unless}}
{{/if}}

{{#or description jsdoctags returnType}}
{{#or jsdoctags returnType}}
<tr>
<td class="col-md-4">
{{#if description}}
Expand All @@ -74,6 +74,7 @@
<tr>
<td>Name</td>
<td>Type</td>
<td>Optional</td>
<td>Description</td>
</tr>
</thead>
Expand All @@ -86,6 +87,11 @@
{{> link-type type=type }}
{{/if}}
</td>
<td>
{{#if optional}}
true
{{/if}}
</td>
<td>{{{comment}}}</td>
</tr>
{{/each}}
Expand Down
20 changes: 20 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ export function markedtags(tags) {
return mtags;
};

export function mergeTagsAndArgs(args, jsdoctags?) {
var margs = _.cloneDeep(args);
_.forEach(margs, (arg) => {
arg.tagName = {
text: 'param'
};
if (jsdoctags) {
_.forEach(jsdoctags, (jsdoctag) => {
if (jsdoctag.name && jsdoctag.name.text === arg.name) {
arg.tagName = jsdoctag.tagName;
arg.name = jsdoctag.name;
arg.comment = jsdoctag.comment;
arg.typeExpression = jsdoctag.typeExpression;
}
});
}
});
return margs;
}

export function readConfig(configFile: string): any {
let result = ts.readConfigFile(configFile, ts.sys.readFile);
if (result.error) {
Expand Down
7 changes: 6 additions & 1 deletion test/src/cli/cli-generation-big-app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('CLI simple generation - big app', () => {
expect(file).to.contain('Promise&lt;void&gt;');
expect(file).to.contain('string | number');
expect(file).to.contain('number[]');
expect(file).to.contain('<code>stopMonitoring(theTodo: <a href="../interfaces/LabelledTodo.html">LabelledTodo</a>)</code>');
expect(file).to.contain('<code>stopMonitoring(theTodo?: <a href="../interfaces/LabelledTodo.html">LabelledTodo</a>)</code>');
expect(file).to.contain('service is a todo store');
expect(file).to.contain('all todos status (completed');
expect(file).to.contain('Local array of Todos');
Expand Down Expand Up @@ -245,4 +245,9 @@ describe('CLI simple generation - big app', () => {
expect(file).to.contain('Extends');
});

it('should support optional', () => {
let file = read('documentation/injectables/TodoStore.html');
expect(file).to.contain('true');
});

});
2 changes: 1 addition & 1 deletion test/src/todomvc-ng2/src/app/shared/services/todo.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class TodoStore {
* @param {LabelledTodo} theTodo A todo
* @returns {Promise<void>} promise resolved once we stop monitoring the todo or it is rejected
*/
stopMonitoring(theTodo: LabelledTodo): Promise<void> {
stopMonitoring(theTodo?: LabelledTodo): Promise<void> {
return new Promise<void>((resolve, reject) => {
//TODO
});
Expand Down

0 comments on commit 5bcef12

Please sign in to comment.