ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
99 lines (98 loc) • 3.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var manipulation_1 = require("../../../manipulation");
var textSeek_1 = require("../../../manipulation/textSeek");
var utils_1 = require("../../../utils");
var common_1 = require("../common");
var callBaseGetStructure_1 = require("../callBaseGetStructure");
var callBaseSet_1 = require("../callBaseSet");
exports.JSDocBase = common_1.Node;
/**
* JS doc node.
*/
var JSDoc = /** @class */ (function (_super) {
tslib_1.__extends(JSDoc, _super);
function JSDoc() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Gets the tags of the JSDoc.
*/
JSDoc.prototype.getTags = function () {
var _this = this;
var tags = this.compilerNode.tags;
if (tags == null)
return [];
return tags.map(function (t) { return _this._getNodeFromCompilerNode(t); });
};
/**
* Gets the comment.
*/
JSDoc.prototype.getComment = function () {
return this.compilerNode.comment;
};
/**
* Gets the JSDoc's text without the surrounding comment.
*/
JSDoc.prototype.getInnerText = function () {
var innerTextWithStars = this.getText().replace(/^\/\*\*[^\S\n]*\n?/, "").replace(/(\r?\n)?[^\S\n]*\*\/$/, "");
return innerTextWithStars.split(/\n/).map(function (line) {
var starPos = line.indexOf("*");
if (starPos === -1)
return line;
var substringStart = line[starPos + 1] === " " ? starPos + 2 : starPos + 1;
return line.substring(substringStart);
}).join("\n");
};
/**
* Sets the comment.
* @param textOrWriterFunction - Text or writer function to set.
*/
JSDoc.prototype.setComment = function (textOrWriterFunction) {
var tags = this.getTags();
var startEditPos = this.getStart() + 3;
var endEditPos = tags.length > 0 ? textSeek_1.getPreviousMatchingPos(this._sourceFile.getFullText(), tags[0].getStart(), function (c) { return c === "*"; }) - 1 : this.getEnd() - 2;
var indentationText = this.getIndentationText();
var newLineKind = this._context.manipulationSettings.getNewLineKindAsString();
var text = utils_1.getTextFromStringOrWriter(this._getWriter(), textOrWriterFunction);
var newText = newLineKind + text.split(/\r?\n/).map(function (l) { return indentationText + " * " + l; }).join(newLineKind) + newLineKind + indentationText + " ";
manipulation_1.replaceTextPossiblyCreatingChildNodes({
parent: this,
newText: newText,
replacePos: startEditPos,
replacingLength: endEditPos - startEditPos
});
return this;
};
/**
* Removes this JSDoc.
*/
JSDoc.prototype.remove = function () {
manipulation_1.removeChildren({
children: [this],
removeFollowingSpaces: true,
removeFollowingNewLines: true
});
};
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
JSDoc.prototype.set = function (structure) {
callBaseSet_1.callBaseSet(exports.JSDocBase.prototype, this, structure);
if (structure.description != null)
this.setComment(structure.description);
return this;
};
/**
* Gets the structure equivalent to this node.
*/
JSDoc.prototype.getStructure = function () {
return callBaseGetStructure_1.callBaseGetStructure(exports.JSDocBase.prototype, this, {
description: this.getInnerText() // good enough for now
});
};
return JSDoc;
}(exports.JSDocBase));
exports.JSDoc = JSDoc;