Skip to content

Commit b87eece

Browse files
committed
PIXI-in-Docs: prop support
- Added support for nested/ad-hoc properties - Also cleanup and documentation / limitation notions
1 parent a7044a6 commit b87eece

2 files changed

Lines changed: 117 additions & 28 deletions

File tree

tasks/pixidoc.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ module.exports = function (grunt) {
4444
}
4545

4646
// Fake in namespace (current limitation)
47+
// A preamble/warning wrt the YUIDoc-to-JSDoc with proper link-outs could
48+
// also be added here.
4749
var header =
4850
"/**\n" +
4951
"* @namespace PIXI\n" +
5052
"*/";
5153

52-
var res = yui2jsdoc.convert(data);
53-
var flat = res.join("\n");
54-
fs.writeFileSync(output, header + "\n" + flat);
54+
var comments = yui2jsdoc.convert(data);
55+
comments.unshift(header);
56+
fs.writeFileSync(output, comments.join("\n"));
5557

5658
});
5759

tasks/yuidoc-to-jsdoc/converter.js

Lines changed: 112 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,35 @@
33
*
44
* Use a JSDoc plugin to handle custom @sourcefile/@sourceline and reattach meta-data.
55
*
6-
* This works on the current PIXI source code (and, unfortunately, exposes a few issues it has).
6+
* This works on the current PIXI source code (and exposes a few documentation bugs).
7+
*
8+
* Known limitations:
9+
* - Does not support (from YUIDoc):
10+
* - @namespace/@module (although all types in the output are fully-resolved)
11+
* - @event, @bubbles, @for, @uses, @chainable, @async
12+
* - @author, @deprecated, @version, @since, @beta
13+
* - Most "YUI-Specific" (@readonly is supported)
14+
* - Does not support file-level documentation
15+
* - All class-level documentation is put into @classdesc as there appears to be no separate
16+
* concept in YUIDoc for constructor vs. class documentation.
17+
* - Probably doesn't work with nested modules/namespaces.
18+
* (And many unknown)
19+
*
720
*/
821
'use strict';
922

