Merge pull request #1600 from vulvulune/dev

Refactoring of the script to generate comments.d.ts
This commit is contained in:
Richard Davey 2015-02-05 22:57:09 +00:00
commit 4d8f9e8723
3 changed files with 1015 additions and 209 deletions

View file

@ -44,7 +44,7 @@
"yuidocjs": "^0.3.50",
"jsdoc": "~3.3.0-alpha10",
"grunt-jsdoc": "~0.6.2-beta",
"typescript-services": "~0.1.5",
"grunt-git": "^0.3.3"
"grunt-git": "^0.3.3",
"typescript": "^1.4.1"
}
}

View file

@ -2,32 +2,26 @@
* Add comments in a TypeScript definition file
*/
'use strict';
var ts = require('typescript-services');
var ts = require('typescript');
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();
this.tsDefFileName = ts.normalizePath(tsDefFileName);
this.tsDefFileContent = fs.readFileSync(this.tsDefFileName, 'utf-8').toString();
this.delintNodeFunction = this.delintNode.bind(this);
var jsonDocsFileContent = fs.readFileSync(jsdocJsonFileName, 'utf-8').toString();
this.docs = JSON.parse(jsonDocsFileContent);
var options = { target: ts.ScriptTarget.ES5, module: ts.ModuleKind.AMD };
var host = ts.createCompilerHost(options);
var program = ts.createProgram([this.tsDefFileName], options, host);
this.sourceFile = program.getSourceFile(this.tsDefFileName);
}
TypeScriptDocGenerator.prototype.cleanEndLine = function (str) {
return str.replace(new RegExp('[' + "\r\n" + ']', 'g'), "\n").replace(new RegExp('[' + "\r" + ']', 'g'), "\n");
};
TypeScriptDocGenerator.prototype.completePrefix = function (oldPrefix, appendedPrefix) {
if (oldPrefix === "") {
return appendedPrefix;
}
else {
return oldPrefix + "." + appendedPrefix;
}
TypeScriptDocGenerator.prototype.getTsDefCommentedFileContent = function () {
this.scan();
return this.tsDefFileContent;
};
TypeScriptDocGenerator.prototype.repeatSpaces = function (nb) {
var res = "";
@ -36,20 +30,6 @@ var TypeScriptDocGenerator = (function () {
}
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.extractPropertyName = function (pn) {
return pn.withLeadingTrivia().text().trim();
};
TypeScriptDocGenerator.prototype.insertComment = function (commentLines, position) {
if ((commentLines != null) && (commentLines.length > 0)) {
var nbChars = 0;
@ -57,9 +37,9 @@ var TypeScriptDocGenerator = (function () {
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 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++) {
comment += this.repeatSpaces(nbSpaces) + "* " + commentLines[j].trimRight() + "\r\n";
@ -70,6 +50,9 @@ var TypeScriptDocGenerator = (function () {
}
}
};
TypeScriptDocGenerator.prototype.cleanEndLine = function (str) {
return str.replace(new RegExp('[' + "\r\n" + ']', 'g'), "\n").replace(new RegExp('[' + "\r" + ']', 'g'), "\n");
};
TypeScriptDocGenerator.prototype.findClass = function (className) {
if (className.indexOf("p2.") === 0) {
className = className.replace("p2.", "Phaser.Physics.P2.");
@ -79,6 +62,17 @@ var TypeScriptDocGenerator = (function () {
});
return elements[0];
};
TypeScriptDocGenerator.prototype.generateClassComments = function (className) {
var c = this.findClass(className);
if (c != null) {
var comments = [];
comments = comments.concat(this.cleanEndLine(c.description).split("\n"));
return comments;
}
else {
return null;
}
};
TypeScriptDocGenerator.prototype.generateMemberComments = function (className, memberName) {
var c = this.findClass(className);
if (c != null) {
@ -98,12 +92,50 @@ var TypeScriptDocGenerator = (function () {
return null;
}
};
TypeScriptDocGenerator.prototype.generateClassComments = function (className) {
TypeScriptDocGenerator.prototype.generateFunctionComments = function (className, functionName) {
var c = this.findClass(className);
if (c != null) {
var comments = [];
comments = comments.concat(this.cleanEndLine(c.description).split("\n"));
return comments;
for (var i = 0; i < c.functions.length; i++) {
if (c.functions[i].name === functionName) {
var f = c.functions[i];
var comments = [];
comments = comments.concat(this.cleanEndLine(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 = "";
if ((p.default != null) && (p.default !== "")) {
def = " - Default: " + p.default;
}
var paramComments = this.cleanEndLine(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 : ""));
}
}
}
if ((f.returns != null) && (f.returns.description.trim().length > 0)) {
var returnComments = this.cleanEndLine(f.returns.description).split("\n");
for (var l = 0; l < returnComments.length; l++) {
if (l === 0) {
comments.push("@return " + returnComments[l].trim());
}
else {
comments.push(this.repeatSpaces(("@return ").length) + returnComments[l].trim());
}
}
}
return comments;
}
}
}
else {
return null;
@ -143,121 +175,42 @@ var TypeScriptDocGenerator = (function () {
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++) {
if (c.functions[i].name === functionName) {
var f = c.functions[i];
var comments = [];
comments = comments.concat(this.cleanEndLine(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 = "";
if ((p.default != null) && (p.default !== "")) {
def = " - Default: " + p.default;
}
var paramComments = this.cleanEndLine(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 : ""));
}
}
}
if ((f.returns != null) && (f.returns.description.trim().length > 0)) {
var returnComments = this.cleanEndLine(f.returns.description).split("\n");
for (var l = 0; l < returnComments.length; l++) {
if (l === 0) {
comments.push("@return " + returnComments[l].trim());
}
else
{
comments.push(this.repeatSpaces(("@return ").length) + returnComments[l].trim());
}
}
}
return comments;
}
}
}
else {
return null;
}
TypeScriptDocGenerator.prototype.scan = function () {
this.delintNode(this.sourceFile);
};
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, this.extractPropertyName(elem.variableDeclarator.propertyName)), classPos + this.leadingWidth(elem));
break;
case ts.SyntaxKind.MemberFunctionDeclaration:
this.insertComment(this.generateFunctionComments(fullName, this.extractPropertyName(elem.propertyName)), classPos + this.leadingWidth(elem));
break;
}
if (elem.kind() !== ts.SyntaxKind.List) {
classPos += elem.fullWidth();
}
}
TypeScriptDocGenerator.prototype.getClassName = function (node) {
var fullName = '';
if (node.kind === ts.SyntaxKind.ClassDeclaration) {
fullName = node.name.getText();
}
return classPos;
var parent = node.parent;
while (parent != null) {
if (parent.kind === ts.SyntaxKind.ModuleDeclaration || parent.kind === ts.SyntaxKind.ClassDeclaration) {
fullName = parent.name.getText() + ((fullName !== '') ? "." + fullName : fullName);
}
parent = parent.parent;
}
return fullName;
};
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;
}
if ((elem.kind() !== ts.SyntaxKind.List) && (elem.kind() !== ts.SyntaxKind.ModuleDeclaration)) {
this.pos += elem.fullWidth();
}
TypeScriptDocGenerator.prototype.delintNode = function (node) {
switch (node.kind) {
case ts.SyntaxKind.Constructor:
this.insertComment(this.generateConstructorComments(this.getClassName(node)), node.getStart());
break;
case ts.SyntaxKind.ClassDeclaration:
this.insertComment(this.generateClassComments(this.getClassName(node)), node.getStart());
break;
case ts.SyntaxKind.Property:
this.insertComment(this.generateMemberComments(this.getClassName(node), node.name.getText()), node.getStart());
break;
case ts.SyntaxKind.Method:
this.insertComment(this.generateFunctionComments(this.getClassName(node), node.name.getText()), node.getStart());
break;
}
};
TypeScriptDocGenerator.prototype.getTsDefCommentedFileContent = function () {
for (var i = 0; i < this.sourceUnit.childCount() ; i++) {
this.scan(this.sourceUnit.childAt(i), "");
}
return this.tsDefFileContent;
ts.forEachChild(node, this.delintNodeFunction);
};
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);

File diff suppressed because it is too large Load diff