ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
91 lines (90 loc) • 4.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var manipulation_1 = require("../../../manipulation");
var getStructureFuncs = require("../../../manipulation/helpers/getStructureFunctions");
var typescript_1 = require("../../../typescript");
var base_1 = require("../base");
var callBaseSet_1 = require("../callBaseSet");
var common_1 = require("../common");
var function_1 = require("../function");
var base_2 = require("./base");
var callBaseGetStructure_1 = require("../callBaseGetStructure");
exports.MethodDeclarationBase = base_1.ChildOrderableNode(base_1.TextInsertableNode(function_1.OverloadableNode(base_1.BodyableNode(base_1.DecoratableNode(base_2.AbstractableNode(base_1.ScopedNode(base_1.QuestionTokenableNode(base_1.StaticableNode(base_1.AsyncableNode(base_1.GeneratorableNode(function_1.FunctionLikeDeclaration(base_1.PropertyNamedNode(common_1.Node)))))))))))));
exports.MethodDeclarationOverloadBase = base_1.JSDocableNode(base_1.ChildOrderableNode(base_1.TextInsertableNode(base_1.ScopedNode(base_1.TypeParameteredNode(base_2.AbstractableNode(base_1.QuestionTokenableNode(base_1.StaticableNode(base_1.AsyncableNode(base_1.ModifierableNode(base_1.GeneratorableNode(base_1.SignaturedDeclaration(common_1.Node))))))))))));
var MethodDeclaration = /** @class */ (function (_super) {
tslib_1.__extends(MethodDeclaration, _super);
function MethodDeclaration() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
MethodDeclaration.prototype.set = function (structure) {
callBaseSet_1.callBaseSet(exports.MethodDeclarationBase.prototype, this, structure);
if (structure.overloads != null) {
this.getOverloads().forEach(function (o) { return o.remove(); });
this.addOverloads(structure.overloads);
}
return this;
};
/**
* Add a method overload.
* @param structure - Structure to add.
*/
MethodDeclaration.prototype.addOverload = function (structure) {
return this.addOverloads([structure])[0];
};
/**
* Add method overloads.
* @param structures - Structures to add.
*/
MethodDeclaration.prototype.addOverloads = function (structures) {
return this.insertOverloads(this.getOverloads().length, structures);
};
/**
* Inserts a method overload.
* @param index - Child index to insert at.
* @param structure - Structures to insert.
*/
MethodDeclaration.prototype.insertOverload = function (index, structure) {
return this.insertOverloads(index, [structure])[0];
};
/**
* Inserts method overloads.
* @param index - Child index to insert at.
* @param structures - Structures to insert.
*/
MethodDeclaration.prototype.insertOverloads = function (index, structures) {
var thisName = this.getName();
var childCodes = structures.map(function (structure) { return thisName + "();"; });
return function_1.insertOverloads({
node: this,
index: index,
structures: structures,
childCodes: childCodes,
getThisStructure: getStructureFuncs.fromMethodDeclarationOverload,
setNodeFromStructure: function (node, structure) { return node.set(structure); },
expectedSyntaxKind: typescript_1.SyntaxKind.MethodDeclaration
});
};
/**
* Removes the method.
*/
MethodDeclaration.prototype.remove = function () {
manipulation_1.removeOverloadableClassMember(this);
};
/**
* Gets the structure equivalent to this node.
*/
MethodDeclaration.prototype.getStructure = function () {
var hasImplementation = this.getImplementation() != null;
var isOverload = this.isOverload();
var basePrototype = isOverload && hasImplementation ? exports.MethodDeclarationOverloadBase.prototype : exports.MethodDeclarationBase.prototype;
var structure = !hasImplementation || isOverload ? {} : { overloads: this.getOverloads().map(function (o) { return o.getStructure(); }) };
return callBaseGetStructure_1.callBaseGetStructure(basePrototype, this, structure);
};
return MethodDeclaration;
}(exports.MethodDeclarationBase));
exports.MethodDeclaration = MethodDeclaration;