ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
65 lines (63 loc) • 2.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const errors = require("./../../errors");
const manipulation_1 = require("./../../manipulation");
const Expression_1 = require("./Expression");
class ArrayLiteralExpression extends Expression_1.Expression {
/**
* Gets the array's elements.
*/
getElements() {
return this.compilerNode.elements.map(e => this.global.compilerFactory.getNodeFromCompilerNode(e, this.sourceFile));
}
/**
* Adds an element to the array.
* @param text - Text to add as an element.
*/
addElement(text) {
return this.addElements([text])[0];
}
/**
* Adds elements to the array.
* @param texts - Texts to add as elements.
*/
addElements(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.
*/
insertElement(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.
*/
insertElements(index, texts) {
const elements = this.getElements();
index = manipulation_1.verifyAndGetIndex(index, elements.length);
manipulation_1.insertIntoCommaSeparatedNodes({
parent: this,
currentNodes: elements,
insertIndex: index,
newTexts: texts
});
return this.getElements().slice(index, index + texts.length);
}
removeElement(elementOrIndex) {
const elements = this.getElements();
if (elements.length === 0)
throw new errors.InvalidOperationError("Cannot remove an element when none exist.");
const elementToRemove = typeof elementOrIndex === "number" ? getElementFromIndex(elementOrIndex) : elementOrIndex;
manipulation_1.removeCommaSeparatedChild(elementToRemove);
function getElementFromIndex(index) {
return elements[manipulation_1.verifyAndGetIndex(index, elements.length - 1)];
}
}
}
exports.ArrayLiteralExpression = ArrayLiteralExpression;
//# sourceMappingURL=ArrayLiteralExpression.js.map