UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

148 lines (147 loc) 6.31 kB
"use strict"; var __read = (this && this.__read)/* istanbul ignore next */ || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread)/* istanbul ignore next */ || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); var ts = require("typescript"); var formatting_1 = require("./../formatting"); var doManipulation_1 = require("./doManipulation"); var textManipulators_1 = require("./../textManipulators"); var nodeHandlers_1 = require("./../nodeHandlers"); function removeChildren(opts) { var children = opts.children; if (children.length === 0) return; doManipulation_1.doManipulation(children[0].getSourceFile(), new textManipulators_1.RemoveChildrenTextManipulator(opts), new nodeHandlers_1.NodeHandlerFactory().getForChildIndex({ parent: children[0].getParentSyntaxList() || children[0].getParentOrThrow(), childIndex: children[0].getChildIndex(), childCount: -1 * children.length })); } exports.removeChildren = removeChildren; function removeChildrenWithFormattingFromCollapsibleSyntaxList(opts) { var children = opts.children; if (children.length === 0) return; var syntaxList = children[0].getParentSyntaxListOrThrow(); if (syntaxList.getChildCount() === children.length) { removeChildrenWithFormatting({ children: [syntaxList], getSiblingFormatting: function () { return formatting_1.FormattingKind.None; } }); } else removeChildrenWithFormatting(opts); } exports.removeChildrenWithFormattingFromCollapsibleSyntaxList = removeChildrenWithFormattingFromCollapsibleSyntaxList; function removeChildrenWithFormatting(opts) { var children = opts.children, getSiblingFormatting = opts.getSiblingFormatting; if (children.length === 0) return; doManipulation_1.doManipulation(children[0].sourceFile, new textManipulators_1.RemoveChildrenWithFormattingTextManipulator({ children: children, getSiblingFormatting: getSiblingFormatting }), new nodeHandlers_1.NodeHandlerFactory().getForChildIndex({ parent: children[0].getParentSyntaxList() || children[0].getParentOrThrow(), childIndex: children[0].getChildIndex(), childCount: -1 * children.length })); } exports.removeChildrenWithFormatting = removeChildrenWithFormatting; function removeOverloadableClassMember(classMember) { if (classMember.isOverload()) { if (classMember.getParentOrThrow().isAmbient()) removeClassMember(classMember); else removeChildren({ children: [classMember], removeFollowingSpaces: true, removeFollowingNewLines: true }); } else removeClassMembers(__spread(classMember.getOverloads(), [classMember])); } exports.removeOverloadableClassMember = removeOverloadableClassMember; function removeClassMember(classMember) { removeClassMembers([classMember]); } exports.removeClassMember = removeClassMember; function removeClassMembers(classMembers) { removeChildrenWithFormatting({ getSiblingFormatting: formatting_1.getClassMemberFormatting, children: classMembers }); } exports.removeClassMembers = removeClassMembers; function removeInterfaceMember(classMember) { removeInterfaceMembers([classMember]); } exports.removeInterfaceMember = removeInterfaceMember; function removeInterfaceMembers(classMembers) { removeChildrenWithFormatting({ getSiblingFormatting: formatting_1.getInterfaceMemberFormatting, children: classMembers }); } exports.removeInterfaceMembers = removeInterfaceMembers; function removeCommaSeparatedChild(child, opts) { var _a = (opts || {}).removePrecedingSpaces, removePrecedingSpaces = _a === void 0 ? undefined : _a; var childrenToRemove = [child]; var syntaxList = child.getParentSyntaxListOrThrow(); addNextCommaIfAble(); addPreviousCommaIfAble(); 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; function removeOverloadableStatementedNodeChild(node) { if (node.isOverload()) removeChildren({ children: [node], removeFollowingSpaces: true, removeFollowingNewLines: true }); else removeStatementedNodeChildren(__spread(node.getOverloads(), [node])); } exports.removeOverloadableStatementedNodeChild = removeOverloadableStatementedNodeChild; function removeStatementedNodeChild(node) { removeStatementedNodeChildren([node]); } exports.removeStatementedNodeChild = removeStatementedNodeChild; function removeStatementedNodeChildren(node) { removeChildrenWithFormatting({ getSiblingFormatting: formatting_1.getStatementedNodeChildFormatting, children: node }); } exports.removeStatementedNodeChildren = removeStatementedNodeChildren; function unwrapNode(node) { doManipulation_1.doManipulation(node.sourceFile, new textManipulators_1.UnwrapTextManipulator(node), new nodeHandlers_1.NodeHandlerFactory().getForUnwrappingNode(node)); } exports.unwrapNode = unwrapNode;