forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParameterDeclaration.ts
156 lines (135 loc) · 5.03 KB
/
ParameterDeclaration.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { insertIntoParentTextRange, removeChildren, removeCommaSeparatedChild } from "../../../manipulation";
import { ParameterDeclarationStructure, AbstractableNodeStructure, ParameterDeclarationSpecificStructure } from "../../../structures";
// import { ParameterDeclarationStructure } from "../../structures";
import { WriterFunction } from "../../../types";
import { ts, SyntaxKind } from "../../../typescript";
import { TypeGuards } from "../../../utils";
import { DeclarationNamedNode, DecoratableNode, InitializerExpressionableNode, ModifierableNode, QuestionTokenableNode, ReadonlyableNode, ScopeableNode, TypedNode } from "../base";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common/Node";
import { callBaseGetStructure } from "../callBaseGetStructure";
export const ParameterDeclarationBase = QuestionTokenableNode(DecoratableNode(ScopeableNode(ReadonlyableNode(ModifierableNode(
TypedNode(InitializerExpressionableNode(DeclarationNamedNode(Node)))
)))));
export class ParameterDeclaration extends ParameterDeclarationBase<ts.ParameterDeclaration> {
/**
* Gets the dot dot dot token (...) if it exists, for a rest parameter.
*/
getDotDotDotToken() {
return this._getNodeFromCompilerNodeIfExists(this.compilerNode.dotDotDotToken);
}
/**
* Gets if it's a rest parameter.
*/
isRestParameter() {
return this.compilerNode.dotDotDotToken != null;
}
/**
* Gets if this is a property with a scope or readonly keyword (found in class constructors).
*/
isParameterProperty() {
return this.getScope() != null || this.isReadonly();
}
/**
* Sets if it's a rest parameter.
* @param value - Sets if it's a rest parameter or not.
*/
setIsRestParameter(value: boolean) {
if (this.isRestParameter() === value)
return this;
if (value) {
addParensIfNecessary(this);
insertIntoParentTextRange({
insertPos: this.getNameNodeOrThrow().getStart(),
parent: this,
newText: "..."
});
}
else
removeChildren({ children: [this.getDotDotDotToken()!] });
return this;
}
/**
* Gets if it's optional.
*/
isOptional() {
return this.compilerNode.questionToken != null || this.isRestParameter() || this.hasInitializer();
}
/**
* Remove this parameter.
*/
remove() {
removeCommaSeparatedChild(this);
}
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<ParameterDeclarationStructure>) {
callBaseSet(ParameterDeclarationBase.prototype, this, structure);
if (structure.isRestParameter != null)
this.setIsRestParameter(structure.isRestParameter);
return this;
}
/**
* Gets the structure equivalent to this node.
*/
getStructure(): ParameterDeclarationStructure {
return callBaseGetStructure<ParameterDeclarationSpecificStructure>(ParameterDeclarationBase.prototype, this, {
isRestParameter: this.isRestParameter()
}) as any as ParameterDeclarationStructure;
}
// ------ Methods to override to add parens ------
/**
* Sets if this node has a question token.
* @param value - If it should have a question token or not.
*/
setHasQuestionToken(value: boolean) {
if (value)
addParensIfNecessary(this);
super.setHasQuestionToken(value);
return this;
}
/**
* Sets the initializer.
* @param text - Text or writer function to set for the initializer.
*/
setInitializer(textOrWriterFunction: string | WriterFunction) {
addParensIfNecessary(this);
super.setInitializer(textOrWriterFunction);
return this;
}
/**
* Sets the type.
* @param textOrWriterFunction - Text or writer function to set the type with.
*/
setType(textOrWriterFunction: string | WriterFunction) {
addParensIfNecessary(this);
super.setType.call(this, textOrWriterFunction);
return this;
}
}
function addParensIfNecessary(parameter: Node) {
const parent = parameter.getParentOrThrow();
if (isParameterWithoutParens())
addParens();
function isParameterWithoutParens() {
return TypeGuards.isArrowFunction(parent)
&& parent.compilerNode.parameters.length === 1
&& parameter.getParentSyntaxListOrThrow().getPreviousSiblingIfKind(SyntaxKind.OpenParenToken) == null;
}
function addParens() {
const paramText = parameter.getText();
insertIntoParentTextRange({
parent,
insertPos: parameter.getStart(),
newText: `(${paramText})`,
replacing: {
textLength: paramText.length
},
customMappings: newParent => {
return [{ currentNode: parameter, newNode: (newParent as ts.ArrowFunction).parameters[0] }];
}
});
}
}