1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 |
|
---|
8 | var _buffer = require("./buffer");
|
---|
9 |
|
---|
10 | var n = require("./node");
|
---|
11 |
|
---|
12 | var t = require("@babel/types");
|
---|
13 |
|
---|
14 | var generatorFunctions = require("./generators");
|
---|
15 |
|
---|
16 | const SCIENTIFIC_NOTATION = /e/i;
|
---|
17 | const ZERO_DECIMAL_INTEGER = /\.0+$/;
|
---|
18 | const NON_DECIMAL_LITERAL = /^0[box]/;
|
---|
19 | const PURE_ANNOTATION_RE = /^\s*[@#]__PURE__\s*$/;
|
---|
20 |
|
---|
21 | class Printer {
|
---|
22 | constructor(format, map) {
|
---|
23 | this.inForStatementInitCounter = 0;
|
---|
24 | this._printStack = [];
|
---|
25 | this._indent = 0;
|
---|
26 | this._insideAux = false;
|
---|
27 | this._parenPushNewlineState = null;
|
---|
28 | this._noLineTerminator = false;
|
---|
29 | this._printAuxAfterOnNextUserNode = false;
|
---|
30 | this._printedComments = new WeakSet();
|
---|
31 | this._endsWithInteger = false;
|
---|
32 | this._endsWithWord = false;
|
---|
33 | this.format = format;
|
---|
34 | this._buf = new _buffer.default(map);
|
---|
35 | }
|
---|
36 |
|
---|
37 | generate(ast) {
|
---|
38 | this.print(ast);
|
---|
39 |
|
---|
40 | this._maybeAddAuxComment();
|
---|
41 |
|
---|
42 | return this._buf.get();
|
---|
43 | }
|
---|
44 |
|
---|
45 | indent() {
|
---|
46 | if (this.format.compact || this.format.concise) return;
|
---|
47 | this._indent++;
|
---|
48 | }
|
---|
49 |
|
---|
50 | dedent() {
|
---|
51 | if (this.format.compact || this.format.concise) return;
|
---|
52 | this._indent--;
|
---|
53 | }
|
---|
54 |
|
---|
55 | semicolon(force = false) {
|
---|
56 | this._maybeAddAuxComment();
|
---|
57 |
|
---|
58 | this._append(";", !force);
|
---|
59 | }
|
---|
60 |
|
---|
61 | rightBrace() {
|
---|
62 | if (this.format.minified) {
|
---|
63 | this._buf.removeLastSemicolon();
|
---|
64 | }
|
---|
65 |
|
---|
66 | this.token("}");
|
---|
67 | }
|
---|
68 |
|
---|
69 | space(force = false) {
|
---|
70 | if (this.format.compact) return;
|
---|
71 |
|
---|
72 | if (this._buf.hasContent() && !this.endsWith(" ") && !this.endsWith("\n") || force) {
|
---|
73 | this._space();
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | word(str) {
|
---|
78 | if (this._endsWithWord || this.endsWith("/") && str.indexOf("/") === 0) {
|
---|
79 | this._space();
|
---|
80 | }
|
---|
81 |
|
---|
82 | this._maybeAddAuxComment();
|
---|
83 |
|
---|
84 | this._append(str);
|
---|
85 |
|
---|
86 | this._endsWithWord = true;
|
---|
87 | }
|
---|
88 |
|
---|
89 | number(str) {
|
---|
90 | this.word(str);
|
---|
91 | this._endsWithInteger = Number.isInteger(+str) && !NON_DECIMAL_LITERAL.test(str) && !SCIENTIFIC_NOTATION.test(str) && !ZERO_DECIMAL_INTEGER.test(str) && str[str.length - 1] !== ".";
|
---|
92 | }
|
---|
93 |
|
---|
94 | token(str) {
|
---|
95 | if (str === "--" && this.endsWith("!") || str[0] === "+" && this.endsWith("+") || str[0] === "-" && this.endsWith("-") || str[0] === "." && this._endsWithInteger) {
|
---|
96 | this._space();
|
---|
97 | }
|
---|
98 |
|
---|
99 | this._maybeAddAuxComment();
|
---|
100 |
|
---|
101 | this._append(str);
|
---|
102 | }
|
---|
103 |
|
---|
104 | newline(i) {
|
---|
105 | if (this.format.retainLines || this.format.compact) return;
|
---|
106 |
|
---|
107 | if (this.format.concise) {
|
---|
108 | this.space();
|
---|
109 | return;
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (this.endsWith("\n\n")) return;
|
---|
113 | if (typeof i !== "number") i = 1;
|
---|
114 | i = Math.min(2, i);
|
---|
115 | if (this.endsWith("{\n") || this.endsWith(":\n")) i--;
|
---|
116 | if (i <= 0) return;
|
---|
117 |
|
---|
118 | for (let j = 0; j < i; j++) {
|
---|
119 | this._newline();
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | endsWith(str) {
|
---|
124 | return this._buf.endsWith(str);
|
---|
125 | }
|
---|
126 |
|
---|
127 | removeTrailingNewline() {
|
---|
128 | this._buf.removeTrailingNewline();
|
---|
129 | }
|
---|
130 |
|
---|
131 | exactSource(loc, cb) {
|
---|
132 | this._catchUp("start", loc);
|
---|
133 |
|
---|
134 | this._buf.exactSource(loc, cb);
|
---|
135 | }
|
---|
136 |
|
---|
137 | source(prop, loc) {
|
---|
138 | this._catchUp(prop, loc);
|
---|
139 |
|
---|
140 | this._buf.source(prop, loc);
|
---|
141 | }
|
---|
142 |
|
---|
143 | withSource(prop, loc, cb) {
|
---|
144 | this._catchUp(prop, loc);
|
---|
145 |
|
---|
146 | this._buf.withSource(prop, loc, cb);
|
---|
147 | }
|
---|
148 |
|
---|
149 | _space() {
|
---|
150 | this._append(" ", true);
|
---|
151 | }
|
---|
152 |
|
---|
153 | _newline() {
|
---|
154 | this._append("\n", true);
|
---|
155 | }
|
---|
156 |
|
---|
157 | _append(str, queue = false) {
|
---|
158 | this._maybeAddParen(str);
|
---|
159 |
|
---|
160 | this._maybeIndent(str);
|
---|
161 |
|
---|
162 | if (queue) this._buf.queue(str);else this._buf.append(str);
|
---|
163 | this._endsWithWord = false;
|
---|
164 | this._endsWithInteger = false;
|
---|
165 | }
|
---|
166 |
|
---|
167 | _maybeIndent(str) {
|
---|
168 | if (this._indent && this.endsWith("\n") && str[0] !== "\n") {
|
---|
169 | this._buf.queue(this._getIndent());
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | _maybeAddParen(str) {
|
---|
174 | const parenPushNewlineState = this._parenPushNewlineState;
|
---|
175 | if (!parenPushNewlineState) return;
|
---|
176 | let i;
|
---|
177 |
|
---|
178 | for (i = 0; i < str.length && str[i] === " "; i++) continue;
|
---|
179 |
|
---|
180 | if (i === str.length) {
|
---|
181 | return;
|
---|
182 | }
|
---|
183 |
|
---|
184 | const cha = str[i];
|
---|
185 |
|
---|
186 | if (cha !== "\n") {
|
---|
187 | if (cha !== "/" || i + 1 === str.length) {
|
---|
188 | this._parenPushNewlineState = null;
|
---|
189 | return;
|
---|
190 | }
|
---|
191 |
|
---|
192 | const chaPost = str[i + 1];
|
---|
193 |
|
---|
194 | if (chaPost === "*") {
|
---|
195 | if (PURE_ANNOTATION_RE.test(str.slice(i + 2, str.length - 2))) {
|
---|
196 | return;
|
---|
197 | }
|
---|
198 | } else if (chaPost !== "/") {
|
---|
199 | this._parenPushNewlineState = null;
|
---|
200 | return;
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | this.token("(");
|
---|
205 | this.indent();
|
---|
206 | parenPushNewlineState.printed = true;
|
---|
207 | }
|
---|
208 |
|
---|
209 | _catchUp(prop, loc) {
|
---|
210 | if (!this.format.retainLines) return;
|
---|
211 | const pos = loc ? loc[prop] : null;
|
---|
212 |
|
---|
213 | if ((pos == null ? void 0 : pos.line) != null) {
|
---|
214 | const count = pos.line - this._buf.getCurrentLine();
|
---|
215 |
|
---|
216 | for (let i = 0; i < count; i++) {
|
---|
217 | this._newline();
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | _getIndent() {
|
---|
223 | return this.format.indent.style.repeat(this._indent);
|
---|
224 | }
|
---|
225 |
|
---|
226 | startTerminatorless(isLabel = false) {
|
---|
227 | if (isLabel) {
|
---|
228 | this._noLineTerminator = true;
|
---|
229 | return null;
|
---|
230 | } else {
|
---|
231 | return this._parenPushNewlineState = {
|
---|
232 | printed: false
|
---|
233 | };
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | endTerminatorless(state) {
|
---|
238 | this._noLineTerminator = false;
|
---|
239 |
|
---|
240 | if (state != null && state.printed) {
|
---|
241 | this.dedent();
|
---|
242 | this.newline();
|
---|
243 | this.token(")");
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | print(node, parent) {
|
---|
248 | if (!node) return;
|
---|
249 | const oldConcise = this.format.concise;
|
---|
250 |
|
---|
251 | if (node._compact) {
|
---|
252 | this.format.concise = true;
|
---|
253 | }
|
---|
254 |
|
---|
255 | const printMethod = this[node.type];
|
---|
256 |
|
---|
257 | if (!printMethod) {
|
---|
258 | throw new ReferenceError(`unknown node of type ${JSON.stringify(node.type)} with constructor ${JSON.stringify(node == null ? void 0 : node.constructor.name)}`);
|
---|
259 | }
|
---|
260 |
|
---|
261 | this._printStack.push(node);
|
---|
262 |
|
---|
263 | const oldInAux = this._insideAux;
|
---|
264 | this._insideAux = !node.loc;
|
---|
265 |
|
---|
266 | this._maybeAddAuxComment(this._insideAux && !oldInAux);
|
---|
267 |
|
---|
268 | let needsParens = n.needsParens(node, parent, this._printStack);
|
---|
269 |
|
---|
270 | if (this.format.retainFunctionParens && node.type === "FunctionExpression" && node.extra && node.extra.parenthesized) {
|
---|
271 | needsParens = true;
|
---|
272 | }
|
---|
273 |
|
---|
274 | if (needsParens) this.token("(");
|
---|
275 |
|
---|
276 | this._printLeadingComments(node);
|
---|
277 |
|
---|
278 | const loc = t.isProgram(node) || t.isFile(node) ? null : node.loc;
|
---|
279 | this.withSource("start", loc, () => {
|
---|
280 | printMethod.call(this, node, parent);
|
---|
281 | });
|
---|
282 |
|
---|
283 | this._printTrailingComments(node);
|
---|
284 |
|
---|
285 | if (needsParens) this.token(")");
|
---|
286 |
|
---|
287 | this._printStack.pop();
|
---|
288 |
|
---|
289 | this.format.concise = oldConcise;
|
---|
290 | this._insideAux = oldInAux;
|
---|
291 | }
|
---|
292 |
|
---|
293 | _maybeAddAuxComment(enteredPositionlessNode) {
|
---|
294 | if (enteredPositionlessNode) this._printAuxBeforeComment();
|
---|
295 | if (!this._insideAux) this._printAuxAfterComment();
|
---|
296 | }
|
---|
297 |
|
---|
298 | _printAuxBeforeComment() {
|
---|
299 | if (this._printAuxAfterOnNextUserNode) return;
|
---|
300 | this._printAuxAfterOnNextUserNode = true;
|
---|
301 | const comment = this.format.auxiliaryCommentBefore;
|
---|
302 |
|
---|
303 | if (comment) {
|
---|
304 | this._printComment({
|
---|
305 | type: "CommentBlock",
|
---|
306 | value: comment
|
---|
307 | });
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | _printAuxAfterComment() {
|
---|
312 | if (!this._printAuxAfterOnNextUserNode) return;
|
---|
313 | this._printAuxAfterOnNextUserNode = false;
|
---|
314 | const comment = this.format.auxiliaryCommentAfter;
|
---|
315 |
|
---|
316 | if (comment) {
|
---|
317 | this._printComment({
|
---|
318 | type: "CommentBlock",
|
---|
319 | value: comment
|
---|
320 | });
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | getPossibleRaw(node) {
|
---|
325 | const extra = node.extra;
|
---|
326 |
|
---|
327 | if (extra && extra.raw != null && extra.rawValue != null && node.value === extra.rawValue) {
|
---|
328 | return extra.raw;
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | printJoin(nodes, parent, opts = {}) {
|
---|
333 | if (!(nodes != null && nodes.length)) return;
|
---|
334 | if (opts.indent) this.indent();
|
---|
335 | const newlineOpts = {
|
---|
336 | addNewlines: opts.addNewlines
|
---|
337 | };
|
---|
338 |
|
---|
339 | for (let i = 0; i < nodes.length; i++) {
|
---|
340 | const node = nodes[i];
|
---|
341 | if (!node) continue;
|
---|
342 | if (opts.statement) this._printNewline(true, node, parent, newlineOpts);
|
---|
343 | this.print(node, parent);
|
---|
344 |
|
---|
345 | if (opts.iterator) {
|
---|
346 | opts.iterator(node, i);
|
---|
347 | }
|
---|
348 |
|
---|
349 | if (opts.separator && i < nodes.length - 1) {
|
---|
350 | opts.separator.call(this);
|
---|
351 | }
|
---|
352 |
|
---|
353 | if (opts.statement) this._printNewline(false, node, parent, newlineOpts);
|
---|
354 | }
|
---|
355 |
|
---|
356 | if (opts.indent) this.dedent();
|
---|
357 | }
|
---|
358 |
|
---|
359 | printAndIndentOnComments(node, parent) {
|
---|
360 | const indent = node.leadingComments && node.leadingComments.length > 0;
|
---|
361 | if (indent) this.indent();
|
---|
362 | this.print(node, parent);
|
---|
363 | if (indent) this.dedent();
|
---|
364 | }
|
---|
365 |
|
---|
366 | printBlock(parent) {
|
---|
367 | const node = parent.body;
|
---|
368 |
|
---|
369 | if (!t.isEmptyStatement(node)) {
|
---|
370 | this.space();
|
---|
371 | }
|
---|
372 |
|
---|
373 | this.print(node, parent);
|
---|
374 | }
|
---|
375 |
|
---|
376 | _printTrailingComments(node) {
|
---|
377 | this._printComments(this._getComments(false, node));
|
---|
378 | }
|
---|
379 |
|
---|
380 | _printLeadingComments(node) {
|
---|
381 | this._printComments(this._getComments(true, node), true);
|
---|
382 | }
|
---|
383 |
|
---|
384 | printInnerComments(node, indent = true) {
|
---|
385 | var _node$innerComments;
|
---|
386 |
|
---|
387 | if (!((_node$innerComments = node.innerComments) != null && _node$innerComments.length)) return;
|
---|
388 | if (indent) this.indent();
|
---|
389 |
|
---|
390 | this._printComments(node.innerComments);
|
---|
391 |
|
---|
392 | if (indent) this.dedent();
|
---|
393 | }
|
---|
394 |
|
---|
395 | printSequence(nodes, parent, opts = {}) {
|
---|
396 | opts.statement = true;
|
---|
397 | return this.printJoin(nodes, parent, opts);
|
---|
398 | }
|
---|
399 |
|
---|
400 | printList(items, parent, opts = {}) {
|
---|
401 | if (opts.separator == null) {
|
---|
402 | opts.separator = commaSeparator;
|
---|
403 | }
|
---|
404 |
|
---|
405 | return this.printJoin(items, parent, opts);
|
---|
406 | }
|
---|
407 |
|
---|
408 | _printNewline(leading, node, parent, opts) {
|
---|
409 | if (this.format.retainLines || this.format.compact) return;
|
---|
410 |
|
---|
411 | if (this.format.concise) {
|
---|
412 | this.space();
|
---|
413 | return;
|
---|
414 | }
|
---|
415 |
|
---|
416 | let lines = 0;
|
---|
417 |
|
---|
418 | if (this._buf.hasContent()) {
|
---|
419 | if (!leading) lines++;
|
---|
420 | if (opts.addNewlines) lines += opts.addNewlines(leading, node) || 0;
|
---|
421 | const needs = leading ? n.needsWhitespaceBefore : n.needsWhitespaceAfter;
|
---|
422 | if (needs(node, parent)) lines++;
|
---|
423 | }
|
---|
424 |
|
---|
425 | this.newline(lines);
|
---|
426 | }
|
---|
427 |
|
---|
428 | _getComments(leading, node) {
|
---|
429 | return node && (leading ? node.leadingComments : node.trailingComments) || [];
|
---|
430 | }
|
---|
431 |
|
---|
432 | _printComment(comment, skipNewLines) {
|
---|
433 | if (!this.format.shouldPrintComment(comment.value)) return;
|
---|
434 | if (comment.ignore) return;
|
---|
435 | if (this._printedComments.has(comment)) return;
|
---|
436 |
|
---|
437 | this._printedComments.add(comment);
|
---|
438 |
|
---|
439 | const isBlockComment = comment.type === "CommentBlock";
|
---|
440 | const printNewLines = isBlockComment && !skipNewLines && !this._noLineTerminator;
|
---|
441 | if (printNewLines && this._buf.hasContent()) this.newline(1);
|
---|
442 | if (!this.endsWith("[") && !this.endsWith("{")) this.space();
|
---|
443 | let val = !isBlockComment && !this._noLineTerminator ? `//${comment.value}\n` : `/*${comment.value}*/`;
|
---|
444 |
|
---|
445 | if (isBlockComment && this.format.indent.adjustMultilineComment) {
|
---|
446 | var _comment$loc;
|
---|
447 |
|
---|
448 | const offset = (_comment$loc = comment.loc) == null ? void 0 : _comment$loc.start.column;
|
---|
449 |
|
---|
450 | if (offset) {
|
---|
451 | const newlineRegex = new RegExp("\\n\\s{1," + offset + "}", "g");
|
---|
452 | val = val.replace(newlineRegex, "\n");
|
---|
453 | }
|
---|
454 |
|
---|
455 | const indentSize = Math.max(this._getIndent().length, this.format.retainLines ? 0 : this._buf.getCurrentColumn());
|
---|
456 | val = val.replace(/\n(?!$)/g, `\n${" ".repeat(indentSize)}`);
|
---|
457 | }
|
---|
458 |
|
---|
459 | if (this.endsWith("/")) this._space();
|
---|
460 | this.withSource("start", comment.loc, () => {
|
---|
461 | this._append(val);
|
---|
462 | });
|
---|
463 | if (printNewLines) this.newline(1);
|
---|
464 | }
|
---|
465 |
|
---|
466 | _printComments(comments, inlinePureAnnotation) {
|
---|
467 | if (!(comments != null && comments.length)) return;
|
---|
468 |
|
---|
469 | if (inlinePureAnnotation && comments.length === 1 && PURE_ANNOTATION_RE.test(comments[0].value)) {
|
---|
470 | this._printComment(comments[0], this._buf.hasContent() && !this.endsWith("\n"));
|
---|
471 | } else {
|
---|
472 | for (const comment of comments) {
|
---|
473 | this._printComment(comment);
|
---|
474 | }
|
---|
475 | }
|
---|
476 | }
|
---|
477 |
|
---|
478 | printAssertions(node) {
|
---|
479 | var _node$assertions;
|
---|
480 |
|
---|
481 | if ((_node$assertions = node.assertions) != null && _node$assertions.length) {
|
---|
482 | this.space();
|
---|
483 | this.word("assert");
|
---|
484 | this.space();
|
---|
485 | this.token("{");
|
---|
486 | this.space();
|
---|
487 | this.printList(node.assertions, node);
|
---|
488 | this.space();
|
---|
489 | this.token("}");
|
---|
490 | }
|
---|
491 | }
|
---|
492 |
|
---|
493 | }
|
---|
494 |
|
---|
495 | Object.assign(Printer.prototype, generatorFunctions);
|
---|
496 | {
|
---|
497 | Printer.prototype.Noop = function Noop() {};
|
---|
498 | }
|
---|
499 | var _default = Printer;
|
---|
500 | exports.default = _default;
|
---|
501 |
|
---|
502 | function commaSeparator() {
|
---|
503 | this.token(",");
|
---|
504 | this.space();
|
---|
505 | } |
---|