ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
110 lines (109 loc) • 5.02 kB
JavaScript
;
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 typescript_1 = require("./../../../typescript");
var errors = require("./../../../errors");
var manipulation_1 = require("./../../../manipulation");
var PrimaryExpression_1 = require("./../PrimaryExpression");
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.getNodeFromCompilerNode(e); });
};
/**
* Adds an element to the array.
* @param text - Text to add as an element.
* @param options - Options.
*/
ArrayLiteralExpression.prototype.addElement = function (text, options) {
return this.addElements([text], options)[0];
};
/**
* Adds elements to the array.
* @param texts - Texts to add as elements.
* @param options - Options.
*/
ArrayLiteralExpression.prototype.addElements = function (texts, options) {
return this.insertElements(this.compilerNode.elements.length, texts, options);
};
/**
* Insert an element into the array.
* @param index - Index to insert at.
* @param text - Text to insert as an element.
* @param options - Options.
*/
ArrayLiteralExpression.prototype.insertElement = function (index, text, options) {
return this.insertElements(index, [text], options)[0];
};
ArrayLiteralExpression.prototype.insertElements = function (index, textsOrWriterFunction, options) {
if (options === void 0) { options = {}; }
var elements = this.getElements();
index = manipulation_1.verifyAndGetIndex(index, elements.length);
var useNewLines = getUseNewLines(this);
if (textsOrWriterFunction instanceof Function) {
var writer = this.getWriterWithChildIndentation();
textsOrWriterFunction(writer);
return insertTexts(this, [writer.toString()]);
}
else {
var childIndentationText_1 = useNewLines ? this.getChildIndentationText() : "";
return insertTexts(this, textsOrWriterFunction.map(function (t) { return childIndentationText_1 + t; }));
}
function insertTexts(node, newTexts) {
manipulation_1.insertIntoCommaSeparatedNodes({
parent: node.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.SyntaxList),
currentNodes: elements,
insertIndex: index,
newTexts: newTexts,
useNewLines: useNewLines
});
var newElements = node.getElements();
return newElements.slice(index, index + (newElements.length - elements.length));
}
function getUseNewLines(node) {
if (options.useNewLines != null)
return options.useNewLines;
if (elements.length > 1)
return allElementsOnDifferentLines();
return node.getStartLineNumber() !== node.getEndLineNumber();
function allElementsOnDifferentLines() {
var previousLine = elements[0].getStartLineNumber();
for (var i = 1; i < elements.length; i++) {
var currentLine = elements[i].getStartLineNumber();
if (previousLine === currentLine)
return false;
previousLine = currentLine;
}
return true;
}
}
};
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;
}(PrimaryExpression_1.PrimaryExpression));
exports.ArrayLiteralExpression = ArrayLiteralExpression;