UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

40 lines (38 loc) 1.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const textSeek_1 = require("./../textSeek"); const tree_1 = require("./../tree"); function removeChildren(opts) { const { children, removePrecedingSpaces = false, removeFollowingSpaces = false } = opts; if (children.length === 0) return; const sourceFile = children[0].getSourceFile(); const fullText = sourceFile.getFullText(); const newText = getPrefix() + getSuffix(); const tempSourceFile = sourceFile.global.compilerFactory.createTempSourceFileFromText(newText, sourceFile.getFilePath()); tree_1.replaceTreeWithChildIndex({ replacementSourceFile: tempSourceFile, parent: children[0].getParentSyntaxList() || children[0].getParentOrThrow(), childIndex: children[0].getChildIndex(), childCount: -1 * children.length }); function getPrefix() { return fullText.substring(0, getRemovalPos()); } function getSuffix() { return fullText.substring(getRemovalEnd()); } function getRemovalPos() { const pos = children[0].getStart(); return removePrecedingSpaces ? textSeek_1.getPreviousMatchingPos(fullText, pos, charNotSpaceOrTab) : pos; } function getRemovalEnd() { const end = children[children.length - 1].getEnd(); return removeFollowingSpaces ? textSeek_1.getNextMatchingPos(fullText, end, charNotSpaceOrTab) : end; } function charNotSpaceOrTab(char) { return char !== " " && char !== "\t"; } } exports.removeChildren = removeChildren; //# sourceMappingURL=removeChildren.js.map