UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for static analysis and code manipulation.

65 lines (64 loc) 3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var manipulation_1 = require("../../../manipulation"); var utils_1 = require("../../../utils"); var callBaseSet_1 = require("../callBaseSet"); var callBaseGetStructure_1 = require("../callBaseGetStructure"); function JSDocableNode(Base) { return /** @class */ (function (_super) { tslib_1.__extends(class_1, _super); function class_1() { return _super !== null && _super.apply(this, arguments) || this; } class_1.prototype.getJsDocs = function () { var _this = this; var nodes = this.compilerNode.jsDoc; if (nodes == null) return []; return nodes.map(function (n) { return _this._getNodeFromCompilerNode(n); }); }; class_1.prototype.addJsDoc = function (structure) { return this.addJsDocs([structure])[0]; }; class_1.prototype.addJsDocs = function (structures) { return this.insertJsDocs(manipulation_1.getEndIndexFromArray(this.compilerNode.jsDoc), structures); }; class_1.prototype.insertJsDoc = function (index, structure) { return this.insertJsDocs(index, [structure])[0]; }; class_1.prototype.insertJsDocs = function (index, structures) { if (utils_1.ArrayUtils.isNullOrEmpty(structures)) return []; var writer = this._getWriterWithQueuedIndentation(); var structurePrinter = this._context.structurePrinterFactory.forJSDoc(); structurePrinter.printDocs(writer, structures); writer.write(""); // final indentation var code = writer.toString(); var nodes = this.getJsDocs(); index = manipulation_1.verifyAndGetIndex(index, nodes.length); var insertPos = index === nodes.length ? this.getStart() : nodes[index].getStart(); manipulation_1.insertIntoParentTextRange({ insertPos: insertPos, parent: this, newText: code }); return manipulation_1.getNodesToReturn(this.getJsDocs(), index, structures.length); }; class_1.prototype.set = function (structure) { callBaseSet_1.callBaseSet(Base.prototype, this, structure); if (structure.docs != null) { this.getJsDocs().forEach(function (doc) { return doc.remove(); }); this.addJsDocs(structure.docs); } return this; }; class_1.prototype.getStructure = function () { return callBaseGetStructure_1.callBaseGetStructure(Base.prototype, this, { docs: this.getJsDocs().map(function (jsdoc) { return jsdoc.getStructure(); }) }); }; return class_1; }(Base)); } exports.JSDocableNode = JSDocableNode;