forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnumMember.ts
74 lines (66 loc) · 2.46 KB
/
EnumMember.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
import { FormattingKind, removeChildrenWithFormatting } from "../../../manipulation";
import { EnumMemberStructure, EnumMemberSpecificStructure, EnumDeclarationStructure } from "../../../structures";
import { SyntaxKind, ts } from "../../../typescript";
import { StringUtils } from "../../../utils";
import { InitializerExpressionableNode, JSDocableNode, PropertyNamedNode } from "../base";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { callBaseGetStructure } from "../callBaseGetStructure";
export const EnumMemberBase = JSDocableNode(InitializerExpressionableNode(PropertyNamedNode(Node)));
export class EnumMember extends EnumMemberBase<ts.EnumMember> {
/**
* Gets the constant value of the enum.
*/
getValue() {
return this._context.typeChecker.getConstantValue(this);
}
/**
* Sets the enum value.
* @param value - Enum value.
*/
setValue(value: string | number) {
let text: string;
if (typeof value === "string") {
const quoteKind = this._context.manipulationSettings.getQuoteKind();
text = quoteKind + StringUtils.escapeForWithinString(value, quoteKind) + quoteKind;
}
else {
text = value.toString();
}
this.setInitializer(text);
return this;
}
/**
* Removes this enum member.
*/
remove() {
const childrenToRemove: Node[] = [this];
const commaToken = this.getNextSiblingIfKind(SyntaxKind.CommaToken);
if (commaToken != null)
childrenToRemove.push(commaToken);
removeChildrenWithFormatting({
children: childrenToRemove,
getSiblingFormatting: () => FormattingKind.Newline
});
}
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<EnumMemberStructure>) {
callBaseSet(EnumMemberBase.prototype, this, structure);
if (structure.value != null)
this.setValue(structure.value);
else if (structure.hasOwnProperty(nameof(structure.value)))
this.removeInitializer();
return this;
}
/**
* Gets the structure equivalent to this node.
*/
getStructure() {
return callBaseGetStructure<EnumMemberSpecificStructure>(EnumMemberBase.prototype, this, {
value: this.hasInitializer() ? this.getValue() : undefined
}) as EnumMemberStructure;
}
}