ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
48 lines (47 loc) • 2.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var errors = require("../../errors");
var utils_1 = require("../../utils");
var NodeHandlerHelper_1 = require("./NodeHandlerHelper");
/**
* Replacement handler for replacing parts of the tree that should be equal.
*/
var StraightReplacementNodeHandler = /** @class */ (function () {
function StraightReplacementNodeHandler(compilerFactory) {
this.compilerFactory = compilerFactory;
this.helper = new NodeHandlerHelper_1.NodeHandlerHelper(compilerFactory);
}
StraightReplacementNodeHandler.prototype.handleNode = function (currentNode, newNode, newSourceFile) {
/* istanbul ignore if */
if (currentNode.getKind() !== newNode.kind)
throw new errors.InvalidOperationError("Error replacing tree! Perhaps a syntax error was inserted " +
("(Current: " + currentNode.getKindName() + " -- New: " + utils_1.getSyntaxKindName(newNode.kind) + ")."));
if (currentNode._hasWrappedChildren())
this.handleChildren(currentNode, newNode, newSourceFile);
this.compilerFactory.replaceCompilerNode(currentNode, newNode);
};
StraightReplacementNodeHandler.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;
this.helper.handleForValues(this, currentNodeChild, newNodeChildren.next().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; }
}
/* istanbul ignore if */
if (!newNodeChildren.next().done)
throw new Error("Error replacing tree: Should not have new children left over.");
};
return StraightReplacementNodeHandler;
}());
exports.StraightReplacementNodeHandler = StraightReplacementNodeHandler;