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