ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
96 lines (95 loc) • 3.84 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var manipulation_1 = require("../../../manipulation");
var errors = require("../../../errors");
var base_1 = require("../base");
var callBaseSet_1 = require("../callBaseSet");
var TypeElement_1 = require("./TypeElement");
var callBaseGetStructure_1 = require("../callBaseGetStructure");
exports.IndexSignatureDeclarationBase = base_1.ReturnTypedNode(base_1.ChildOrderableNode(base_1.JSDocableNode(base_1.ReadonlyableNode(base_1.ModifierableNode(TypeElement_1.TypeElement)))));
var IndexSignatureDeclaration = /** @class */ (function (_super) {
tslib_1.__extends(IndexSignatureDeclaration, _super);
function IndexSignatureDeclaration() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Gets the key name.
*/
IndexSignatureDeclaration.prototype.getKeyName = function () {
return this.getKeyNameNode().getText();
};
/**
* Sets the key name.
* @param name - New name.
*/
IndexSignatureDeclaration.prototype.setKeyName = function (name) {
errors.throwIfWhitespaceOrNotString(name, "name");
if (this.getKeyName() === name)
return;
this.getKeyNameNode().replaceWithText(name, this._getWriterWithQueuedChildIndentation());
};
/**
* Gets the key name node.
*/
IndexSignatureDeclaration.prototype.getKeyNameNode = function () {
var param = this.compilerNode.parameters[0];
return this._getNodeFromCompilerNode(param.name);
};
/**
* Gets the key type.
*/
IndexSignatureDeclaration.prototype.getKeyType = function () {
return this.getKeyNameNode().getType();
};
/**
* Sets the key type.
* @param type - Type.
*/
IndexSignatureDeclaration.prototype.setKeyType = function (type) {
errors.throwIfWhitespaceOrNotString(type, "type");
var keyTypeNode = this.getKeyTypeNode();
if (keyTypeNode.getText() === type)
return this;
keyTypeNode.replaceWithText(type, this._getWriterWithQueuedChildIndentation());
return this;
};
/**
* Gets the key type node.
*/
IndexSignatureDeclaration.prototype.getKeyTypeNode = function () {
// this node must exist for it to be an index signature (otherwise it's a property signature)
var param = this.compilerNode.parameters[0];
return this._getNodeFromCompilerNode(param.type);
};
/**
* Removes this index signature.
*/
IndexSignatureDeclaration.prototype.remove = function () {
manipulation_1.removeInterfaceMember(this);
};
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
IndexSignatureDeclaration.prototype.set = function (structure) {
callBaseSet_1.callBaseSet(exports.IndexSignatureDeclarationBase.prototype, this, structure);
if (structure.keyName != null)
this.setKeyName(structure.keyName);
if (structure.keyType != null)
this.setKeyType(structure.keyType);
return this;
};
/**
* Gets the structure equivalent to this node.
*/
IndexSignatureDeclaration.prototype.getStructure = function () {
var keyTypeNode = this.getKeyTypeNode();
return callBaseGetStructure_1.callBaseGetStructure(exports.IndexSignatureDeclarationBase.prototype, this, {
keyName: this.getKeyName(),
keyType: keyTypeNode.getText()
});
};
return IndexSignatureDeclaration;
}(exports.IndexSignatureDeclarationBase));
exports.IndexSignatureDeclaration = IndexSignatureDeclaration;