Debian Codegraph @

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

raw symbols SCIP
1 import type Printer from "../printer";
2 import {
3 isFor,
4 isForStatement,
5 isIfStatement,
6 isStatement,
7 } from "@babel/types";
8 import type * as t from "@babel/types";
9 import * as charCodes from "charcodes";
10
11 export function WithStatement(this: Printer, node: t.WithStatement) {
12 this.word("with");
13 this.space();
14 this.token("(");
15 this.print(node.object, node);
16 this.token(")");
17 this.printBlock(node);
18 }
19
20 export function IfStatement(this: Printer, node: t.IfStatement) {
21 this.word("if");
22 this.space();
23 this.token("(");
24 this.print(node.test, node);
25 this.token(")");
26 this.space();
27
28 const needsBlock =
29 node.alternate && isIfStatement(getLastStatement(node.consequent));
30 if (needsBlock) {
31 this.token("{");
32 this.newline();
33 this.indent();
34 }
35
36 this.printAndIndentOnComments(node.consequent, node);
37
38 if (needsBlock) {
39 this.dedent();
40 this.newline();
41 this.token("}");
42 }
43
44 if (node.alternate) {
45 if (this.endsWith(charCodes.rightCurlyBrace)) this.space();
46 this.word("else");
47 this.space();
48 this.printAndIndentOnComments(node.alternate, node);
49 }
50 }
51
52 // Recursively get the last statement.
53 function getLastStatement(statement: t.Statement): t.Statement {
54 // @ts-expect-error: If statement.body is empty or not a Node, isStatement will return false
55 const { body } = statement;
56 if (isStatement(body) === false) {
57 return statement;
58 }
59
60 return getLastStatement(body);
61 }
62
63 export function ForStatement(this: Printer, node: t.ForStatement) {
64 this.word("for");
65 this.space();
66 this.token("(");
67
68 this.inForStatementInitCounter++;
69 this.print(node.init, node);
70 this.inForStatementInitCounter--;
71 this.token(";");
72
73 if (node.test) {
74 this.space();
75 this.print(node.test, node);
76 }
77 this.token(";");
78
79 if (node.update) {
80 this.space();
81 this.print(node.update, node);
82 }
83
84 this.token(")");
85 this.printBlock(node);
86 }
87
88 export function WhileStatement(this: Printer, node: t.WhileStatement) {
89 this.word("while");
90 this.space();
91 this.token("(");
92 this.print(node.test, node);
93 this.token(")");
94 this.printBlock(node);
95 }
96
97 function ForXStatement(this: Printer, node: t.ForXStatement) {
98 this.word("for");
99 this.space();
100 const isForOf = node.type === "ForOfStatement";
101 if (isForOf && node.await) {
102 this.word("await");
103 this.space();
104 }
105 this.noIndentInnerCommentsHere();
106 this.token("(");
107 this.print(node.left, node);
108 this.space();
109 this.word(isForOf ? "of" : "in");
110 this.space();
111 this.print(node.right, node);
112 this.token(")");
113 this.printBlock(node);
114 }
115
116 export const ForInStatement = ForXStatement;
117 export const ForOfStatement = ForXStatement;
118
119 export function DoWhileStatement(this: Printer, node: t.DoWhileStatement) {
120 this.word("do");
121 this.space();
122 this.print(node.body, node);
123 this.space();
124 this.word("while");
125 this.space();
126 this.token("(");
127 this.print(node.test, node);
128 this.token(")");
129 this.semicolon();
130 }
131
132 function printStatementAfterKeyword(
133 printer: Printer,
134 node: t.Node,
135 parent: t.Node,
136 isLabel: boolean,
137 ) {
138 if (node) {
139 printer.space();
140 printer.printTerminatorless(node, parent, isLabel);
141 }
142
143 printer.semicolon();
144 }
145
146 export function BreakStatement(this: Printer, node: t.ContinueStatement) {
147 this.word("break");
148 printStatementAfterKeyword(this, node.label, node, true);
149 }
150
151 export function ContinueStatement(this: Printer, node: t.ContinueStatement) {
152 this.word("continue");
153 printStatementAfterKeyword(this, node.label, node, true);
154 }
155
156 export function ReturnStatement(this: Printer, node: t.ReturnStatement) {
157 this.word("return");
158 printStatementAfterKeyword(this, node.argument, node, false);
159 }
160
161 export function ThrowStatement(this: Printer, node: t.ThrowStatement) {
162 this.word("throw");
163 printStatementAfterKeyword(this, node.argument, node, false);
164 }
165
166 export function LabeledStatement(this: Printer, node: t.LabeledStatement) {
167 this.print(node.label, node);
168 this.token(":");
169 this.space();
170 this.print(node.body, node);
171 }
172
173 export function TryStatement(this: Printer, node: t.TryStatement) {
174 this.word("try");
175 this.space();
176 this.print(node.block, node);
177 this.space();
178
179 // Esprima bug puts the catch clause in a `handlers` array.
180 // see https://code.google.com/p/esprima/issues/detail?id=433
181 // We run into this from regenerator generated ast.
182 // @ts-expect-error todo(flow->ts) should ast node type be updated to support this?
183 if (node.handlers) {
184 // @ts-expect-error todo(flow->ts) should ast node type be updated to support this?
185 this.print(node.handlers[0], node);
186 } else {
187 this.print(node.handler, node);
188 }
189
190 if (node.finalizer) {
191 this.space();
192 this.word("finally");
193 this.space();
194 this.print(node.finalizer, node);
195 }
196 }
197
198 export function CatchClause(this: Printer, node: t.CatchClause) {
199 this.word("catch");
200 this.space();
201 if (node.param) {
202 this.token("(");
203 this.print(node.param, node);
204 this.print(node.param.typeAnnotation, node);
205 this.token(")");
206 this.space();
207 }
208 this.print(node.body, node);
209 }
210
211 export function SwitchStatement(this: Printer, node: t.SwitchStatement) {
212 this.word("switch");
213 this.space();
214 this.token("(");
215 this.print(node.discriminant, node);
216 this.token(")");
217 this.space();
218 this.token("{");
219
220 this.printSequence(node.cases, node, {
221 indent: true,
222 addNewlines(leading, cas) {
223 if (!leading && node.cases[node.cases.length - 1] === cas) return -1;
224 },
225 });
226
227 this.token("}");
228 }
229
230 export function SwitchCase(this: Printer, node: t.SwitchCase) {
231 if (node.test) {
232 this.word("case");
233 this.space();
234 this.print(node.test, node);
235 this.token(":");
236 } else {
237 this.word("default");
238 this.token(":");
239 }
240
241 if (node.consequent.length) {
242 this.newline();
243 this.printSequence(node.consequent, node, { indent: true });
244 }
245 }
246
247 export function DebuggerStatement(this: Printer) {
248 this.word("debugger");
249 this.semicolon();
250 }
251
252 export function VariableDeclaration(
253 this: Printer,
254 node: t.VariableDeclaration,
255 parent: t.Node,
256 ) {
257 if (node.declare) {
258 // TS
259 this.word("declare");
260 this.space();
261 }
262
263 const { kind } = node;
264 this.word(kind, kind === "using");
265 this.space();
266
267 let hasInits = false;
268 // don't add whitespace to loop heads
269 if (!isFor(parent)) {
270 for (const declar of node.declarations) {
271 if (declar.init) {
272 // has an init so let's split it up over multiple lines
273 hasInits = true;
274 }
275 }
276 }
277
278 //
279 // use a pretty separator when we aren't in compact mode, have initializers and don't have retainLines on
280 // this will format declarations like:
281 //
282 // let foo = "bar", bar = "foo";
283 //
284 // into
285 //
286 // let foo = "bar",
287 // bar = "foo";
288 //
289
290 this.printList(node.declarations, node, {
291 separator: hasInits
292 ? function (this: Printer) {
293 this.token(",");
294 this.newline();
295 }
296 : undefined,
297 indent: node.declarations.length > 1 ? true : false,
298 });
299
300 if (isFor(parent)) {
301 // don't give semicolons to these nodes since they'll be inserted in the parent generator
302 if (isForStatement(parent)) {
303 if (parent.init === node) return;
304 } else {
305 if (parent.left === node) return;
306 }
307 }
308
309 this.semicolon();
310 }
311
312 export function VariableDeclarator(this: Printer, node: t.VariableDeclarator) {
313 this.print(node.id, node);
314 if (node.definite) this.token("!"); // TS
315 // @ts-expect-error todo(flow-ts) Property 'typeAnnotation' does not exist on type 'MemberExpression'.
316 this.print(node.id.typeAnnotation, node);
317 if (node.init) {
318 this.space();
319 this.token("=");
320 this.space();
321 this.print(node.init, node);
322 }
323 }