UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

41 lines (39 loc) 1.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ts = require("typescript"); const manipulation_1 = require("./../../../manipulation"); const utils_1 = require("./../../../utils"); /** * @internal */ function setBodyTextForNode(body, textOrWriterFunction) { const childSyntaxList = body.getChildSyntaxListOrThrow(); const childrenToRemove = childSyntaxList.getChildren(); const childIndentationText = body.getChildIndentationText(); const newLineKind = body.global.manipulationSettings.getNewLineKind(); const newText = getNewText(); const openBrace = body.getFirstChildByKindOrThrow(ts.SyntaxKind.FirstPunctuation); const 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, parent: body, replacing: { textLength: closeBrace.getStart() - openBrace.getEnd(), nodes: [childSyntaxList] } }); function getNewText() { let text = utils_1.getTextFromStringOrWriter(body.global.manipulationSettings, textOrWriterFunction) || ""; if (text.length > 0) text = newLineKind + text.split(/\r?\n/).map(t => t.length > 0 ? childIndentationText + t : t).join(newLineKind); if (!text.endsWith(newLineKind)) text += newLineKind; return text + body.getParentOrThrow().getIndentationText(); } } exports.setBodyTextForNode = setBodyTextForNode; //# sourceMappingURL=setBodyTextForNode.js.map