ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
114 lines (113 loc) • 6.42 kB
JavaScript
;
var __read = (this && this.__read)/* istanbul ignore next */ || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread)/* istanbul ignore next */ || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var ts = require("typescript");
var errors = require("./../../errors");
var DefaultParentHandler_1 = require("./DefaultParentHandler");
var ParentFinderReplacementNodeHandler_1 = require("./ParentFinderReplacementNodeHandler");
var RangeParentHandler_1 = require("./RangeParentHandler");
var StraightReplacementNodeHandler_1 = require("./StraightReplacementNodeHandler");
var ForgetChangedNodeHandler_1 = require("./ForgetChangedNodeHandler");
var TryOrForgetNodeHandler_1 = require("./TryOrForgetNodeHandler");
var ChangeChildOrderParentHandler_1 = require("./ChangeChildOrderParentHandler");
var UnwrapParentHandler_1 = require("./UnwrapParentHandler");
var NodeHandlerFactory = /** @class */ (function () {
/* istanbul ignore next */ function NodeHandlerFactory() {
}
NodeHandlerFactory.prototype.getDefault = function (opts) {
var changingParent = opts.parent, isFirstChild = opts.isFirstChild, childCount = opts.childCount, customMappings = opts.customMappings;
var sourceFile = changingParent.getSourceFile();
var compilerFactory = sourceFile.global.compilerFactory;
var replacingNodes = opts.replacingNodes == null ? undefined : __spread(opts.replacingNodes);
var parentHandler = new DefaultParentHandler_1.DefaultParentHandler(compilerFactory, { childCount: childCount, isFirstChild: isFirstChild, replacingNodes: replacingNodes, customMappings: customMappings });
if (changingParent === sourceFile)
return parentHandler;
else
return new ParentFinderReplacementNodeHandler_1.ParentFinderReplacementNodeHandler(compilerFactory, parentHandler, changingParent);
};
NodeHandlerFactory.prototype.getForCreatingSyntaxList = function (opts) {
var parent = opts.parent, insertPos = opts.insertPos;
return this.getDefault({
parent: parent,
childCount: 1,
isFirstChild: function (currentNode, newNode) { return newNode.getKind() === ts.SyntaxKind.SyntaxList && insertPos <= newNode.getStart(); }
});
};
NodeHandlerFactory.prototype.getForRange = function (opts) {
var changingParent = opts.parent, start = opts.start, end = opts.end;
var sourceFile = changingParent.getSourceFile();
var compilerFactory = sourceFile.global.compilerFactory;
var parentHandler = new RangeParentHandler_1.RangeParentHandler(compilerFactory, { start: start, end: end });
if (changingParent === sourceFile)
return parentHandler;
else
return new ParentFinderReplacementNodeHandler_1.ParentFinderReplacementNodeHandler(compilerFactory, parentHandler, changingParent);
};
NodeHandlerFactory.prototype.getForChildIndex = function (opts) {
var parent = opts.parent, childIndex = opts.childIndex, childCount = opts.childCount, replacingNodes = opts.replacingNodes, customMappings = opts.customMappings;
var parentChildren = parent.getChildren();
errors.throwIfOutOfRange(childIndex, [0, parentChildren.length], "opts.childIndex");
if (childCount < 0)
errors.throwIfOutOfRange(childCount, [childIndex - parentChildren.length, 0], "opts.childCount");
var i = 0;
var isFirstChild = function () { return i++ === childIndex; };
return this.getDefault({
parent: parent,
isFirstChild: isFirstChild,
childCount: childCount,
replacingNodes: replacingNodes,
customMappings: customMappings
});
};
NodeHandlerFactory.prototype.getForStraightReplacement = function (compilerFactory) {
return new StraightReplacementNodeHandler_1.StraightReplacementNodeHandler(compilerFactory);
};
NodeHandlerFactory.prototype.getForForgetChanged = function (compilerFactory) {
return new ForgetChangedNodeHandler_1.ForgetChangedNodeHandler(compilerFactory);
};
NodeHandlerFactory.prototype.getForTryOrForget = function (handler) {
return new TryOrForgetNodeHandler_1.TryOrForgetNodeHandler(handler);
};
NodeHandlerFactory.prototype.getForChangingChildOrder = function (opts) {
var changingParent = opts.parent, oldIndex = opts.oldIndex, newIndex = opts.newIndex;
var sourceFile = changingParent.getSourceFile();
var compilerFactory = sourceFile.global.compilerFactory;
var changeChildOrderParentHandler = new ChangeChildOrderParentHandler_1.ChangeChildOrderParentHandler(compilerFactory, { oldIndex: oldIndex, newIndex: newIndex });
if (changingParent === sourceFile)
return changeChildOrderParentHandler;
else
return new ParentFinderReplacementNodeHandler_1.ParentFinderReplacementNodeHandler(compilerFactory, changeChildOrderParentHandler, changingParent);
};
NodeHandlerFactory.prototype.getForUnwrappingNode = function (unwrappingNode) {
var changingParent = unwrappingNode.getParentSyntaxList() || unwrappingNode.getParentOrThrow();
var childIndex = unwrappingNode.getChildIndex();
var sourceFile = changingParent.getSourceFile();
var compilerFactory = sourceFile.global.compilerFactory;
var unwrapParentHandler = new UnwrapParentHandler_1.UnwrapParentHandler(compilerFactory, childIndex);
if (changingParent === sourceFile)
return unwrapParentHandler;
else
return new ParentFinderReplacementNodeHandler_1.ParentFinderReplacementNodeHandler(compilerFactory, unwrapParentHandler, changingParent);
};
return NodeHandlerFactory;
}());
exports.NodeHandlerFactory = NodeHandlerFactory;