phaser/v2/tasks/buildtsdoc-debug.js

462 lines
14 KiB
JavaScript
Raw Normal View History

2015-08-24 14:43:45 +00:00
/**
* Add comments in a TypeScript definition file
*/
'use strict';
var ts = require('typescript');
var fs = require('fs');
var _grunt = null;
2015-08-24 14:43:45 +00:00
var TypeScriptDocGenerator = (function () {
2015-08-24 14:43:45 +00:00
function TypeScriptDocGenerator(tsDefFileName, jsdocJsonFileName) {
_grunt.log.writeln("TS Defs: " + tsDefFileName + " json: " + jsdocJsonFileName);
2015-08-24 14:43:45 +00:00
this.nbCharsAdded = 0;
this.tsDefFileName = ts.normalizePath(tsDefFileName);
this.tsDefFileContent = fs.readFileSync(this.tsDefFileName, 'utf-8').toString();
this.delintNodeFunction = this.delintNode.bind(this);
2015-08-24 14:43:45 +00:00
var jsonDocsFileContent = fs.readFileSync(jsdocJsonFileName, 'utf-8').toString();
2015-08-24 14:43:45 +00:00
this.docs = JSON.parse(jsonDocsFileContent);
_grunt.log.writeln("json parsed");
2015-08-24 14:43:45 +00:00
var options = { target: ts.ScriptTarget.ES5, module: ts.ModuleKind.AMD };
var host = ts.createCompilerHost(options);
var program = ts.createProgram([this.tsDefFileName], options, host);
2015-08-24 14:43:45 +00:00
this.sourceFile = program.getSourceFile(this.tsDefFileName);
2015-08-24 14:43:45 +00:00
}
2015-08-24 14:43:45 +00:00
TypeScriptDocGenerator.prototype.getTsDefCommentedFileContent = function () {
2015-08-24 14:43:45 +00:00
this.scan();
2015-08-24 14:43:45 +00:00
return this.tsDefFileContent;
2015-08-24 14:43:45 +00:00
};
2015-08-24 14:43:45 +00:00
TypeScriptDocGenerator.prototype.repeatSpaces = function (nb) {
2015-08-24 14:43:45 +00:00
var res = "";
for (var i = 0; i < nb; i++)
{
2015-08-24 14:43:45 +00:00
res += " ";
}
2015-08-24 14:43:45 +00:00
return res;
2015-08-24 14:43:45 +00:00
};
2015-08-24 14:43:45 +00:00
TypeScriptDocGenerator.prototype.insertComment = function (commentLines, position) {
if ((commentLines != null) && (commentLines.length > 0))
{
2015-08-24 14:43:45 +00:00
var nbChars = 0;
for (var i = 0; i < commentLines.length; i++)
{
2015-08-24 14:43:45 +00:00
nbChars += commentLines[i].trim().length;
}
if (nbChars > 0)
{
2015-08-24 14:43:45 +00:00
var lc = this.sourceFile.getLineAndCharacterFromPosition(position);
var nbSpaces = lc.character - 1;
var startLinePosition = this.sourceFile.getLineStarts()[lc.line - 1];
var comment = "\r\n" + this.repeatSpaces(nbSpaces) + "/**\r\n";
for (var j = 0; j < commentLines.length; j++)
{
2015-08-24 14:43:45 +00:00
comment += this.repeatSpaces(nbSpaces) + "* " + commentLines[j].trimRight() + "\r\n";
}
2015-08-24 14:43:45 +00:00
comment += this.repeatSpaces(nbSpaces) + "*/\r\n";
2015-08-24 14:43:45 +00:00
this.tsDefFileContent = this.tsDefFileContent.substr(0, startLinePosition + this.nbCharsAdded) + comment + this.tsDefFileContent.substr(startLinePosition + this.nbCharsAdded);
this.nbCharsAdded += comment.length;
// _grunt.log.writeln("comment: " + comment);
2015-08-24 14:43:45 +00:00
}
}
2015-08-24 14:43:45 +00:00
};
2015-08-24 14:43:45 +00:00
TypeScriptDocGenerator.prototype.cleanEndLine = function (str) {
2015-08-24 14:43:45 +00:00
return str.replace(new RegExp('[' + "\r\n" + ']', 'g'), "\n").replace(new RegExp('[' + "\r" + ']', 'g'), "\n");
2015-08-24 14:43:45 +00:00
};
2015-08-24 14:43:45 +00:00
TypeScriptDocGenerator.prototype.findClass = function (className) {
// _grunt.log.writeln("findClass: " + className);
if (className.indexOf("p2.") === 0)
{
2015-08-24 14:43:45 +00:00
className = className.replace("p2.", "Phaser.Physics.P2.");
}
2015-08-24 14:43:45 +00:00
var elements = this.docs.classes.filter(function (element) {
return (element.name === className);
});
2015-08-24 14:43:45 +00:00
return elements[0];
2015-08-24 14:43:45 +00:00
};
2015-08-24 14:43:45 +00:00
TypeScriptDocGenerator.prototype.generateClassComments = function (className) {
// _grunt.log.writeln("generateClassComments: " + className);
2015-08-24 14:43:45 +00:00
var c = this.findClass(className);
// _grunt.log.writeln("generateClassComments class found: " + JSON.stringify(c));
if (c !== null && c !== undefined)
{
2015-08-24 14:43:45 +00:00
var comments = [];
comments = comments.concat(this.cleanEndLine(c.description).split("\n"));
// _grunt.log.writeln("generateClassComments return comments");
2015-08-24 14:43:45 +00:00
return comments;
}
else
{
// _grunt.log.writeln("generateClassComments return null");
2015-08-24 14:43:45 +00:00
return null;
}
2015-08-24 14:43:45 +00:00
};
2015-08-24 14:43:45 +00:00
TypeScriptDocGenerator.prototype.generateMemberComments = function (className, memberName) {
_grunt.log.writeln("generateMemberComments: " + className + " = " + memberName);
2015-08-24 14:43:45 +00:00
var c = this.findClass(className);
if (c !== null)
{
for (var i = 0; i < c.members.length; i++)
{
if (c.members[i].name === memberName)
{
2015-08-24 14:43:45 +00:00
var m = c.members[i];
var comments = [];
comments = comments.concat(this.cleanEndLine(m.description).split("\n"));
if ((m.default != null) && (m.default !== ""))
{
2015-08-24 14:43:45 +00:00
comments.push("Default: " + m.default);
}
2015-08-24 14:43:45 +00:00
return comments;
}
}
}
else
{
2015-08-24 14:43:45 +00:00
return null;
}
2015-08-24 14:43:45 +00:00
};
2015-08-24 14:43:45 +00:00
TypeScriptDocGenerator.prototype.generateFunctionComments = function (className, functionName) {
_grunt.log.writeln("generateFunctionComments: " + className);
2015-08-24 14:43:45 +00:00
var c = this.findClass(className);
if (c !== null)
{
for (var i = 0; i < c.functions.length; i++)
{
if (c.functions[i].name === functionName)
{
2015-08-24 14:43:45 +00:00
var f = c.functions[i];
var comments = [];
comments = comments.concat(this.cleanEndLine(f.description).split("\n"));
if (f.parameters.length > 0)
{
2015-08-24 14:43:45 +00:00
comments.push("");
}
for (var j = 0; j < f.parameters.length; j++)
{
2015-08-24 14:43:45 +00:00
var p = f.parameters[j];
if (p.type === "*")
{
2015-08-24 14:43:45 +00:00
p.name = "args";
}
2015-08-24 14:43:45 +00:00
var def = "";
if ((p.default != null) && (p.default !== ""))
{
2015-08-24 14:43:45 +00:00
def = " - Default: " + p.default;
}
2015-08-24 14:43:45 +00:00
var paramComments = this.cleanEndLine(p.description).split("\n");
for (var k = 0; k < paramComments.length; k++)
{
if (k === 0)
{
2015-08-24 14:43:45 +00:00
comments.push("@param " + p.name + " " + paramComments[k].trim() + ((k === paramComments.length - 1) ? def : ""));
}
else
{
2015-08-24 14:43:45 +00:00
comments.push(this.repeatSpaces(("@param " + p.name + " ").length) + paramComments[k].trim() + ((k === paramComments.length - 1) ? def : ""));
}
}
}
if ((f.returns != null) && (f.returns.description.trim().length > 0))
{
2015-08-24 14:43:45 +00:00
var returnComments = this.cleanEndLine(f.returns.description).split("\n");
for (var l = 0; l < returnComments.length; l++)
{
if (l === 0)
{
2015-08-24 14:43:45 +00:00
comments.push("@return " + returnComments[l].trim());
}
else
{
2015-08-24 14:43:45 +00:00
comments.push(this.repeatSpaces(("@return ").length) + returnComments[l].trim());
}
}
}
2015-08-24 14:43:45 +00:00
return comments;
}
}
}
else
{
2015-08-24 14:43:45 +00:00
return null;
}
2015-08-24 14:43:45 +00:00
};
2015-08-24 14:43:45 +00:00
TypeScriptDocGenerator.prototype.generateConstructorComments = function (className) {
_grunt.log.writeln("generateConstructorComments: " + className);
2015-08-24 14:43:45 +00:00
var c = this.findClass(className);
if (c !== null)
{
// _grunt.log.writeln("Class: " + c);
2015-08-24 14:43:45 +00:00
var con = c.constructor;
var comments = [];
2015-08-24 14:43:45 +00:00
comments = comments.concat(this.cleanEndLine(con.description).split("\n"));
if (con.parameters.length > 0)
{
2015-08-24 14:43:45 +00:00
comments.push("");
}
for (var j = 0; j < con.parameters.length; j++)
{
2015-08-24 14:43:45 +00:00
var p = con.parameters[j];
if (p.type === "*")
{
2015-08-24 14:43:45 +00:00
p.name = "args";
}
2015-08-24 14:43:45 +00:00
var def = "";
if ((p.default != null) && (p.default !== ""))
{
2015-08-24 14:43:45 +00:00
def = " - Default: " + p.default;
}
2015-08-24 14:43:45 +00:00
var paramComments = this.cleanEndLine(p.description).split("\n");
for (var k = 0; k < paramComments.length; k++)
{
if (k === 0)
{
2015-08-24 14:43:45 +00:00
comments.push("@param " + p.name + " " + paramComments[k].trim() + ((k === paramComments.length - 1) ? def : ""));
}
else
{
2015-08-24 14:43:45 +00:00
comments.push(this.repeatSpaces(("@param " + p.name + " ").length) + paramComments[k].trim() + ((k === paramComments.length - 1) ? def : ""));
}
}
}
2015-08-24 14:43:45 +00:00
return comments;
2015-08-24 14:43:45 +00:00
}
else
{
2015-08-24 14:43:45 +00:00
return null;
}
2015-08-24 14:43:45 +00:00
};
2015-08-24 14:43:45 +00:00
TypeScriptDocGenerator.prototype.scan = function () {
2015-08-24 14:43:45 +00:00
this.delintNode(this.sourceFile);
2015-08-24 14:43:45 +00:00
};
2015-08-24 14:43:45 +00:00
TypeScriptDocGenerator.prototype.getClassName = function (node) {
// _grunt.log.writeln("getClassName: " + JSON.stringify(node));
// _grunt.log.writeln("getClassName: " + JSON.stringify(node.kind));
// _grunt.log.writeln("getClassName: " + JSON.stringify(node.name));
2015-08-24 14:43:45 +00:00
var fullName = '';
if (node.name !== undefined && node.kind === ts.SyntaxKind.ClassDeclaration)
{
// _grunt.log.writeln("getClassName 1a: " + node.name.text);
try {
fullName = node.name.getText();
// _grunt.log.writeln("getClassName 1b: " + fullName);
}
catch (e)
{
fullName = node.name.text;
// _grunt.log.writeln("getClassName bail");
// return '';
}
2015-08-24 14:43:45 +00:00
}
2015-08-24 14:43:45 +00:00
var parent = node.parent;
while (parent !== null && parent !== undefined)
{
// _grunt.log.writeln("getClassName 2");
if (parent.kind === ts.SyntaxKind.ModuleDeclaration || parent.kind === ts.SyntaxKind.ClassDeclaration)
{
2015-08-24 14:43:45 +00:00
fullName = parent.name.getText() + ((fullName !== '') ? "." + fullName : fullName);
}
2015-08-24 14:43:45 +00:00
parent = parent.parent;
}
if (fullName === undefined || fullName === null)
{
fullName = '';
}
// _grunt.log.writeln("getClassName: " + fullName);
2015-08-24 14:43:45 +00:00
return fullName;
2015-08-24 14:43:45 +00:00
};
2015-08-24 14:43:45 +00:00
TypeScriptDocGenerator.prototype.delintNode = function (node) {
var c = this.getClassName(node);
var r = true;
try {
r = node.getStart();
}
catch (e)
{
r = false;
}
switch (node.kind)
{
2015-08-24 14:43:45 +00:00
case ts.SyntaxKind.Constructor:
// _grunt.log.writeln("insert1: " + node);
if (c !== '' && r)
{
this.insertComment(this.generateConstructorComments(c, r));
}
2015-08-24 14:43:45 +00:00
break;
2015-08-24 14:43:45 +00:00
case ts.SyntaxKind.ClassDeclaration:
// _grunt.log.writeln("insertX2: " + JSON.stringify(node));
// _grunt.log.writeln("insertX2a: " + JSON.stringify(node.name));
// _grunt.log.writeln("insertX2b: " + this.getClassName(node));
// _grunt.log.writeln("insertX2c: " + r);
// _grunt.log.writeln("insertX2d ...");
if (c !== '' && r)
{
this.insertComment(this.generateClassComments(c, r));
}
2015-08-24 14:43:45 +00:00
break;
2015-08-24 14:43:45 +00:00
case ts.SyntaxKind.Property:
// _grunt.log.writeln("insert3: " + node);
var t = true;
try {
t = node.name.getText();
}
catch (e)
{
t = false;
}
if (c !== '' && r && t)
{
this.insertComment(this.generateMemberComments(c, t, r));
}
2015-08-24 14:43:45 +00:00
break;
2015-08-24 14:43:45 +00:00
case ts.SyntaxKind.Method:
// _grunt.log.writeln("insert4: " + node);
2016-06-17 02:10:06 +00:00
var t2 = true;
try {
2016-06-17 02:10:06 +00:00
t2 = node.name.getText();
}
catch (e)
{
2016-06-17 02:10:06 +00:00
t2 = false;
}
2016-06-17 02:10:06 +00:00
if (c !== '' && r && t2)
{
2016-06-17 02:10:06 +00:00
this.insertComment(this.generateFunctionComments(c, t2, r));
}
2015-08-24 14:43:45 +00:00
break;
}
2015-08-24 14:43:45 +00:00
ts.forEachChild(node, this.delintNodeFunction);
2015-08-24 14:43:45 +00:00
};
2015-08-24 14:43:45 +00:00
return TypeScriptDocGenerator;
2015-08-24 14:43:45 +00:00
})();
2015-08-24 14:43:45 +00:00
module.exports = function (grunt) {
_grunt = grunt;
2015-08-24 14:43:45 +00:00
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');
});
2015-08-24 14:43:45 +00:00
};