UNPKG

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