UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for static analysis and code manipulation.

124 lines (123 loc) 5.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var errors = require("../../../errors"); 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 module_1 = require("../module"); var statement_1 = require("../statement"); var callBaseGetStructure_1 = require("../callBaseGetStructure"); exports.EnumDeclarationBase = base_1.ChildOrderableNode(base_1.TextInsertableNode(module_1.NamespaceChildableNode(base_1.JSDocableNode(base_1.AmbientableNode(base_1.ExportableNode(base_1.ModifierableNode(base_1.NamedNode(statement_1.Statement)))))))); var EnumDeclaration = /** @class */ (function (_super) { tslib_1.__extends(EnumDeclaration, _super); function EnumDeclaration() { return _super !== null && _super.apply(this, arguments) || this; } /** * Sets the node from a structure. * @param structure - Structure to set the node with. */ EnumDeclaration.prototype.set = function (structure) { callBaseSet_1.callBaseSet(exports.EnumDeclarationBase.prototype, this, structure); if (structure.isConst != null) this.setIsConstEnum(structure.isConst); if (structure.members != null) { this.getMembers().forEach(function (m) { return m.remove(); }); this.addMembers(structure.members); } return this; }; /** * Adds a member to the enum. * @param structure - Structure of the enum. */ EnumDeclaration.prototype.addMember = function (structure) { return this.addMembers([structure])[0]; }; /** * Adds members to the enum. * @param structures - Structures of the enums. */ EnumDeclaration.prototype.addMembers = function (structures) { return this.insertMembers(this.getMembers().length, structures); }; /** * Inserts a member to the enum. * @param index - Child index to insert at. * @param structure - Structure of the enum. */ EnumDeclaration.prototype.insertMember = function (index, structure) { return this.insertMembers(index, [structure])[0]; }; /** * Inserts members to an enum. * @param index - Child index to insert at. * @param structures - Structures of the enums. */ EnumDeclaration.prototype.insertMembers = function (index, structures) { var members = this.getMembers(); index = manipulation_1.verifyAndGetIndex(index, members.length); if (structures.length === 0) return []; // create member code // todo: pass in the StructureToText to the function below var writer = this._getWriterWithChildIndentation(); var structurePrinter = this._context.structurePrinterFactory.forEnumMember(); structurePrinter.printTexts(writer, structures); // insert manipulation_1.insertIntoCommaSeparatedNodes({ parent: this.getChildSyntaxListOrThrow(), currentNodes: members, insertIndex: index, newText: writer.toString(), useNewLines: true }); // get the members return manipulation_1.getNodesToReturn(this.getMembers(), index, structures.length); }; EnumDeclaration.prototype.getMember = function (nameOrFindFunction) { return utils_1.getNodeByNameOrFindFunction(this.getMembers(), nameOrFindFunction); }; EnumDeclaration.prototype.getMemberOrThrow = function (nameOrFindFunction) { return errors.throwIfNullOrUndefined(this.getMember(nameOrFindFunction), function () { return utils_1.getNotFoundErrorMessageForNameOrFindFunction("enum member", nameOrFindFunction); }); }; /** * Gets the enum's members. */ EnumDeclaration.prototype.getMembers = function () { var _this = this; return this.compilerNode.members.map(function (m) { return _this._getNodeFromCompilerNode(m); }); }; /** * Toggle if it's a const enum. */ EnumDeclaration.prototype.setIsConstEnum = function (value) { return this.toggleModifier("const", value); }; /** * Gets if it's a const enum. */ EnumDeclaration.prototype.isConstEnum = function () { return this.getConstKeyword() != null; }; /** * Gets the const enum keyword or undefined if not exists. */ EnumDeclaration.prototype.getConstKeyword = function () { return this.getFirstModifierByKind(typescript_1.SyntaxKind.ConstKeyword); }; /** * Gets the structure equivalent to this node. */ EnumDeclaration.prototype.getStructure = function () { return callBaseGetStructure_1.callBaseGetStructure(exports.EnumDeclarationBase.prototype, this, { isConst: this.isConstEnum(), members: this.getMembers().map(function (member) { return member.getStructure(); }) }); }; return EnumDeclaration; }(exports.EnumDeclarationBase)); exports.EnumDeclaration = EnumDeclaration;