UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

139 lines (138 loc) 5.36 kB
"use strict"; var __read = (this && this.__read)/* istanbul ignore next */ || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spread = (this && this.__spread)/* istanbul ignore next */ || function () { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("./../../utils"); var textSeek_1 = require("./../textSeek"); var getSpacingBetweenNodes_1 = require("./getSpacingBetweenNodes"); function getTextChangingChildOrder(opts) { var parent = opts.parent, oldIndex = opts.oldIndex, newIndex = opts.newIndex, getSiblingFormatting = opts.getSiblingFormatting; var children = parent.getChildren(); var newLineKind = parent.global.manipulationSettings.getNewLineKind(); var movingNode = children[oldIndex]; var fullText = parent.sourceFile.getFullText(); var movingNodeStart = textSeek_1.getPosAtNextNonBlankLine(fullText, movingNode.getPos()); var movingNodeText = fullText.substring(movingNodeStart, movingNode.getEnd()); var lowerIndex = Math.min(newIndex, oldIndex); var upperIndex = Math.max(newIndex, oldIndex); var childrenInNewOrder = getChildrenInNewOrder(); var isParentSourceFile = utils_1.TypeGuards.isSourceFile(parent.getParentOrThrow()); var finalText = ""; fillPrefixText(); fillTextForIndex(lowerIndex); fillMiddleText(); fillTextForIndex(upperIndex); fillSuffixText(); return finalText; function getChildrenInNewOrder() { var result = __spread(children); result.splice(oldIndex, 1); result.splice(newIndex, 0, movingNode); return result; } function fillPrefixText() { finalText += fullText.substring(0, children[lowerIndex].getPos()); if (lowerIndex === 0 && !isParentSourceFile) finalText += newLineKind; } function fillMiddleText() { var startPos; var endPos; if (lowerIndex === oldIndex) { startPos = textSeek_1.getPosAtNextNonBlankLine(fullText, children[lowerIndex].getEnd()); endPos = children[upperIndex].getEnd(); } else { startPos = textSeek_1.getPosAtNextNonBlankLine(fullText, children[lowerIndex].getPos()); endPos = children[upperIndex].getPos(); } finalText += fullText.substring(startPos, endPos); } function fillSuffixText() { if (children.length - 1 === upperIndex && !isParentSourceFile) finalText += newLineKind; finalText += fullText.substring(textSeek_1.getPosAtNextNonBlankLine(fullText, children[upperIndex].getEnd())); } function fillTextForIndex(index) { if (index === oldIndex) fillSpacingForRemoval(); else { fillSpacingBeforeInsertion(); finalText += movingNodeText; fillSpacingAfterInsertion(); } } function fillSpacingForRemoval() { if (oldIndex === 0 || oldIndex === children.length - 1) return; fillSpacingCommon({ previousSibling: childrenInNewOrder[oldIndex - 1], nextSibling: childrenInNewOrder[oldIndex] }); } function fillSpacingBeforeInsertion() { if (newIndex === 0) return; fillSpacingCommon({ previousSibling: childrenInNewOrder[newIndex - 1], nextSibling: childrenInNewOrder[newIndex] }); } function fillSpacingAfterInsertion() { fillSpacingCommon({ previousSibling: childrenInNewOrder[newIndex], nextSibling: childrenInNewOrder[newIndex + 1] }); } function fillSpacingCommon(spacingOpts) { var spacing = getSpacingBetweenNodes_1.getSpacingBetweenNodes({ parent: parent, getSiblingFormatting: getSiblingFormatting, newLineKind: newLineKind, previousSibling: spacingOpts.previousSibling, nextSibling: spacingOpts.nextSibling }); var twoNewLines = newLineKind + newLineKind; if (spacing === twoNewLines) { if (utils_1.StringUtils.endsWith(finalText, twoNewLines)) return; else if (utils_1.StringUtils.endsWith(finalText, newLineKind)) finalText += newLineKind; else finalText += twoNewLines; } else if (spacing === newLineKind) { if (utils_1.StringUtils.endsWith(finalText, newLineKind)) return; else finalText += newLineKind; } else if (spacing === " ") { if (utils_1.StringUtils.endsWith(finalText, " ")) return; else finalText += " "; } else finalText += spacing; } } exports.getTextChangingChildOrder = getTextChangingChildOrder;