forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetAccessorDeclaration.ts
61 lines (55 loc) · 2.46 KB
/
SetAccessorDeclaration.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
import * as errors from "../../../errors";
import { removeClassMember } from "../../../manipulation";
import { SetAccessorDeclarationStructure, SetAccessorDeclarationSpecificStructure } from "../../../structures";
import { SyntaxKind, ts } from "../../../typescript";
import { BodyableNode, ChildOrderableNode, DecoratableNode, PropertyNamedNode, ScopedNode, StaticableNode, TextInsertableNode } from "../base";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { FunctionLikeDeclaration } from "../function";
import { AbstractableNode } from "./base";
import { GetAccessorDeclaration } from "./GetAccessorDeclaration";
import { callBaseGetStructure } from "../callBaseGetStructure";
export const SetAccessorDeclarationBase = ChildOrderableNode(TextInsertableNode(DecoratableNode(AbstractableNode(ScopedNode(StaticableNode(
FunctionLikeDeclaration(BodyableNode(PropertyNamedNode(Node))
)))))));
export class SetAccessorDeclaration extends SetAccessorDeclarationBase<ts.SetAccessorDeclaration> {
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<SetAccessorDeclarationStructure>) {
callBaseSet(SetAccessorDeclarationBase.prototype, this, structure);
return this;
}
/**
* Gets the corresponding get accessor if one exists.
*/
getGetAccessor(): GetAccessorDeclaration | undefined {
const parent = this.getParentIfKindOrThrow(SyntaxKind.ClassDeclaration);
const thisName = this.getName();
for (const prop of parent.getInstanceProperties()) {
if (prop.getKind() === SyntaxKind.GetAccessor && prop.getName() === thisName)
return prop as GetAccessorDeclaration;
}
return undefined;
}
/**
* Gets the corresponding get accessor or throws if not exists.
*/
getGetAccessorOrThrow(): GetAccessorDeclaration {
return errors.throwIfNullOrUndefined(this.getGetAccessor(), () => `Expected to find a corresponding get accessor for ${this.getName()}.`);
}
/**
* Removes the set accessor.
*/
remove() {
removeClassMember(this);
}
/**
* Gets the structure equivalent to this node.
*/
getStructure(): SetAccessorDeclarationStructure {
return callBaseGetStructure<SetAccessorDeclarationSpecificStructure>(SetAccessorDeclarationBase.prototype, this, {
}) as any as SetAccessorDeclarationStructure;
}
}