UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

80 lines (79 loc) 3.44 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 ts = require("typescript"); var errors = require("./../../errors"); var manipulation_1 = require("./../../manipulation"); var Expression_1 = require("./Expression"); var ArrayLiteralExpression = /** @class */ (function (_super) { __extends(ArrayLiteralExpression, _super); function ArrayLiteralExpression() { return _super !== null && _super.apply(this, arguments) || this; } /** * Gets the array's elements. */ ArrayLiteralExpression.prototype.getElements = function () { var _this = this; return this.compilerNode.elements.map(function (e) { return _this.global.compilerFactory.getNodeFromCompilerNode(e, _this.sourceFile); }); }; /** * Adds an element to the array. * @param text - Text to add as an element. */ ArrayLiteralExpression.prototype.addElement = function (text) { return this.addElements([text])[0]; }; /** * Adds elements to the array. * @param texts - Texts to add as elements. */ ArrayLiteralExpression.prototype.addElements = function (texts) { return this.insertElements(this.compilerNode.elements.length, texts); }; /** * Insert an element into the array. * @param index - Index to insert at. * @param text - Text to insert as an element. */ ArrayLiteralExpression.prototype.insertElement = function (index, text) { return this.insertElements(index, [text])[0]; }; /** * Insert elements into the array. * @param index - Index to insert at. * @param texts - Texts to insert as elements. */ ArrayLiteralExpression.prototype.insertElements = function (index, texts) { var elements = this.getElements(); index = manipulation_1.verifyAndGetIndex(index, elements.length); manipulation_1.insertIntoCommaSeparatedNodes({ parent: this.getFirstChildByKindOrThrow(ts.SyntaxKind.SyntaxList), currentNodes: elements, insertIndex: index, newTexts: texts }); return this.getElements().slice(index, index + texts.length); }; ArrayLiteralExpression.prototype.removeElement = function (elementOrIndex) { var elements = this.getElements(); if (elements.length === 0) throw new errors.InvalidOperationError("Cannot remove an element when none exist."); var elementToRemove = typeof elementOrIndex === "number" ? getElementFromIndex(elementOrIndex) : elementOrIndex; manipulation_1.removeCommaSeparatedChild(elementToRemove); function getElementFromIndex(index) { return elements[manipulation_1.verifyAndGetIndex(index, elements.length - 1)]; } }; return ArrayLiteralExpression; }(Expression_1.Expression)); exports.ArrayLiteralExpression = ArrayLiteralExpression;