UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

67 lines (66 loc) 2.75 kB
"use strict"; 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 __()); }; })(); 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 InsertionTextManipulator_1 = require("./InsertionTextManipulator"); var UnwrapTextManipulator = /** @class */ (function (_super) { __extends(UnwrapTextManipulator, _super); function UnwrapTextManipulator(node) { return _super.call(this, { insertPos: node.getPos(), newText: getReplacementText(node), replacingLength: node.getFullWidth() }) || this; } return UnwrapTextManipulator; }(InsertionTextManipulator_1.InsertionTextManipulator)); exports.UnwrapTextManipulator = UnwrapTextManipulator; function getReplacementText(node) { var childSyntaxList = node.getChildSyntaxListOrThrow(); var indentationText = node.getIndentationText(); var childIndentationText = node.getChildIndentationText(); var indentationDifference = childIndentationText.replace(indentationText, ""); var replaceRegex = new RegExp("^" + indentationDifference); var originalText = childSyntaxList.getFullText(); var sourceFile = node.sourceFile; var lines = originalText.split("\n"); var pos = childSyntaxList.getPos(); var newLines = []; try { for (var lines_1 = __values(lines), lines_1_1 = lines_1.next(); !lines_1_1.done; lines_1_1 = lines_1.next()) { var line = lines_1_1.value; if (sourceFile.isInStringAtPos(pos)) newLines.push(line); else newLines.push(line.replace(replaceRegex, "")); pos += line.length + 1; } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (lines_1_1 && !lines_1_1.done && (_a = lines_1.return)) _a.call(lines_1); } finally { if (e_1) throw e_1.error; } } return newLines.join("\n").replace(/^\r?\n/, ""); var e_1, _a; }