From 35b905fb11beda7646eda4dd2b2227b286d301ce Mon Sep 17 00:00:00 2001 From: PanJarda Date: Fri, 24 Jun 2016 14:40:58 +0200 Subject: [PATCH 1/3] RiotTagCodeGenerator --- CodeGenerators/RiotTagCodeGenerator.js | 254 +++++++++++++++++++++++++ JSCodeGenerator.js | 5 + JavaScriptConfigure.js | 3 +- 3 files changed, 261 insertions(+), 1 deletion(-) create mode 100644 CodeGenerators/RiotTagCodeGenerator.js diff --git a/CodeGenerators/RiotTagCodeGenerator.js b/CodeGenerators/RiotTagCodeGenerator.js new file mode 100644 index 0000000..aac11e6 --- /dev/null +++ b/CodeGenerators/RiotTagCodeGenerator.js @@ -0,0 +1,254 @@ +/** + * Created by cotlod on 15-11-09. + */ + +define(function (require, exports, module) { + + var CodeGenerator = require("CodeGenerators/CodeGenerator").CodeGenerator; + + function RiotTagCodeGenerator(options) { + + this.tabSize = options.indentSpaces || 4; + + } + + RiotTagCodeGenerator.prototype = new CodeGenerator(); + + RiotTagCodeGenerator.prototype.getFileName = function(fileName, withExtension){ + var extension = ""; + if (withExtension) { + extension = ".tag"; + } + return fileName + extension; + }; + + RiotTagCodeGenerator.prototype.matchDocPattern = function(node){ + return(node.replace("\n", "\n*" + this.getTab()) + "\n" + this.getTab() + " *\n"); + } + + RiotTagCodeGenerator.prototype.getMethodDocumentation = function (op) { + + var s = ""; + + s += "\n" + this.getTab() + "/**\n"; + + if (op.documentation && op.documentation !== "") { + s += this.getTab() + " * @documentation: " + this.matchDocPattern(op.documentation); + } + + if (op.specification && op.specification !== "") { + s += this.getTab() + " * @specification: " + this.matchDocPattern(op.specification); + } + + var preconditionsLength = op.preconditions.length; + + for (var i = 0; i < preconditionsLength; i++) { + + var precondition = op.preconditions[i]; + + if (precondition instanceof type.UMLConstraint) + s += this.getTab() + " * @precondition " + precondition.name + " : " + this.matchDocPattern(precondition.specification); + } + + var postconditionsLength = op.postconditions.length; + + for (i = 0; i < postconditionsLength; i++) { + + var postcondition = op.postconditions[i]; + + if (postcondition instanceof type.UMLConstraint) + s += this.getTab() + " * @postcondition " + postcondition.name + " : " + this.matchDocPattern(postcondition.specification); + } + + s += this.getDocumentationParameters(op); + s += this.getTab() + " */\n"; + + return s; + }; + + RiotTagCodeGenerator.prototype.getDocumentationParameters = function(op) { + + var parametersString = ""; + + var parametersLength = op.parameters.length; + + for (var p = 0; p < parametersLength; p++) { + + var parameter = op.parameters[p]; + + switch(parameter.direction){ + case "return": + parametersString += this.getTab() + " * @return "; + break; + + case "in": + parametersString += this.getTab() + " * @param "; + break; + } + + parametersString += parameter.name; + + if (parameter.type) parametersString += " {" + parameter.type + "} "; + + parametersString += this.matchDocPattern(parameter.documentation); + } + + return parametersString; + }; + + RiotTagCodeGenerator.prototype.getDependencies = function (elem) { + + if (!elem || !elem.ownedElements || !elem.ownedElements.length) { + + return ""; + } + + var s = ""; + + var ownedElementsLength = elem.ownedElements.length; + + for (var i = 0; i < ownedElementsLength; i++) { + + var ownedElement = elem.ownedElements[i]; + + if (ownedElement instanceof type.UMLGeneralization) { + + if (ownedElement.target instanceof type.UMLClass) { + + s += "require('./" + ownedElement.target.name + ".tag')\n"; + } + + } else if (this.validUMLAssociation(ownedElement)) { + + s += "require('./" + ownedElement.end1.name + ".tag')\n"; + + } else if ( ownedElement instanceof type.UMLDependency && + ownedElement.target instanceof type.UMLClass && + ownedElement.target.name){ + + s += "require('./" + ownedElement.target.name + ".tag')\n"; + + } + } + + return s; + + }; + RiotTagCodeGenerator.prototype.validUMLAssociation = function(elem) { + + return elem instanceof type.UMLAssociation && + elem.end1 instanceof type.UMLAssociationEnd && + elem.end2 instanceof type.UMLAssociationEnd && + elem.end2.reference instanceof type.UMLClass && + elem.end1.name !== "" && + elem.end2.reference.name !== ""; + } + + RiotTagCodeGenerator.prototype.getOperation = function (elem, op) { + + var s = ""; + + s += this.getMethodDocumentation(op); + + //function name + s += this.getTab() + op.name + "(" + this.getOperationParams(op) + ") {"; + + //end function + if (elem.isAbstract) { + s += "\n" + this.getTab() + "throw 'AbstractMethodNotImplementedError';\n\n" + this.getTab() + "}\n\n"; + } else { + s += "\n" + this.getTab() + this.getTab() + "//TODO: Implement Me \n\n" + this.getTab() + "}\n\n"; + } + + return s; + }; + + RiotTagCodeGenerator.prototype.getAttributeDefinitions = function (elem) { + + var s = ""; + + if (!elem || !elem.attributes || !elem.attributes.length) { + + return s; + } + + var ownedElementsLength = elem.ownedElements.length; + + for (var i = 0; i < ownedElementsLength; i++) { + + var ownedElement = elem.ownedElements[i]; + + if (this.validUMLAssociation(ownedElement)){ + s += this.getTab() + "<" + ownedElement.end1.name + "/>\n"; + } + + } + + var attributesLength = elem.attributes.length; + + for (var i = 0; i < attributesLength; i++) { + + s += this.getTab() + "this." + elem.attributes[i].name + " = null;\n"; + } + + return s; + + }; + + RiotTagCodeGenerator.prototype.getInheritance = function (elem) { + + if (!elem || !elem.ownedElements || !elem.ownedElements.length) { + + return ""; + } + + var s = ""; + + for (var i = 0; i < elem.ownedElements.length; i++) { + + if (elem.ownedElements[i] instanceof type.UMLGeneralization) { + + if (elem.ownedElements[i].target instanceof type.UMLClass) { + + s += this.getTab() + "<" + elem.ownedElements[i].target.name + "/>\n"; + } + } + } + + s += "\n"; + + return s; + }; + + + RiotTagCodeGenerator.prototype.getClassDefinition = function (elem) { + + var s = ""; + + s += this.getDependencies(elem); + + s += "<" + elem.name + ">\n"; + + s += this.getInheritance(elem); + + s += this.getAttributeDefinitions(elem); + + s += this.getOperations(elem); + + s += "\n"; + + return s; + }; + + + RiotTagCodeGenerator.prototype.generate = function (elem) { + + var s = ""; + + s += this.getClassDefinition(elem); + + return s; + }; + + exports.RiotTagCodeGenerator = RiotTagCodeGenerator; +}); diff --git a/JSCodeGenerator.js b/JSCodeGenerator.js index cc1c563..0dd11b4 100644 --- a/JSCodeGenerator.js +++ b/JSCodeGenerator.js @@ -15,6 +15,7 @@ define(function (require, exports, module) { var MongooseCodeGenerator = require("CodeGenerators/MongooseCodeGenerator").MongooseCodeGenerator; var EmberDSCodeGenerator = require("CodeGenerators/EmberDSCodeGenerator").EmberDSCodeGenerator; var ES2015CodeGenerator = require("CodeGenerators/ES2015CodeGenerator").ES2015CodeGenerator; + var RiotTagCodeGenerator = require("CodeGenerators/RiotTagCodeGenerator").RiotTagCodeGenerator; /** * @@ -138,6 +139,10 @@ define(function (require, exports, module) { case "es2015": generator = new ES2015CodeGenerator(options); break; + case "riot": + generator = new RiotTagCodeGenerator(options); + break; + } console.log(generator); return generator; diff --git a/JavaScriptConfigure.js b/JavaScriptConfigure.js index 613cbb1..dd2f1c2 100644 --- a/JavaScriptConfigure.js +++ b/JavaScriptConfigure.js @@ -35,7 +35,8 @@ define(function (require, exports, module) { {value: "functional", text: "functional"}, {value: "mongoose", text: "mongoose"}, {value: "ember", text: "ember"}, - {value: "es2015", text: "es2015"}], + {value: "es2015", text: "es2015"}, + {value: "riot", text: "riot"}], default : {value: "es2015", text: "es2015"} }, "javascript.gen.indentSpaces": { From 9f94c37fa137e8fa6835968f3bdb5ea800640d73 Mon Sep 17 00:00:00 2001 From: PanJarda Date: Sat, 25 Jun 2016 16:09:46 +0200 Subject: [PATCH 2/3] code style change --- CodeGenerators/RiotTagCodeGenerator.js | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/CodeGenerators/RiotTagCodeGenerator.js b/CodeGenerators/RiotTagCodeGenerator.js index aac11e6..fa43075 100644 --- a/CodeGenerators/RiotTagCodeGenerator.js +++ b/CodeGenerators/RiotTagCodeGenerator.js @@ -1,5 +1,5 @@ /** - * Created by cotlod on 15-11-09. + * Created by PanJarda on 06-25-2016. */ define(function (require, exports, module) { @@ -167,22 +167,17 @@ define(function (require, exports, module) { var s = ""; - if (!elem || !elem.attributes || !elem.attributes.length) { + var s = "" - return s; - } - - var ownedElementsLength = elem.ownedElements.length; - - for (var i = 0; i < ownedElementsLength; i++) { - - var ownedElement = elem.ownedElements[i]; + if (!elem || !elem.attributes || !elem.attributes.length) + return s - if (this.validUMLAssociation(ownedElement)){ - s += this.getTab() + "<" + ownedElement.end1.name + "/>\n"; - } + var tags = elem.ownedElements - } + tags.forEach(function(tag) { + if (this.validUMLAssociation(tag)) + s += this.getTab() + "<" + tag.end1.name + "/>\n" + }, this) var attributesLength = elem.attributes.length; From 06ca98befca5168fbaff9a19885ded5fd517920d Mon Sep 17 00:00:00 2001 From: PanJarda Date: Sat, 25 Jun 2016 22:31:47 +0200 Subject: [PATCH 3/3] New comment option Better JSDoc comments. New option to turn them off --- CodeGenerators/RiotTagCodeGenerator.js | 120 +++++++++++++++---------- JavaScriptConfigure.js | 6 ++ 2 files changed, 81 insertions(+), 45 deletions(-) diff --git a/CodeGenerators/RiotTagCodeGenerator.js b/CodeGenerators/RiotTagCodeGenerator.js index fa43075..1f5c39a 100644 --- a/CodeGenerators/RiotTagCodeGenerator.js +++ b/CodeGenerators/RiotTagCodeGenerator.js @@ -9,6 +9,8 @@ define(function (require, exports, module) { function RiotTagCodeGenerator(options) { this.tabSize = options.indentSpaces || 4; + console.log(options) + this.comments = options.comments; } @@ -23,44 +25,31 @@ define(function (require, exports, module) { }; RiotTagCodeGenerator.prototype.matchDocPattern = function(node){ - return(node.replace("\n", "\n*" + this.getTab()) + "\n" + this.getTab() + " *\n"); + return(node.replace("\n", "\n*" + this.getTab()) + "\n"); } RiotTagCodeGenerator.prototype.getMethodDocumentation = function (op) { - var s = ""; - - s += "\n" + this.getTab() + "/**\n"; + console.log(this.comments) - if (op.documentation && op.documentation !== "") { - s += this.getTab() + " * @documentation: " + this.matchDocPattern(op.documentation); - } - - if (op.specification && op.specification !== "") { - s += this.getTab() + " * @specification: " + this.matchDocPattern(op.specification); - } + if (!this.comments) + return "" - var preconditionsLength = op.preconditions.length; + console.log('ahoj') - for (var i = 0; i < preconditionsLength; i++) { - - var precondition = op.preconditions[i]; - - if (precondition instanceof type.UMLConstraint) - s += this.getTab() + " * @precondition " + precondition.name + " : " + this.matchDocPattern(precondition.specification); - } + var s = "" - var postconditionsLength = op.postconditions.length; + s += "\n" + this.getTab() + "/**\n" - for (i = 0; i < postconditionsLength; i++) { + s += this.getTab() + " * @method " + op.name + "\n" - var postcondition = op.postconditions[i]; + if (op.documentation && op.documentation !== "") + s += this.getTab() + " * @documentation: " + this.matchDocPattern(op.documentation) - if (postcondition instanceof type.UMLConstraint) - s += this.getTab() + " * @postcondition " + postcondition.name + " : " + this.matchDocPattern(postcondition.specification); - } + if (op.specification && op.specification !== "") + s += this.getTab() + " * @specification: " + this.matchDocPattern(op.specification) - s += this.getDocumentationParameters(op); + s += this.getDocumentationParameters(op); s += this.getTab() + " */\n"; return s; @@ -74,24 +63,25 @@ define(function (require, exports, module) { for (var p = 0; p < parametersLength; p++) { - var parameter = op.parameters[p]; + var parameter = op.parameters[p]; - switch(parameter.direction){ - case "return": - parametersString += this.getTab() + " * @return "; - break; + switch(parameter.direction){ + case "return": + parametersString += this.getTab() + " * @return "; + break; - case "in": - parametersString += this.getTab() + " * @param "; - break; - } + case "in": + parametersString += this.getTab() + " * @param "; + break; + } - parametersString += parameter.name; + parametersString += parameter.name; - if (parameter.type) parametersString += " {" + parameter.type + "} "; + if (parameter.type) + parametersString += " {" + parameter.type.toString() + "} " - parametersString += this.matchDocPattern(parameter.documentation); - } + parametersString += this.matchDocPattern(parameter.documentation) + } return parametersString; }; @@ -165,26 +155,44 @@ define(function (require, exports, module) { RiotTagCodeGenerator.prototype.getAttributeDefinitions = function (elem) { - var s = ""; - var s = "" if (!elem || !elem.attributes || !elem.attributes.length) return s + var tags = elem.ownedElements tags.forEach(function(tag) { + if (this.validUMLAssociation(tag)) s += this.getTab() + "<" + tag.end1.name + "/>\n" + }, this) - var attributesLength = elem.attributes.length; - for (var i = 0; i < attributesLength; i++) { + var attrs = elem.attributes - s += this.getTab() + "this." + elem.attributes[i].name + " = null;\n"; - } + attrs.forEach(function(attr) { + + if (this.comments && (attr.documentation || attr.type)) { + s += "\n" + this.getTab() + "/**\n" + + if (attr.documentation) + s += this.getTab() + " * " + attr.documentation + "\n" + + if (attr.type) + s += this.getTab() + " * @type {" + attr.type.toString() + "}\n" + + s += this.getTab() + " */\n" + } + + s += this.getTab() + "this." + attr.name + " = " + s += (attr.defaultValue ? attr.defaultValue : 'null') + "\n" + + }, this) + + s += "\n" return s; @@ -216,6 +224,26 @@ define(function (require, exports, module) { }; + RiotTagCodeGenerator.prototype.getHeader = function (elem) { + + if (!this.comments) return ""; + + var now = new Date(); + var s = ""; + + s += "/**\n"; + s += " * Generated On: "+ now.getFullYear()+"-"+(now.getMonth()+1)+"-"+now.getDate()+"\n"; + s += " * Tag: " + elem.name+"\n"; + + if(elem.documentation && elem.documentation !== "") + s += " * Description: "+elem.documentation.replace("\n", "\n* ")+"\n"; + + s += " */\n\n"; + + return s; + + }; + RiotTagCodeGenerator.prototype.getClassDefinition = function (elem) { var s = ""; @@ -240,6 +268,8 @@ define(function (require, exports, module) { var s = ""; + s += this.getHeader(elem); + s += this.getClassDefinition(elem); return s; diff --git a/JavaScriptConfigure.js b/JavaScriptConfigure.js index dd2f1c2..e302dc3 100644 --- a/JavaScriptConfigure.js +++ b/JavaScriptConfigure.js @@ -45,6 +45,12 @@ define(function (require, exports, module) { type : "Number", default : 4 }, + "javascript.gen.comments": { + text : "Generate comments", + description: "Generate comments in JSDoc style.", + type : "Check", + default : false + }, "javascript.gen.generateUnitTests": { text : "Generate Unit Tests", description: "Generate unit tests using Mocha unit test framework",