UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

85 lines (84 loc) 4 kB
"use strict"; var __values = (this && this.__values)/* istanbul ignore next */ || function (o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; }; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("./../../../utils"); var StraightReplacementNodeHandler_1 = require("./StraightReplacementNodeHandler"); var NodeHandlerHelper_1 = require("./NodeHandlerHelper"); /** * Parent handler used to unwrap the */ var UnwrapParentHandler = /** @class */ (function () { function UnwrapParentHandler(compilerFactory, childIndex) { this.compilerFactory = compilerFactory; this.childIndex = childIndex; this.straightReplacementNodeHandler = new StraightReplacementNodeHandler_1.StraightReplacementNodeHandler(compilerFactory); this.helper = new NodeHandlerHelper_1.NodeHandlerHelper(compilerFactory); } UnwrapParentHandler.prototype.handleNode = function (currentNode, newNode) { var helper = this.helper; var currentNodeChildren = new utils_1.AdvancedIterator(utils_1.ArrayUtils.toIterator(currentNode.getCompilerChildren())); var newNodeChildren = new utils_1.AdvancedIterator(newNode.getChildrenIterator()); var index = 0; // replace normally until reaching the first child while (!currentNodeChildren.done && !newNodeChildren.done && index++ < this.childIndex) this.helper.handleForValues(this.straightReplacementNodeHandler, currentNodeChildren.next(), newNodeChildren.next()); // the child syntax list's children should map to the newNodes next children var currentChild = this.compilerFactory.getExistingCompilerNode(currentNodeChildren.next()); var childSyntaxList = currentChild.getChildSyntaxListOrThrow(); try { for (var _a = __values(childSyntaxList.getCompilerChildren()), _b = _a.next(); !_b.done; _b = _a.next()) { var child = _b.value; this.helper.handleForValues(this.straightReplacementNodeHandler, child, newNodeChildren.next()); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_1) throw e_1.error; } } // destroy all the current child's children except for the children of its child syntax list forgetNodes(currentChild); function forgetNodes(node) { if (node === childSyntaxList) { node.forgetOnlyThis(); return; } try { for (var _a = __values(node.getChildrenInCacheIterator()), _b = _a.next(); !_b.done; _b = _a.next()) { var child = _b.value; forgetNodes(child); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_b && !_b.done && (_c = _a.return)) _c.call(_a); } finally { if (e_2) throw e_2.error; } } node.forgetOnlyThis(); var e_2, _c; } // handle the rest while (!currentNodeChildren.done) this.helper.handleForValues(this.straightReplacementNodeHandler, currentNodeChildren.next(), newNodeChildren.next()); // ensure the new children iterator is done too if (!newNodeChildren.done) throw new Error("Error replacing tree: Should not have more children left over."); // todo: better error message this.compilerFactory.replaceCompilerNode(currentNode, newNode.compilerNode); var e_1, _c; }; return UnwrapParentHandler; }()); exports.UnwrapParentHandler = UnwrapParentHandler;