/
methods.ts
217 lines (192 loc) 路 4.88 KB
/
methods.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import type Printer from "../printer";
import { isIdentifier } from "@babel/types";
import type * as t from "@babel/types";
export function _params(
this: Printer,
node: t.Function | t.TSDeclareMethod | t.TSDeclareFunction,
) {
this.print(node.typeParameters, node);
this.token("(");
this._parameters(node.params, node);
this.token(")");
this._noLineTerminator = true;
this.print(node.returnType, node, node.type === "ArrowFunctionExpression");
}
export function _parameters(
this: Printer,
parameters: t.Function["params"],
parent:
| t.Function
| t.TSIndexSignature
| t.TSDeclareMethod
| t.TSDeclareFunction
| t.TSFunctionType
| t.TSConstructorType,
) {
const paramLength = parameters.length;
for (let i = 0; i < paramLength; i++) {
this._param(parameters[i], parent);
if (i < parameters.length - 1) {
this.token(",");
this.space();
}
}
}
export function _param(
this: Printer,
parameter: t.Identifier | t.RestElement | t.Pattern | t.TSParameterProperty,
parent?:
| t.Function
| t.TSIndexSignature
| t.TSDeclareMethod
| t.TSDeclareFunction
| t.TSFunctionType
| t.TSConstructorType,
) {
this.printJoin(parameter.decorators, parameter);
this.print(parameter, parent);
if (
// @ts-expect-error optional is not in TSParameterProperty
parameter.optional
) {
this.token("?"); // TS / flow
}
this.print(
// @ts-expect-error typeAnnotation is not in TSParameterProperty
parameter.typeAnnotation,
parameter,
); // TS / flow
}
export function _methodHead(this: Printer, node: t.Method | t.TSDeclareMethod) {
const kind = node.kind;
const key = node.key;
if (kind === "get" || kind === "set") {
this.word(kind);
this.space();
}
if (node.async) {
this.word("async", true);
this.space();
}
if (
kind === "method" ||
// @ts-expect-error Fixme: kind: "init" is not defined
kind === "init"
) {
if (node.generator) {
this.token("*");
}
}
if (node.computed) {
this.token("[");
this.print(key, node);
this.token("]");
} else {
this.print(key, node);
}
if (
// @ts-expect-error optional is not in ObjectMethod
node.optional
) {
// TS
this.token("?");
}
this._params(node);
}
export function _predicate(
this: Printer,
node:
| t.FunctionDeclaration
| t.FunctionExpression
| t.ArrowFunctionExpression,
noLineTerminatorAfter?: boolean,
) {
if (node.predicate) {
if (!node.returnType) {
this.token(":");
}
this.space();
this.print(node.predicate, node, noLineTerminatorAfter);
}
}
export function _functionHead(
this: Printer,
node: t.FunctionDeclaration | t.FunctionExpression | t.TSDeclareFunction,
) {
if (node.async) {
this.word("async");
// We prevent inner comments from being printed here,
// so that they are always consistently printed in the
// same place regardless of the function type.
this._endsWithInnerRaw = false;
this.space();
}
this.word("function");
if (node.generator) {
// We prevent inner comments from being printed here,
// so that they are always consistently printed in the
// same place regardless of the function type.
this._endsWithInnerRaw = false;
this.token("*");
}
this.space();
if (node.id) {
this.print(node.id, node);
}
this._params(node);
if (node.type !== "TSDeclareFunction") {
this._predicate(node);
}
}
export function FunctionExpression(this: Printer, node: t.FunctionExpression) {
this._functionHead(node);
this.space();
this.print(node.body, node);
}
export { FunctionExpression as FunctionDeclaration };
export function ArrowFunctionExpression(
this: Printer,
node: t.ArrowFunctionExpression,
) {
if (node.async) {
this.word("async", true);
this.space();
}
// Try to avoid printing parens in simple cases, but only if we're pretty
// sure that they aren't needed by type annotations or potential newlines.
let firstParam;
if (
!this.format.retainLines &&
node.params.length === 1 &&
isIdentifier((firstParam = node.params[0])) &&
!hasTypesOrComments(node, firstParam)
) {
this.print(firstParam, node, true);
} else {
this._params(node);
}
this._predicate(node, true);
this.space();
// When printing (x)/*1*/=>{}, we remove the parentheses
// and thus there aren't two contiguous inner tokens.
// We forcefully print inner comments here.
this.printInnerComments();
this.token("=>");
this.space();
this.print(node.body, node);
}
function hasTypesOrComments(
node: t.ArrowFunctionExpression,
param: t.Identifier,
): boolean {
return !!(
node.typeParameters ||
node.returnType ||
node.predicate ||
param.typeAnnotation ||
param.optional ||
// Flow does not support `foo /*: string*/ => {};`
param.leadingComments?.length ||
param.trailingComments?.length
);
}