UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

51 lines (50 loc) 2.12 kB
"use strict"; var __values = (this && this.__values)/* istanbul ignore next */ || function (o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; }; Object.defineProperty(exports, "__esModule", { value: true }); var RenameLocationTextManipulator = /** @class */ (function () { function RenameLocationTextManipulator(renameLocations, newName) { this.renameLocations = renameLocations; this.newName = newName; } RenameLocationTextManipulator.prototype.getNewText = function (inputText) { // todo: go in reverse so that the difference doesn't need to be kept track of var newFileText = inputText; var difference = 0; try { for (var _a = __values(this.renameLocations.map(function (l) { return l.getTextSpan(); })), _b = _a.next(); !_b.done; _b = _a.next()) { var textSpan = _b.value; var start = textSpan.getStart(); var end = start + textSpan.getLength(); start -= difference; end -= difference; newFileText = newFileText.substring(0, start) + this.newName + newFileText.substring(end); difference += textSpan.getLength() - this.newName.length; } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_1) throw e_1.error; } } return newFileText; var e_1, _c; }; RenameLocationTextManipulator.prototype.getTextForError = function (newText) { if (this.renameLocations.length === 0) return newText; return "..." + newText.substring(this.renameLocations[0].getTextSpan().getStart()); }; return RenameLocationTextManipulator; }()); exports.RenameLocationTextManipulator = RenameLocationTextManipulator;