UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

29 lines (28 loc) 938 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ts = require("typescript"); var utils_1 = require("./../../utils"); /** * Gets the insert pos from an index. */ function getInsertPosFromIndex(index, parent, children) { if (index === 0) { if (utils_1.TypeGuards.isSourceFile(parent)) return 0; else { var parentContainer = getParentContainer(parent); var openBraceToken = parentContainer.getFirstChildByKindOrThrow(ts.SyntaxKind.OpenBraceToken); return openBraceToken.getEnd(); } } return children[index - 1].getEnd(); } exports.getInsertPosFromIndex = getInsertPosFromIndex; function getParentContainer(parent) { if (utils_1.TypeGuards.isBodiedNode(parent)) return parent.getBody(); if (utils_1.TypeGuards.isBodyableNode(parent)) return parent.getBodyOrThrow(); else return parent; }