forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexSignatureDeclaration.ts
105 lines (90 loc) · 3.24 KB
/
IndexSignatureDeclaration.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
import { removeInterfaceMember } from "../../../manipulation";
import { IndexSignatureDeclarationStructure, IndexSignatureDeclarationSpecificStructure } from "../../../structures";
import * as errors from "../../../errors";
import { ts } from "../../../typescript";
import { Type } from "../../types";
import { ChildOrderableNode, JSDocableNode, ModifierableNode, ReadonlyableNode, ReturnTypedNode } from "../base";
import { callBaseSet } from "../callBaseSet";
import { TypeNode } from "../type";
import { TypeElement } from "./TypeElement";
import { callBaseGetStructure } from "../callBaseGetStructure";
export const IndexSignatureDeclarationBase = ReturnTypedNode(ChildOrderableNode(JSDocableNode(ReadonlyableNode(ModifierableNode(TypeElement)))));
export class IndexSignatureDeclaration extends IndexSignatureDeclarationBase<ts.IndexSignatureDeclaration> {
/**
* Gets the key name.
*/
getKeyName() {
return this.getKeyNameNode().getText();
}
/**
* Sets the key name.
* @param name - New name.
*/
setKeyName(name: string) {
errors.throwIfWhitespaceOrNotString(name, nameof(name));
if (this.getKeyName() === name)
return;
this.getKeyNameNode().replaceWithText(name, this._getWriterWithQueuedChildIndentation());
}
/**
* Gets the key name node.
*/
getKeyNameNode() {
const param = this.compilerNode.parameters[0];
return this._getNodeFromCompilerNode(param.name);
}
/**
* Gets the key type.
*/
getKeyType(): Type {
return this.getKeyNameNode().getType();
}
/**
* Sets the key type.
* @param type - Type.
*/
setKeyType(type: string) {
errors.throwIfWhitespaceOrNotString(type, nameof(type));
const keyTypeNode = this.getKeyTypeNode();
if (keyTypeNode.getText() === type)
return this;
keyTypeNode.replaceWithText(type, this._getWriterWithQueuedChildIndentation());
return this;
}
/**
* Gets the key type node.
*/
getKeyTypeNode(): TypeNode {
// this node must exist for it to be an index signature (otherwise it's a property signature)
const param = this.compilerNode.parameters[0];
return this._getNodeFromCompilerNode(param.type!);
}
/**
* Removes this index signature.
*/
remove() {
removeInterfaceMember(this);
}
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<IndexSignatureDeclarationStructure>) {
callBaseSet(IndexSignatureDeclarationBase.prototype, this, structure);
if (structure.keyName != null)
this.setKeyName(structure.keyName);
if (structure.keyType != null)
this.setKeyType(structure.keyType);
return this;
}
/**
* Gets the structure equivalent to this node.
*/
getStructure(): IndexSignatureDeclarationStructure {
const keyTypeNode = this.getKeyTypeNode();
return callBaseGetStructure<IndexSignatureDeclarationSpecificStructure>(IndexSignatureDeclarationBase.prototype, this, {
keyName: this.getKeyName(),
keyType: keyTypeNode.getText()
});
}
}