Skip to content

Commit

Permalink
Merge pull request #196 from docsbydoxdox/feature/updated-jsdoctypepa…
Browse files Browse the repository at this point in the history
…rser
  • Loading branch information
Twipped authored Sep 7, 2022
2 parents 1324a03 + 08a8cc7 commit 807ed32
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 222 deletions.
62 changes: 46 additions & 16 deletions lib/dox.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,32 +406,62 @@ exports.parseTagTypes = function(str, tag) {
}
return [];
}
var Parser = require('jsdoctypeparser').Parser;
var Builder = require('jsdoctypeparser').Builder;
var result = new Parser().parse(str.substr(1, str.length - 2));
var {parse, publish, createDefaultPublisher, NodeType, SyntaxType} = require('jsdoctypeparser');
var result = parse(str.substring(1, str.length - 1));

var customPublisher = Object.assign({}, createDefaultPublisher(), {
NAME(nameNode) {
var output = '<code>' + nameNode.name + '</code>';

if (result.type === NodeType.OPTIONAL) {
output += '|<code>undefined</code>';
} else if (result.type === NodeType.NULLABLE) {
output += '|<code>null</code>';
}

return output;
}
});

var types = (function transform(type) {
if(type instanceof Builder.TypeUnion) {
return type.types.map(transform);
} else if(type instanceof Builder.TypeName) {
return type.name;
} else if(type instanceof Builder.RecordType) {
return type.entries.reduce(function(obj, entry) {
obj[entry.name] = transform(entry.typeUnion);
if (type && type.type === NodeType.UNION) {
return [transform(type.left), transform(type.right)].flat();
} else if (type && type.type === NodeType.NAME) {
return [type.name];
} else if (type && type.type === NodeType.RECORD) {
return [type.entries.reduce(function (obj, entry) {
obj[entry.key] = transform(entry.value);
return obj;
}, {});
}, {})];
} else if (type && type.type === NodeType.GENERIC) {
if (type.meta.syntax === SyntaxType.GenericTypeSyntax.ANGLE_BRACKET) {
return [type.subject.name + '<' + transform(type.objects[0]).join('|') + '>'];
} else if (type.meta.syntax === SyntaxType.GenericTypeSyntax.ANGLE_BRACKET_WITH_DOT) {
return [type.subject.name + '.<' + transform(type.objects[0]).join('|') + '>'];
} else if (type.meta.syntax === SyntaxType.GenericTypeSyntax.SQUARE_BRACKET) {
return [type.subject.name + '[' + transform(type.objects[0]).join('|') + ']'];
} else if (type.meta.syntax === SyntaxType.VariadicTypeSyntax.PREFIX_DOTS) {
return [`...${type.subject.name}`];
} else if (type.meta.syntax === SyntaxType.VariadicTypeSyntax.SUFFIX_DOTS) {
return [`${type.subject.name}...`];
} else if (type.meta.syntax === SyntaxType.VariadicTypeSyntax.ONLY_DOTS) {
return ['...'];
}
return [type.subject.name]
} else if (type && type.value) {
return transform(type.value);
} else {
return type.toString();
}
}(result));

if(tag) {
tag.types = types;
tag.typesDescription = result.toHtml();
tag.optional = (tag.name && tag.name.slice(0,1) === '[') || result.optional;
tag.nullable = result.nullable;
tag.nonNullable = result.nonNullable;
tag.variable = result.variable;
tag.typesDescription = publish(result, customPublisher).replace(/^\?|=$/, '');
tag.optional = (tag.name && tag.name.slice(0,1) === '[') || result.type === NodeType.OPTIONAL;
tag.nullable = result.type === NodeType.NULLABLE;
tag.nonNullable = result.meta ? result.meta.syntax === 'SUFFIX_QUESTION_MARK' || result.meta.syntax === 'PREFIX_BANG': false;
tag.variable = result.type === NodeType.VARIADIC;
}

return types;
Expand Down
Loading

0 comments on commit 807ed32

Please sign in to comment.