UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

30 lines (28 loc) 1.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const errors = require("./../../../errors"); const getInsertErrorMessageText_1 = require("./../../getInsertErrorMessageText"); /** * Replacement handler for replacing parts of the tree that should be equal. */ class StraightReplacementNodeHandler { constructor(compilerFactory) { this.compilerFactory = compilerFactory; } handleNode(currentNode, newNode) { /* istanbul ignore if */ if (currentNode.getKind() !== newNode.getKind()) throw new errors.InvalidOperationError(getInsertErrorMessageText_1.getInsertErrorMessageText("Error replacing tree!", currentNode, newNode)); const newNodeChildren = newNode.getChildrenIterator(); for (const currentNodeChild of currentNode.getChildrenIterator()) { const newNodeChild = newNodeChildren.next().value; this.handleNode(currentNodeChild, newNodeChild); } /* istanbul ignore if */ if (!newNodeChildren.next().done) throw new Error("Error replacing tree: Should not have new children left over."); // todo: better error message this.compilerFactory.replaceCompilerNode(currentNode, newNode.compilerNode); } } exports.StraightReplacementNodeHandler = StraightReplacementNodeHandler; //# sourceMappingURL=StraightReplacementNodeHandler.js.map