forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnumDeclaration.ts
158 lines (140 loc) · 5.61 KB
/
EnumDeclaration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import * as errors from "../../../errors";
import { getNodesToReturn, insertIntoCommaSeparatedNodes, verifyAndGetIndex } from "../../../manipulation";
import { EnumDeclarationStructure, EnumMemberStructure, EnumDeclarationSpecificStructure } from "../../../structures";
import { SyntaxKind, ts } from "../../../typescript";
import { getNodeByNameOrFindFunction, getNotFoundErrorMessageForNameOrFindFunction } from "../../../utils";
import { AmbientableNode, ChildOrderableNode, ExportableNode, JSDocableNode, ModifierableNode, NamedNode, TextInsertableNode } from "../base";
import { callBaseSet } from "../callBaseSet";
import { NamespaceChildableNode } from "../module";
import { Statement } from "../statement";
import { EnumMember } from "./EnumMember";
import { callBaseGetStructure } from "../callBaseGetStructure";
export const EnumDeclarationBase = ChildOrderableNode(TextInsertableNode(NamespaceChildableNode(JSDocableNode(AmbientableNode(ExportableNode(
ModifierableNode(NamedNode(Statement))
))))));
export class EnumDeclaration extends EnumDeclarationBase<ts.EnumDeclaration> {
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<EnumDeclarationStructure>) {
callBaseSet(EnumDeclarationBase.prototype, this, structure);
if (structure.isConst != null)
this.setIsConstEnum(structure.isConst);
if (structure.members != null) {
this.getMembers().forEach(m => m.remove());
this.addMembers(structure.members);
}
return this;
}
/**
* Adds a member to the enum.
* @param structure - Structure of the enum.
*/
addMember(structure: EnumMemberStructure) {
return this.addMembers([structure])[0];
}
/**
* Adds members to the enum.
* @param structures - Structures of the enums.
*/
addMembers(structures: ReadonlyArray<EnumMemberStructure>) {
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.
*/
insertMember(index: number, structure: EnumMemberStructure) {
return this.insertMembers(index, [structure])[0];
}
/**
* Inserts members to an enum.
* @param index - Child index to insert at.
* @param structures - Structures of the enums.
*/
insertMembers(index: number, structures: ReadonlyArray<EnumMemberStructure>) {
const members = this.getMembers();
index = verifyAndGetIndex(index, members.length);
if (structures.length === 0)
return [];
// create member code
// todo: pass in the StructureToText to the function below
const writer = this._getWriterWithChildIndentation();
const structurePrinter = this._context.structurePrinterFactory.forEnumMember();
structurePrinter.printTexts(writer, structures);
// insert
insertIntoCommaSeparatedNodes({
parent: this.getChildSyntaxListOrThrow(),
currentNodes: members,
insertIndex: index,
newText: writer.toString(),
useNewLines: true
});
// get the members
return getNodesToReturn(this.getMembers(), index, structures.length) as EnumMember[];
}
/**
* Gets an enum member.
* @param name - Name of the member.
*/
getMember(name: string): EnumMember | undefined;
/**
* Gets an enum member.
* @param findFunction - Function to use to find the member.
*/
getMember(findFunction: (declaration: EnumMember) => boolean): EnumMember | undefined;
/** @internal */
getMember(nameOrFindFunction: string | ((declaration: EnumMember) => boolean)): EnumMember | undefined;
getMember(nameOrFindFunction: string | ((declaration: EnumMember) => boolean)): EnumMember | undefined {
return getNodeByNameOrFindFunction(this.getMembers(), nameOrFindFunction);
}
/**
* Gets an enum member or throws if not found.
* @param name - Name of the member.
*/
getMemberOrThrow(name: string): EnumMember;
/**
* Gets an enum member or throws if not found.
* @param findFunction - Function to use to find the member.
*/
getMemberOrThrow(findFunction: (declaration: EnumMember) => boolean): EnumMember;
getMemberOrThrow(nameOrFindFunction: string | ((declaration: EnumMember) => boolean)): EnumMember {
return errors.throwIfNullOrUndefined(this.getMember(nameOrFindFunction),
() => getNotFoundErrorMessageForNameOrFindFunction("enum member", nameOrFindFunction));
}
/**
* Gets the enum's members.
*/
getMembers(): EnumMember[] {
return this.compilerNode.members.map(m => this._getNodeFromCompilerNode(m));
}
/**
* Toggle if it's a const enum.
*/
setIsConstEnum(value: boolean) {
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(SyntaxKind.ConstKeyword);
}
/**
* Gets the structure equivalent to this node.
*/
getStructure() {
return callBaseGetStructure<EnumDeclarationSpecificStructure>(EnumDeclarationBase.prototype, this, {
isConst: this.isConstEnum(),
members: this.getMembers().map(member => member.getStructure())
}) as EnumDeclarationStructure;
}
}