ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
47 lines (46 loc) • 2.31 kB
JavaScript
;
var __extends = (this && this.__extends)/* istanbul ignore next */ || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var StraightReplacementNodeHandler_1 = require("./StraightReplacementNodeHandler");
/**
* Replacement handler that tries to find the parents.
*/
var ParentFinderReplacementNodeHandler = /** @class */ (function (_super) {
__extends(ParentFinderReplacementNodeHandler, _super);
function ParentFinderReplacementNodeHandler(compilerFactory, parentNodeHandler, changingParent) {
var _this = _super.call(this, compilerFactory) || this;
_this.parentNodeHandler = parentNodeHandler;
_this.changingParent = changingParent;
_this.foundParent = false;
_this.changingParentParent = _this.changingParent.getParentSyntaxList() || _this.changingParent.getParent();
return _this;
}
ParentFinderReplacementNodeHandler.prototype.handleNode = function (currentNode, newNode) {
if (!this.foundParent && areNodesEqual(newNode, this.changingParent) && areNodesEqual(newNode.getParentSyntaxList() || newNode.getParent(), this.changingParentParent)) {
this.foundParent = true; // don't bother checking for the parent once it's found
this.parentNodeHandler.handleNode(currentNode, newNode);
}
else
_super.prototype.handleNode.call(this, currentNode, newNode);
};
return ParentFinderReplacementNodeHandler;
}(StraightReplacementNodeHandler_1.StraightReplacementNodeHandler));
exports.ParentFinderReplacementNodeHandler = ParentFinderReplacementNodeHandler;
function areNodesEqual(a, b) {
if (a == null && b == null)
return true;
if (a == null || b == null)
return false;
if (a.getPos() === b.getPos() && a.getKind() === b.getKind())
return true;
return false;
}