UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

58 lines (56 loc) 2.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const errors = require("./../../errors"); const manipulation_1 = require("./../../manipulation"); function TextInsertableNode(Base) { return class extends Base { insertText(pos, text) { this.replaceText([pos, pos], text); return this; } removeText(pos, end) { this.replaceText([pos, end], ""); return this; } replaceText(range, text) { const thisNode = this; const childSyntaxList = this.getChildSyntaxListOrThrow(); const pos = range[0]; const end = range[1]; verifyArguments(); // ideally this wouldn't replace the existing syntax list manipulation_1.insertIntoParent({ insertPos: pos, childIndex: childSyntaxList.getChildIndex(), insertItemsCount: 1, newText: text, parent: this, replacing: { length: end - pos, nodes: [childSyntaxList] } }); return this; function verifyArguments() { verifyInRange(pos); verifyInRange(end); if (pos > end) throw new errors.ArgumentError("range", "Cannot specify a start position greater than the end position."); } function verifyInRange(i) { const nodeToVerifyRange = getNodeToVerifyRange(); if (i >= nodeToVerifyRange.getPos() && i <= nodeToVerifyRange.getEnd()) return; throw new errors.InvalidOperationError(`Cannot insert or replace text outside the bounds of the node. ` + `Expected a position between [${nodeToVerifyRange.getPos()}, ${nodeToVerifyRange.getEnd()}], but received ${i}.`); } function getNodeToVerifyRange() { if (thisNode.isSourceFile()) return thisNode; return childSyntaxList; } } }; } exports.TextInsertableNode = TextInsertableNode; //# sourceMappingURL=TextInsertableNode.js.map