terser 4.8.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/output.js ADDED
@@ -0,0 +1,2192 @@
1
+ /***********************************************************************
2
+
3
+ A JavaScript tokenizer / parser / beautifier / compressor.
4
+ https://github.com/mishoo/UglifyJS2
5
+
6
+ -------------------------------- (C) ---------------------------------
7
+
8
+ Author: Mihai Bazon
9
+ <mihai.bazon@gmail.com>
10
+ http://mihai.bazon.net/blog
11
+
12
+ Distributed under the BSD license:
13
+
14
+ Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
15
+
16
+ Redistribution and use in source and binary forms, with or without
17
+ modification, are permitted provided that the following conditions
18
+ are met:
19
+
20
+ * Redistributions of source code must retain the above
21
+ copyright notice, this list of conditions and the following
22
+ disclaimer.
23
+
24
+ * Redistributions in binary form must reproduce the above
25
+ copyright notice, this list of conditions and the following
26
+ disclaimer in the documentation and/or other materials
27
+ provided with the distribution.
28
+
29
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
30
+ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
33
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
34
+ OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
38
+ TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
39
+ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40
+ SUCH DAMAGE.
41
+
42
+ ***********************************************************************/
43
+
44
+ "use strict";
45
+
46
+ import {
47
+ defaults,
48
+ makePredicate,
49
+ noop,
50
+ regexp_source_fix,
51
+ sort_regexp_flags,
52
+ return_false,
53
+ return_true,
54
+ } from "./utils/index.js";
55
+ import { first_in_statement, left_is_object } from "./utils/first_in_statement.js";
56
+ import {
57
+ AST_Array,
58
+ AST_Arrow,
59
+ AST_Assign,
60
+ AST_Await,
61
+ AST_BigInt,
62
+ AST_Binary,
63
+ AST_BlockStatement,
64
+ AST_Break,
65
+ AST_Call,
66
+ AST_Case,
67
+ AST_Catch,
68
+ AST_Class,
69
+ AST_ClassExpression,
70
+ AST_ClassProperty,
71
+ AST_ConciseMethod,
72
+ AST_Conditional,
73
+ AST_Const,
74
+ AST_Constant,
75
+ AST_Continue,
76
+ AST_Debugger,
77
+ AST_Default,
78
+ AST_DefaultAssign,
79
+ AST_Definitions,
80
+ AST_Defun,
81
+ AST_Destructuring,
82
+ AST_Directive,
83
+ AST_Do,
84
+ AST_Dot,
85
+ AST_EmptyStatement,
86
+ AST_Exit,
87
+ AST_Expansion,
88
+ AST_Export,
89
+ AST_Finally,
90
+ AST_For,
91
+ AST_ForIn,
92
+ AST_ForOf,
93
+ AST_Function,
94
+ AST_Hole,
95
+ AST_If,
96
+ AST_Import,
97
+ AST_Jump,
98
+ AST_LabeledStatement,
99
+ AST_Lambda,
100
+ AST_Let,
101
+ AST_LoopControl,
102
+ AST_NameMapping,
103
+ AST_New,
104
+ AST_NewTarget,
105
+ AST_Node,
106
+ AST_Number,
107
+ AST_Object,
108
+ AST_ObjectGetter,
109
+ AST_ObjectKeyVal,
110
+ AST_ObjectProperty,
111
+ AST_ObjectSetter,
112
+ AST_PrefixedTemplateString,
113
+ AST_PropAccess,
114
+ AST_RegExp,
115
+ AST_Return,
116
+ AST_Scope,
117
+ AST_Sequence,
118
+ AST_SimpleStatement,
119
+ AST_Statement,
120
+ AST_StatementWithBody,
121
+ AST_String,
122
+ AST_Sub,
123
+ AST_Super,
124
+ AST_Switch,
125
+ AST_SwitchBranch,
126
+ AST_Symbol,
127
+ AST_SymbolClassProperty,
128
+ AST_SymbolMethod,
129
+ AST_SymbolRef,
130
+ AST_TemplateSegment,
131
+ AST_TemplateString,
132
+ AST_This,
133
+ AST_Throw,
134
+ AST_Toplevel,
135
+ AST_Try,
136
+ AST_Unary,
137
+ AST_UnaryPostfix,
138
+ AST_UnaryPrefix,
139
+ AST_Var,
140
+ AST_VarDef,
141
+ AST_While,
142
+ AST_With,
143
+ AST_Yield,
144
+ TreeWalker,
145
+ walk,
146
+ walk_abort
147
+ } from "./ast.js";
148
+ import {
149
+ get_full_char_code,
150
+ get_full_char,
151
+ is_identifier_char,
152
+ is_basic_identifier_string,
153
+ is_identifier_string,
154
+ PRECEDENCE,
155
+ RESERVED_WORDS,
156
+ } from "./parse.js";
157
+
158
+ const EXPECT_DIRECTIVE = /^$|[;{][\s\n]*$/;
159
+ const CODE_LINE_BREAK = 10;
160
+ const CODE_SPACE = 32;
161
+
162
+ const r_annotation = /[@#]__(PURE|INLINE|NOINLINE)__/g;
163
+
164
+ function is_some_comments(comment) {
165
+ // multiline comment
166
+ return (
167
+ (comment.type === "comment2" || comment.type === "comment1")
168
+ && /@preserve|@lic|@cc_on|^\**!/i.test(comment.value)
169
+ );
170
+ }
171
+
172
+ function OutputStream(options) {
173
+
174
+ var readonly = !options;
175
+ options = defaults(options, {
176
+ ascii_only : false,
177
+ beautify : false,
178
+ braces : false,
179
+ comments : "some",
180
+ ecma : 5,
181
+ ie8 : false,
182
+ indent_level : 4,
183
+ indent_start : 0,
184
+ inline_script : true,
185
+ keep_numbers : false,
186
+ keep_quoted_props : false,
187
+ max_line_len : false,
188
+ preamble : null,
189
+ preserve_annotations : false,
190
+ quote_keys : false,
191
+ quote_style : 0,
192
+ safari10 : false,
193
+ semicolons : true,
194
+ shebang : true,
195
+ shorthand : undefined,
196
+ source_map : null,
197
+ webkit : false,
198
+ width : 80,
199
+ wrap_iife : false,
200
+ wrap_func_args : true,
201
+ }, true);
202
+
203
+ if (options.shorthand === undefined)
204
+ options.shorthand = options.ecma > 5;
205
+
206
+ // Convert comment option to RegExp if neccessary and set up comments filter
207
+ var comment_filter = return_false; // Default case, throw all comments away
208
+ if (options.comments) {
209
+ let comments = options.comments;
210
+ if (typeof options.comments === "string" && /^\/.*\/[a-zA-Z]*$/.test(options.comments)) {
211
+ var regex_pos = options.comments.lastIndexOf("/");
212
+ comments = new RegExp(
213
+ options.comments.substr(1, regex_pos - 1),
214
+ options.comments.substr(regex_pos + 1)
215
+ );
216
+ }
217
+ if (comments instanceof RegExp) {
218
+ comment_filter = function(comment) {
219
+ return comment.type != "comment5" && comments.test(comment.value);
220
+ };
221
+ } else if (typeof comments === "function") {
222
+ comment_filter = function(comment) {
223
+ return comment.type != "comment5" && comments(this, comment);
224
+ };
225
+ } else if (comments === "some") {
226
+ comment_filter = is_some_comments;
227
+ } else { // NOTE includes "all" option
228
+ comment_filter = return_true;
229
+ }
230
+ }
231
+
232
+ var indentation = 0;
233
+ var current_col = 0;
234
+ var current_line = 1;
235
+ var current_pos = 0;
236
+ var OUTPUT = "";
237
+ let printed_comments = new Set();
238
+
239
+ var to_utf8 = options.ascii_only ? function(str, identifier) {
240
+ if (options.ecma >= 2015) {
241
+ str = str.replace(/[\ud800-\udbff][\udc00-\udfff]/g, function(ch) {
242
+ var code = get_full_char_code(ch, 0).toString(16);
243
+ return "\\u{" + code + "}";
244
+ });
245
+ }
246
+ return str.replace(/[\u0000-\u001f\u007f-\uffff]/g, function(ch) {
247
+ var code = ch.charCodeAt(0).toString(16);
248
+ if (code.length <= 2 && !identifier) {
249
+ while (code.length < 2) code = "0" + code;
250
+ return "\\x" + code;
251
+ } else {
252
+ while (code.length < 4) code = "0" + code;
253
+ return "\\u" + code;
254
+ }
255
+ });
256
+ } : function(str) {
257
+ return str.replace(/[\ud800-\udbff][\udc00-\udfff]|([\ud800-\udbff]|[\udc00-\udfff])/g, function(match, lone) {
258
+ if (lone) {
259
+ return "\\u" + lone.charCodeAt(0).toString(16);
260
+ }
261
+ return match;
262
+ });
263
+ };
264
+
265
+ function make_string(str, quote) {
266
+ var dq = 0, sq = 0;
267
+ str = str.replace(/[\\\b\f\n\r\v\t\x22\x27\u2028\u2029\0\ufeff]/g,
268
+ function(s, i) {
269
+ switch (s) {
270
+ case '"': ++dq; return '"';
271
+ case "'": ++sq; return "'";
272
+ case "\\": return "\\\\";
273
+ case "\n": return "\\n";
274
+ case "\r": return "\\r";
275
+ case "\t": return "\\t";
276
+ case "\b": return "\\b";
277
+ case "\f": return "\\f";
278
+ case "\x0B": return options.ie8 ? "\\x0B" : "\\v";
279
+ case "\u2028": return "\\u2028";
280
+ case "\u2029": return "\\u2029";
281
+ case "\ufeff": return "\\ufeff";
282
+ case "\0":
283
+ return /[0-9]/.test(get_full_char(str, i+1)) ? "\\x00" : "\\0";
284
+ }
285
+ return s;
286
+ });
287
+ function quote_single() {
288
+ return "'" + str.replace(/\x27/g, "\\'") + "'";
289
+ }
290
+ function quote_double() {
291
+ return '"' + str.replace(/\x22/g, '\\"') + '"';
292
+ }
293
+ function quote_template() {
294
+ return "`" + str.replace(/`/g, "\\`") + "`";
295
+ }
296
+ str = to_utf8(str);
297
+ if (quote === "`") return quote_template();
298
+ switch (options.quote_style) {
299
+ case 1:
300
+ return quote_single();
301
+ case 2:
302
+ return quote_double();
303
+ case 3:
304
+ return quote == "'" ? quote_single() : quote_double();
305
+ default:
306
+ return dq > sq ? quote_single() : quote_double();
307
+ }
308
+ }
309
+
310
+ function encode_string(str, quote) {
311
+ var ret = make_string(str, quote);
312
+ if (options.inline_script) {
313
+ ret = ret.replace(/<\x2f(script)([>\/\t\n\f\r ])/gi, "<\\/$1$2");
314
+ ret = ret.replace(/\x3c!--/g, "\\x3c!--");
315
+ ret = ret.replace(/--\x3e/g, "--\\x3e");
316
+ }
317
+ return ret;
318
+ }
319
+
320
+ function make_name(name) {
321
+ name = name.toString();
322
+ name = to_utf8(name, true);
323
+ return name;
324
+ }
325
+
326
+ function make_indent(back) {
327
+ return " ".repeat(options.indent_start + indentation - back * options.indent_level);
328
+ }
329
+
330
+ /* -----[ beautification/minification ]----- */
331
+
332
+ var has_parens = false;
333
+ var might_need_space = false;
334
+ var might_need_semicolon = false;
335
+ var might_add_newline = 0;
336
+ var need_newline_indented = false;
337
+ var need_space = false;
338
+ var newline_insert = -1;
339
+ var last = "";
340
+ var mapping_token, mapping_name, mappings = options.source_map && [];
341
+
342
+ var do_add_mapping = mappings ? function() {
343
+ mappings.forEach(function(mapping) {
344
+ try {
345
+ options.source_map.add(
346
+ mapping.token.file,
347
+ mapping.line, mapping.col,
348
+ mapping.token.line, mapping.token.col,
349
+ !mapping.name && mapping.token.type == "name" ? mapping.token.value : mapping.name
350
+ );
351
+ } catch(ex) {
352
+ // Ignore bad mapping
353
+ }
354
+ });
355
+ mappings = [];
356
+ } : noop;
357
+
358
+ var ensure_line_len = options.max_line_len ? function() {
359
+ if (current_col > options.max_line_len) {
360
+ if (might_add_newline) {
361
+ var left = OUTPUT.slice(0, might_add_newline);
362
+ var right = OUTPUT.slice(might_add_newline);
363
+ if (mappings) {
364
+ var delta = right.length - current_col;
365
+ mappings.forEach(function(mapping) {
366
+ mapping.line++;
367
+ mapping.col += delta;
368
+ });
369
+ }
370
+ OUTPUT = left + "\n" + right;
371
+ current_line++;
372
+ current_pos++;
373
+ current_col = right.length;
374
+ }
375
+ }
376
+ if (might_add_newline) {
377
+ might_add_newline = 0;
378
+ do_add_mapping();
379
+ }
380
+ } : noop;
381
+
382
+ var requireSemicolonChars = makePredicate("( [ + * / - , . `");
383
+
384
+ function print(str) {
385
+ str = String(str);
386
+ var ch = get_full_char(str, 0);
387
+ if (need_newline_indented && ch) {
388
+ need_newline_indented = false;
389
+ if (ch !== "\n") {
390
+ print("\n");
391
+ indent();
392
+ }
393
+ }
394
+ if (need_space && ch) {
395
+ need_space = false;
396
+ if (!/[\s;})]/.test(ch)) {
397
+ space();
398
+ }
399
+ }
400
+ newline_insert = -1;
401
+ var prev = last.charAt(last.length - 1);
402
+ if (might_need_semicolon) {
403
+ might_need_semicolon = false;
404
+
405
+ if (prev === ":" && ch === "}" || (!ch || !";}".includes(ch)) && prev !== ";") {
406
+ if (options.semicolons || requireSemicolonChars.has(ch)) {
407
+ OUTPUT += ";";
408
+ current_col++;
409
+ current_pos++;
410
+ } else {
411
+ ensure_line_len();
412
+ if (current_col > 0) {
413
+ OUTPUT += "\n";
414
+ current_pos++;
415
+ current_line++;
416
+ current_col = 0;
417
+ }
418
+
419
+ if (/^\s+$/.test(str)) {
420
+ // reset the semicolon flag, since we didn't print one
421
+ // now and might still have to later
422
+ might_need_semicolon = true;
423
+ }
424
+ }
425
+
426
+ if (!options.beautify)
427
+ might_need_space = false;
428
+ }
429
+ }
430
+
431
+ if (might_need_space) {
432
+ if ((is_identifier_char(prev)
433
+ && (is_identifier_char(ch) || ch == "\\"))
434
+ || (ch == "/" && ch == prev)
435
+ || ((ch == "+" || ch == "-") && ch == last)
436
+ ) {
437
+ OUTPUT += " ";
438
+ current_col++;
439
+ current_pos++;
440
+ }
441
+ might_need_space = false;
442
+ }
443
+
444
+ if (mapping_token) {
445
+ mappings.push({
446
+ token: mapping_token,
447
+ name: mapping_name,
448
+ line: current_line,
449
+ col: current_col
450
+ });
451
+ mapping_token = false;
452
+ if (!might_add_newline) do_add_mapping();
453
+ }
454
+
455
+ OUTPUT += str;
456
+ has_parens = str[str.length - 1] == "(";
457
+ current_pos += str.length;
458
+ var a = str.split(/\r?\n/), n = a.length - 1;
459
+ current_line += n;
460
+ current_col += a[0].length;
461
+ if (n > 0) {
462
+ ensure_line_len();
463
+ current_col = a[n].length;
464
+ }
465
+ last = str;
466
+ }
467
+
468
+ var star = function() {
469
+ print("*");
470
+ };
471
+
472
+ var space = options.beautify ? function() {
473
+ print(" ");
474
+ } : function() {
475
+ might_need_space = true;
476
+ };
477
+
478
+ var indent = options.beautify ? function(half) {
479
+ if (options.beautify) {
480
+ print(make_indent(half ? 0.5 : 0));
481
+ }
482
+ } : noop;
483
+
484
+ var with_indent = options.beautify ? function(col, cont) {
485
+ if (col === true) col = next_indent();
486
+ var save_indentation = indentation;
487
+ indentation = col;
488
+ var ret = cont();
489
+ indentation = save_indentation;
490
+ return ret;
491
+ } : function(col, cont) { return cont(); };
492
+
493
+ var newline = options.beautify ? function() {
494
+ if (newline_insert < 0) return print("\n");
495
+ if (OUTPUT[newline_insert] != "\n") {
496
+ OUTPUT = OUTPUT.slice(0, newline_insert) + "\n" + OUTPUT.slice(newline_insert);
497
+ current_pos++;
498
+ current_line++;
499
+ }
500
+ newline_insert++;
501
+ } : options.max_line_len ? function() {
502
+ ensure_line_len();
503
+ might_add_newline = OUTPUT.length;
504
+ } : noop;
505
+
506
+ var semicolon = options.beautify ? function() {
507
+ print(";");
508
+ } : function() {
509
+ might_need_semicolon = true;
510
+ };
511
+
512
+ function force_semicolon() {
513
+ might_need_semicolon = false;
514
+ print(";");
515
+ }
516
+
517
+ function next_indent() {
518
+ return indentation + options.indent_level;
519
+ }
520
+
521
+ function with_block(cont) {
522
+ var ret;
523
+ print("{");
524
+ newline();
525
+ with_indent(next_indent(), function() {
526
+ ret = cont();
527
+ });
528
+ indent();
529
+ print("}");
530
+ return ret;
531
+ }
532
+
533
+ function with_parens(cont) {
534
+ print("(");
535
+ //XXX: still nice to have that for argument lists
536
+ //var ret = with_indent(current_col, cont);
537
+ var ret = cont();
538
+ print(")");
539
+ return ret;
540
+ }
541
+
542
+ function with_square(cont) {
543
+ print("[");
544
+ //var ret = with_indent(current_col, cont);
545
+ var ret = cont();
546
+ print("]");
547
+ return ret;
548
+ }
549
+
550
+ function comma() {
551
+ print(",");
552
+ space();
553
+ }
554
+
555
+ function colon() {
556
+ print(":");
557
+ space();
558
+ }
559
+
560
+ var add_mapping = mappings ? function(token, name) {
561
+ mapping_token = token;
562
+ mapping_name = name;
563
+ } : noop;
564
+
565
+ function get() {
566
+ if (might_add_newline) {
567
+ ensure_line_len();
568
+ }
569
+ return OUTPUT;
570
+ }
571
+
572
+ function has_nlb() {
573
+ let n = OUTPUT.length - 1;
574
+ while (n >= 0) {
575
+ const code = OUTPUT.charCodeAt(n);
576
+ if (code === CODE_LINE_BREAK) {
577
+ return true;
578
+ }
579
+
580
+ if (code !== CODE_SPACE) {
581
+ return false;
582
+ }
583
+ n--;
584
+ }
585
+ return true;
586
+ }
587
+
588
+ function filter_comment(comment) {
589
+ if (!options.preserve_annotations) {
590
+ comment = comment.replace(r_annotation, " ");
591
+ }
592
+ if (/^\s*$/.test(comment)) {
593
+ return "";
594
+ }
595
+ return comment.replace(/(<\s*\/\s*)(script)/i, "<\\/$2");
596
+ }
597
+
598
+ function prepend_comments(node) {
599
+ var self = this;
600
+ var start = node.start;
601
+ if (!start) return;
602
+ var printed_comments = self.printed_comments;
603
+
604
+ // There cannot be a newline between return and its value.
605
+ const return_with_value = node instanceof AST_Exit && node.value;
606
+
607
+ if (
608
+ start.comments_before
609
+ && printed_comments.has(start.comments_before)
610
+ ) {
611
+ if (return_with_value) {
612
+ start.comments_before = [];
613
+ } else {
614
+ return;
615
+ }
616
+ }
617
+
618
+ var comments = start.comments_before;
619
+ if (!comments) {
620
+ comments = start.comments_before = [];
621
+ }
622
+ printed_comments.add(comments);
623
+
624
+ if (return_with_value) {
625
+ var tw = new TreeWalker(function(node) {
626
+ var parent = tw.parent();
627
+ if (parent instanceof AST_Exit
628
+ || parent instanceof AST_Binary && parent.left === node
629
+ || parent.TYPE == "Call" && parent.expression === node
630
+ || parent instanceof AST_Conditional && parent.condition === node
631
+ || parent instanceof AST_Dot && parent.expression === node
632
+ || parent instanceof AST_Sequence && parent.expressions[0] === node
633
+ || parent instanceof AST_Sub && parent.expression === node
634
+ || parent instanceof AST_UnaryPostfix) {
635
+ if (!node.start) return;
636
+ var text = node.start.comments_before;
637
+ if (text && !printed_comments.has(text)) {
638
+ printed_comments.add(text);
639
+ comments = comments.concat(text);
640
+ }
641
+ } else {
642
+ return true;
643
+ }
644
+ });
645
+ tw.push(node);
646
+ node.value.walk(tw);
647
+ }
648
+
649
+ if (current_pos == 0) {
650
+ if (comments.length > 0 && options.shebang && comments[0].type === "comment5"
651
+ && !printed_comments.has(comments[0])) {
652
+ print("#!" + comments.shift().value + "\n");
653
+ indent();
654
+ }
655
+ var preamble = options.preamble;
656
+ if (preamble) {
657
+ print(preamble.replace(/\r\n?|[\n\u2028\u2029]|\s*$/g, "\n"));
658
+ }
659
+ }
660
+
661
+ comments = comments.filter(comment_filter, node).filter(c => !printed_comments.has(c));
662
+ if (comments.length == 0) return;
663
+ var last_nlb = has_nlb();
664
+ comments.forEach(function(c, i) {
665
+ printed_comments.add(c);
666
+ if (!last_nlb) {
667
+ if (c.nlb) {
668
+ print("\n");
669
+ indent();
670
+ last_nlb = true;
671
+ } else if (i > 0) {
672
+ space();
673
+ }
674
+ }
675
+
676
+ if (/comment[134]/.test(c.type)) {
677
+ var value = filter_comment(c.value);
678
+ if (value) {
679
+ print("//" + value + "\n");
680
+ indent();
681
+ }
682
+ last_nlb = true;
683
+ } else if (c.type == "comment2") {
684
+ var value = filter_comment(c.value);
685
+ if (value) {
686
+ print("/*" + value + "*/");
687
+ }
688
+ last_nlb = false;
689
+ }
690
+ });
691
+ if (!last_nlb) {
692
+ if (start.nlb) {
693
+ print("\n");
694
+ indent();
695
+ } else {
696
+ space();
697
+ }
698
+ }
699
+ }
700
+
701
+ function append_comments(node, tail) {
702
+ var self = this;
703
+ var token = node.end;
704
+ if (!token) return;
705
+ var printed_comments = self.printed_comments;
706
+ var comments = token[tail ? "comments_before" : "comments_after"];
707
+ if (!comments || printed_comments.has(comments)) return;
708
+ if (!(node instanceof AST_Statement || comments.every((c) =>
709
+ !/comment[134]/.test(c.type)
710
+ ))) return;
711
+ printed_comments.add(comments);
712
+ var insert = OUTPUT.length;
713
+ comments.filter(comment_filter, node).forEach(function(c, i) {
714
+ if (printed_comments.has(c)) return;
715
+ printed_comments.add(c);
716
+ need_space = false;
717
+ if (need_newline_indented) {
718
+ print("\n");
719
+ indent();
720
+ need_newline_indented = false;
721
+ } else if (c.nlb && (i > 0 || !has_nlb())) {
722
+ print("\n");
723
+ indent();
724
+ } else if (i > 0 || !tail) {
725
+ space();
726
+ }
727
+ if (/comment[134]/.test(c.type)) {
728
+ const value = filter_comment(c.value);
729
+ if (value) {
730
+ print("//" + value);
731
+ }
732
+ need_newline_indented = true;
733
+ } else if (c.type == "comment2") {
734
+ const value = filter_comment(c.value);
735
+ if (value) {
736
+ print("/*" + value + "*/");
737
+ }
738
+ need_space = true;
739
+ }
740
+ });
741
+ if (OUTPUT.length > insert) newline_insert = insert;
742
+ }
743
+
744
+ var stack = [];
745
+ return {
746
+ get : get,
747
+ toString : get,
748
+ indent : indent,
749
+ in_directive : false,
750
+ use_asm : null,
751
+ active_scope : null,
752
+ indentation : function() { return indentation; },
753
+ current_width : function() { return current_col - indentation; },
754
+ should_break : function() { return options.width && this.current_width() >= options.width; },
755
+ has_parens : function() { return has_parens; },
756
+ newline : newline,
757
+ print : print,
758
+ star : star,
759
+ space : space,
760
+ comma : comma,
761
+ colon : colon,
762
+ last : function() { return last; },
763
+ semicolon : semicolon,
764
+ force_semicolon : force_semicolon,
765
+ to_utf8 : to_utf8,
766
+ print_name : function(name) { print(make_name(name)); },
767
+ print_string : function(str, quote, escape_directive) {
768
+ var encoded = encode_string(str, quote);
769
+ if (escape_directive === true && !encoded.includes("\\")) {
770
+ // Insert semicolons to break directive prologue
771
+ if (!EXPECT_DIRECTIVE.test(OUTPUT)) {
772
+ force_semicolon();
773
+ }
774
+ force_semicolon();
775
+ }
776
+ print(encoded);
777
+ },
778
+ print_template_string_chars: function(str) {
779
+ var encoded = encode_string(str, "`").replace(/\${/g, "\\${");
780
+ return print(encoded.substr(1, encoded.length - 2));
781
+ },
782
+ encode_string : encode_string,
783
+ next_indent : next_indent,
784
+ with_indent : with_indent,
785
+ with_block : with_block,
786
+ with_parens : with_parens,
787
+ with_square : with_square,
788
+ add_mapping : add_mapping,
789
+ option : function(opt) { return options[opt]; },
790
+ printed_comments: printed_comments,
791
+ prepend_comments: readonly ? noop : prepend_comments,
792
+ append_comments : readonly || comment_filter === return_false ? noop : append_comments,
793
+ line : function() { return current_line; },
794
+ col : function() { return current_col; },
795
+ pos : function() { return current_pos; },
796
+ push_node : function(node) { stack.push(node); },
797
+ pop_node : function() { return stack.pop(); },
798
+ parent : function(n) {
799
+ return stack[stack.length - 2 - (n || 0)];
800
+ }
801
+ };
802
+
803
+ }
804
+
805
+ /* -----[ code generators ]----- */
806
+
807
+ (function() {
808
+
809
+ /* -----[ utils ]----- */
810
+
811
+ function DEFPRINT(nodetype, generator) {
812
+ nodetype.DEFMETHOD("_codegen", generator);
813
+ }
814
+
815
+ AST_Node.DEFMETHOD("print", function(output, force_parens) {
816
+ var self = this, generator = self._codegen;
817
+ if (self instanceof AST_Scope) {
818
+ output.active_scope = self;
819
+ } else if (!output.use_asm && self instanceof AST_Directive && self.value == "use asm") {
820
+ output.use_asm = output.active_scope;
821
+ }
822
+ function doit() {
823
+ output.prepend_comments(self);
824
+ self.add_source_map(output);
825
+ generator(self, output);
826
+ output.append_comments(self);
827
+ }
828
+ output.push_node(self);
829
+ if (force_parens || self.needs_parens(output)) {
830
+ output.with_parens(doit);
831
+ } else {
832
+ doit();
833
+ }
834
+ output.pop_node();
835
+ if (self === output.use_asm) {
836
+ output.use_asm = null;
837
+ }
838
+ });
839
+ AST_Node.DEFMETHOD("_print", AST_Node.prototype.print);
840
+
841
+ AST_Node.DEFMETHOD("print_to_string", function(options) {
842
+ var output = OutputStream(options);
843
+ this.print(output);
844
+ return output.get();
845
+ });
846
+
847
+ /* -----[ PARENTHESES ]----- */
848
+
849
+ function PARENS(nodetype, func) {
850
+ if (Array.isArray(nodetype)) {
851
+ nodetype.forEach(function(nodetype) {
852
+ PARENS(nodetype, func);
853
+ });
854
+ } else {
855
+ nodetype.DEFMETHOD("needs_parens", func);
856
+ }
857
+ }
858
+
859
+ PARENS(AST_Node, return_false);
860
+
861
+ // a function expression needs parens around it when it's provably
862
+ // the first token to appear in a statement.
863
+ PARENS(AST_Function, function(output) {
864
+ if (!output.has_parens() && first_in_statement(output)) {
865
+ return true;
866
+ }
867
+
868
+ if (output.option("webkit")) {
869
+ var p = output.parent();
870
+ if (p instanceof AST_PropAccess && p.expression === this) {
871
+ return true;
872
+ }
873
+ }
874
+
875
+ if (output.option("wrap_iife")) {
876
+ var p = output.parent();
877
+ if (p instanceof AST_Call && p.expression === this) {
878
+ return true;
879
+ }
880
+ }
881
+
882
+ if (output.option("wrap_func_args")) {
883
+ var p = output.parent();
884
+ if (p instanceof AST_Call && p.args.includes(this)) {
885
+ return true;
886
+ }
887
+ }
888
+
889
+ return false;
890
+ });
891
+
892
+ PARENS(AST_Arrow, function(output) {
893
+ var p = output.parent();
894
+ return p instanceof AST_PropAccess && p.expression === this;
895
+ });
896
+
897
+ // same goes for an object literal, because otherwise it would be
898
+ // interpreted as a block of code.
899
+ PARENS(AST_Object, function(output) {
900
+ return !output.has_parens() && first_in_statement(output);
901
+ });
902
+
903
+ PARENS(AST_ClassExpression, first_in_statement);
904
+
905
+ PARENS(AST_Unary, function(output) {
906
+ var p = output.parent();
907
+ return p instanceof AST_PropAccess && p.expression === this
908
+ || p instanceof AST_Call && p.expression === this
909
+ || p instanceof AST_Binary
910
+ && p.operator === "**"
911
+ && this instanceof AST_UnaryPrefix
912
+ && p.left === this
913
+ && this.operator !== "++"
914
+ && this.operator !== "--";
915
+ });
916
+
917
+ PARENS(AST_Await, function(output) {
918
+ var p = output.parent();
919
+ return p instanceof AST_PropAccess && p.expression === this
920
+ || p instanceof AST_Call && p.expression === this
921
+ || output.option("safari10") && p instanceof AST_UnaryPrefix;
922
+ });
923
+
924
+ PARENS(AST_Sequence, function(output) {
925
+ var p = output.parent();
926
+ return p instanceof AST_Call // (foo, bar)() or foo(1, (2, 3), 4)
927
+ || p instanceof AST_Unary // !(foo, bar, baz)
928
+ || p instanceof AST_Binary // 1 + (2, 3) + 4 ==> 8
929
+ || p instanceof AST_VarDef // var a = (1, 2), b = a + a; ==> b == 4
930
+ || p instanceof AST_PropAccess // (1, {foo:2}).foo or (1, {foo:2})["foo"] ==> 2
931
+ || p instanceof AST_Array // [ 1, (2, 3), 4 ] ==> [ 1, 3, 4 ]
932
+ || p instanceof AST_ObjectProperty // { foo: (1, 2) }.foo ==> 2
933
+ || p instanceof AST_Conditional /* (false, true) ? (a = 10, b = 20) : (c = 30)
934
+ * ==> 20 (side effect, set a := 10 and b := 20) */
935
+ || p instanceof AST_Arrow // x => (x, x)
936
+ || p instanceof AST_DefaultAssign // x => (x = (0, function(){}))
937
+ || p instanceof AST_Expansion // [...(a, b)]
938
+ || p instanceof AST_ForOf && this === p.object // for (e of (foo, bar)) {}
939
+ || p instanceof AST_Yield // yield (foo, bar)
940
+ || p instanceof AST_Export // export default (foo, bar)
941
+ ;
942
+ });
943
+
944
+ PARENS(AST_Binary, function(output) {
945
+ var p = output.parent();
946
+ // (foo && bar)()
947
+ if (p instanceof AST_Call && p.expression === this)
948
+ return true;
949
+ // typeof (foo && bar)
950
+ if (p instanceof AST_Unary)
951
+ return true;
952
+ // (foo && bar)["prop"], (foo && bar).prop
953
+ if (p instanceof AST_PropAccess && p.expression === this)
954
+ return true;
955
+ // this deals with precedence: 3 * (2 + 1)
956
+ if (p instanceof AST_Binary) {
957
+ const po = p.operator;
958
+ const so = this.operator;
959
+
960
+ if (so === "??" && (po === "||" || po === "&&")) {
961
+ return true;
962
+ }
963
+
964
+ const pp = PRECEDENCE[po];
965
+ const sp = PRECEDENCE[so];
966
+ if (pp > sp
967
+ || (pp == sp
968
+ && (this === p.right || po == "**"))) {
969
+ return true;
970
+ }
971
+ }
972
+ });
973
+
974
+ PARENS(AST_Yield, function(output) {
975
+ var p = output.parent();
976
+ // (yield 1) + (yield 2)
977
+ // a = yield 3
978
+ if (p instanceof AST_Binary && p.operator !== "=")
979
+ return true;
980
+ // (yield 1)()
981
+ // new (yield 1)()
982
+ if (p instanceof AST_Call && p.expression === this)
983
+ return true;
984
+ // (yield 1) ? yield 2 : yield 3
985
+ if (p instanceof AST_Conditional && p.condition === this)
986
+ return true;
987
+ // -(yield 4)
988
+ if (p instanceof AST_Unary)
989
+ return true;
990
+ // (yield x).foo
991
+ // (yield x)['foo']
992
+ if (p instanceof AST_PropAccess && p.expression === this)
993
+ return true;
994
+ });
995
+
996
+ PARENS(AST_PropAccess, function(output) {
997
+ var p = output.parent();
998
+ if (p instanceof AST_New && p.expression === this) {
999
+ // i.e. new (foo.bar().baz)
1000
+ //
1001
+ // if there's one call into this subtree, then we need
1002
+ // parens around it too, otherwise the call will be
1003
+ // interpreted as passing the arguments to the upper New
1004
+ // expression.
1005
+ return walk(this, node => {
1006
+ if (node instanceof AST_Scope) return true;
1007
+ if (node instanceof AST_Call) {
1008
+ return walk_abort; // makes walk() return true.
1009
+ }
1010
+ });
1011
+ }
1012
+ });
1013
+
1014
+ PARENS(AST_Call, function(output) {
1015
+ var p = output.parent(), p1;
1016
+ if (p instanceof AST_New && p.expression === this
1017
+ || p instanceof AST_Export && p.is_default && this.expression instanceof AST_Function)
1018
+ return true;
1019
+
1020
+ // workaround for Safari bug.
1021
+ // https://bugs.webkit.org/show_bug.cgi?id=123506
1022
+ return this.expression instanceof AST_Function
1023
+ && p instanceof AST_PropAccess
1024
+ && p.expression === this
1025
+ && (p1 = output.parent(1)) instanceof AST_Assign
1026
+ && p1.left === p;
1027
+ });
1028
+
1029
+ PARENS(AST_New, function(output) {
1030
+ var p = output.parent();
1031
+ if (this.args.length === 0
1032
+ && (p instanceof AST_PropAccess // (new Date).getTime(), (new Date)["getTime"]()
1033
+ || p instanceof AST_Call && p.expression === this)) // (new foo)(bar)
1034
+ return true;
1035
+ });
1036
+
1037
+ PARENS(AST_Number, function(output) {
1038
+ var p = output.parent();
1039
+ if (p instanceof AST_PropAccess && p.expression === this) {
1040
+ var value = this.getValue();
1041
+ if (value < 0 || /^0/.test(make_num(value))) {
1042
+ return true;
1043
+ }
1044
+ }
1045
+ });
1046
+
1047
+ PARENS(AST_BigInt, function(output) {
1048
+ var p = output.parent();
1049
+ if (p instanceof AST_PropAccess && p.expression === this) {
1050
+ var value = this.getValue();
1051
+ if (value.startsWith("-")) {
1052
+ return true;
1053
+ }
1054
+ }
1055
+ });
1056
+
1057
+ PARENS([ AST_Assign, AST_Conditional ], function(output) {
1058
+ var p = output.parent();
1059
+ // !(a = false) → true
1060
+ if (p instanceof AST_Unary)
1061
+ return true;
1062
+ // 1 + (a = 2) + 3 → 6, side effect setting a = 2
1063
+ if (p instanceof AST_Binary && !(p instanceof AST_Assign))
1064
+ return true;
1065
+ // (a = func)() —or— new (a = Object)()
1066
+ if (p instanceof AST_Call && p.expression === this)
1067
+ return true;
1068
+ // (a = foo) ? bar : baz
1069
+ if (p instanceof AST_Conditional && p.condition === this)
1070
+ return true;
1071
+ // (a = foo)["prop"] —or— (a = foo).prop
1072
+ if (p instanceof AST_PropAccess && p.expression === this)
1073
+ return true;
1074
+ // ({a, b} = {a: 1, b: 2}), a destructuring assignment
1075
+ if (this instanceof AST_Assign && this.left instanceof AST_Destructuring && this.left.is_array === false)
1076
+ return true;
1077
+ });
1078
+
1079
+ /* -----[ PRINTERS ]----- */
1080
+
1081
+ DEFPRINT(AST_Directive, function(self, output) {
1082
+ output.print_string(self.value, self.quote);
1083
+ output.semicolon();
1084
+ });
1085
+
1086
+ DEFPRINT(AST_Expansion, function (self, output) {
1087
+ output.print("...");
1088
+ self.expression.print(output);
1089
+ });
1090
+
1091
+ DEFPRINT(AST_Destructuring, function (self, output) {
1092
+ output.print(self.is_array ? "[" : "{");
1093
+ var len = self.names.length;
1094
+ self.names.forEach(function (name, i) {
1095
+ if (i > 0) output.comma();
1096
+ name.print(output);
1097
+ // If the final element is a hole, we need to make sure it
1098
+ // doesn't look like a trailing comma, by inserting an actual
1099
+ // trailing comma.
1100
+ if (i == len - 1 && name instanceof AST_Hole) output.comma();
1101
+ });
1102
+ output.print(self.is_array ? "]" : "}");
1103
+ });
1104
+
1105
+ DEFPRINT(AST_Debugger, function(self, output) {
1106
+ output.print("debugger");
1107
+ output.semicolon();
1108
+ });
1109
+
1110
+ /* -----[ statements ]----- */
1111
+
1112
+ function display_body(body, is_toplevel, output, allow_directives) {
1113
+ var last = body.length - 1;
1114
+ output.in_directive = allow_directives;
1115
+ body.forEach(function(stmt, i) {
1116
+ if (output.in_directive === true && !(stmt instanceof AST_Directive ||
1117
+ stmt instanceof AST_EmptyStatement ||
1118
+ (stmt instanceof AST_SimpleStatement && stmt.body instanceof AST_String)
1119
+ )) {
1120
+ output.in_directive = false;
1121
+ }
1122
+ if (!(stmt instanceof AST_EmptyStatement)) {
1123
+ output.indent();
1124
+ stmt.print(output);
1125
+ if (!(i == last && is_toplevel)) {
1126
+ output.newline();
1127
+ if (is_toplevel) output.newline();
1128
+ }
1129
+ }
1130
+ if (output.in_directive === true &&
1131
+ stmt instanceof AST_SimpleStatement &&
1132
+ stmt.body instanceof AST_String
1133
+ ) {
1134
+ output.in_directive = false;
1135
+ }
1136
+ });
1137
+ output.in_directive = false;
1138
+ }
1139
+
1140
+ AST_StatementWithBody.DEFMETHOD("_do_print_body", function(output) {
1141
+ force_statement(this.body, output);
1142
+ });
1143
+
1144
+ DEFPRINT(AST_Statement, function(self, output) {
1145
+ self.body.print(output);
1146
+ output.semicolon();
1147
+ });
1148
+ DEFPRINT(AST_Toplevel, function(self, output) {
1149
+ display_body(self.body, true, output, true);
1150
+ output.print("");
1151
+ });
1152
+ DEFPRINT(AST_LabeledStatement, function(self, output) {
1153
+ self.label.print(output);
1154
+ output.colon();
1155
+ self.body.print(output);
1156
+ });
1157
+ DEFPRINT(AST_SimpleStatement, function(self, output) {
1158
+ self.body.print(output);
1159
+ output.semicolon();
1160
+ });
1161
+ function print_braced_empty(self, output) {
1162
+ output.print("{");
1163
+ output.with_indent(output.next_indent(), function() {
1164
+ output.append_comments(self, true);
1165
+ });
1166
+ output.print("}");
1167
+ }
1168
+ function print_braced(self, output, allow_directives) {
1169
+ if (self.body.length > 0) {
1170
+ output.with_block(function() {
1171
+ display_body(self.body, false, output, allow_directives);
1172
+ });
1173
+ } else print_braced_empty(self, output);
1174
+ }
1175
+ DEFPRINT(AST_BlockStatement, function(self, output) {
1176
+ print_braced(self, output);
1177
+ });
1178
+ DEFPRINT(AST_EmptyStatement, function(self, output) {
1179
+ output.semicolon();
1180
+ });
1181
+ DEFPRINT(AST_Do, function(self, output) {
1182
+ output.print("do");
1183
+ output.space();
1184
+ make_block(self.body, output);
1185
+ output.space();
1186
+ output.print("while");
1187
+ output.space();
1188
+ output.with_parens(function() {
1189
+ self.condition.print(output);
1190
+ });
1191
+ output.semicolon();
1192
+ });
1193
+ DEFPRINT(AST_While, function(self, output) {
1194
+ output.print("while");
1195
+ output.space();
1196
+ output.with_parens(function() {
1197
+ self.condition.print(output);
1198
+ });
1199
+ output.space();
1200
+ self._do_print_body(output);
1201
+ });
1202
+ DEFPRINT(AST_For, function(self, output) {
1203
+ output.print("for");
1204
+ output.space();
1205
+ output.with_parens(function() {
1206
+ if (self.init) {
1207
+ if (self.init instanceof AST_Definitions) {
1208
+ self.init.print(output);
1209
+ } else {
1210
+ parenthesize_for_noin(self.init, output, true);
1211
+ }
1212
+ output.print(";");
1213
+ output.space();
1214
+ } else {
1215
+ output.print(";");
1216
+ }
1217
+ if (self.condition) {
1218
+ self.condition.print(output);
1219
+ output.print(";");
1220
+ output.space();
1221
+ } else {
1222
+ output.print(";");
1223
+ }
1224
+ if (self.step) {
1225
+ self.step.print(output);
1226
+ }
1227
+ });
1228
+ output.space();
1229
+ self._do_print_body(output);
1230
+ });
1231
+ DEFPRINT(AST_ForIn, function(self, output) {
1232
+ output.print("for");
1233
+ if (self.await) {
1234
+ output.space();
1235
+ output.print("await");
1236
+ }
1237
+ output.space();
1238
+ output.with_parens(function() {
1239
+ self.init.print(output);
1240
+ output.space();
1241
+ output.print(self instanceof AST_ForOf ? "of" : "in");
1242
+ output.space();
1243
+ self.object.print(output);
1244
+ });
1245
+ output.space();
1246
+ self._do_print_body(output);
1247
+ });
1248
+ DEFPRINT(AST_With, function(self, output) {
1249
+ output.print("with");
1250
+ output.space();
1251
+ output.with_parens(function() {
1252
+ self.expression.print(output);
1253
+ });
1254
+ output.space();
1255
+ self._do_print_body(output);
1256
+ });
1257
+
1258
+ /* -----[ functions ]----- */
1259
+ AST_Lambda.DEFMETHOD("_do_print", function(output, nokeyword) {
1260
+ var self = this;
1261
+ if (!nokeyword) {
1262
+ if (self.async) {
1263
+ output.print("async");
1264
+ output.space();
1265
+ }
1266
+ output.print("function");
1267
+ if (self.is_generator) {
1268
+ output.star();
1269
+ }
1270
+ if (self.name) {
1271
+ output.space();
1272
+ }
1273
+ }
1274
+ if (self.name instanceof AST_Symbol) {
1275
+ self.name.print(output);
1276
+ } else if (nokeyword && self.name instanceof AST_Node) {
1277
+ output.with_square(function() {
1278
+ self.name.print(output); // Computed method name
1279
+ });
1280
+ }
1281
+ output.with_parens(function() {
1282
+ self.argnames.forEach(function(arg, i) {
1283
+ if (i) output.comma();
1284
+ arg.print(output);
1285
+ });
1286
+ });
1287
+ output.space();
1288
+ print_braced(self, output, true);
1289
+ });
1290
+ DEFPRINT(AST_Lambda, function(self, output) {
1291
+ self._do_print(output);
1292
+ });
1293
+
1294
+ DEFPRINT(AST_PrefixedTemplateString, function(self, output) {
1295
+ var tag = self.prefix;
1296
+ var parenthesize_tag = tag instanceof AST_Lambda
1297
+ || tag instanceof AST_Binary
1298
+ || tag instanceof AST_Conditional
1299
+ || tag instanceof AST_Sequence
1300
+ || tag instanceof AST_Unary
1301
+ || tag instanceof AST_Dot && tag.expression instanceof AST_Object;
1302
+ if (parenthesize_tag) output.print("(");
1303
+ self.prefix.print(output);
1304
+ if (parenthesize_tag) output.print(")");
1305
+ self.template_string.print(output);
1306
+ });
1307
+ DEFPRINT(AST_TemplateString, function(self, output) {
1308
+ var is_tagged = output.parent() instanceof AST_PrefixedTemplateString;
1309
+
1310
+ output.print("`");
1311
+ for (var i = 0; i < self.segments.length; i++) {
1312
+ if (!(self.segments[i] instanceof AST_TemplateSegment)) {
1313
+ output.print("${");
1314
+ self.segments[i].print(output);
1315
+ output.print("}");
1316
+ } else if (is_tagged) {
1317
+ output.print(self.segments[i].raw);
1318
+ } else {
1319
+ output.print_template_string_chars(self.segments[i].value);
1320
+ }
1321
+ }
1322
+ output.print("`");
1323
+ });
1324
+
1325
+ AST_Arrow.DEFMETHOD("_do_print", function(output) {
1326
+ var self = this;
1327
+ var parent = output.parent();
1328
+ var needs_parens = (parent instanceof AST_Binary && !(parent instanceof AST_Assign)) ||
1329
+ parent instanceof AST_Unary ||
1330
+ (parent instanceof AST_Call && self === parent.expression);
1331
+ if (needs_parens) { output.print("("); }
1332
+ if (self.async) {
1333
+ output.print("async");
1334
+ output.space();
1335
+ }
1336
+ if (self.argnames.length === 1 && self.argnames[0] instanceof AST_Symbol) {
1337
+ self.argnames[0].print(output);
1338
+ } else {
1339
+ output.with_parens(function() {
1340
+ self.argnames.forEach(function(arg, i) {
1341
+ if (i) output.comma();
1342
+ arg.print(output);
1343
+ });
1344
+ });
1345
+ }
1346
+ output.space();
1347
+ output.print("=>");
1348
+ output.space();
1349
+ const first_statement = self.body[0];
1350
+ if (
1351
+ self.body.length === 1
1352
+ && first_statement instanceof AST_Return
1353
+ ) {
1354
+ const returned = first_statement.value;
1355
+ if (!returned) {
1356
+ output.print("{}");
1357
+ } else if (left_is_object(returned)) {
1358
+ output.print("(");
1359
+ returned.print(output);
1360
+ output.print(")");
1361
+ } else {
1362
+ returned.print(output);
1363
+ }
1364
+ } else {
1365
+ print_braced(self, output);
1366
+ }
1367
+ if (needs_parens) { output.print(")"); }
1368
+ });
1369
+
1370
+ /* -----[ exits ]----- */
1371
+ AST_Exit.DEFMETHOD("_do_print", function(output, kind) {
1372
+ output.print(kind);
1373
+ if (this.value) {
1374
+ output.space();
1375
+ const comments = this.value.start.comments_before;
1376
+ if (comments && comments.length && !output.printed_comments.has(comments)) {
1377
+ output.print("(");
1378
+ this.value.print(output);
1379
+ output.print(")");
1380
+ } else {
1381
+ this.value.print(output);
1382
+ }
1383
+ }
1384
+ output.semicolon();
1385
+ });
1386
+ DEFPRINT(AST_Return, function(self, output) {
1387
+ self._do_print(output, "return");
1388
+ });
1389
+ DEFPRINT(AST_Throw, function(self, output) {
1390
+ self._do_print(output, "throw");
1391
+ });
1392
+
1393
+ /* -----[ yield ]----- */
1394
+
1395
+ DEFPRINT(AST_Yield, function(self, output) {
1396
+ var star = self.is_star ? "*" : "";
1397
+ output.print("yield" + star);
1398
+ if (self.expression) {
1399
+ output.space();
1400
+ self.expression.print(output);
1401
+ }
1402
+ });
1403
+
1404
+ DEFPRINT(AST_Await, function(self, output) {
1405
+ output.print("await");
1406
+ output.space();
1407
+ var e = self.expression;
1408
+ var parens = !(
1409
+ e instanceof AST_Call
1410
+ || e instanceof AST_SymbolRef
1411
+ || e instanceof AST_PropAccess
1412
+ || e instanceof AST_Unary
1413
+ || e instanceof AST_Constant
1414
+ );
1415
+ if (parens) output.print("(");
1416
+ self.expression.print(output);
1417
+ if (parens) output.print(")");
1418
+ });
1419
+
1420
+ /* -----[ loop control ]----- */
1421
+ AST_LoopControl.DEFMETHOD("_do_print", function(output, kind) {
1422
+ output.print(kind);
1423
+ if (this.label) {
1424
+ output.space();
1425
+ this.label.print(output);
1426
+ }
1427
+ output.semicolon();
1428
+ });
1429
+ DEFPRINT(AST_Break, function(self, output) {
1430
+ self._do_print(output, "break");
1431
+ });
1432
+ DEFPRINT(AST_Continue, function(self, output) {
1433
+ self._do_print(output, "continue");
1434
+ });
1435
+
1436
+ /* -----[ if ]----- */
1437
+ function make_then(self, output) {
1438
+ var b = self.body;
1439
+ if (output.option("braces")
1440
+ || output.option("ie8") && b instanceof AST_Do)
1441
+ return make_block(b, output);
1442
+ // The squeezer replaces "block"-s that contain only a single
1443
+ // statement with the statement itself; technically, the AST
1444
+ // is correct, but this can create problems when we output an
1445
+ // IF having an ELSE clause where the THEN clause ends in an
1446
+ // IF *without* an ELSE block (then the outer ELSE would refer
1447
+ // to the inner IF). This function checks for this case and
1448
+ // adds the block braces if needed.
1449
+ if (!b) return output.force_semicolon();
1450
+ while (true) {
1451
+ if (b instanceof AST_If) {
1452
+ if (!b.alternative) {
1453
+ make_block(self.body, output);
1454
+ return;
1455
+ }
1456
+ b = b.alternative;
1457
+ } else if (b instanceof AST_StatementWithBody) {
1458
+ b = b.body;
1459
+ } else break;
1460
+ }
1461
+ force_statement(self.body, output);
1462
+ }
1463
+ DEFPRINT(AST_If, function(self, output) {
1464
+ output.print("if");
1465
+ output.space();
1466
+ output.with_parens(function() {
1467
+ self.condition.print(output);
1468
+ });
1469
+ output.space();
1470
+ if (self.alternative) {
1471
+ make_then(self, output);
1472
+ output.space();
1473
+ output.print("else");
1474
+ output.space();
1475
+ if (self.alternative instanceof AST_If)
1476
+ self.alternative.print(output);
1477
+ else
1478
+ force_statement(self.alternative, output);
1479
+ } else {
1480
+ self._do_print_body(output);
1481
+ }
1482
+ });
1483
+
1484
+ /* -----[ switch ]----- */
1485
+ DEFPRINT(AST_Switch, function(self, output) {
1486
+ output.print("switch");
1487
+ output.space();
1488
+ output.with_parens(function() {
1489
+ self.expression.print(output);
1490
+ });
1491
+ output.space();
1492
+ var last = self.body.length - 1;
1493
+ if (last < 0) print_braced_empty(self, output);
1494
+ else output.with_block(function() {
1495
+ self.body.forEach(function(branch, i) {
1496
+ output.indent(true);
1497
+ branch.print(output);
1498
+ if (i < last && branch.body.length > 0)
1499
+ output.newline();
1500
+ });
1501
+ });
1502
+ });
1503
+ AST_SwitchBranch.DEFMETHOD("_do_print_body", function(output) {
1504
+ output.newline();
1505
+ this.body.forEach(function(stmt) {
1506
+ output.indent();
1507
+ stmt.print(output);
1508
+ output.newline();
1509
+ });
1510
+ });
1511
+ DEFPRINT(AST_Default, function(self, output) {
1512
+ output.print("default:");
1513
+ self._do_print_body(output);
1514
+ });
1515
+ DEFPRINT(AST_Case, function(self, output) {
1516
+ output.print("case");
1517
+ output.space();
1518
+ self.expression.print(output);
1519
+ output.print(":");
1520
+ self._do_print_body(output);
1521
+ });
1522
+
1523
+ /* -----[ exceptions ]----- */
1524
+ DEFPRINT(AST_Try, function(self, output) {
1525
+ output.print("try");
1526
+ output.space();
1527
+ print_braced(self, output);
1528
+ if (self.bcatch) {
1529
+ output.space();
1530
+ self.bcatch.print(output);
1531
+ }
1532
+ if (self.bfinally) {
1533
+ output.space();
1534
+ self.bfinally.print(output);
1535
+ }
1536
+ });
1537
+ DEFPRINT(AST_Catch, function(self, output) {
1538
+ output.print("catch");
1539
+ if (self.argname) {
1540
+ output.space();
1541
+ output.with_parens(function() {
1542
+ self.argname.print(output);
1543
+ });
1544
+ }
1545
+ output.space();
1546
+ print_braced(self, output);
1547
+ });
1548
+ DEFPRINT(AST_Finally, function(self, output) {
1549
+ output.print("finally");
1550
+ output.space();
1551
+ print_braced(self, output);
1552
+ });
1553
+
1554
+ /* -----[ var/const ]----- */
1555
+ AST_Definitions.DEFMETHOD("_do_print", function(output, kind) {
1556
+ output.print(kind);
1557
+ output.space();
1558
+ this.definitions.forEach(function(def, i) {
1559
+ if (i) output.comma();
1560
+ def.print(output);
1561
+ });
1562
+ var p = output.parent();
1563
+ var in_for = p instanceof AST_For || p instanceof AST_ForIn;
1564
+ var output_semicolon = !in_for || p && p.init !== this;
1565
+ if (output_semicolon)
1566
+ output.semicolon();
1567
+ });
1568
+ DEFPRINT(AST_Let, function(self, output) {
1569
+ self._do_print(output, "let");
1570
+ });
1571
+ DEFPRINT(AST_Var, function(self, output) {
1572
+ self._do_print(output, "var");
1573
+ });
1574
+ DEFPRINT(AST_Const, function(self, output) {
1575
+ self._do_print(output, "const");
1576
+ });
1577
+ DEFPRINT(AST_Import, function(self, output) {
1578
+ output.print("import");
1579
+ output.space();
1580
+ if (self.imported_name) {
1581
+ self.imported_name.print(output);
1582
+ }
1583
+ if (self.imported_name && self.imported_names) {
1584
+ output.print(",");
1585
+ output.space();
1586
+ }
1587
+ if (self.imported_names) {
1588
+ if (self.imported_names.length === 1 && self.imported_names[0].foreign_name.name === "*") {
1589
+ self.imported_names[0].print(output);
1590
+ } else {
1591
+ output.print("{");
1592
+ self.imported_names.forEach(function (name_import, i) {
1593
+ output.space();
1594
+ name_import.print(output);
1595
+ if (i < self.imported_names.length - 1) {
1596
+ output.print(",");
1597
+ }
1598
+ });
1599
+ output.space();
1600
+ output.print("}");
1601
+ }
1602
+ }
1603
+ if (self.imported_name || self.imported_names) {
1604
+ output.space();
1605
+ output.print("from");
1606
+ output.space();
1607
+ }
1608
+ self.module_name.print(output);
1609
+ output.semicolon();
1610
+ });
1611
+
1612
+ DEFPRINT(AST_NameMapping, function(self, output) {
1613
+ var is_import = output.parent() instanceof AST_Import;
1614
+ var definition = self.name.definition();
1615
+ var names_are_different =
1616
+ (definition && definition.mangled_name || self.name.name) !==
1617
+ self.foreign_name.name;
1618
+ if (names_are_different) {
1619
+ if (is_import) {
1620
+ output.print(self.foreign_name.name);
1621
+ } else {
1622
+ self.name.print(output);
1623
+ }
1624
+ output.space();
1625
+ output.print("as");
1626
+ output.space();
1627
+ if (is_import) {
1628
+ self.name.print(output);
1629
+ } else {
1630
+ output.print(self.foreign_name.name);
1631
+ }
1632
+ } else {
1633
+ self.name.print(output);
1634
+ }
1635
+ });
1636
+
1637
+ DEFPRINT(AST_Export, function(self, output) {
1638
+ output.print("export");
1639
+ output.space();
1640
+ if (self.is_default) {
1641
+ output.print("default");
1642
+ output.space();
1643
+ }
1644
+ if (self.exported_names) {
1645
+ if (self.exported_names.length === 1 && self.exported_names[0].name.name === "*") {
1646
+ self.exported_names[0].print(output);
1647
+ } else {
1648
+ output.print("{");
1649
+ self.exported_names.forEach(function(name_export, i) {
1650
+ output.space();
1651
+ name_export.print(output);
1652
+ if (i < self.exported_names.length - 1) {
1653
+ output.print(",");
1654
+ }
1655
+ });
1656
+ output.space();
1657
+ output.print("}");
1658
+ }
1659
+ } else if (self.exported_value) {
1660
+ self.exported_value.print(output);
1661
+ } else if (self.exported_definition) {
1662
+ self.exported_definition.print(output);
1663
+ if (self.exported_definition instanceof AST_Definitions) return;
1664
+ }
1665
+ if (self.module_name) {
1666
+ output.space();
1667
+ output.print("from");
1668
+ output.space();
1669
+ self.module_name.print(output);
1670
+ }
1671
+ if (self.exported_value
1672
+ && !(self.exported_value instanceof AST_Defun ||
1673
+ self.exported_value instanceof AST_Function ||
1674
+ self.exported_value instanceof AST_Class)
1675
+ || self.module_name
1676
+ || self.exported_names
1677
+ ) {
1678
+ output.semicolon();
1679
+ }
1680
+ });
1681
+
1682
+ function parenthesize_for_noin(node, output, noin) {
1683
+ var parens = false;
1684
+ // need to take some precautions here:
1685
+ // https://github.com/mishoo/UglifyJS2/issues/60
1686
+ if (noin) {
1687
+ parens = walk(node, node => {
1688
+ if (node instanceof AST_Scope) return true;
1689
+ if (node instanceof AST_Binary && node.operator == "in") {
1690
+ return walk_abort; // makes walk() return true
1691
+ }
1692
+ });
1693
+ }
1694
+ node.print(output, parens);
1695
+ }
1696
+
1697
+ DEFPRINT(AST_VarDef, function(self, output) {
1698
+ self.name.print(output);
1699
+ if (self.value) {
1700
+ output.space();
1701
+ output.print("=");
1702
+ output.space();
1703
+ var p = output.parent(1);
1704
+ var noin = p instanceof AST_For || p instanceof AST_ForIn;
1705
+ parenthesize_for_noin(self.value, output, noin);
1706
+ }
1707
+ });
1708
+
1709
+ /* -----[ other expressions ]----- */
1710
+ DEFPRINT(AST_Call, function(self, output) {
1711
+ self.expression.print(output);
1712
+ if (self instanceof AST_New && self.args.length === 0)
1713
+ return;
1714
+ if (self.expression instanceof AST_Call || self.expression instanceof AST_Lambda) {
1715
+ output.add_mapping(self.start);
1716
+ }
1717
+ output.with_parens(function() {
1718
+ self.args.forEach(function(expr, i) {
1719
+ if (i) output.comma();
1720
+ expr.print(output);
1721
+ });
1722
+ });
1723
+ });
1724
+ DEFPRINT(AST_New, function(self, output) {
1725
+ output.print("new");
1726
+ output.space();
1727
+ AST_Call.prototype._codegen(self, output);
1728
+ });
1729
+
1730
+ AST_Sequence.DEFMETHOD("_do_print", function(output) {
1731
+ this.expressions.forEach(function(node, index) {
1732
+ if (index > 0) {
1733
+ output.comma();
1734
+ if (output.should_break()) {
1735
+ output.newline();
1736
+ output.indent();
1737
+ }
1738
+ }
1739
+ node.print(output);
1740
+ });
1741
+ });
1742
+ DEFPRINT(AST_Sequence, function(self, output) {
1743
+ self._do_print(output);
1744
+ // var p = output.parent();
1745
+ // if (p instanceof AST_Statement) {
1746
+ // output.with_indent(output.next_indent(), function(){
1747
+ // self._do_print(output);
1748
+ // });
1749
+ // } else {
1750
+ // self._do_print(output);
1751
+ // }
1752
+ });
1753
+ DEFPRINT(AST_Dot, function(self, output) {
1754
+ var expr = self.expression;
1755
+ expr.print(output);
1756
+ var prop = self.property;
1757
+ var print_computed = RESERVED_WORDS.has(prop)
1758
+ ? output.option("ie8")
1759
+ : !is_identifier_string(prop, output.option("ecma") >= 2015);
1760
+ if (print_computed) {
1761
+ output.print("[");
1762
+ output.add_mapping(self.end);
1763
+ output.print_string(prop);
1764
+ output.print("]");
1765
+ } else {
1766
+ if (expr instanceof AST_Number && expr.getValue() >= 0) {
1767
+ if (!/[xa-f.)]/i.test(output.last())) {
1768
+ output.print(".");
1769
+ }
1770
+ }
1771
+ output.print(".");
1772
+ // the name after dot would be mapped about here.
1773
+ output.add_mapping(self.end);
1774
+ output.print_name(prop);
1775
+ }
1776
+ });
1777
+ DEFPRINT(AST_Sub, function(self, output) {
1778
+ self.expression.print(output);
1779
+ output.print("[");
1780
+ self.property.print(output);
1781
+ output.print("]");
1782
+ });
1783
+ DEFPRINT(AST_UnaryPrefix, function(self, output) {
1784
+ var op = self.operator;
1785
+ output.print(op);
1786
+ if (/^[a-z]/i.test(op)
1787
+ || (/[+-]$/.test(op)
1788
+ && self.expression instanceof AST_UnaryPrefix
1789
+ && /^[+-]/.test(self.expression.operator))) {
1790
+ output.space();
1791
+ }
1792
+ self.expression.print(output);
1793
+ });
1794
+ DEFPRINT(AST_UnaryPostfix, function(self, output) {
1795
+ self.expression.print(output);
1796
+ output.print(self.operator);
1797
+ });
1798
+ DEFPRINT(AST_Binary, function(self, output) {
1799
+ var op = self.operator;
1800
+ self.left.print(output);
1801
+ if (op[0] == ">" /* ">>" ">>>" ">" ">=" */
1802
+ && self.left instanceof AST_UnaryPostfix
1803
+ && self.left.operator == "--") {
1804
+ // space is mandatory to avoid outputting -->
1805
+ output.print(" ");
1806
+ } else {
1807
+ // the space is optional depending on "beautify"
1808
+ output.space();
1809
+ }
1810
+ output.print(op);
1811
+ if ((op == "<" || op == "<<")
1812
+ && self.right instanceof AST_UnaryPrefix
1813
+ && self.right.operator == "!"
1814
+ && self.right.expression instanceof AST_UnaryPrefix
1815
+ && self.right.expression.operator == "--") {
1816
+ // space is mandatory to avoid outputting <!--
1817
+ output.print(" ");
1818
+ } else {
1819
+ // the space is optional depending on "beautify"
1820
+ output.space();
1821
+ }
1822
+ self.right.print(output);
1823
+ });
1824
+ DEFPRINT(AST_Conditional, function(self, output) {
1825
+ self.condition.print(output);
1826
+ output.space();
1827
+ output.print("?");
1828
+ output.space();
1829
+ self.consequent.print(output);
1830
+ output.space();
1831
+ output.colon();
1832
+ self.alternative.print(output);
1833
+ });
1834
+
1835
+ /* -----[ literals ]----- */
1836
+ DEFPRINT(AST_Array, function(self, output) {
1837
+ output.with_square(function() {
1838
+ var a = self.elements, len = a.length;
1839
+ if (len > 0) output.space();
1840
+ a.forEach(function(exp, i) {
1841
+ if (i) output.comma();
1842
+ exp.print(output);
1843
+ // If the final element is a hole, we need to make sure it
1844
+ // doesn't look like a trailing comma, by inserting an actual
1845
+ // trailing comma.
1846
+ if (i === len - 1 && exp instanceof AST_Hole)
1847
+ output.comma();
1848
+ });
1849
+ if (len > 0) output.space();
1850
+ });
1851
+ });
1852
+ DEFPRINT(AST_Object, function(self, output) {
1853
+ if (self.properties.length > 0) output.with_block(function() {
1854
+ self.properties.forEach(function(prop, i) {
1855
+ if (i) {
1856
+ output.print(",");
1857
+ output.newline();
1858
+ }
1859
+ output.indent();
1860
+ prop.print(output);
1861
+ });
1862
+ output.newline();
1863
+ });
1864
+ else print_braced_empty(self, output);
1865
+ });
1866
+ DEFPRINT(AST_Class, function(self, output) {
1867
+ output.print("class");
1868
+ output.space();
1869
+ if (self.name) {
1870
+ self.name.print(output);
1871
+ output.space();
1872
+ }
1873
+ if (self.extends) {
1874
+ var parens = (
1875
+ !(self.extends instanceof AST_SymbolRef)
1876
+ && !(self.extends instanceof AST_PropAccess)
1877
+ && !(self.extends instanceof AST_ClassExpression)
1878
+ && !(self.extends instanceof AST_Function)
1879
+ );
1880
+ output.print("extends");
1881
+ if (parens) {
1882
+ output.print("(");
1883
+ } else {
1884
+ output.space();
1885
+ }
1886
+ self.extends.print(output);
1887
+ if (parens) {
1888
+ output.print(")");
1889
+ } else {
1890
+ output.space();
1891
+ }
1892
+ }
1893
+ if (self.properties.length > 0) output.with_block(function() {
1894
+ self.properties.forEach(function(prop, i) {
1895
+ if (i) {
1896
+ output.newline();
1897
+ }
1898
+ output.indent();
1899
+ prop.print(output);
1900
+ });
1901
+ output.newline();
1902
+ });
1903
+ else output.print("{}");
1904
+ });
1905
+ DEFPRINT(AST_NewTarget, function(self, output) {
1906
+ output.print("new.target");
1907
+ });
1908
+
1909
+ function print_property_name(key, quote, output) {
1910
+ if (output.option("quote_keys")) {
1911
+ return output.print_string(key);
1912
+ }
1913
+ if ("" + +key == key && key >= 0) {
1914
+ if (output.option("keep_numbers")) {
1915
+ return output.print(key);
1916
+ }
1917
+ return output.print(make_num(key));
1918
+ }
1919
+ var print_string = RESERVED_WORDS.has(key)
1920
+ ? output.option("ie8")
1921
+ : (
1922
+ output.option("ecma") < 2015
1923
+ ? !is_basic_identifier_string(key)
1924
+ : !is_identifier_string(key, true)
1925
+ );
1926
+ if (print_string || (quote && output.option("keep_quoted_props"))) {
1927
+ return output.print_string(key, quote);
1928
+ }
1929
+ return output.print_name(key);
1930
+ }
1931
+
1932
+ DEFPRINT(AST_ObjectKeyVal, function(self, output) {
1933
+ function get_name(self) {
1934
+ var def = self.definition();
1935
+ return def ? def.mangled_name || def.name : self.name;
1936
+ }
1937
+
1938
+ var allowShortHand = output.option("shorthand");
1939
+ if (allowShortHand &&
1940
+ self.value instanceof AST_Symbol &&
1941
+ is_identifier_string(self.key, output.option("ecma") >= 2015) &&
1942
+ get_name(self.value) === self.key &&
1943
+ !RESERVED_WORDS.has(self.key)
1944
+ ) {
1945
+ print_property_name(self.key, self.quote, output);
1946
+
1947
+ } else if (allowShortHand &&
1948
+ self.value instanceof AST_DefaultAssign &&
1949
+ self.value.left instanceof AST_Symbol &&
1950
+ is_identifier_string(self.key, output.option("ecma") >= 2015) &&
1951
+ get_name(self.value.left) === self.key
1952
+ ) {
1953
+ print_property_name(self.key, self.quote, output);
1954
+ output.space();
1955
+ output.print("=");
1956
+ output.space();
1957
+ self.value.right.print(output);
1958
+ } else {
1959
+ if (!(self.key instanceof AST_Node)) {
1960
+ print_property_name(self.key, self.quote, output);
1961
+ } else {
1962
+ output.with_square(function() {
1963
+ self.key.print(output);
1964
+ });
1965
+ }
1966
+ output.colon();
1967
+ self.value.print(output);
1968
+ }
1969
+ });
1970
+ DEFPRINT(AST_ClassProperty, (self, output) => {
1971
+ if (self.static) {
1972
+ output.print("static");
1973
+ output.space();
1974
+ }
1975
+
1976
+ if (self.key instanceof AST_SymbolClassProperty) {
1977
+ print_property_name(self.key.name, self.quote, output);
1978
+ } else {
1979
+ output.print("[");
1980
+ self.key.print(output);
1981
+ output.print("]");
1982
+ }
1983
+
1984
+ if (self.value) {
1985
+ output.print("=");
1986
+ self.value.print(output);
1987
+ }
1988
+
1989
+ output.semicolon();
1990
+ });
1991
+ AST_ObjectProperty.DEFMETHOD("_print_getter_setter", function(type, output) {
1992
+ var self = this;
1993
+ if (self.static) {
1994
+ output.print("static");
1995
+ output.space();
1996
+ }
1997
+ if (type) {
1998
+ output.print(type);
1999
+ output.space();
2000
+ }
2001
+ if (self.key instanceof AST_SymbolMethod) {
2002
+ print_property_name(self.key.name, self.quote, output);
2003
+ } else {
2004
+ output.with_square(function() {
2005
+ self.key.print(output);
2006
+ });
2007
+ }
2008
+ self.value._do_print(output, true);
2009
+ });
2010
+ DEFPRINT(AST_ObjectSetter, function(self, output) {
2011
+ self._print_getter_setter("set", output);
2012
+ });
2013
+ DEFPRINT(AST_ObjectGetter, function(self, output) {
2014
+ self._print_getter_setter("get", output);
2015
+ });
2016
+ DEFPRINT(AST_ConciseMethod, function(self, output) {
2017
+ var type;
2018
+ if (self.is_generator && self.async) {
2019
+ type = "async*";
2020
+ } else if (self.is_generator) {
2021
+ type = "*";
2022
+ } else if (self.async) {
2023
+ type = "async";
2024
+ }
2025
+ self._print_getter_setter(type, output);
2026
+ });
2027
+ AST_Symbol.DEFMETHOD("_do_print", function(output) {
2028
+ var def = this.definition();
2029
+ output.print_name(def ? def.mangled_name || def.name : this.name);
2030
+ });
2031
+ DEFPRINT(AST_Symbol, function (self, output) {
2032
+ self._do_print(output);
2033
+ });
2034
+ DEFPRINT(AST_Hole, noop);
2035
+ DEFPRINT(AST_This, function(self, output) {
2036
+ output.print("this");
2037
+ });
2038
+ DEFPRINT(AST_Super, function(self, output) {
2039
+ output.print("super");
2040
+ });
2041
+ DEFPRINT(AST_Constant, function(self, output) {
2042
+ output.print(self.getValue());
2043
+ });
2044
+ DEFPRINT(AST_String, function(self, output) {
2045
+ output.print_string(self.getValue(), self.quote, output.in_directive);
2046
+ });
2047
+ DEFPRINT(AST_Number, function(self, output) {
2048
+ if ((output.option("keep_numbers") || output.use_asm) && self.start && self.start.raw != null) {
2049
+ output.print(self.start.raw);
2050
+ } else {
2051
+ output.print(make_num(self.getValue()));
2052
+ }
2053
+ });
2054
+ DEFPRINT(AST_BigInt, function(self, output) {
2055
+ output.print(self.getValue() + "n");
2056
+ });
2057
+
2058
+ const r_slash_script = /(<\s*\/\s*script)/i;
2059
+ const slash_script_replace = (_, $1) => $1.replace("/", "\\/");
2060
+ DEFPRINT(AST_RegExp, function(self, output) {
2061
+ let { source, flags } = self.getValue();
2062
+ source = regexp_source_fix(source);
2063
+ flags = flags ? sort_regexp_flags(flags) : "";
2064
+ source = source.replace(r_slash_script, slash_script_replace);
2065
+ output.print(output.to_utf8(`/${source}/${flags}`));
2066
+ const parent = output.parent();
2067
+ if (
2068
+ parent instanceof AST_Binary
2069
+ && /^\w/.test(parent.operator)
2070
+ && parent.left === self
2071
+ ) {
2072
+ output.print(" ");
2073
+ }
2074
+ });
2075
+
2076
+ function force_statement(stat, output) {
2077
+ if (output.option("braces")) {
2078
+ make_block(stat, output);
2079
+ } else {
2080
+ if (!stat || stat instanceof AST_EmptyStatement)
2081
+ output.force_semicolon();
2082
+ else
2083
+ stat.print(output);
2084
+ }
2085
+ }
2086
+
2087
+ function best_of(a) {
2088
+ var best = a[0], len = best.length;
2089
+ for (var i = 1; i < a.length; ++i) {
2090
+ if (a[i].length < len) {
2091
+ best = a[i];
2092
+ len = best.length;
2093
+ }
2094
+ }
2095
+ return best;
2096
+ }
2097
+
2098
+ function make_num(num) {
2099
+ var str = num.toString(10).replace(/^0\./, ".").replace("e+", "e");
2100
+ var candidates = [ str ];
2101
+ if (Math.floor(num) === num) {
2102
+ if (num < 0) {
2103
+ candidates.push("-0x" + (-num).toString(16).toLowerCase());
2104
+ } else {
2105
+ candidates.push("0x" + num.toString(16).toLowerCase());
2106
+ }
2107
+ }
2108
+ var match, len, digits;
2109
+ if (match = /^\.0+/.exec(str)) {
2110
+ len = match[0].length;
2111
+ digits = str.slice(len);
2112
+ candidates.push(digits + "e-" + (digits.length + len - 1));
2113
+ } else if (match = /0+$/.exec(str)) {
2114
+ len = match[0].length;
2115
+ candidates.push(str.slice(0, -len) + "e" + len);
2116
+ } else if (match = /^(\d)\.(\d+)e(-?\d+)$/.exec(str)) {
2117
+ candidates.push(match[1] + match[2] + "e" + (match[3] - match[2].length));
2118
+ }
2119
+ return best_of(candidates);
2120
+ }
2121
+
2122
+ function make_block(stmt, output) {
2123
+ if (!stmt || stmt instanceof AST_EmptyStatement)
2124
+ output.print("{}");
2125
+ else if (stmt instanceof AST_BlockStatement)
2126
+ stmt.print(output);
2127
+ else output.with_block(function() {
2128
+ output.indent();
2129
+ stmt.print(output);
2130
+ output.newline();
2131
+ });
2132
+ }
2133
+
2134
+ /* -----[ source map generators ]----- */
2135
+
2136
+ function DEFMAP(nodetype, generator) {
2137
+ nodetype.forEach(function(nodetype) {
2138
+ nodetype.DEFMETHOD("add_source_map", generator);
2139
+ });
2140
+ }
2141
+
2142
+ DEFMAP([
2143
+ // We could easily add info for ALL nodes, but it seems to me that
2144
+ // would be quite wasteful, hence this noop in the base class.
2145
+ AST_Node,
2146
+ // since the label symbol will mark it
2147
+ AST_LabeledStatement,
2148
+ AST_Toplevel,
2149
+ ], noop);
2150
+
2151
+ // XXX: I'm not exactly sure if we need it for all of these nodes,
2152
+ // or if we should add even more.
2153
+ DEFMAP([
2154
+ AST_Array,
2155
+ AST_BlockStatement,
2156
+ AST_Catch,
2157
+ AST_Class,
2158
+ AST_Constant,
2159
+ AST_Debugger,
2160
+ AST_Definitions,
2161
+ AST_Directive,
2162
+ AST_Finally,
2163
+ AST_Jump,
2164
+ AST_Lambda,
2165
+ AST_New,
2166
+ AST_Object,
2167
+ AST_StatementWithBody,
2168
+ AST_Symbol,
2169
+ AST_Switch,
2170
+ AST_SwitchBranch,
2171
+ AST_TemplateString,
2172
+ AST_TemplateSegment,
2173
+ AST_Try,
2174
+ ], function(output) {
2175
+ output.add_mapping(this.start);
2176
+ });
2177
+
2178
+ DEFMAP([
2179
+ AST_ObjectGetter,
2180
+ AST_ObjectSetter,
2181
+ ], function(output) {
2182
+ output.add_mapping(this.start, this.key.name);
2183
+ });
2184
+
2185
+ DEFMAP([ AST_ObjectProperty ], function(output) {
2186
+ output.add_mapping(this.start, this.key);
2187
+ });
2188
+ })();
2189
+
2190
+ export {
2191
+ OutputStream,
2192
+ };