UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

33 lines (31 loc) 1.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ts = require("typescript"); const manipulation_1 = require("./../../../manipulation"); /** * @internal */ function setBodyTextForNode(body, text) { const childSyntaxList = body.getChildSyntaxListOrThrow(); const childrenToRemove = childSyntaxList.getChildren(); const childIndentationText = body.getChildIndentationText(); const newLineKind = body.global.manipulationSettings.getNewLineKind(); const newText = (text.length > 0 ? newLineKind + text.split(/\r?\n/).map(t => t.length > 0 ? childIndentationText + t : t).join(newLineKind) : "") + newLineKind + body.getParentOrThrow().getIndentationText(); 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: { length: closeBrace.getStart() - openBrace.getEnd(), nodes: [childSyntaxList] } }); } exports.setBodyTextForNode = setBodyTextForNode; //# sourceMappingURL=setBodyTextForNode.js.map