UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

44 lines (42 loc) 1.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ts = require("typescript"); const insertIntoCreatableSyntaxList_1 = require("./insertIntoCreatableSyntaxList"); function insertIntoCommaSeparatedNodes(opts) { const { currentNodes, insertIndex, newTexts, parent } = opts; const nextNode = currentNodes[insertIndex]; const previousNode = currentNodes[insertIndex - 1]; const numberOfSyntaxListItemsInserting = newTexts.length * 2 - 1; if (nextNode != null) { insertIntoCreatableSyntaxList_1.insertIntoCreatableSyntaxList({ parent, insertPos: nextNode.getStart(), newText: `${newTexts.join(", ")}, `, syntaxList: nextNode.getParentSyntaxListOrThrow(), childIndex: nextNode.getChildIndex(), insertItemsCount: numberOfSyntaxListItemsInserting + 1 // extra comma }); } else if (previousNode != null) { insertIntoCreatableSyntaxList_1.insertIntoCreatableSyntaxList({ parent, insertPos: previousNode.getEnd(), newText: `, ${newTexts.join(", ")}`, syntaxList: previousNode.getParentSyntaxListOrThrow(), childIndex: previousNode.getChildIndex() + 1, insertItemsCount: numberOfSyntaxListItemsInserting + 1 // extra comma }); } else { insertIntoCreatableSyntaxList_1.insertIntoCreatableSyntaxList({ parent, insertPos: parent.getFirstChildByKindOrThrow(ts.SyntaxKind.SyntaxList).getPos(), syntaxList: parent.getFirstChildByKind(ts.SyntaxKind.SyntaxList), newText: newTexts.join(", "), childIndex: 0, insertItemsCount: numberOfSyntaxListItemsInserting }); } } exports.insertIntoCommaSeparatedNodes = insertIntoCommaSeparatedNodes; //# sourceMappingURL=insertIntoCommaSeparatedNodes.js.map