UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

44 lines (43 loc) 1.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /* barrel:ignore */ var ts = require("typescript"); var manipulation_1 = require("./../../../manipulation"); var utils_1 = require("./../../../utils"); /** * @internal */ function setBodyTextForNode(body, textOrWriterFunction) { var childSyntaxList = body.getChildSyntaxListOrThrow(); var childrenToRemove = childSyntaxList.getChildren(); var childIndentationText = body.getChildIndentationText(); var newLineKind = body.global.manipulationSettings.getNewLineKind(); var newText = getNewText(); var openBrace = body.getFirstChildByKindOrThrow(ts.SyntaxKind.FirstPunctuation); var closeBrace = body.getFirstChildByKindOrThrow(ts.SyntaxKind.CloseBraceToken); // ideally this wouldn't replace the existing syntax list manipulation_1.insertIntoParent({ insertPos: openBrace.getEnd(), childIndex: childSyntaxList.getChildIndex(), insertItemsCount: 1, newText: newText, parent: body, replacing: { textLength: closeBrace.getStart() - openBrace.getEnd(), nodes: [childSyntaxList] } }); function getNewText() { var text = manipulation_1.getIndentedText({ textOrWriterFunction: textOrWriterFunction, manipulationSettings: body.global.manipulationSettings, indentationText: childIndentationText }); if (text.length > 0) text = newLineKind + text; if (!utils_1.StringUtils.endsWith(text, newLineKind)) text += newLineKind; return text + body.getParentOrThrow().getIndentationText(); } } exports.setBodyTextForNode = setBodyTextForNode;