Skip to content

Commit

Permalink
CLI: Added support for TypeScript enums to pbts
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 2, 2017
1 parent 0cda72a commit 3c77553
Showing 1 changed file with 45 additions and 25 deletions.
70 changes: 45 additions & 25 deletions lib/tsd-jsdoc/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ exports.publish = function publish(taffy, opts) {
}

// handle all
getChildrenOf(undefined).forEach(handleElement);
getChildrenOf(undefined).forEach(function(child) {
handleElement(child, null);
});

// process queued
while (queuedInterfaces.length) {
Expand Down Expand Up @@ -228,7 +230,7 @@ function getTypeOf(element) {

// begins writing the definition of the specified element
function begin(element, is_interface) {
writeComment(element.comment, is_interface || isInterface(element) || isClassLike(element) || isNamespace(element));
writeComment(element.comment, is_interface || isInterface(element) || isClassLike(element) || isNamespace(element) || element.isEnum);
if (element.scope !== "global" || options.module || is_interface || isInterface(element))
return;
write("export ");
Expand Down Expand Up @@ -308,28 +310,43 @@ function writeInterface(element) {
//

// handles a single element of any understood type
function handleElement(element, parent) {
function handleElement(element, parent, insideClass) {
if (seen[element.longname])
return;
seen[element.longname] = element;
if (isClassLike(element))
return handleClass(element, parent);
switch (element.kind) {
return true;
if (isClassLike(element)) {
if (insideClass)
return false;
handleClass(element, parent);
} else switch (element.kind) {
case "module":
case "namespace":
return handleNamespace(element, parent);
if (insideClass)
return false;
handleNamespace(element, parent);
break;
case "constant":
case "member":
return handleMember(element, parent);
if (insideClass && element.isEnum)
return false;
handleMember(element, parent);
break;
case "function":
return handleFunction(element, parent);
handleFunction(element, parent);
break;
case "typedef":
return handleTypeDef(element, parent);
if (insideClass)
return false;
handleTypeDef(element, parent);
break;
case "package":
break;
}
seen[element.longname] = element;
return true;
}

// handles (just) a namespace
function handleNamespace(element, parent) {
function handleNamespace(element/*, parent*/) {
begin(element);
writeln("namespace ", element.name, " {");
++indent;
Expand Down Expand Up @@ -383,24 +400,25 @@ function handleClass(element, parent) {
if (!is_interface && !element.virtual)
handleFunction(element, parent, true);

// members except inner classes
var innerClasses = [];
// class-compatible members
var inner = [];
getChildrenOf(element).forEach(function(child) {
if (isClassLike(child))
innerClasses.push(child);
else
handleElement(child, element);
if (!handleElement(child, element, true))
inner.push(child);
});

--indent;
writeln("}");

if (innerClasses.length) {
begin(element);
// class-incompatible members
if (inner.length) {
writeln();
if (element.scope === "global" && !options.module)
write("export ");
writeln("namespace ", element.name, " {");
++indent;
innerClasses.forEach(function(inner) {
handleClass(inner, element);
inner.forEach(function(child) {
handleElement(child, element);
});
--indent;
writeln("}");
Expand All @@ -413,11 +431,13 @@ function handleMember(element, parent) {

if (element.isEnum) {

writeln("enum ", element.name, "{");
writeln("enum ", element.name, " {");
++indent;
element.properties.forEach(function(property, i) {
writeComment(property);
write(p.name);
write(property.name);
if (property.defaultvalue !== undefined)
write(" = ", JSON.stringify(property.defaultvalue));
if (i < element.properties.length - 1)
writeln(",");
else
Expand Down

0 comments on commit 3c77553

Please sign in to comment.