Skip to content

Commit 6ae7e6e

Browse files
committed
Merge pull request phaserjs#1287 from pnstickne/wip-props-to-members
Docs: PIXI Integration and "property to member" decorator for JSDoc
2 parents f4237b3 + 66ab47f commit 6ae7e6e

8 files changed

Lines changed: 612 additions & 4 deletions

File tree

Gruntfile.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ module.exports = function (grunt) {
1919

2020
grunt.registerTask('dist', ['replace', 'build', 'copy']);
2121

22+
grunt.registerTask('docs', ['pixidoc', 'builddoc']);
23+
2224
};

docs/build/conf.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
},
55
"source": {
66
"include": [
7+
"../pixi-jsdoc.js",
78
"../../src/Phaser.js",
89
"../../src/animation/",
910
"../../src/core/",
@@ -28,7 +29,11 @@
2829
"includePattern": ".+\\.js(doc)?$",
2930
"excludePattern": "(^|\\/|\\\\)_"
3031
},
31-
"plugins" : ["plugins/markdown"],
32+
"plugins" : [
33+
"local-plugins/proptomember",
34+
"local-plugins/sourceproxy",
35+
"plugins/markdown"
36+
],
3237
"templates": {
3338
"cleverLinks" : false,
3439
"monospaceLinks" : false,
@@ -56,4 +61,4 @@
5661
"private": false,
5762
"lenient": true
5863
}
59-
}
64+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Transform @property tags to @member tags if it looks like @property was incorrectly used.
3+
* - That is, there is only one property and it has the same name as the member.
4+
* The result is less-redundancy and better type exposure in the JSDoc output.
5+
*
6+
* If the member type is not assigned then the property type is used.
7+
*
8+
* A meld of the description are used; appending the property description if appropriate.
9+
*
10+
* This approach works for most cases in Phaser because JSDoc automatically determines the name if not specified in @name, @method, @member or @field.
11+
*/
12+
13+
var path = require('path');
14+
15+
function looksLikeItMightContain (haystack, needle) {
16+
17+
haystack = haystack || '';
18+
needle = needle || '';
19+
20+
haystack = haystack.replace(/[^a-z]/gi, '').toLowerCase();
21+
needle = needle.replace(/[^a-z]/gi, '').toLowerCase();
22+
23+
return haystack.indexOf(needle) > -1;
24+
25+
}
26+
27+
exports.handlers = {};
28+
exports.handlers.newDoclet = function (e) {
29+
30+
var doclet = e.doclet;
31+
var props = e.doclet.properties;
32+
33+
if (doclet.kind === 'member' &&
34+
props && props.length === 1 &&
35+
props[0].name === doclet.name)
36+
{
37+
// "Duplicate"
38+
var prop = props[0];
39+
40+
if (!doclet.type)
41+
{
42+
doclet.type = prop.type;
43+
}
44+
45+
if (!doclet.description)
46+
{
47+
doclet.description = prop.description;
48+
}
49+
else if (prop.description &&
50+
!looksLikeItMightContain(doclet.description, prop.description))
51+
{
52+
// Tack it on..
53+
doclet.description += " " + prop.description;
54+
}
55+
56+
// And no more prop
57+
e.doclet.properties = undefined;
58+
}
59+
60+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* For use with custom `@sourcepath`, `@sourceline`, `@nosource` properties
3+
* (which are used in YUIDoc-to-JSDoc to supply source documentation)
4+
*/
5+
6+
var path = require('path');
7+
8+
exports.defineTags = function(dictionary) {
9+
10+
dictionary.defineTag('nosource', {
11+
onTagged: function (doclet, tag) {
12+
doclet.meta.nosource = true;
13+
//doclet.meta.path = '';
14+
//doclet.meta.filename = '';
15+
}
16+
});
17+
18+
dictionary.defineTag('sourcefile', {
19+
onTagged: function (doclet, tag) {
20+
var filename = tag.value;
21+
doclet.meta.path = path.dirname(filename);
22+
doclet.meta.filename = path.basename(filename);
23+
}
24+
});
25+
26+
dictionary.defineTag('sourceline', {
27+
onTagged: function (doclet, tag) {
28+
var lineno = tag.value;
29+
doclet.meta.lineno = lineno;
30+
}
31+
});
32+
33+
}
34+
35+
exports.handlers = {};
36+
exports.handlers.newDoclet = function (e) {
37+
38+
};

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"grunt-contrib-uglify": "^0.4.0",
4141
"grunt-notify": "^0.3.0",
4242
"grunt-text-replace": "^0.3.11",
43-
"load-grunt-config": "~0.7.2"
43+
"load-grunt-config": "~0.7.2",
44+
"yuidocjs": "^0.3.50"
4445
}
45-
}
46+
}

tasks/builddoc.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* A quick stub-task for running generating the JSDocs.
3+
* This should probably be migrated to use grunt-jsdoc@beta (for jsdoc 3.x) or similar.
4+
*/
5+
'use strict';
6+
7+
module.exports = function (grunt) {
8+
9+
grunt.registerTask('builddoc', 'Builds the project documentation', function () {
10+
11+
var done = this.async();
12+
13+
grunt.util.spawn({
14+
cmd: 'jsdoc',
15+
args: ['-c', 'conf.json', '../../README.md'],
16+
opts: {
17+
cwd: 'docs/build'
18+
}
19+
}, function (error, result, code) {
20+
if (error) {
21+
grunt.fail.warn("" + result);
22+
done(false);
23+
} else {
24+
done();
25+
}
26+
});
27+
28+
});
29+
30+
};

tasks/pixidoc.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Generates the appropriate JSDoc from some (PIXI) YUIDoc.
3+
* This could be turned into a more general pacakge.
4+
*
5+
* Requires: yuidocjs
6+
*/
7+
'use strict';
8+
9+
function generateYuiDoc (sourcePaths, grunt) {
10+
11+
var Y = require('yuidocjs');
12+
13+
var options = {
14+
parseOnly: true,
15+
quiet: true,
16+
paths: sourcePaths
17+
};
18+
19+
return (new Y.YUIDoc(options)).run();
20+
}
21+
22+
module.exports = function (grunt) {
23+
24+
grunt.registerTask('pixidoc', 'Generates JSDoc from the PIXI YUIdocs', function () {
25+
26+
var sources = ['src/pixi'];
27+
var output = 'docs/pixi-jsdoc.js';
28+
29+
var yui2jsdoc = require('./yuidoc-to-jsdoc/converter');
30+
var fs = require('fs');
31+
var path = require('path');
32+
33+
// Right now yuidocsjs requires an absolute path so it emits an
34+
// absolute path in the jsdoc (or the JSDoc will error on missing files)
35+
sources = sources.map(function (source) {
36+
return path.resolve(source);
37+
});
38+
39+
var data = generateYuiDoc(sources);
40+
41+
if (!data) {
42+
grunt.fail.warn("PIXI YUIDoc not generated - nothing to do");
43+
return;
44+
}
45+
46+
// Fake in namespace (current limitation)
47+
// A preamble/warning wrt the YUIDoc-to-JSDoc with proper link-outs could
48+
// also be added here.
49+
var header =
50+
"/**\n" +
51+
"* @namespace PIXI\n" +
52+
"*/";
53+
54+
var comments = yui2jsdoc.convert(data);
55+
comments.unshift(header);
56+
fs.writeFileSync(output, comments.join("\n"));
57+
58+
});
59+
60+
};

0 commit comments

Comments
 (0)