Skip to content

Commit 3c77553

Browse files
committed
CLI: Added support for TypeScript enums to pbts
1 parent 0cda72a commit 3c77553

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

lib/tsd-jsdoc/publish.js

+45-25
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ exports.publish = function publish(taffy, opts) {
7777
}
7878

7979
// handle all
80-
getChildrenOf(undefined).forEach(handleElement);
80+
getChildrenOf(undefined).forEach(function(child) {
81+
handleElement(child, null);
82+
});
8183

8284
// process queued
8385
while (queuedInterfaces.length) {
@@ -228,7 +230,7 @@ function getTypeOf(element) {
228230

229231
// begins writing the definition of the specified element
230232
function begin(element, is_interface) {
231-
writeComment(element.comment, is_interface || isInterface(element) || isClassLike(element) || isNamespace(element));
233+
writeComment(element.comment, is_interface || isInterface(element) || isClassLike(element) || isNamespace(element) || element.isEnum);
232234
if (element.scope !== "global" || options.module || is_interface || isInterface(element))
233235
return;
234236
write("export ");
@@ -308,28 +310,43 @@ function writeInterface(element) {
308310
//
309311

310312
// handles a single element of any understood type
311-
function handleElement(element, parent) {
313+
function handleElement(element, parent, insideClass) {
312314
if (seen[element.longname])
313-
return;
314-
seen[element.longname] = element;
315-
if (isClassLike(element))
316-
return handleClass(element, parent);
317-
switch (element.kind) {
315+
return true;
316+
if (isClassLike(element)) {
317+
if (insideClass)
318+
return false;
319+
handleClass(element, parent);
320+
} else switch (element.kind) {
318321
case "module":
319322
case "namespace":
320-
return handleNamespace(element, parent);
323+
if (insideClass)
324+
return false;
325+
handleNamespace(element, parent);
326+
break;
321327
case "constant":
322328
case "member":
323-
return handleMember(element, parent);
329+
if (insideClass && element.isEnum)
330+
return false;
331+
handleMember(element, parent);
332+
break;
324333
case "function":
325-
return handleFunction(element, parent);
334+
handleFunction(element, parent);
335+
break;
326336
case "typedef":
327-
return handleTypeDef(element, parent);
337+
if (insideClass)
338+
return false;
339+
handleTypeDef(element, parent);
340+
break;
341+
case "package":
342+
break;
328343
}
344+
seen[element.longname] = element;
345+
return true;
329346
}
330347

331348
// handles (just) a namespace
332-
function handleNamespace(element, parent) {
349+
function handleNamespace(element/*, parent*/) {
333350
begin(element);
334351
writeln("namespace ", element.name, " {");
335352
++indent;
@@ -383,24 +400,25 @@ function handleClass(element, parent) {
383400
if (!is_interface && !element.virtual)
384401
handleFunction(element, parent, true);
385402

386-
// members except inner classes
387-
var innerClasses = [];
403+
// class-compatible members
404+
var inner = [];
388405
getChildrenOf(element).forEach(function(child) {
389-
if (isClassLike(child))
390-
innerClasses.push(child);
391-
else
392-
handleElement(child, element);
406+
if (!handleElement(child, element, true))
407+
inner.push(child);
393408
});
394409

395410
--indent;
396411
writeln("}");
397412

398-
if (innerClasses.length) {
399-
begin(element);
413+
// class-incompatible members
414+
if (inner.length) {
415+
writeln();
416+
if (element.scope === "global" && !options.module)
417+
write("export ");
400418
writeln("namespace ", element.name, " {");
401419
++indent;
402-
innerClasses.forEach(function(inner) {
403-
handleClass(inner, element);
420+
inner.forEach(function(child) {
421+
handleElement(child, element);
404422
});
405423
--indent;
406424
writeln("}");
@@ -413,11 +431,13 @@ function handleMember(element, parent) {
413431

414432
if (element.isEnum) {
415433

416-
writeln("enum ", element.name, "{");
434+
writeln("enum ", element.name, " {");
417435
++indent;
418436
element.properties.forEach(function(property, i) {
419437
writeComment(property);
420-
write(p.name);
438+
write(property.name);
439+
if (property.defaultvalue !== undefined)
440+
write(" = ", JSON.stringify(property.defaultvalue));
421441
if (i < element.properties.length - 1)
422442
writeln(",");
423443
else

0 commit comments

Comments
 (0)