Debian Codegraph @

packages/babel-generator/src/generators/methods.ts

raw symbols SCIP
1 import type Printer from "../printer";
2 import { isIdentifier } from "@babel/types";
3 import type * as t from "@babel/types";
4
5 export function _params(
6 this: Printer,
7 node: t.Function | t.TSDeclareMethod | t.TSDeclareFunction,
8 ) {
9 this.print(node.typeParameters, node);
10 this.token("(");
11 this._parameters(node.params, node);
12 this.token(")");
13
14 const noLineTerminator = node.type === "ArrowFunctionExpression";
15 this.print(node.returnType, node, noLineTerminator);
16
17 this._noLineTerminator = noLineTerminator;
18 }
19
20 export function _parameters(
21 this: Printer,
22 parameters: t.Function["params"],
23 parent:
24 | t.Function
25 | t.TSIndexSignature
26 | t.TSDeclareMethod
27 | t.TSDeclareFunction
28 | t.TSFunctionType
29 | t.TSConstructorType,
30 ) {
31 const paramLength = parameters.length;
32 for (let i = 0; i < paramLength; i++) {
33 this._param(parameters[i], parent);
34
35 if (i < parameters.length - 1) {
36 this.token(",");
37 this.space();
38 }
39 }
40 }
41
42 export function _param(
43 this: Printer,
44 parameter: t.Identifier | t.RestElement | t.Pattern | t.TSParameterProperty,
45 parent?:
46 | t.Function
47 | t.TSIndexSignature
48 | t.TSDeclareMethod
49 | t.TSDeclareFunction
50 | t.TSFunctionType
51 | t.TSConstructorType,
52 ) {
53 this.printJoin(parameter.decorators, parameter);
54 this.print(parameter, parent);
55 if (
56 // @ts-expect-error optional is not in TSParameterProperty
57 parameter.optional
58 ) {
59 this.token("?"); // TS / flow
60 }
61
62 this.print(
63 // @ts-expect-error typeAnnotation is not in TSParameterProperty
64 parameter.typeAnnotation,
65 parameter,
66 ); // TS / flow
67 }
68
69 export function _methodHead(this: Printer, node: t.Method | t.TSDeclareMethod) {
70 const kind = node.kind;
71 const key = node.key;
72
73 if (kind === "get" || kind === "set") {
74 this.word(kind);
75 this.space();
76 }
77
78 if (node.async) {
79 this.word("async", true);
80 this.space();
81 }
82
83 if (
84 kind === "method" ||
85 // @ts-expect-error Fixme: kind: "init" is not defined
86 kind === "init"
87 ) {
88 if (node.generator) {
89 this.token("*");
90 }
91 }
92
93 if (node.computed) {
94 this.token("[");
95 this.print(key, node);
96 this.token("]");
97 } else {
98 this.print(key, node);
99 }
100
101 if (
102 // @ts-expect-error optional is not in ObjectMethod
103 node.optional
104 ) {
105 // TS
106 this.token("?");
107 }
108
109 this._params(node);
110 }
111
112 export function _predicate(
113 this: Printer,
114 node:
115 | t.FunctionDeclaration
116 | t.FunctionExpression
117 | t.ArrowFunctionExpression,
118 noLineTerminatorAfter?: boolean,
119 ) {
120 if (node.predicate) {
121 if (!node.returnType) {
122 this.token(":");
123 }
124 this.space();
125 this.print(node.predicate, node, noLineTerminatorAfter);
126 }
127 }
128
129 export function _functionHead(
130 this: Printer,
131 node: t.FunctionDeclaration | t.FunctionExpression | t.TSDeclareFunction,
132 ) {
133 if (node.async) {
134 this.word("async");
135 // We prevent inner comments from being printed here,
136 // so that they are always consistently printed in the
137 // same place regardless of the function type.
138 this._endsWithInnerRaw = false;
139 this.space();
140 }
141 this.word("function");
142 if (node.generator) {
143 // We prevent inner comments from being printed here,
144 // so that they are always consistently printed in the
145 // same place regardless of the function type.
146 this._endsWithInnerRaw = false;
147 this.token("*");
148 }
149
150 this.space();
151 if (node.id) {
152 this.print(node.id, node);
153 }
154
155 this._params(node);
156 if (node.type !== "TSDeclareFunction") {
157 this._predicate(node);
158 }
159 }
160
161 export function FunctionExpression(this: Printer, node: t.FunctionExpression) {
162 this._functionHead(node);
163 this.space();
164 this.print(node.body, node);
165 }
166
167 export { FunctionExpression as FunctionDeclaration };
168
169 export function ArrowFunctionExpression(
170 this: Printer,
171 node: t.ArrowFunctionExpression,
172 ) {
173 if (node.async) {
174 this.word("async", true);
175 this.space();
176 }
177
178 // Try to avoid printing parens in simple cases, but only if we're pretty
179 // sure that they aren't needed by type annotations or potential newlines.
180 let firstParam;
181 if (
182 !this.format.retainLines &&
183 node.params.length === 1 &&
184 isIdentifier((firstParam = node.params[0])) &&
185 !hasTypesOrComments(node, firstParam)
186 ) {
187 this.print(firstParam, node, true);
188 } else {
189 this._params(node);
190 }
191
192 this._predicate(node, true);
193 this.space();
194 // When printing (x)/*1*/=>{}, we remove the parentheses
195 // and thus there aren't two contiguous inner tokens.
196 // We forcefully print inner comments here.
197 this.printInnerComments();
198 this.token("=>");
199
200 this.space();
201
202 this.print(node.body, node);
203 }
204
205 function hasTypesOrComments(
206 node: t.ArrowFunctionExpression,
207 param: t.Identifier,
208 ): boolean {
209 return !!(
210 node.typeParameters ||
211 node.returnType ||
212 node.predicate ||
213 param.typeAnnotation ||
214 param.optional ||
215 // Flow does not support `foo /*: string*/ => {};`
216 param.leadingComments?.length ||
217 param.trailingComments?.length
218 );
219 }