UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

60 lines (59 loc) 2.31 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 tree_1 = require("./../tree"); var getNewReplacementSourceFile_1 = require("./../getNewReplacementSourceFile"); function unwrapNode(node) { var tempSourceFile = getNewReplacementSourceFile_1.getNewReplacementSourceFile({ sourceFile: node.getSourceFile(), insertPos: node.getPos(), newText: getReplacementText(node), replacingLength: node.getFullWidth() }); tree_1.replaceTreeUnwrappingNode({ parent: node.getParentSyntaxList() || node.getParentOrThrow(), childIndex: node.getChildIndex(), replacementSourceFile: tempSourceFile }); } exports.unwrapNode = unwrapNode; 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; }