UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

33 lines (32 loc) 1.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var typescript_1 = 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 if (utils_1.TypeGuards.isCaseClause(parent) || utils_1.TypeGuards.isDefaultClause(parent)) { var colonToken = parent.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.ColonToken); return colonToken.getEnd(); } else { var parentContainer = getParentContainer(parent); var openBraceToken = parentContainer.getFirstChildByKindOrThrow(typescript_1.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; }