forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMethodDeclaration.ts
120 lines (106 loc) · 5.18 KB
/
MethodDeclaration.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
import * as getStructureFuncs from "../../../manipulation/helpers/getStructureFunctions";
import { MethodDeclarationOverloadStructure, MethodDeclarationStructure, MethodDeclarationSpecificStructure, StructureKind, OptionalKind,
MethodDeclarationOverloadSpecificStructure } from "../../../structures";
import { SyntaxKind, ts } from "../../../typescript";
import { isNodeAmbientOrInAmbientContext } from "../../../utils";
import { AsyncableNode, BodyableNode, ChildOrderableNode, DecoratableNode, GeneratorableNode, PropertyNamedNode, ScopedNode, StaticableNode,
TextInsertableNode, SignaturedDeclaration, ModifierableNode, JSDocableNode, TypeParameteredNode, QuestionTokenableNode } from "../base";
import { callBaseSet } from "../callBaseSet";
import { FunctionLikeDeclaration, insertOverloads, OverloadableNode } from "../function";
import { AbstractableNode } from "./base";
import { callBaseGetStructure } from "../callBaseGetStructure";
import { ClassElement } from "./ClassElement";
export const MethodDeclarationBase = ChildOrderableNode(TextInsertableNode(OverloadableNode(BodyableNode(DecoratableNode(AbstractableNode(ScopedNode(
QuestionTokenableNode(StaticableNode(AsyncableNode(GeneratorableNode(FunctionLikeDeclaration(PropertyNamedNode(ClassElement))))))
)))))));
export const MethodDeclarationOverloadBase = JSDocableNode(ChildOrderableNode(TextInsertableNode(ScopedNode(TypeParameteredNode(AbstractableNode(
QuestionTokenableNode(StaticableNode(AsyncableNode(ModifierableNode(GeneratorableNode(SignaturedDeclaration(ClassElement))))))
))))));
export class MethodDeclaration extends MethodDeclarationBase<ts.MethodDeclaration> {
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<MethodDeclarationStructure>) {
callBaseSet(MethodDeclarationBase.prototype, this, structure);
if (structure.overloads != null) {
this.getOverloads().forEach(o => o.remove());
this.addOverloads(structure.overloads);
}
return this;
}
/**
* Add a method overload.
* @param structure - Structure to add.
*/
addOverload(structure: OptionalKind<MethodDeclarationOverloadStructure>) {
return this.addOverloads([structure])[0];
}
/**
* Add method overloads.
* @param structures - Structures to add.
*/
addOverloads(structures: ReadonlyArray<OptionalKind<MethodDeclarationOverloadStructure>>) {
return this.insertOverloads(this.getOverloads().length, structures);
}
/**
* Inserts a method overload.
* @param index - Child index to insert at.
* @param structure - Structures to insert.
*/
insertOverload(index: number, structure: OptionalKind<MethodDeclarationOverloadStructure>) {
return this.insertOverloads(index, [structure])[0];
}
/**
* Inserts method overloads.
* @param index - Child index to insert at.
* @param structures - Structures to insert.
*/
insertOverloads(index: number, structures: ReadonlyArray<OptionalKind<MethodDeclarationOverloadStructure>>) {
const thisName = this.getName();
const printer = this._context.structurePrinterFactory.forMethodDeclaration({
isAmbient: isNodeAmbientOrInAmbientContext(this)
});
return insertOverloads<MethodDeclaration, OptionalKind<MethodDeclarationOverloadStructure>>({
node: this,
index,
structures,
printStructure: (writer, structure) => {
printer.printOverload(writer, thisName, structure);
},
getThisStructure: getStructureFuncs.fromMethodDeclarationOverload,
expectedSyntaxKind: SyntaxKind.MethodDeclaration
});
}
/**
* Gets the structure equivalent to this node.
*/
getStructure(): MethodDeclarationStructure | MethodDeclarationOverloadStructure {
const hasImplementation = this.getImplementation() != null;
const isOverload = this.isOverload();
const basePrototype = isOverload && hasImplementation ? MethodDeclarationOverloadBase.prototype : MethodDeclarationBase.prototype;
return callBaseGetStructure<any>(
basePrototype,
this,
getStructure(this)
) as any as MethodDeclarationStructure | MethodDeclarationOverloadStructure;
function getStructure(thisNode: MethodDeclaration) {
if (hasImplementation && isOverload)
return getOverloadSpecificStructure();
return getSpecificStructure();
function getOverloadSpecificStructure(): MethodDeclarationOverloadSpecificStructure {
return { kind: StructureKind.MethodOverload };
}
function getSpecificStructure(): MethodDeclarationSpecificStructure {
if (!hasImplementation)
return { kind: StructureKind.Method };
else {
return {
kind: StructureKind.Method,
overloads: thisNode.getOverloads().map(o => o.getStructure() as MethodDeclarationOverloadStructure)
};
}
}
}
}
}