yadop
Version:
Yet another Doc Parser
214 lines • 8.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var doctrine_1 = require("doctrine");
var tags_1 = require("../tags");
var Syntax = doctrine_1.type.Syntax;
/** Ngdoc comments mapper. */
var NgdocMapper = /** @class */ (function () {
function NgdocMapper() {
var _this = this;
/**
* Gets all the entities for the given module.
* @param {doctrine.Annotation[]} comments The comments.
* @param {Module} module The module.
* @returns {Entity[]} entities The entities.
*/
this.getEntities = function (comments, module) {
return comments
.filter(function (comment) { return comment.tags
.filter(tags_1.tags.annotations.module)
.filter(function (tag) { return tag.name === module.name; }) // match module name
.length > 0; })
.map(_this._toEntity);
};
/**
* Gets all the methods for the given entity.
* @param {doctrine.Annotation[]} comments The comments.
* @param {Entity} entity The entity.
* @returns {Method[]} methods The methods.
*/
this.getMethods = function (comments, entity) {
return comments
.filter(function (comment) { return comment.tags
.filter(tags_1.tags.annotations.methodOfTag)
.filter(function (tag) { return tag.description === entity.name; })
.length > 0; })
.filter(function (comment) { return comment.tags
.filter(tags_1.tags.values.method)
.length > 0; })
.map(_this._toMethod);
};
/**
* Gets all the modules.
* @param {doctrine.Annotation[]} comments The comments.
* @returns {Module[]} modules The modules.
*/
this.getModules = function (comments) {
return comments
.filter(function (comment) { return comment.tags
.filter(tags_1.tags.values.module).length > 0; })
.map(_this._toModule);
};
/**
* Gets the return if present.
* @param {doctrine.Annotation} comment The entity.
* @return {AttributeType} returnType The returnType.
* @private
*/
this._getReturn = function (comment) {
var returnType;
var tag = comment.tags.find(tags_1.tags.annotations.returns);
if (tag) {
returnType = {
name: tag.description
};
if (tag.type) {
returnType.type = tag.type.name;
}
}
return returnType;
};
/**
* Gets the deprecated tag if present.
* @param {doctrine.Annotation} comment The entity.
* @return {doctrine.Tag} tag The tag.
* @private
*/
this._getDeprecated = function (comment) {
return comment.tags.find(tags_1.tags.annotations.deprecated);
};
this._getDescription = function (comment) {
return comment.tags.find(tags_1.tags.annotations.description);
};
/**
* Gets all the requires for the given entity.
* @param {doctrine.Annotation} comment The comment.
* @returns {string[]} requires The requires.
*/
this._getRequires = function (comment) {
return comment.tags.filter(tags_1.tags.annotations.requires).map(function (tag) { return tag.name; });
};
/**
* Gets all the attributes for the given entity.
* @param {doctrine.Annotation} comment The comment.
* @returns {AttributeType[]} attributes The attributes.
*/
this._getAttributes = function (comment) {
return comment.tags
.filter(tags_1.tags.annotations.param)
.map(_this._toAttributeType);
};
this._getBindings = function (comment) {
return comment.tags
.filter(tags_1.tags.annotations.param)
.map(_this._toAttributeType)
.map(function (attributeType) {
var binding = /([@&<=]+)/.exec(attributeType.description);
if (binding) {
attributeType.description = attributeType.description
.replace(binding[0], '')
.replace(/^[\n\r]+/, '');
attributeType.binding = binding[0];
}
return attributeType;
});
};
this._toAttributeType = function (tag) {
var attributeType = {
name: tag.name,
optional: false
};
if (tag.description) {
attributeType.description = tag.description.replace(/^[\n\r]+/, '');
}
attributeType.optional = !!tag.type && tag.type.type === Syntax.OptionalType;
if (tag.type && tag.type.type === Syntax.OptionalType) {
if (tag.type.expression.type === Syntax.NameExpression) {
attributeType.type = tag.type.expression.name;
}
attributeType.defaultValue = tag['default'];
}
return attributeType;
};
/**
* Converts the given comment to an Entity.
* @param {doctrine.Annotation} comment The entity.
* @return {Entity} entity The entity.
* @private
*/
this._toEntity = function (comment) {
var entity = _this._createYadop(comment);
var type = comment.tags.find(tags_1.tags.annotations.ngdoc);
if (type) {
entity.type = type.description;
if (['directive', 'component'].indexOf(entity.type)) {
entity.attributes = _this._getBindings(comment);
}
else {
entity.attributes = _this._getAttributes(comment);
}
}
entity.requires = _this._getRequires(comment);
return entity;
};
/**
* Converts the given comment to a method.
* @param {doctrine.Annotation} comment The entity.
* @return {Method} method The method.
* @private
*/
this._toMethod = function (comment) {
var method = _this._createYadop(comment);
var returnType = _this._getReturn(comment);
if (returnType) {
method.returns = returnType;
}
var params = _this._getAttributes(comment);
if (params.length > 0) {
method.params = params;
}
return method;
};
/**
* Converts the given comment to a Module.
* @param {doctrine.Annotation} comment The comment.
* @return {Module} module The module.
* @private
*/
this._toModule = function (comment) {
return _this._createYadop(comment);
};
this._createYadop = function (comment) {
var yadop = {
name: comment.tags.find(function (tag) { return tag.title === 'name'; }).name
};
var description = _this._getDescription(comment);
if (description) {
yadop.description = description.description;
}
var deprecated = _this._getDeprecated(comment);
if (deprecated) {
yadop.deprecated = deprecated.description;
}
return yadop;
};
}
/**
* Maps the comments to a workable output.
* @param comments The comments.
*/
NgdocMapper.prototype.map = function (comments) {
var _this = this;
var modules = this.getModules(comments);
modules.forEach(function (module) {
module.entities = _this.getEntities(comments, module);
module.entities.forEach(function (entity) {
entity.methods = _this.getMethods(comments, entity);
});
});
return modules;
};
return NgdocMapper;
}());
exports.default = NgdocMapper;
//# sourceMappingURL=mapper.js.map