ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
75 lines (74 loc) • 2.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var manipulation_1 = require("../../../manipulation");
var typescript_1 = require("../../../typescript");
var utils_1 = require("../../../utils");
var base_1 = require("../base");
var callBaseSet_1 = require("../callBaseSet");
var common_1 = require("../common");
var callBaseGetStructure_1 = require("../callBaseGetStructure");
exports.EnumMemberBase = base_1.JSDocableNode(base_1.InitializerExpressionableNode(base_1.PropertyNamedNode(common_1.Node)));
var EnumMember = /** @class */ (function (_super) {
tslib_1.__extends(EnumMember, _super);
function EnumMember() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Gets the constant value of the enum.
*/
EnumMember.prototype.getValue = function () {
return this._context.typeChecker.getConstantValue(this);
};
/**
* Sets the enum value.
* @param value - Enum value.
*/
EnumMember.prototype.setValue = function (value) {
var text;
if (typeof value === "string") {
var quoteKind = this._context.manipulationSettings.getQuoteKind();
text = quoteKind + utils_1.StringUtils.escapeForWithinString(value, quoteKind) + quoteKind;
}
else {
text = value.toString();
}
this.setInitializer(text);
return this;
};
/**
* Removes this enum member.
*/
EnumMember.prototype.remove = function () {
var childrenToRemove = [this];
var commaToken = this.getNextSiblingIfKind(typescript_1.SyntaxKind.CommaToken);
if (commaToken != null)
childrenToRemove.push(commaToken);
manipulation_1.removeChildrenWithFormatting({
children: childrenToRemove,
getSiblingFormatting: function () { return manipulation_1.FormattingKind.Newline; }
});
};
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
EnumMember.prototype.set = function (structure) {
callBaseSet_1.callBaseSet(exports.EnumMemberBase.prototype, this, structure);
if (structure.value != null)
this.setValue(structure.value);
else if (structure.hasOwnProperty("value"))
this.removeInitializer();
return this;
};
/**
* Gets the structure equivalent to this node.
*/
EnumMember.prototype.getStructure = function () {
return callBaseGetStructure_1.callBaseGetStructure(exports.EnumMemberBase.prototype, this, {
value: this.hasInitializer() ? this.getValue() : undefined
});
};
return EnumMember;
}(exports.EnumMemberBase));
exports.EnumMember = EnumMember;