UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

58 lines (57 loc) 2.56 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 utils_1 = require("./../../utils"); var Node_1 = require("./Node"); var SyntaxList = /** @class */ (function (_super) { __extends(SyntaxList, _super); function SyntaxList() { return _super !== null && _super.apply(this, arguments) || this; } SyntaxList.prototype.addChildText = function (textOrWriterFunction) { return this.insertChildText(this.getChildCount(), textOrWriterFunction); }; SyntaxList.prototype.insertChildText = function (index, textOrWriterFunction) { // get index var initialChildCount = this.getChildCount(); var newLineKind = this.global.manipulationSettings.getNewLineKind(); var parent = this.getParentOrThrow(); index = manipulation_1.verifyAndGetIndex(index, initialChildCount); // get text var insertText = manipulation_1.getIndentedText({ textOrWriterFunction: textOrWriterFunction, manipulationSettings: this.global.manipulationSettings, indentationText: parent.getChildIndentationText() }); if (insertText.length === 0) return []; if (index === 0 && utils_1.TypeGuards.isSourceFile(parent)) { if (!utils_1.StringUtils.endsWith(insertText, newLineKind)) insertText += newLineKind; } else insertText = newLineKind + insertText; // insert var insertPos = manipulation_1.getInsertPosFromIndex(index, parent, this.getChildren()); manipulation_1.insertIntoParentTextRange({ insertPos: insertPos, newText: insertText, parent: this }); // get inserted children var finalChildren = this.getChildren(); return finalChildren.slice(index, index + finalChildren.length - initialChildCount); }; return SyntaxList; }(Node_1.Node)); exports.SyntaxList = SyntaxList;