docblock-parser
Version:
A standalone docblock parser
84 lines (69 loc) • 2.43 kB
JavaScript
"use strict"
/**
* This class is used to process a docblock line by line.
*/
;
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Docblock = (function () {
function Docblock(lines, startPattern, endPattern, linePattern) {
_classCallCheck(this, Docblock);
this._startPattern = startPattern || /^\s*\/\*\*\s?/;
this._endPattern = endPattern || /\*\/\s*$/;
this._linePattern = linePattern || /^\s*\* ?/;
this._current = 0;
this._lines = lines;
this._lines[0] = lines[0].replace(this._startPattern, '');
this._lines[lines.length - 1] = lines[lines.length - 1].replace(this._endPattern, '');
}
/**
* Returns the current line.
*
* @return {string}
*/
_createClass(Docblock, [{
key: 'peek',
value: function peek() {
if (this._current < this._lines.length) {
return this._lines[this._current].replace(this._linePattern, '');
}
return null;
}
/**
* Returns the current line and advances to the next line.
*
* @return {string}
*/
}, {
key: 'pop',
value: function pop() {
var line = this.peek();
if (line != null) {
this._current += 1;
return line;
}
return null;
}
/**
* Allows to replace the current line with this value. This shouldn't be
* called by consumers.
*/
}, {
key: 'replace',
value: function replace(line) {
this._lines[this._current] = line;
}
/**
* Returns true if the all of docblock was processed.
*
* @return {boolean}
*/
}, {
key: 'isExhausted',
value: function isExhausted() {
return this._current >= this._lines.length;
}
}]);
return Docblock;
})();
module.exports = Docblock;