UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

82 lines (81 loc) 3.39 kB
"use strict"; var __extends = (this && this.__extends)/* istanbul ignore next */ || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var manipulation_1 = require("./../../manipulation"); var textSeek_1 = require("./../../manipulation/textSeek"); var utils_1 = require("./../../utils"); var common_1 = require("./../common"); /** * JS doc node. */ var JSDoc = /** @class */ (function (_super) { __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.global.compilerFactory.getNodeFromCompilerNode(t, _this.sourceFile); }); }; /** * 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]*\*\/$/, ""); var leadingTest = /[\s\t\*\r]/; return innerTextWithStars.split(/\n/).map(function (line) { // skip over the " * " part of the text var start = textSeek_1.getNextMatchingPos(line, 0, function (char) { return !leadingTest.test(char); }); return line.substring(start); }).join("\n"); }; 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.global.manipulationSettings.getNewLineKind(); var text = utils_1.getTextFromStringOrWriter(this.global.manipulationSettings, 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 }); }; return JSDoc; }(common_1.Node)); exports.JSDoc = JSDoc;