ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
63 lines (62 loc) • 2.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var errors = require("../../../../errors");
var typescript_1 = require("../../../../typescript");
var callBaseSet_1 = require("../../callBaseSet");
var callBaseGetStructure_1 = require("../../callBaseGetStructure");
var ReferenceFindableNode_1 = require("./ReferenceFindableNode");
var RenameableNode_1 = require("./RenameableNode");
function DeclarationNamedNode(Base) {
return DeclarationNamedNodeInternal(ReferenceFindableNode_1.ReferenceFindableNode(RenameableNode_1.RenameableNode(Base)));
}
exports.DeclarationNamedNode = DeclarationNamedNode;
function DeclarationNamedNodeInternal(Base) {
return /** @class */ (function (_super) {
tslib_1.__extends(class_1, _super);
function class_1() {
return _super !== null && _super.apply(this, arguments) || this;
}
class_1.prototype.getNameNodeOrThrow = function () {
var nameNode = this.getNameNode();
if (nameNode == null)
throw new errors.InvalidOperationError("Expected a name node.");
return nameNode;
};
class_1.prototype.getNameNode = function () {
var compilerNameNode = this.compilerNode.name;
if (compilerNameNode == null)
return undefined;
switch (compilerNameNode.kind) {
case typescript_1.SyntaxKind.Identifier:
return this._getNodeFromCompilerNode(compilerNameNode);
/* istanbul ignore next */
default:
throw errors.getNotImplementedForSyntaxKindError(compilerNameNode.kind);
}
};
class_1.prototype.getNameOrThrow = function () {
return errors.throwIfNullOrUndefined(this.getName(), "Expected to find a name.");
};
class_1.prototype.getName = function () {
var nameNode = this.getNameNode();
return nameNode == null ? undefined : nameNode.getText();
};
class_1.prototype.set = function (structure) {
callBaseSet_1.callBaseSet(Base.prototype, this, structure);
if (structure.name != null) {
var nameNode = this.getNameNode();
if (nameNode == null)
throw new errors.NotImplementedError("Not implemented scenario when a name node doesn't exist. Please open an issue if you need this functionality.");
nameNode.replaceWithText(structure.name);
}
return this;
};
class_1.prototype.getStructure = function () {
return callBaseGetStructure_1.callBaseGetStructure(Base.prototype, this, {
name: this.getName()
});
};
return class_1;
}(Base));
}