UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

30 lines (29 loc) 1.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ts = require("typescript"); var removeChildren_1 = require("./removeChildren"); function removeCommaSeparatedChild(child, opts) { var _a = (opts || {}).removePrecedingSpaces, removePrecedingSpaces = _a === void 0 ? undefined : _a; var childrenToRemove = [child]; var syntaxList = child.getParentSyntaxListOrThrow(); addNextCommaIfAble(); addPreviousCommaIfAble(); removeChildren_1.removeChildren({ children: childrenToRemove, removePrecedingSpaces: removePrecedingSpaces == null ? true : removePrecedingSpaces, removeFollowingSpaces: childrenToRemove[0] === syntaxList.getFirstChild() }); function addNextCommaIfAble() { var commaToken = child.getNextSiblingIfKind(ts.SyntaxKind.CommaToken); if (commaToken != null) childrenToRemove.push(commaToken); } function addPreviousCommaIfAble() { if (syntaxList.getLastChild() !== childrenToRemove[childrenToRemove.length - 1]) return; var precedingComma = child.getPreviousSiblingIfKind(ts.SyntaxKind.CommaToken); if (precedingComma != null) childrenToRemove.unshift(precedingComma); } } exports.removeCommaSeparatedChild = removeCommaSeparatedChild;