yadop
Version:
Yet another Doc Parser
77 lines • 2.66 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var glob = require("glob");
var espree = require("espree");
var doctrine = require("doctrine");
var path = require("path");
var fs = require("fs-extra");
/** Docs processor. */
var Processor = /** @class */ (function () {
/**
* Constructor.
* @param configuration The configuraton object.
*/
function Processor(configuration) {
var _this = this;
this.configuration = configuration;
this.DEFAULT_CWD = process.cwd();
this.DEFAULT_PATTERN = '**/*.js';
this.DEFAULT_IGNORE = [];
this.ESPREE_PARSE_OPTIONS = {
comments: true,
attachComment: true,
};
/**
* Reads and parses the provided file using espree.
* @param file The file
* @return {Comment[]} comments The comments from the provided file.
* @private
*/
this._toEspreeComments = function (file) {
var filename = path.join(_this.options.cwd, file);
var code = fs.readFileSync(filename);
return espree.parse(code, _this.ESPREE_PARSE_OPTIONS).comments;
};
/**
* Merge the comments.
* @param previousComments The list of previous comments.
* @param currentComments The list of current comments.
*/
this._concatComments = function (previousComments, currentComments) {
return previousComments.concat(currentComments);
};
/**
* Converts the espree comments to doctrine.Annotations.
* @param comment The comment.
* @private
*/
this._toDoctrineComments = function (comment) {
return doctrine.parse(comment.value, {
unwrap: true,
sloppy: true,
lineNumbers: true
});
};
this.pattern = configuration.pattern || this.DEFAULT_PATTERN;
this.options = {
cwd: configuration.cwd || this.DEFAULT_CWD,
ignore: configuration.ignore || this.DEFAULT_IGNORE,
nodir: true,
nosort: true
};
}
/**
* Process all matching files and return the comments.s
* @return {[Comment,Comment,Comment,Comment,Comment]}
*/
Processor.prototype.process = function () {
return glob
.sync(this.pattern, this.options)
.map(this._toEspreeComments)
.reduce(this._concatComments)
.map(this._toDoctrineComments);
};
return Processor;
}());
exports.default = Processor;
//# sourceMappingURL=processor.js.map