10-
// Does not work with 'props'
11-
function paramdesc_to_str(desc, typedescs) {
23+
/**
24+
* Convert a parameter into a parameter tag string; also do this for each desc.props, specifying the baseprop.
25+
*/
26+
function paramdesc_to_str(desc, typedescs, basename) {
1227
var name = desc.name;
1328
var typename = desc.type;
1429
var description = desc.description;
1530

31+
if (basename) {
32+
name = basename + "." + name;
33+
}
34+
1635
if (desc.optional) {
1736
if (desc.optdefault) {
1837
name = "[" + name + "=" + desc.optdefault + "]";
@@ -24,6 +43,28 @@ function paramdesc_to_str(desc, typedescs) {
2443
return "{" + resolve_typename(typename, typedescs) + "} " + name + " - " + description;
2544
}
2645

46+
/**
47+
* Convert a parameter to as many @params as required; this is to map YUIDoc "props"
48+
*/
49+
function paramdesc_to_attrs(desc, typedescs, attrs, baseprop) {
50+
51+
attrs = attrs || [];
52+
53+
attrs.push(paramdesc_to_str(desc, typedescs, baseprop ? baseprop.name : ''));
54+
55+
if (desc.props) {
56+
desc.props.forEach(function (prop) {
57+
paramdesc_to_attrs(prop, typedescs, attrs, desc);
58+
});
59+
}
60+
61+
return attrs;
62+
63+
}
64+
65+
/**
66+
* Convert a return into a return tag string.
67+
*/
2768
function returndesc_to_string(desc, typedescs) {
2869
var typename = desc.type;
2970
var description = desc.description;
@@ -36,8 +77,8 @@ function returndesc_to_string(desc, typedescs) {
3677

3778
/**
3879
* Convert flat 'typeitems' found in YUIDoc to a dictionary:
39-
* className: {
40-
* items: [..] - only properties and methods
80+
* typename: {
81+
* items: [..] - properties and methods
4182
* }
4283
*/
4384
function group_typeitems(typeitems) {
@@ -65,7 +106,10 @@ function group_typeitems(typeitems) {
65106

66107
}
67108

68-
// Use this for anything but trivial types; it takes apart complex types
109+
/**
110+
* Process a complex (possibly multiple) type.
111+
* (This has limited ability now: will not recurse, handle special arrays, etc.)
112+
*/
69113
function resolve_typename(typename, typedescs) {
70114

71115
if (!typename) { typename = "Any"; }
@@ -88,7 +132,11 @@ function resolve_typename(typename, typedescs) {
88132

89133
// This may happen for some terribly invalid input; ideally this would not be
90134
// "handled" here, but trying to work with some not-correct input..
135+
var origpart = part;
91136
part = part.replace(/[^a-zA-Z0-9_$<>.]/g, '');
137+
if (origpart !== part) {
138+
console.log("Mutilating questionable type: " + origpart);
139+
}
92140

93141
var resolved = resolve_single_typename(part, typedescs);
94142
if (repeating) {
@@ -105,9 +153,12 @@ function resolve_typename(typename, typedescs) {
105153
}
106154
}
107155

156+
/**
157+
* Process a single type
158+
*/
108159
function resolve_single_typename(typename, typedescs) {
109160

110-
if (!typename || typename.toLowerCase() === "Any" || typename === "*") {
161+
if (!typename || typename.toLowerCase() === "any" || typename === "*") {
111162
return ""; // "Any"
112163
}
113164

@@ -119,23 +170,43 @@ function resolve_single_typename(typename, typedescs) {
119170
}
120171
}
121172

122-
function resolve_item_name(name, typedesc, typedescs) {
173+
function resolve_item_qualifiedname(itemdesc, typedesc, typedescs) {
174+
var name = itemdesc.name;
123175
var typename = resolve_single_typename(typedesc.name, typedescs);
124-
return typename + "#" + name;
176+
if (itemdesc['static']) {
177+
return typename + "." + name;
178+
} else {
179+
return typename + "#" + name;
180+
}
181+
}
182+
183+
function add_itemdesc_common_attrs (itemdesc, typedesc, typedescs, attrs) {
184+
185+
if (typedesc.file) {
186+
attrs.push(['sourcefile', typedesc.file]);
187+
attrs.push(['sourceline', typedesc.line]);
188+
}
189+
125190
}
126191

192+
/**
193+
* Process Method
194+
*/
127195
function methoddesc_to_attrs(itemdesc, typedesc, typedescs)
128196
{
129197
var attrs = [];
130198

131199
if (itemdesc.description) {
132200
attrs.push(['description', itemdesc.description]);
133201
}
134-
attrs.push(['method', resolve_item_name(itemdesc.name, typedesc, typedescs)]);
202+
attrs.push(['method', resolve_item_qualifiedname(itemdesc, typedesc, typedescs)]);
135203
if (itemdesc.params)
136204
{
137205
itemdesc.params.forEach(function (param, i) {
138-
attrs.push(['param', paramdesc_to_str(param, typedescs)]);
206+
var paramattrs = paramdesc_to_attrs(param, typedescs);
207+
paramattrs.forEach(function (paramattr) {
208+
attrs.push(['param', paramattr]);
209+
});
139210
});
140211
}
141212

@@ -144,22 +215,22 @@ function methoddesc_to_attrs(itemdesc, typedesc, typedescs)
144215
attrs.push(['return', returndesc_to_string(itemdesc['return'], typedescs)]);
145216
}
146217

147-
if (typedesc.file) {
148-
attrs.push(['sourcefile', typedesc.file]);
149-
attrs.push(['sourceline', typedesc.line]);
150-
}
218+
add_itemdesc_common_attrs(itemdesc, typedesc, typedescs, attrs);
151219

152220
return attrs;
153221
}
154222

223+
/**
224+
* Process Property - Member in JSDoc
225+
*/
155226
function propertydesc_to_attrs(itemdesc, typedesc, typedescs)
156227
{
157228
var attrs = [];
158229

159230
if (itemdesc.description) {
160231
attrs.push(['description', itemdesc.description]);
161232
}
162-
attrs.push(['member', resolve_item_name(itemdesc.name, typedesc, typedescs)]);
233+
attrs.push(['member', resolve_item_qualifiedname(itemdesc, typedesc, typedescs)]);
163234
attrs.push(['type', "{" + resolve_typename(itemdesc.type, typedescs) + "}"]);
164235

165236
var access = itemdesc['access'];
@@ -175,10 +246,7 @@ function propertydesc_to_attrs(itemdesc, typedesc, typedescs)
175246
attrs.push(['default', itemdesc['default']]);
176247
}
177248

178-
if (typedesc.file) {
179-
attrs.push(['sourcefile', typedesc.file]);
180-
attrs.push(['sourceline', typedesc.line]);
181-
}
249+
add_itemdesc_common_attrs(itemdesc, typedesc, typedescs, attrs);
182250

183251
return attrs;
184252
}
@@ -203,6 +271,17 @@ function write_attr_block (attrs, res) {
203271

204272
}
205273

274+
/**
275+
* Turns an array of "attributes" into a JSDoc comment block.
276+
*/
277+
function flatten_jsdoc_comment (attrs) {
278+
279+
var res = [];
280+
write_attr_block(attrs, res);
281+
return res.join("\n");
282+
283+
}
284+
206285
function itemdesc_to_attrs(itemdesc, typedesc, typedescs) {
207286

208287
if (itemdesc.itemtype === 'method')
@@ -213,6 +292,10 @@ function itemdesc_to_attrs(itemdesc, typedesc, typedescs) {
213292
{
214293
return propertydesc_to_attrs(itemdesc, typedesc, typedescs);
215294
}
295+
else
296+
{
297+
console.warn("Unable to process item: " + itemdesc.name);
298+
}
216299

217300
}
218301

@@ -264,33 +347,37 @@ function typedesc_to_attrs (typedesc, typedescs) {
264347
}
265348

266349
/**
267-
* Convert a "data.json" (as JSON, not text) from YUIDoc to an equivalent JSDoc markup.
350+
* Converts YUIDoc JSON (as found in data.json after generating documentation) into JSDoc comments.
351+
*
352+
* @method
353+
* @param {} data - YUIDoc data.
354+
* @return {string[]} An array of comment blocks.
268355
*/
269356
function yuidocdata_to_jsdoc(data) {
270357

271358
var typedescs = data.classes;
272359
var type_itemdesc_groups = group_typeitems(data.classitems);
273360

274-
var res = [];
361+
var comments = [];
275362

276363
Object.keys(typedescs).forEach(function (name) {
277364
var typedesc = typedescs[name];
278365

279366
var typeattrs = typedesc_to_attrs(typedesc, typedescs);
280-
write_attr_block(typeattrs, res);
367+
comments.push(flatten_jsdoc_comment(typeattrs));
281368

282369
var type_itemdesc = type_itemdesc_groups[name];
283370
if (type_itemdesc) {
284371
type_itemdesc.items.forEach(function (itemdesc, i) {
285372
var attrs = itemdesc_to_attrs(itemdesc, typedesc, typedescs);
286-
write_attr_block(attrs, res);
373+
comments.push(flatten_jsdoc_comment(attrs));
287374
});
288375
} else {
289376
console.log("No items for " + name);
290377
}
291378
});
292379

293-
return res;
380+
return comments;
294381

295382
}
296383

0 commit comments

Comments
 (0)