Skip to content

Commit 12b2d44

Browse files
committed
yuidoc-to-jsdoc - bug fix and jsobj-like mapping
Fixed bug with incorrect generation of "Array<>". Added a primitive type-accepter for jsobject-like (for some very primitive value); but it covers the PIXI case.
1 parent 0d9678e commit 12b2d44

1 file changed

Lines changed: 48 additions & 5 deletions

File tree

tasks/yuidoc-to-jsdoc/converter.js

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ function group_typeitems(typeitems) {
104104

105105
/**
106106
* Convert ident to the "closest" valid non-quoted identifier.
107+
* May return the empty string (which is not a valid identifier).
107108
*/
108109
function as_valid_identifier (ident) {
109110
ident = ident.replace(/\s/g, '_');
@@ -137,26 +138,57 @@ function fixup_yuidoc_array (rawtype) {
137138
r = r.replace(/[\s.]*([<>])[\s.]*/g, '$1');
138139

139140
// match T<..>, where T != 'array'
140-
m = r.match(/^(\S+)(?:<.*>)$/i);
141+
m = r.match(/^([\w$.]+)(?:<.*>)$/i);
141142
if (m && m[1].toLowerCase() !== 'array') {
142-
return 'Array<' + as_valid_identifier(m[1] || 'unknown') + '>';
143+
return 'Array<' + (as_valid_identifier(m[1]) || 'unknown') + '>';
143144
}
144145

145146
// match Array <T>
146147
m = r.match(/^Array<(.*)>$/i);
147148
if (m) {
148-
return 'Array<' + as_valid_identifier(m[1] || 'unknown') + '>';
149+
return 'Array<' + (as_valid_identifier(m[1]) || 'unknown') + '>';
149150
}
150151

151152
// match Array..of T
152153
m = r.match(/^Array.*?of\b\s*(.*)$/i);
153154
if (m) {
154-
return 'Array<' + as_valid_identifier(m[1] || 'unknown') + '>';
155+
return 'Array<' + (as_valid_identifier(m[1]) || 'unknown') + '>';
155156
}
156157

157158
return '';
158159
}
159160

161+
/**
162+
* Try to fixup a type if it looks like it may conform to `{key: value, ..}`.
163+
* Nesting is not supported and quoted keys are not supported.
164+
*
165+
* Returns the fixed up version or ''.
166+
*/
167+
function fixup_jsobject_like (rawType) {
168+
169+
var r = rawType;
170+
171+
// Trim spaces
172+
r = r.replace(/^\s+|\s+$/g, '');
173+
// And duplicate brackets
174+
if (r.match(/^{\s*{.*}\s*}$/)) {
175+
r = r.replace(/^{\s*(.*?)\s*}$/, '$1');
176+
}
177+
178+
if (r.match(/^{([\w$.]+:\s*[\w$.]+,?\s*)+}$/)) {
179+
r = r.replace(/([\w$.]+):\s*([\w$.]+)(,?\s*)/g, function (m, a, b, c) {
180+
if (c) { c = ", " };
181+
return as_valid_identifier(a) + ": " + as_valid_identifier(b) + c;
182+
});
183+
return r;
184+
}
185+
else
186+
{
187+
return '';
188+
}
189+
190+
}
191+
160192
/**
161193
* Process a complex (possibly multiple) type.
162194
* (This has limited ability now: will not recurse, handle special arrays, etc.)
@@ -179,6 +211,7 @@ function resolve_typename(typename, typedescs) {
179211
var loss = false;
180212
var repeating = false;
181213
var array = false;
214+
var objlike = false;
182215

183216
// Don't accept quotes in names from upstream
184217
prev = part;
@@ -190,6 +223,14 @@ function resolve_typename(typename, typedescs) {
190223
part = part.replace(/^\.{3,}|\.{3,}$/g, '');
191224
repeating = prev !== part;
192225

226+
prev = part;
227+
part = fixup_jsobject_like(part);
228+
if (part) {
229+
objlike = true;
230+
} else {
231+
part = prev;
232+
}
233+
193234
prev = part;
194235
part = fixup_yuidoc_array(part);
195236
if (part) {
@@ -198,7 +239,9 @@ function resolve_typename(typename, typedescs) {
198239
part = prev;
199240
}
200241

201-
if (array) {
242+
if (objlike) {
243+
loss = loss || orig.replace(/\W+/g, '') !== part.replace(/\W+/g, '');
244+
} else if (array) {
202245
loss = loss || orig.replace(/^\W/, '') !== part.replace(/^\W/, '');
203246
} else {
204247
prev = part;

0 commit comments

Comments
 (0)