UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

55 lines (54 loc) 2.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("./../../utils"); var textChecks_1 = require("./../textChecks"); var insertIntoParent_1 = require("./insertIntoParent"); var getInsertPosFromIndex_1 = require("./getInsertPosFromIndex"); /** * Used to insert non-comma separated nodes into braces or a source file. */ function insertIntoBracesOrSourceFile(opts) { var parent = opts.parent, index = opts.index, childCodes = opts.childCodes, separator = opts.separator, children = opts.children; var sourceFile = parent.getSourceFile(); var insertPos = getInsertPosFromIndex_1.getInsertPosFromIndex(index, parent, children); var newLineChar = sourceFile.global.manipulationSettings.getNewLineKind(); var newText = ""; for (var i = 0; i < childCodes.length; i++) { if (i > 0) { newText += separator; if (opts.separatorNewlineWhen != null && opts.separatorNewlineWhen(opts.structures[i - 1], opts.structures[i])) newText += newLineChar; } newText += childCodes[i]; } if (index !== 0) newText = separator + newText; else if (insertPos !== 0) newText = newLineChar + newText; else if (parent.getFullWidth() > 0) newText = newText + separator; if (opts.previousBlanklineWhen != null) { var previousMember = children[index - 1]; var firstStructure = opts.structures[0]; if (previousMember != null && opts.previousBlanklineWhen(previousMember, firstStructure)) newText = newLineChar + newText; } var nextMember = children[index]; if (opts.nextBlanklineWhen != null) { var lastStructure = opts.structures[opts.structures.length - 1]; if (nextMember != null && opts.nextBlanklineWhen(nextMember, lastStructure)) { if (!textChecks_1.isBlankLineAtPos(sourceFile, insertPos)) newText = newText + newLineChar; } } if (utils_1.TypeGuards.isSourceFile(parent) && nextMember == null && !utils_1.StringUtils.endsWith(newText, newLineChar) && !utils_1.StringUtils.endsWith(sourceFile.getFullText(), "\n")) newText = newText + newLineChar; insertIntoParent_1.insertIntoParent({ parent: parent.getChildSyntaxListOrThrow(), insertPos: insertPos, newText: newText, childIndex: index, insertItemsCount: childCodes.length }); } exports.insertIntoBracesOrSourceFile = insertIntoBracesOrSourceFile;