2015-01-20 16:18:56 +00:00
|
|
|
|
/**
|
|
|
|
|
* Add comments in a TypeScript definition file
|
|
|
|
|
*/
|
|
|
|
|
'use strict';
|
|
|
|
|
var ts = require('typescript-services');
|
|
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
|
|
var TypeScriptDocGenerator = (function () {
|
|
|
|
|
function TypeScriptDocGenerator(tsDefFileName, jsdocJsonFileName) {
|
|
|
|
|
this.pos = 0;
|
|
|
|
|
this.nbCharsAdded = 0;
|
|
|
|
|
this.jsonDocsFileName = jsdocJsonFileName;
|
|
|
|
|
this.tsDefFileName = tsDefFileName;
|
|
|
|
|
this.jsonDocsFileContent = fs.readFileSync(this.jsonDocsFileName, 'utf-8');
|
|
|
|
|
this.docs = JSON.parse(this.jsonDocsFileContent);
|
|
|
|
|
this.tsDefFileContent = fs.readFileSync(this.tsDefFileName, 'utf-8');
|
|
|
|
|
this.tree = ts.Parser.parse('', ts.SimpleText.fromString(this.tsDefFileContent), true, new ts.ParseOptions(ts.LanguageVersion.EcmaScript5, true));
|
|
|
|
|
this.sourceUnit = this.tree.sourceUnit();
|
|
|
|
|
this.lineMap = this.tree.lineMap();
|
|
|
|
|
}
|
|
|
|
|
TypeScriptDocGenerator.prototype.completePrefix = function (oldPrefix, appendedPrefix) {
|
2015-01-20 17:02:04 +00:00
|
|
|
|
if (oldPrefix === "") {
|
2015-01-20 16:18:56 +00:00
|
|
|
|
return appendedPrefix;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return oldPrefix + "." + appendedPrefix;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
TypeScriptDocGenerator.prototype.repeatSpaces = function (nb) {
|
|
|
|
|
var res = "";
|
|
|
|
|
for (var i = 0; i < nb; i++) {
|
|
|
|
|
res += " ";
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
TypeScriptDocGenerator.prototype.leadingWidth = function (nodeOrToken) {
|
|
|
|
|
if (nodeOrToken != null) {
|
|
|
|
|
for (var i = 0; i < nodeOrToken.childCount() ; i++) {
|
|
|
|
|
var ltw = nodeOrToken.childAt(i).leadingTriviaWidth();
|
|
|
|
|
if (ltw > 0) {
|
|
|
|
|
return ltw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
};
|
|
|
|
|
TypeScriptDocGenerator.prototype.insertComment = function (commentLines, position) {
|
|
|
|
|
if ((commentLines != null) && (commentLines.length > 0)) {
|
|
|
|
|
var nbChars = 0;
|
|
|
|
|
for (var i = 0; i < commentLines.length; i++) {
|
|
|
|
|
nbChars += commentLines[i].trim().length;
|
|
|
|
|
}
|
|
|
|
|
if (nbChars > 0) {
|
|
|
|
|
var lc = this.lineMap.getLineAndCharacterFromPosition(position);
|
|
|
|
|
var nbSpaces = lc.character();
|
|
|
|
|
var startLinePosition = this.lineMap.getLineStartPosition(lc.line());
|
|
|
|
|
var comment = "\n" + this.repeatSpaces(nbSpaces) + "/**\n";
|
2015-01-20 17:02:04 +00:00
|
|
|
|
for (var j = 0; j < commentLines.length; j++) {
|
|
|
|
|
comment += this.repeatSpaces(nbSpaces) + "* " + commentLines[j] + "\n";
|
2015-01-20 16:18:56 +00:00
|
|
|
|
}
|
|
|
|
|
comment += this.repeatSpaces(nbSpaces) + "*/\n";
|
|
|
|
|
this.tsDefFileContent = this.tsDefFileContent.substr(0, startLinePosition + this.nbCharsAdded) + comment + this.tsDefFileContent.substr(startLinePosition + this.nbCharsAdded);
|
|
|
|
|
this.nbCharsAdded += comment.length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
TypeScriptDocGenerator.prototype.findClass = function (className) {
|
2015-01-20 17:02:04 +00:00
|
|
|
|
if (className.indexOf("p2.") === 0) {
|
2015-01-20 16:18:56 +00:00
|
|
|
|
className = className.replace("p2.", "Phaser.Physics.P2.");
|
|
|
|
|
}
|
|
|
|
|
var elements = this.docs.classes.filter(function (element) {
|
|
|
|
|
return (element.name === className);
|
|
|
|
|
});
|
|
|
|
|
return elements[0];
|
|
|
|
|
};
|
|
|
|
|
TypeScriptDocGenerator.prototype.generateMemberComments = function (className, memberName) {
|
|
|
|
|
var c = this.findClass(className);
|
|
|
|
|
if (c != null) {
|
|
|
|
|
for (var i = 0; i < c.members.length; i++) {
|
2015-01-20 17:14:53 +00:00
|
|
|
|
if (c.members[i].name === memberName) {
|
2015-01-20 16:18:56 +00:00
|
|
|
|
var m = c.members[i];
|
2015-01-20 17:02:04 +00:00
|
|
|
|
var comments = [];
|
2015-01-20 16:18:56 +00:00
|
|
|
|
comments = comments.concat(m.description.split("\n"));
|
2015-01-20 17:02:04 +00:00
|
|
|
|
if ((m.default != null) && (m.default !== "")) {
|
2015-01-20 16:18:56 +00:00
|
|
|
|
comments.push("Default: " + m.default);
|
|
|
|
|
}
|
|
|
|
|
return comments;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
TypeScriptDocGenerator.prototype.generateClassComments = function (className) {
|
|
|
|
|
var c = this.findClass(className);
|
|
|
|
|
if (c != null) {
|
2015-01-20 17:02:04 +00:00
|
|
|
|
var comments = [];
|
2015-01-20 16:18:56 +00:00
|
|
|
|
comments = comments.concat(c.description.split("\n"));
|
|
|
|
|
return comments;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
TypeScriptDocGenerator.prototype.generateConstructorComments = function (className) {
|
|
|
|
|
var c = this.findClass(className);
|
|
|
|
|
if (c != null) {
|
|
|
|
|
var con = c.constructor;
|
2015-01-20 17:02:04 +00:00
|
|
|
|
var comments = [];
|
2015-01-20 16:18:56 +00:00
|
|
|
|
comments = comments.concat(con.description.split("\n"));
|
|
|
|
|
if (con.parameters.length > 0) {
|
|
|
|
|
comments.push("");
|
|
|
|
|
}
|
|
|
|
|
for (var j = 0; j < con.parameters.length; j++) {
|
|
|
|
|
var p = con.parameters[j];
|
|
|
|
|
if (p.type === "*") {
|
|
|
|
|
p.name = "args";
|
|
|
|
|
}
|
|
|
|
|
var def = "";
|
2015-01-20 17:02:04 +00:00
|
|
|
|
if ((p.default != null) && (p.default !== "")) {
|
2015-01-20 16:18:56 +00:00
|
|
|
|
def = " - Default: " + p.default;
|
|
|
|
|
}
|
2015-01-21 07:50:14 +00:00
|
|
|
|
var paramComments = p.description.split("\n");
|
|
|
|
|
for (var k = 0; k < paramComments.length; k++) {
|
|
|
|
|
if (k === 0) {
|
|
|
|
|
comments.push("@param " + p.name + " " + paramComments[k].trim() + ((k === paramComments.length - 1) ? def : ""));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
comments.push(this.repeatSpaces(("@param " + p.name + " ").length) + paramComments[k].trim() + ((k === paramComments.length - 1) ? def : ""));
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-20 16:18:56 +00:00
|
|
|
|
}
|
|
|
|
|
return comments;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
TypeScriptDocGenerator.prototype.generateFunctionComments = function (className, functionName) {
|
|
|
|
|
var c = this.findClass(className);
|
|
|
|
|
if (c != null) {
|
|
|
|
|
for (var i = 0; i < c.functions.length; i++) {
|
2015-01-20 17:02:04 +00:00
|
|
|
|
if (c.functions[i].name === functionName) {
|
2015-01-20 16:18:56 +00:00
|
|
|
|
var f = c.functions[i];
|
2015-01-20 17:02:04 +00:00
|
|
|
|
var comments = [];
|
2015-01-20 16:18:56 +00:00
|
|
|
|
comments = comments.concat(f.description.split("\n"));
|
|
|
|
|
if (f.parameters.length > 0) {
|
|
|
|
|
comments.push("");
|
|
|
|
|
}
|
|
|
|
|
for (var j = 0; j < f.parameters.length; j++) {
|
|
|
|
|
var p = f.parameters[j];
|
|
|
|
|
if (p.type === "*") {
|
|
|
|
|
p.name = "args";
|
|
|
|
|
}
|
|
|
|
|
var def = "";
|
2015-01-20 17:02:04 +00:00
|
|
|
|
if ((p.default != null) && (p.default !== "")) {
|
2015-01-20 16:18:56 +00:00
|
|
|
|
def = " - Default: " + p.default;
|
|
|
|
|
}
|
2015-01-21 07:50:14 +00:00
|
|
|
|
|
|
|
|
|
var paramComments = p.description.split("\n");
|
|
|
|
|
for (var k = 0; k < paramComments.length; k++)
|
|
|
|
|
{
|
|
|
|
|
if (k === 0)
|
|
|
|
|
{
|
|
|
|
|
comments.push("@param " + p.name + " " + paramComments[k].trim() + ((k === paramComments.length - 1) ? def : ""));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
comments.push(this.repeatSpaces(("@param " + p.name + " ").length) + paramComments[k].trim() + ((k === paramComments.length - 1) ? def : ""));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-20 16:18:56 +00:00
|
|
|
|
}
|
|
|
|
|
if (f.returns != null) {
|
2015-01-21 07:50:14 +00:00
|
|
|
|
comments.push("@return " + f.returns.description);
|
2015-01-20 16:18:56 +00:00
|
|
|
|
}
|
|
|
|
|
return comments;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
TypeScriptDocGenerator.prototype.scanClass = function (c, fullName, classPos) {
|
|
|
|
|
for (var i = 0; i < c.childCount() ; i++) {
|
|
|
|
|
var elem = c.childAt(i);
|
|
|
|
|
if (elem != null) {
|
|
|
|
|
switch (elem.kind()) {
|
|
|
|
|
case ts.SyntaxKind.List:
|
|
|
|
|
classPos = this.scanClass(elem, fullName, classPos);
|
|
|
|
|
break;
|
|
|
|
|
case ts.SyntaxKind.ConstructorDeclaration:
|
|
|
|
|
this.insertComment(this.generateConstructorComments(fullName), classPos + this.leadingWidth(elem));
|
|
|
|
|
break;
|
|
|
|
|
case ts.SyntaxKind.MemberVariableDeclaration:
|
|
|
|
|
this.insertComment(this.generateMemberComments(fullName, elem.variableDeclarator.propertyName.fullText().trim()), classPos + this.leadingWidth(elem));
|
|
|
|
|
break;
|
|
|
|
|
case ts.SyntaxKind.MemberFunctionDeclaration:
|
|
|
|
|
this.insertComment(this.generateFunctionComments(fullName, elem.propertyName.fullText().trim()), classPos + this.leadingWidth(elem));
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-01-20 17:02:04 +00:00
|
|
|
|
if (elem.kind() !== ts.SyntaxKind.List) {
|
2015-01-20 16:18:56 +00:00
|
|
|
|
classPos += elem.fullWidth();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return classPos;
|
|
|
|
|
};
|
|
|
|
|
TypeScriptDocGenerator.prototype.scan = function (elem, prefix) {
|
|
|
|
|
if (elem != null) {
|
|
|
|
|
switch (elem.kind()) {
|
|
|
|
|
case ts.SyntaxKind.List:
|
|
|
|
|
for (var k = 0; k < elem.childCount() ; k++) {
|
|
|
|
|
this.scan(elem.childAt(k), prefix);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case ts.SyntaxKind.InterfaceDeclaration:
|
|
|
|
|
break;
|
|
|
|
|
case ts.SyntaxKind.ClassDeclaration:
|
|
|
|
|
var fullClassName = this.completePrefix(prefix, elem.identifier.fullText().trim());
|
|
|
|
|
this.insertComment(this.generateClassComments(fullClassName), this.pos + this.leadingWidth(elem));
|
|
|
|
|
this.scanClass(elem, fullClassName, this.pos);
|
|
|
|
|
break;
|
|
|
|
|
case ts.SyntaxKind.ModuleDeclaration:
|
|
|
|
|
for (var j = 0; j < elem.childCount() ; j++) {
|
|
|
|
|
this.scan(elem.childAt(j), this.completePrefix(prefix, elem.name.fullText().trim()));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-01-20 17:02:04 +00:00
|
|
|
|
if ((elem.kind() !== ts.SyntaxKind.List) && (elem.kind() !== ts.SyntaxKind.ModuleDeclaration)) {
|
2015-01-20 16:18:56 +00:00
|
|
|
|
this.pos += elem.fullWidth();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
TypeScriptDocGenerator.prototype.getTsDefCommentedFileContent = function () {
|
|
|
|
|
for (var i = 0; i < this.sourceUnit.childCount() ; i++) {
|
|
|
|
|
this.scan(this.sourceUnit.childAt(i), "");
|
|
|
|
|
}
|
|
|
|
|
return this.tsDefFileContent;
|
|
|
|
|
};
|
|
|
|
|
return TypeScriptDocGenerator;
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
module.exports = function (grunt) {
|
|
|
|
|
grunt.registerMultiTask('buildtsdoc', 'Generate a TypeScript def with comments', function () {
|
|
|
|
|
var tsdg = new TypeScriptDocGenerator(this.data.tsDefFileName, this.data.jsdocJsonFileName);
|
|
|
|
|
fs.writeFileSync(this.data.dest, tsdg.getTsDefCommentedFileContent(), 'utf8');
|
|
|
|
|
});
|
|
|
|
|
};
|