ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
60 lines (59 loc) • 2.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var utils_1 = require("../../utils");
var StraightReplacementNodeHandler_1 = require("./StraightReplacementNodeHandler");
/**
* Replacement handler that tries to find the parents.
*/
var ParentFinderReplacementNodeHandler = /** @class */ (function (_super) {
tslib_1.__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();
_this.parentsAtSamePos = _this.changingParentParent != null && _this.changingParentParent.getPos() === _this.changingParent.getPos();
return _this;
}
ParentFinderReplacementNodeHandler.prototype.handleNode = function (currentNode, newNode, newSourceFile) {
if (!this.foundParent && this.isParentNode(newNode)) {
this.foundParent = true; // don't bother checking for the parent once it's found
this.parentNodeHandler.handleNode(currentNode, newNode, newSourceFile);
}
else
_super.prototype.handleNode.call(this, currentNode, newNode, newSourceFile);
};
ParentFinderReplacementNodeHandler.prototype.isParentNode = function (newNode) {
var positionsAndKindsEqual = areNodesEqual(newNode, this.changingParent)
&& areNodesEqual(utils_1.getParentSyntaxList(newNode) || newNode.parent, this.changingParentParent);
if (!positionsAndKindsEqual)
return false;
if (!this.parentsAtSamePos)
return true;
// Need to do some additional checks if the parents are in the same position.
// For example, some nodes like `this` in `this.is.nested.deep;`... in this case, just check the depths are equal
return getAncestorLength(this.changingParent.compilerNode) === getAncestorLength(newNode);
function getAncestorLength(nodeToCheck) {
var node = nodeToCheck;
var count = 0;
while (node.parent != null) {
count++;
node = node.parent;
}
return count;
}
};
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.pos === b.getPos() && a.kind === b.getKind())
return true;
return false;
}