source: trip-planner-front/node_modules/@babel/generator/lib/generators/statements.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 6.7 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.WithStatement = WithStatement;
7exports.IfStatement = IfStatement;
8exports.ForStatement = ForStatement;
9exports.WhileStatement = WhileStatement;
10exports.DoWhileStatement = DoWhileStatement;
11exports.LabeledStatement = LabeledStatement;
12exports.TryStatement = TryStatement;
13exports.CatchClause = CatchClause;
14exports.SwitchStatement = SwitchStatement;
15exports.SwitchCase = SwitchCase;
16exports.DebuggerStatement = DebuggerStatement;
17exports.VariableDeclaration = VariableDeclaration;
18exports.VariableDeclarator = VariableDeclarator;
19exports.ThrowStatement = exports.BreakStatement = exports.ReturnStatement = exports.ContinueStatement = exports.ForOfStatement = exports.ForInStatement = void 0;
20
21var t = require("@babel/types");
22
23function WithStatement(node) {
24 this.word("with");
25 this.space();
26 this.token("(");
27 this.print(node.object, node);
28 this.token(")");
29 this.printBlock(node);
30}
31
32function IfStatement(node) {
33 this.word("if");
34 this.space();
35 this.token("(");
36 this.print(node.test, node);
37 this.token(")");
38 this.space();
39 const needsBlock = node.alternate && t.isIfStatement(getLastStatement(node.consequent));
40
41 if (needsBlock) {
42 this.token("{");
43 this.newline();
44 this.indent();
45 }
46
47 this.printAndIndentOnComments(node.consequent, node);
48
49 if (needsBlock) {
50 this.dedent();
51 this.newline();
52 this.token("}");
53 }
54
55 if (node.alternate) {
56 if (this.endsWith("}")) this.space();
57 this.word("else");
58 this.space();
59 this.printAndIndentOnComments(node.alternate, node);
60 }
61}
62
63function getLastStatement(statement) {
64 if (!t.isStatement(statement.body)) return statement;
65 return getLastStatement(statement.body);
66}
67
68function ForStatement(node) {
69 this.word("for");
70 this.space();
71 this.token("(");
72 this.inForStatementInitCounter++;
73 this.print(node.init, node);
74 this.inForStatementInitCounter--;
75 this.token(";");
76
77 if (node.test) {
78 this.space();
79 this.print(node.test, node);
80 }
81
82 this.token(";");
83
84 if (node.update) {
85 this.space();
86 this.print(node.update, node);
87 }
88
89 this.token(")");
90 this.printBlock(node);
91}
92
93function WhileStatement(node) {
94 this.word("while");
95 this.space();
96 this.token("(");
97 this.print(node.test, node);
98 this.token(")");
99 this.printBlock(node);
100}
101
102const buildForXStatement = function (op) {
103 return function (node) {
104 this.word("for");
105 this.space();
106
107 if (op === "of" && node.await) {
108 this.word("await");
109 this.space();
110 }
111
112 this.token("(");
113 this.print(node.left, node);
114 this.space();
115 this.word(op);
116 this.space();
117 this.print(node.right, node);
118 this.token(")");
119 this.printBlock(node);
120 };
121};
122
123const ForInStatement = buildForXStatement("in");
124exports.ForInStatement = ForInStatement;
125const ForOfStatement = buildForXStatement("of");
126exports.ForOfStatement = ForOfStatement;
127
128function DoWhileStatement(node) {
129 this.word("do");
130 this.space();
131 this.print(node.body, node);
132 this.space();
133 this.word("while");
134 this.space();
135 this.token("(");
136 this.print(node.test, node);
137 this.token(")");
138 this.semicolon();
139}
140
141function buildLabelStatement(prefix, key = "label") {
142 return function (node) {
143 this.word(prefix);
144 const label = node[key];
145
146 if (label) {
147 this.space();
148 const isLabel = key == "label";
149 const terminatorState = this.startTerminatorless(isLabel);
150 this.print(label, node);
151 this.endTerminatorless(terminatorState);
152 }
153
154 this.semicolon();
155 };
156}
157
158const ContinueStatement = buildLabelStatement("continue");
159exports.ContinueStatement = ContinueStatement;
160const ReturnStatement = buildLabelStatement("return", "argument");
161exports.ReturnStatement = ReturnStatement;
162const BreakStatement = buildLabelStatement("break");
163exports.BreakStatement = BreakStatement;
164const ThrowStatement = buildLabelStatement("throw", "argument");
165exports.ThrowStatement = ThrowStatement;
166
167function LabeledStatement(node) {
168 this.print(node.label, node);
169 this.token(":");
170 this.space();
171 this.print(node.body, node);
172}
173
174function TryStatement(node) {
175 this.word("try");
176 this.space();
177 this.print(node.block, node);
178 this.space();
179
180 if (node.handlers) {
181 this.print(node.handlers[0], node);
182 } else {
183 this.print(node.handler, node);
184 }
185
186 if (node.finalizer) {
187 this.space();
188 this.word("finally");
189 this.space();
190 this.print(node.finalizer, node);
191 }
192}
193
194function CatchClause(node) {
195 this.word("catch");
196 this.space();
197
198 if (node.param) {
199 this.token("(");
200 this.print(node.param, node);
201 this.print(node.param.typeAnnotation, node);
202 this.token(")");
203 this.space();
204 }
205
206 this.print(node.body, node);
207}
208
209function SwitchStatement(node) {
210 this.word("switch");
211 this.space();
212 this.token("(");
213 this.print(node.discriminant, node);
214 this.token(")");
215 this.space();
216 this.token("{");
217 this.printSequence(node.cases, node, {
218 indent: true,
219
220 addNewlines(leading, cas) {
221 if (!leading && node.cases[node.cases.length - 1] === cas) return -1;
222 }
223
224 });
225 this.token("}");
226}
227
228function SwitchCase(node) {
229 if (node.test) {
230 this.word("case");
231 this.space();
232 this.print(node.test, node);
233 this.token(":");
234 } else {
235 this.word("default");
236 this.token(":");
237 }
238
239 if (node.consequent.length) {
240 this.newline();
241 this.printSequence(node.consequent, node, {
242 indent: true
243 });
244 }
245}
246
247function DebuggerStatement() {
248 this.word("debugger");
249 this.semicolon();
250}
251
252function variableDeclarationIndent() {
253 this.token(",");
254 this.newline();
255 if (this.endsWith("\n")) for (let i = 0; i < 4; i++) this.space(true);
256}
257
258function constDeclarationIndent() {
259 this.token(",");
260 this.newline();
261 if (this.endsWith("\n")) for (let i = 0; i < 6; i++) this.space(true);
262}
263
264function VariableDeclaration(node, parent) {
265 if (node.declare) {
266 this.word("declare");
267 this.space();
268 }
269
270 this.word(node.kind);
271 this.space();
272 let hasInits = false;
273
274 if (!t.isFor(parent)) {
275 for (const declar of node.declarations) {
276 if (declar.init) {
277 hasInits = true;
278 }
279 }
280 }
281
282 let separator;
283
284 if (hasInits) {
285 separator = node.kind === "const" ? constDeclarationIndent : variableDeclarationIndent;
286 }
287
288 this.printList(node.declarations, node, {
289 separator
290 });
291
292 if (t.isFor(parent)) {
293 if (t.isForStatement(parent)) {
294 if (parent.init === node) return;
295 } else {
296 if (parent.left === node) return;
297 }
298 }
299
300 this.semicolon();
301}
302
303function VariableDeclarator(node) {
304 this.print(node.id, node);
305 if (node.definite) this.token("!");
306 this.print(node.id.typeAnnotation, node);
307
308 if (node.init) {
309 this.space();
310 this.token("=");
311 this.space();
312 this.print(node.init, node);
313 }
314}
Note: See TracBrowser for help on using the repository browser.