ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
64 lines (63 loc) • 3.08 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 ts = require("typescript");
var errors = require("./../../errors");
var manipulation_1 = require("./../../manipulation");
var utils_1 = require("./../../utils");
function ArgumentedNode(Base) {
return /** @class */ (function (_super) {
__extends(class_1, _super);
function class_1() {
return _super !== null && _super.apply(this, arguments) || this;
}
class_1.prototype.getArguments = function () {
var _this = this;
return this.compilerNode.arguments.map(function (a) { return _this.global.compilerFactory.getNodeFromCompilerNode(a, _this.sourceFile); });
};
class_1.prototype.addArgument = function (argumentText) {
return this.addArguments([argumentText])[0];
};
class_1.prototype.addArguments = function (argumentTexts) {
return this.insertArguments(this.getArguments().length, argumentTexts);
};
class_1.prototype.insertArgument = function (index, argumentText) {
return this.insertArguments(index, [argumentText])[0];
};
class_1.prototype.insertArguments = function (index, argumentTexts) {
if (utils_1.ArrayUtils.isNullOrEmpty(argumentTexts))
return [];
var args = this.getArguments();
index = manipulation_1.verifyAndGetIndex(index, args.length);
manipulation_1.insertIntoCommaSeparatedNodes({
parent: this.getFirstChildByKindOrThrow(ts.SyntaxKind.OpenParenToken).getNextSiblingIfKindOrThrow(ts.SyntaxKind.SyntaxList),
currentNodes: args,
insertIndex: index,
newTexts: argumentTexts
});
return this.getArguments().slice(index, index + argumentTexts.length);
};
class_1.prototype.removeArgument = function (argOrIndex) {
var args = this.getArguments();
if (args.length === 0)
throw new errors.InvalidOperationError("Cannot remove an argument when none exist.");
var argToRemove = typeof argOrIndex === "number" ? getArgFromIndex(argOrIndex) : argOrIndex;
manipulation_1.removeCommaSeparatedChild(argToRemove);
return this;
function getArgFromIndex(index) {
return args[manipulation_1.verifyAndGetIndex(index, args.length - 1)];
}
};
return class_1;
}(Base));
}
exports.ArgumentedNode = ArgumentedNode;