UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

42 lines (40 loc) 1.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("./../../utils"); const tree_1 = require("./../tree"); const getNewReplacementSourceFile_1 = require("./../insertion/getNewReplacementSourceFile"); function unwrapNode(node) { const 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) { const childSyntaxList = node.getChildSyntaxListOrThrow(); const indentationText = node.getIndentationText(); const childIndentationText = node.getChildIndentationText(); const indentationDifference = childIndentationText.replace(indentationText, ""); const replaceRegex = new RegExp("^" + indentationDifference); const stringNodeRanges = childSyntaxList.getDescendants().filter(n => utils_1.isStringNode(n)).map(n => [n.getStart(), n.getEnd()]); const originalText = childSyntaxList.getFullText(); const lines = originalText.split("\n"); let pos = childSyntaxList.getPos(); const newLines = []; for (const line of lines) { if (stringNodeRanges.some(n => n[0] < pos && n[1] > pos)) newLines.push(line); else newLines.push(line.replace(replaceRegex, "")); pos += line.length + 1; } return newLines.join("\n").replace(/^\r?\n/, ""); } //# sourceMappingURL=unwrapNode.js.map