1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.WithStatement = WithStatement;
|
---|
7 | exports.IfStatement = IfStatement;
|
---|
8 | exports.ForStatement = ForStatement;
|
---|
9 | exports.WhileStatement = WhileStatement;
|
---|
10 | exports.DoWhileStatement = DoWhileStatement;
|
---|
11 | exports.LabeledStatement = LabeledStatement;
|
---|
12 | exports.TryStatement = TryStatement;
|
---|
13 | exports.CatchClause = CatchClause;
|
---|
14 | exports.SwitchStatement = SwitchStatement;
|
---|
15 | exports.SwitchCase = SwitchCase;
|
---|
16 | exports.DebuggerStatement = DebuggerStatement;
|
---|
17 | exports.VariableDeclaration = VariableDeclaration;
|
---|
18 | exports.VariableDeclarator = VariableDeclarator;
|
---|
19 | exports.ThrowStatement = exports.BreakStatement = exports.ReturnStatement = exports.ContinueStatement = exports.ForOfStatement = exports.ForInStatement = void 0;
|
---|
20 |
|
---|
21 | var t = require("@babel/types");
|
---|
22 |
|
---|
23 | function 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 |
|
---|
32 | function 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 |
|
---|
63 | function getLastStatement(statement) {
|
---|
64 | if (!t.isStatement(statement.body)) return statement;
|
---|
65 | return getLastStatement(statement.body);
|
---|
66 | }
|
---|
67 |
|
---|
68 | function 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 |
|
---|
93 | function 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 |
|
---|
102 | const 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 |
|
---|
123 | const ForInStatement = buildForXStatement("in");
|
---|
124 | exports.ForInStatement = ForInStatement;
|
---|
125 | const ForOfStatement = buildForXStatement("of");
|
---|
126 | exports.ForOfStatement = ForOfStatement;
|
---|
127 |
|
---|
128 | function 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 |
|
---|
141 | function 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 |
|
---|
158 | const ContinueStatement = buildLabelStatement("continue");
|
---|
159 | exports.ContinueStatement = ContinueStatement;
|
---|
160 | const ReturnStatement = buildLabelStatement("return", "argument");
|
---|
161 | exports.ReturnStatement = ReturnStatement;
|
---|
162 | const BreakStatement = buildLabelStatement("break");
|
---|
163 | exports.BreakStatement = BreakStatement;
|
---|
164 | const ThrowStatement = buildLabelStatement("throw", "argument");
|
---|
165 | exports.ThrowStatement = ThrowStatement;
|
---|
166 |
|
---|
167 | function LabeledStatement(node) {
|
---|
168 | this.print(node.label, node);
|
---|
169 | this.token(":");
|
---|
170 | this.space();
|
---|
171 | this.print(node.body, node);
|
---|
172 | }
|
---|
173 |
|
---|
174 | function 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 |
|
---|
194 | function 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 |
|
---|
209 | function 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 |
|
---|
228 | function 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 |
|
---|
247 | function DebuggerStatement() {
|
---|
248 | this.word("debugger");
|
---|
249 | this.semicolon();
|
---|
250 | }
|
---|
251 |
|
---|
252 | function variableDeclarationIndent() {
|
---|
253 | this.token(",");
|
---|
254 | this.newline();
|
---|
255 | if (this.endsWith("\n")) for (let i = 0; i < 4; i++) this.space(true);
|
---|
256 | }
|
---|
257 |
|
---|
258 | function constDeclarationIndent() {
|
---|
259 | this.token(",");
|
---|
260 | this.newline();
|
---|
261 | if (this.endsWith("\n")) for (let i = 0; i < 6; i++) this.space(true);
|
---|
262 | }
|
---|
263 |
|
---|
264 | function 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 |
|
---|
303 | function 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 | } |
---|