UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

59 lines (57 loc) 2.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const errors = require("./../../errors"); const manipulation_1 = require("./../../manipulation"); const utils_1 = require("./../../utils"); function TextInsertableNode(Base) { return class extends Base { insertText(pos, textOrWriterFunction) { this.replaceText([pos, pos], textOrWriterFunction); return this; } removeText(pos, end) { this.replaceText([pos, end], ""); return this; } replaceText(range, textOrWriterFunction) { 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: utils_1.getTextFromStringOrWriter(this.global.manipulationSettings, textOrWriterFunction), 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 (utils_1.TypeGuards.isSourceFile(thisNode)) return thisNode; return childSyntaxList; } } }; } exports.TextInsertableNode = TextInsertableNode; //# sourceMappingURL=TextInsertableNode.js.map