forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetAccessorDeclaration.ts
54 lines (49 loc) · 2.38 KB
/
GetAccessorDeclaration.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
import * as errors from "../../../errors";
import { GetAccessorDeclarationStructure, GetAccessorDeclarationSpecificStructure, StructureKind } from "../../../structures";
import { SyntaxKind, ts } from "../../../typescript";
import { BodyableNode, ChildOrderableNode, DecoratableNode, PropertyNamedNode, ScopedNode, StaticableNode, TextInsertableNode } from "../base";
import { callBaseSet } from "../callBaseSet";
import { FunctionLikeDeclaration } from "../function";
import { AbstractableNode } from "./base";
import { SetAccessorDeclaration } from "./SetAccessorDeclaration";
import { callBaseGetStructure } from "../callBaseGetStructure";
import { ClassElement } from "./ClassElement";
export const GetAccessorDeclarationBase = ChildOrderableNode(TextInsertableNode(DecoratableNode(AbstractableNode(ScopedNode(StaticableNode(
FunctionLikeDeclaration(BodyableNode(PropertyNamedNode(ClassElement)))
))))));
export class GetAccessorDeclaration extends GetAccessorDeclarationBase<ts.GetAccessorDeclaration> {
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<GetAccessorDeclarationStructure>) {
callBaseSet(GetAccessorDeclarationBase.prototype, this, structure);
return this;
}
/**
* Gets the corresponding set accessor if one exists.
*/
getSetAccessor(): SetAccessorDeclaration | undefined {
const parent = this.getParentIfKindOrThrow(SyntaxKind.ClassDeclaration);
const thisName = this.getName();
for (const prop of parent.getInstanceProperties()) {
if (prop.getName() === thisName && prop.getKind() === SyntaxKind.SetAccessor)
return prop as SetAccessorDeclaration;
}
return undefined;
}
/**
* Gets the corresponding set accessor or throws if not exists.
*/
getSetAccessorOrThrow(): SetAccessorDeclaration {
return errors.throwIfNullOrUndefined(this.getSetAccessor(), () => `Expected to find a corresponding set accessor for ${this.getName()}.`);
}
/**
* Gets the structure equivalent to this node.
*/
getStructure(): GetAccessorDeclarationStructure {
return callBaseGetStructure<GetAccessorDeclarationSpecificStructure>(GetAccessorDeclarationBase.prototype, this, {
kind: StructureKind.GetAccessor
}) as any as GetAccessorDeclarationStructure;
}
}