forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionDeclaration.ts
124 lines (109 loc) · 5.25 KB
/
FunctionDeclaration.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
121
122
123
124
import { removeOverloadableStatementedNodeChild } from "../../../manipulation";
import * as getStructureFuncs from "../../../manipulation/helpers/getStructureFunctions";
import { FunctionDeclarationOverloadStructure, FunctionDeclarationOverloadSpecificStructure, FunctionDeclarationStructure,
FunctionDeclarationSpecificStructure, StructureKind, OptionalKind } from "../../../structures";
import { SyntaxKind, ts } from "../../../typescript";
import { AmbientableNode, AsyncableNode, BodyableNode, ExportableNode, GeneratorableNode, ModifierableNode, NameableNode, TextInsertableNode, UnwrappableNode,
SignaturedDeclaration, TypeParameteredNode, JSDocableNode } from "../base";
import { callBaseSet } from "../callBaseSet";
import { Statement } from "../statement";
import { NamespaceChildableNode } from "../module";
import { FunctionLikeDeclaration } from "./FunctionLikeDeclaration";
import { insertOverloads, OverloadableNode } from "./OverloadableNode";
import { callBaseGetStructure } from "../callBaseGetStructure";
export const FunctionDeclarationBase = UnwrappableNode(TextInsertableNode(OverloadableNode(BodyableNode(AsyncableNode(GeneratorableNode(
AmbientableNode(ExportableNode(FunctionLikeDeclaration(NamespaceChildableNode(NameableNode(Statement)))))
))))));
export const FunctionDeclarationOverloadBase = UnwrappableNode(TextInsertableNode(AsyncableNode(GeneratorableNode(ModifierableNode(
SignaturedDeclaration(AmbientableNode(NamespaceChildableNode(JSDocableNode(TypeParameteredNode(ExportableNode(ModifierableNode(Statement)))))))
)))));
export class FunctionDeclaration extends FunctionDeclarationBase<ts.FunctionDeclaration> {
/**
* Adds a function overload.
* @param structure - Structure of the overload.
*/
addOverload(structure: OptionalKind<FunctionDeclarationOverloadStructure>) {
return this.addOverloads([structure])[0];
}
/**
* Adds function overloads.
* @param structures - Structures of the overloads.
*/
addOverloads(structures: ReadonlyArray<OptionalKind<FunctionDeclarationOverloadStructure>>) {
return this.insertOverloads(this.getOverloads().length, structures);
}
/**
* Inserts a function overload.
* @param index - Child index to insert at.
* @param structure - Structure of the overload.
*/
insertOverload(index: number, structure: OptionalKind<FunctionDeclarationOverloadStructure>) {
return this.insertOverloads(index, [structure])[0];
}
/**
* Inserts function overloads.
* @param index - Child index to insert at.
* @param structure - Structures of the overloads.
*/
insertOverloads(index: number, structures: ReadonlyArray<OptionalKind<FunctionDeclarationOverloadStructure>>) {
const thisName = this.getName();
const printer = this._context.structurePrinterFactory.forFunctionDeclaration({
isAmbient: this.isAmbient()
});
return insertOverloads<FunctionDeclaration, OptionalKind<FunctionDeclarationOverloadStructure>>({
node: this,
index,
structures,
printStructure: (writer, structure) => {
printer.printOverload(writer, thisName, structure);
},
getThisStructure: getStructureFuncs.fromFunctionDeclarationOverload,
expectedSyntaxKind: SyntaxKind.FunctionDeclaration
});
}
/**
* Removes this function declaration.
*/
remove() {
removeOverloadableStatementedNodeChild(this);
}
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<FunctionDeclarationStructure>) {
callBaseSet(FunctionDeclarationBase.prototype, this, structure);
if (structure.overloads != null) {
this.getOverloads().forEach(o => o.remove());
this.addOverloads(structure.overloads);
}
return this;
}
/**
* Gets the structure equivalent to this node.
*/
getStructure(): FunctionDeclarationStructure | FunctionDeclarationOverloadStructure {
const isOverload = this.isOverload();
const hasImplementation = this.getImplementation();
const basePrototype = isOverload && hasImplementation ? FunctionDeclarationOverloadBase.prototype : FunctionDeclarationBase.prototype;
return callBaseGetStructure<any>(basePrototype, this, getStructure(this));
function getStructure(thisNode: FunctionDeclaration) {
if (hasImplementation && isOverload)
return getOverloadSpecificStructure();
return getSpecificStructure();
function getOverloadSpecificStructure(): FunctionDeclarationOverloadSpecificStructure {
return { kind: StructureKind.FunctionOverload };
}
function getSpecificStructure(): FunctionDeclarationSpecificStructure {
if (!hasImplementation)
return { kind: StructureKind.Function };
else {
return {
kind: StructureKind.Function,
overloads: thisNode.getOverloads().map(o => o.getStructure() as FunctionDeclarationOverloadStructure)
};
}
}
}
}
}