ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
149 lines (147 loc) • 5.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
const errors = require("./../../errors");
const manipulation_1 = require("./../../manipulation");
const utils_1 = require("./../../utils");
const callBaseFill_1 = require("./../callBaseFill");
const base_1 = require("./../base");
const common_1 = require("./../common");
const namespace_1 = require("./../namespace");
exports.EnumDeclarationBase = base_1.TextInsertableNode(namespace_1.NamespaceChildableNode(base_1.DocumentationableNode(base_1.AmbientableNode(base_1.ExportableNode(base_1.ModifierableNode(base_1.NamedNode(common_1.Node)))))));
class EnumDeclaration extends exports.EnumDeclarationBase {
/**
* Fills the node from a structure.
* @param structure - Structure to fill.
*/
fill(structure) {
callBaseFill_1.callBaseFill(exports.EnumDeclarationBase.prototype, this, structure);
if (structure.isConst != null)
this.setIsConstEnum(structure.isConst);
if (structure.members != null && structure.members.length > 0)
this.addMembers(structure.members);
return this;
}
/**
* Adds a member to the enum.
* @param structure - Structure of the enum.
*/
addMember(structure) {
return this.addMembers([structure])[0];
}
/**
* Adds members to the enum.
* @param structures - Structures of the enums.
*/
addMembers(structures) {
return this.insertMembers(this.getMembers().length, structures);
}
/**
* Inserts a member to the enum.
* @param index - Index to insert at.
* @param structure - Structure of the enum.
*/
insertMember(index, structure) {
return this.insertMembers(index, [structure])[0];
}
/**
* Inserts members to an enum.
* @param index - Index to insert at.
* @param structures - Structures of the enums.
*/
insertMembers(index, structures) {
const members = this.getMembers();
index = manipulation_1.verifyAndGetIndex(index, members.length);
if (structures.length === 0)
return [];
const previousMember = members[index - 1];
const previousMemberComma = previousMember == null ? undefined : previousMember.getNextSiblingIfKind(ts.SyntaxKind.CommaToken);
const nextMember = members[index];
const indentationText = this.getChildIndentationText();
const newLineChar = this.global.manipulationSettings.getNewLineKind();
const syntaxList = this.getChildSyntaxListOrThrow();
const syntaxListChildren = syntaxList.getChildren();
const insertChildIndex = previousMember == null ? 0 : syntaxListChildren.indexOf(previousMemberComma || previousMember) + 1;
// create member code
let numberChildren = 1;
let code = "";
if (previousMember != null && previousMemberComma == null) {
code += ",";
numberChildren++;
}
code += `${newLineChar}${getMemberText(structures[0])}`;
for (const structure of structures.slice(1)) {
code += `,${newLineChar}${getMemberText(structure)}`;
numberChildren += 2;
}
if (nextMember != null) {
code += ",";
numberChildren++;
}
function getMemberText(structure) {
let memberText = `${indentationText}${structure.name}`;
if (typeof structure.value !== "undefined")
memberText += ` = ${structure.value}`;
return memberText;
}
// get the insert position
let insertPos;
if (previousMember == null)
insertPos = this.getFirstChildByKindOrThrow(ts.SyntaxKind.OpenBraceToken).getEnd();
else if (previousMemberComma == null)
insertPos = previousMember.getEnd();
else
insertPos = previousMember.getNextSiblingIfKind(ts.SyntaxKind.CommaToken).getEnd();
// insert
manipulation_1.insertIntoCreatableSyntaxList({
parent: this,
insertPos,
newText: code,
syntaxList,
childIndex: insertChildIndex,
insertItemsCount: numberChildren
});
// get the members
const newMembers = this.getMembers().slice(index, index + structures.length);
newMembers.forEach((m, i) => m.fill(structures[i]));
return newMembers;
}
getMember(nameOrFindFunction) {
return utils_1.getNamedNodeByNameOrFindFunction(this.getMembers(), nameOrFindFunction);
}
getMemberOrThrow(nameOrFindFunction) {
return errors.throwIfNullOrUndefined(this.getMember(nameOrFindFunction), () => utils_1.getNotFoundErrorMessageForNameOrFindFunction("enum member", nameOrFindFunction));
}
/**
* Gets the enum's members.
*/
getMembers() {
return this.getChildSyntaxListOrThrow().getChildren().filter(c => utils_1.TypeGuards.isEnumMember(c));
}
/**
* Toggle if it's a const enum
*/
setIsConstEnum(value) {
return this.toggleModifier("const", value);
}
/**
* Gets if it's a const enum.
*/
isConstEnum() {
return this.getConstKeyword() != null;
}
/**
* Gets the const enum keyword or undefined if not exists.
*/
getConstKeyword() {
return this.getFirstModifierByKind(ts.SyntaxKind.ConstKeyword);
}
/**
* Removes this enum declaration.
*/
remove() {
manipulation_1.removeStatementedNodeChild(this);
}
}
exports.EnumDeclaration = EnumDeclaration;
//# sourceMappingURL=EnumDeclaration.js.map