Skip to content

Commit

Permalink
fixes #9 and small fixes for es6 template (#10)
Browse files Browse the repository at this point in the history
* parser: allow quotes for default value
* es6: render string default values properly
* es6: `List<Value>` as an method return value is rendered now properly (fixes #9)
* sample puml updates
  • Loading branch information
Jussi Vatjus-Anttila authored Mar 10, 2019
1 parent 91ea0b1 commit a7d7bf1
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 9 deletions.
4 changes: 3 additions & 1 deletion examples/sample.puml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ interface Type {
class Scheduler {
+constructor(Queue queue, Resources resource)
-{abstract}Type getType()
-setTypes(List<Value> value=[])
-privFunc(Queue q)
-Object *iter()
-async privAsyncFunc(Queue queue=[])
+{abstract} async iterator(Object sort={})
-Queue *queue
-Queue queue
-Resources resoures
}

Expand Down
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ class PlantUmlToCode {
constructor(stream, { logger = dummyLogger } = {}) {
this._stream = stream;
this.logger = logger;
PlantUmlToCode._registerHandlebarsHelpers();
}

static _registerHandlebarsHelpers() {
// helper to avoid escape rendering
// Usage
// {{SafeString this getName}} where 'this' is an class instance and 'getName' are instance function
// or
// {{SafeString this}} where 'this' is an string
const SafeString = (context, method) => new Handlebars.SafeString(
_.isFunction(method) ? method.call(context) : context,
);
Handlebars.registerHelper('SafeString', SafeString);

// Workaround for an apparent bug in Handlebars: functions are not called with the parent scope
// as context.
Expand All @@ -33,7 +46,6 @@ class PlantUmlToCode {
// {{#each getFields}}
// {{#call ../this ../getFullName}}
// {{/each}}

Handlebars.registerHelper('call', (context, member) => member.call(context));
}

Expand Down
13 changes: 12 additions & 1 deletion src/parser/Class.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,25 @@ class Class {
this.nNamespace = null;
}

static splitArrays(acc, dep) {
// List<Value> -> [List, Value]
const parts = dep.split('<');
parts.forEach((part) => {
acc.push(_.trimEnd(part, '>'));
});
return acc;
}

_getDependencies() {
const returnTypes = this.members.map(member => member.getReturnType());
const parameterTypes = this.members
.reduce((acc, member) => [...acc, ...member.getParameters()], [])
.map(params => params.getReturnType());
const ignoreModules = ['void', 'async'];
const all = [...returnTypes, ...parameterTypes]
.reduce(Class.splitArrays, [])
.filter(type => ignoreModules.indexOf(type) === -1);

return _.uniq(all);
}

Expand All @@ -32,7 +43,7 @@ class Class {
const nativeModules = Class.langNativeModules.ecmascript6;
const allDeps = this._getDependencies();
const isValid = dep => nativeModules.indexOf(dep) !== -1;
return _.filter(allDeps, isValid);
return _.uniq(_.filter(allDeps, isValid));
}

get3rdPartyModules() {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/plantuml.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ objectname
membername
= items:([A-Za-z_\*][A-Za-z0-9_]*) { return [items[0], items[1].join("")].join("") }
defaultvalue
= items:([{}\[\]A-Za-z0-9_]*) { return items.join("") }
= items:([{}\[\]A-Za-z0-9_\'\"]*) { return items.join("") }
accessortype
= publicaccessor
/ privateaccessor
Expand Down
4 changes: 2 additions & 2 deletions src/templates/ecmascript6.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class {{getFullName}}{{#if getExtends}} extends {{#with getExtends}}{{getFullNam

/**{{#if getNote}}
* {{getNote}}{{/if}}{{#each getParameters}}
* @param { {{getReturnType}} } {{getName}} TBD{{#if getDefaultValue}}, default={{getDefaultValue}}{{/if}}{{/each}}{{#if needsReturnStatement}}
* @param { {{SafeString this getReturnType}} } {{getName}}{{#if getDefaultValue}}={{SafeString this getDefaultValue}}{{/if}} TBD{{/each}}{{#if needsReturnStatement}}
* @return { {{getReturnType}} }{{/if}}{{#if isPrivate}}
* @private{{/if}}
*/
{{#if isAsync}}async {{/if}}{{getName}}({{#if getParameters}}{{#each getParameters}}{{#if @first}}{{else}}, {{/if}}{{#if getName}}{{getName}}{{#if getDefaultValue}}={{getDefaultValue}}{{/if}}{{else}}param{{@index}}{{/if}}{{/each}}{{/if}}) {
{{#if isAsync}}async {{/if}}{{getName}}({{#if getParameters}}{{#each getParameters}}{{#if @first}}{{else}}, {{/if}}{{#if getName}}{{getName}}{{#if getDefaultValue}}={{SafeString this getDefaultValue}}{{/if}}{{else}}param{{@index}}{{/if}}{{/each}}{{/if}}) {
{{#if isInterface}}throw new Error('Not implemented');{{else}}{{#if this.needsReturnStatement}}return {{getReturnType}};{{else}}// TBD{{/if}}{{/if}}{{/if}}
}{{/each}}
}
Expand Down
4 changes: 2 additions & 2 deletions test/data/car.ecmascript6.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class Car extends Vehicle {
}

/**
* @param { String } model TBD
* @param { String } model='lada' TBD
*/
setModel(model) {
setModel(model='lada') {
// TBD
}

Expand Down
2 changes: 1 addition & 1 deletion test/data/car.puml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface Vehicle {
}

abstract Car {
+ void setModel(String model)
+ void setModel(String model='lada')
+ void setMake(String make)
+ void setYear(Number)
+ String getModel()
Expand Down

0 comments on commit a7d7bf1

Please sign in to comment.