mirror of
https://github.com/photonstorm/phaser
synced 2024-11-24 05:33:35 +00:00
Merge pull request #1600 from vulvulune/dev
Refactoring of the script to generate comments.d.ts
This commit is contained in:
commit
4d8f9e8723
3 changed files with 1015 additions and 209 deletions
|
@ -44,7 +44,7 @@
|
||||||
"yuidocjs": "^0.3.50",
|
"yuidocjs": "^0.3.50",
|
||||||
"jsdoc": "~3.3.0-alpha10",
|
"jsdoc": "~3.3.0-alpha10",
|
||||||
"grunt-jsdoc": "~0.6.2-beta",
|
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,32 +2,26 @@
|
||||||
* Add comments in a TypeScript definition file
|
* Add comments in a TypeScript definition file
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
var ts = require('typescript-services');
|
|
||||||
|
var ts = require('typescript');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
|
||||||
var TypeScriptDocGenerator = (function () {
|
var TypeScriptDocGenerator = (function () {
|
||||||
function TypeScriptDocGenerator(tsDefFileName, jsdocJsonFileName) {
|
function TypeScriptDocGenerator(tsDefFileName, jsdocJsonFileName) {
|
||||||
this.pos = 0;
|
|
||||||
this.nbCharsAdded = 0;
|
this.nbCharsAdded = 0;
|
||||||
this.jsonDocsFileName = jsdocJsonFileName;
|
this.tsDefFileName = ts.normalizePath(tsDefFileName);
|
||||||
this.tsDefFileName = tsDefFileName;
|
this.tsDefFileContent = fs.readFileSync(this.tsDefFileName, 'utf-8').toString();
|
||||||
this.jsonDocsFileContent = fs.readFileSync(this.jsonDocsFileName, 'utf-8');
|
this.delintNodeFunction = this.delintNode.bind(this);
|
||||||
this.docs = JSON.parse(this.jsonDocsFileContent);
|
var jsonDocsFileContent = fs.readFileSync(jsdocJsonFileName, 'utf-8').toString();
|
||||||
this.tsDefFileContent = fs.readFileSync(this.tsDefFileName, 'utf-8');
|
this.docs = JSON.parse(jsonDocsFileContent);
|
||||||
this.tree = ts.Parser.parse('', ts.SimpleText.fromString(this.tsDefFileContent), true, new ts.ParseOptions(ts.LanguageVersion.EcmaScript5, true));
|
var options = { target: ts.ScriptTarget.ES5, module: ts.ModuleKind.AMD };
|
||||||
this.sourceUnit = this.tree.sourceUnit();
|
var host = ts.createCompilerHost(options);
|
||||||
this.lineMap = this.tree.lineMap();
|
var program = ts.createProgram([this.tsDefFileName], options, host);
|
||||||
|
this.sourceFile = program.getSourceFile(this.tsDefFileName);
|
||||||
}
|
}
|
||||||
TypeScriptDocGenerator.prototype.cleanEndLine = function (str) {
|
TypeScriptDocGenerator.prototype.getTsDefCommentedFileContent = function () {
|
||||||
return str.replace(new RegExp('[' + "\r\n" + ']', 'g'), "\n").replace(new RegExp('[' + "\r" + ']', 'g'), "\n");
|
this.scan();
|
||||||
};
|
return this.tsDefFileContent;
|
||||||
TypeScriptDocGenerator.prototype.completePrefix = function (oldPrefix, appendedPrefix) {
|
|
||||||
if (oldPrefix === "") {
|
|
||||||
return appendedPrefix;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return oldPrefix + "." + appendedPrefix;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
TypeScriptDocGenerator.prototype.repeatSpaces = function (nb) {
|
TypeScriptDocGenerator.prototype.repeatSpaces = function (nb) {
|
||||||
var res = "";
|
var res = "";
|
||||||
|
@ -36,20 +30,6 @@ var TypeScriptDocGenerator = (function () {
|
||||||
}
|
}
|
||||||
return 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.extractPropertyName = function (pn) {
|
|
||||||
return pn.withLeadingTrivia().text().trim();
|
|
||||||
};
|
|
||||||
TypeScriptDocGenerator.prototype.insertComment = function (commentLines, position) {
|
TypeScriptDocGenerator.prototype.insertComment = function (commentLines, position) {
|
||||||
if ((commentLines != null) && (commentLines.length > 0)) {
|
if ((commentLines != null) && (commentLines.length > 0)) {
|
||||||
var nbChars = 0;
|
var nbChars = 0;
|
||||||
|
@ -57,9 +37,9 @@ var TypeScriptDocGenerator = (function () {
|
||||||
nbChars += commentLines[i].trim().length;
|
nbChars += commentLines[i].trim().length;
|
||||||
}
|
}
|
||||||
if (nbChars > 0) {
|
if (nbChars > 0) {
|
||||||
var lc = this.lineMap.getLineAndCharacterFromPosition(position);
|
var lc = this.sourceFile.getLineAndCharacterFromPosition(position);
|
||||||
var nbSpaces = lc.character();
|
var nbSpaces = lc.character - 1;
|
||||||
var startLinePosition = this.lineMap.getLineStartPosition(lc.line());
|
var startLinePosition = this.sourceFile.getLineStarts()[lc.line - 1];
|
||||||
var comment = "\r\n" + this.repeatSpaces(nbSpaces) + "/**\r\n";
|
var comment = "\r\n" + this.repeatSpaces(nbSpaces) + "/**\r\n";
|
||||||
for (var j = 0; j < commentLines.length; j++) {
|
for (var j = 0; j < commentLines.length; j++) {
|
||||||
comment += this.repeatSpaces(nbSpaces) + "* " + commentLines[j].trimRight() + "\r\n";
|
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) {
|
TypeScriptDocGenerator.prototype.findClass = function (className) {
|
||||||
if (className.indexOf("p2.") === 0) {
|
if (className.indexOf("p2.") === 0) {
|
||||||
className = className.replace("p2.", "Phaser.Physics.P2.");
|
className = className.replace("p2.", "Phaser.Physics.P2.");
|
||||||
|
@ -79,6 +62,17 @@ var TypeScriptDocGenerator = (function () {
|
||||||
});
|
});
|
||||||
return elements[0];
|
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) {
|
TypeScriptDocGenerator.prototype.generateMemberComments = function (className, memberName) {
|
||||||
var c = this.findClass(className);
|
var c = this.findClass(className);
|
||||||
if (c != null) {
|
if (c != null) {
|
||||||
|
@ -98,12 +92,50 @@ var TypeScriptDocGenerator = (function () {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
TypeScriptDocGenerator.prototype.generateClassComments = function (className) {
|
TypeScriptDocGenerator.prototype.generateFunctionComments = function (className, functionName) {
|
||||||
var c = this.findClass(className);
|
var c = this.findClass(className);
|
||||||
if (c != null) {
|
if (c != null) {
|
||||||
var comments = [];
|
for (var i = 0; i < c.functions.length; i++) {
|
||||||
comments = comments.concat(this.cleanEndLine(c.description).split("\n"));
|
if (c.functions[i].name === functionName) {
|
||||||
return comments;
|
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 {
|
else {
|
||||||
return null;
|
return null;
|
||||||
|
@ -143,124 +175,45 @@ var TypeScriptDocGenerator = (function () {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
TypeScriptDocGenerator.prototype.generateFunctionComments = function (className, functionName) {
|
TypeScriptDocGenerator.prototype.scan = function () {
|
||||||
var c = this.findClass(className);
|
this.delintNode(this.sourceFile);
|
||||||
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.scanClass = function (c, fullName, classPos) {
|
TypeScriptDocGenerator.prototype.getClassName = function (node) {
|
||||||
for (var i = 0; i < c.childCount() ; i++) {
|
var fullName = '';
|
||||||
var elem = c.childAt(i);
|
if (node.kind === ts.SyntaxKind.ClassDeclaration) {
|
||||||
if (elem != null) {
|
fullName = node.name.getText();
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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) {
|
TypeScriptDocGenerator.prototype.delintNode = function (node) {
|
||||||
if (elem != null) {
|
switch (node.kind) {
|
||||||
switch (elem.kind()) {
|
case ts.SyntaxKind.Constructor:
|
||||||
case ts.SyntaxKind.List:
|
this.insertComment(this.generateConstructorComments(this.getClassName(node)), node.getStart());
|
||||||
for (var k = 0; k < elem.childCount() ; k++) {
|
break;
|
||||||
this.scan(elem.childAt(k), prefix);
|
case ts.SyntaxKind.ClassDeclaration:
|
||||||
}
|
this.insertComment(this.generateClassComments(this.getClassName(node)), node.getStart());
|
||||||
break;
|
break;
|
||||||
case ts.SyntaxKind.InterfaceDeclaration:
|
case ts.SyntaxKind.Property:
|
||||||
break;
|
this.insertComment(this.generateMemberComments(this.getClassName(node), node.name.getText()), node.getStart());
|
||||||
case ts.SyntaxKind.ClassDeclaration:
|
break;
|
||||||
var fullClassName = this.completePrefix(prefix, elem.identifier.fullText().trim());
|
case ts.SyntaxKind.Method:
|
||||||
this.insertComment(this.generateClassComments(fullClassName), this.pos + this.leadingWidth(elem));
|
this.insertComment(this.generateFunctionComments(this.getClassName(node), node.name.getText()), node.getStart());
|
||||||
this.scanClass(elem, fullClassName, this.pos);
|
break;
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
ts.forEachChild(node, this.delintNodeFunction);
|
||||||
TypeScriptDocGenerator.prototype.getTsDefCommentedFileContent = function () {
|
|
||||||
for (var i = 0; i < this.sourceUnit.childCount() ; i++) {
|
|
||||||
this.scan(this.sourceUnit.childAt(i), "");
|
|
||||||
}
|
|
||||||
return this.tsDefFileContent;
|
|
||||||
};
|
};
|
||||||
return TypeScriptDocGenerator;
|
return TypeScriptDocGenerator;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
module.exports = function (grunt) {
|
module.exports = function (grunt) {
|
||||||
grunt.registerMultiTask('buildtsdoc', 'Generate a TypeScript def with comments', function () {
|
grunt.registerMultiTask('buildtsdoc', 'Generate a TypeScript def with comments', function () {
|
||||||
var tsdg = new TypeScriptDocGenerator(this.data.tsDefFileName, this.data.jsdocJsonFileName);
|
var tsdg = new TypeScriptDocGenerator(this.data.tsDefFileName, this.data.jsdocJsonFileName);
|
||||||
fs.writeFileSync(this.data.dest, tsdg.getTsDefCommentedFileContent(), 'utf8');
|
fs.writeFileSync(this.data.dest, tsdg.getTsDefCommentedFileContent(), 'utf8');
|
||||||
});
|
});
|
||||||
};
|
};
|
967
typescript/phaser.comments.d.ts
vendored
967
typescript/phaser.comments.d.ts
vendored
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue