UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for static analysis and code manipulation.

52 lines (51 loc) 2.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var utils_1 = require("../../utils"); var NodeHandlerHelper_1 = require("./NodeHandlerHelper"); /** * Replacement handler that goes through the tree and forgets any nodes that have changed kind. */ var ForgetChangedNodeHandler = /** @class */ (function () { function ForgetChangedNodeHandler(compilerFactory) { this.compilerFactory = compilerFactory; this.helper = new NodeHandlerHelper_1.NodeHandlerHelper(compilerFactory); } ForgetChangedNodeHandler.prototype.handleNode = function (currentNode, newNode, newSourceFile) { if (currentNode.getKind() !== newNode.kind) { currentNode.forget(); return; } if (currentNode._hasWrappedChildren()) this.handleChildren(currentNode, newNode, newSourceFile); this.compilerFactory.replaceCompilerNode(currentNode, newNode); }; ForgetChangedNodeHandler.prototype.handleChildren = function (currentNode, newNode, newSourceFile) { var e_1, _a; var _b = tslib_1.__read(this.helper.getChildrenFast(currentNode, newNode, newSourceFile), 2), currentNodeChildren = _b[0], newNodeChildrenArray = _b[1]; var newNodeChildren = utils_1.ArrayUtils.toIterator(newNodeChildrenArray); try { for (var currentNodeChildren_1 = tslib_1.__values(currentNodeChildren), currentNodeChildren_1_1 = currentNodeChildren_1.next(); !currentNodeChildren_1_1.done; currentNodeChildren_1_1 = currentNodeChildren_1.next()) { var currentNodeChild = currentNodeChildren_1_1.value; var nextNodeChildResult = newNodeChildren.next(); if (nextNodeChildResult.done && nextNodeChildResult.value == null) { var existingNode = this.compilerFactory.getExistingCompilerNode(currentNodeChild); if (existingNode != null) existingNode.forget(); } else { this.helper.handleForValues(this, currentNodeChild, nextNodeChildResult.value, newSourceFile); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (currentNodeChildren_1_1 && !currentNodeChildren_1_1.done && (_a = currentNodeChildren_1.return)) _a.call(currentNodeChildren_1); } finally { if (e_1) throw e_1.error; } } }; return ForgetChangedNodeHandler; }()); exports.ForgetChangedNodeHandler = ForgetChangedNodeHandler;