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.
@@ -0,0 +1,1243 @@
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
+ import * as ast from "./ast.js";
45
+ import { make_node } from "./utils/index.js";
46
+ import {
47
+ AST_Accessor,
48
+ AST_Array,
49
+ AST_Arrow,
50
+ AST_Assign,
51
+ AST_Atom,
52
+ AST_Await,
53
+ AST_BigInt,
54
+ AST_Binary,
55
+ AST_Block,
56
+ AST_BlockStatement,
57
+ AST_Boolean,
58
+ AST_Break,
59
+ AST_Call,
60
+ AST_Case,
61
+ AST_Catch,
62
+ AST_Class,
63
+ AST_ClassExpression,
64
+ AST_ClassProperty,
65
+ AST_ConciseMethod,
66
+ AST_Conditional,
67
+ AST_Const,
68
+ AST_Constant,
69
+ AST_Continue,
70
+ AST_Debugger,
71
+ AST_Default,
72
+ AST_DefaultAssign,
73
+ AST_DefClass,
74
+ AST_Definitions,
75
+ AST_Defun,
76
+ AST_Destructuring,
77
+ AST_Directive,
78
+ AST_Do,
79
+ AST_Dot,
80
+ AST_EmptyStatement,
81
+ AST_Expansion,
82
+ AST_Export,
83
+ AST_False,
84
+ AST_Finally,
85
+ AST_For,
86
+ AST_ForIn,
87
+ AST_ForOf,
88
+ AST_Function,
89
+ AST_Hole,
90
+ AST_If,
91
+ AST_Import,
92
+ AST_Label,
93
+ AST_LabeledStatement,
94
+ AST_LabelRef,
95
+ AST_Lambda,
96
+ AST_Let,
97
+ AST_NameMapping,
98
+ AST_New,
99
+ AST_NewTarget,
100
+ AST_Node,
101
+ AST_Null,
102
+ AST_Number,
103
+ AST_Object,
104
+ AST_ObjectGetter,
105
+ AST_ObjectKeyVal,
106
+ AST_ObjectProperty,
107
+ AST_ObjectSetter,
108
+ AST_PrefixedTemplateString,
109
+ AST_PropAccess,
110
+ AST_RegExp,
111
+ AST_Return,
112
+ AST_Sequence,
113
+ AST_SimpleStatement,
114
+ AST_Statement,
115
+ AST_String,
116
+ AST_Sub,
117
+ AST_Super,
118
+ AST_Switch,
119
+ AST_SwitchBranch,
120
+ AST_Symbol,
121
+ AST_SymbolCatch,
122
+ AST_SymbolClass,
123
+ AST_SymbolClassProperty,
124
+ AST_SymbolConst,
125
+ AST_SymbolDefClass,
126
+ AST_SymbolDefun,
127
+ AST_SymbolExport,
128
+ AST_SymbolExportForeign,
129
+ AST_SymbolFunarg,
130
+ AST_SymbolImport,
131
+ AST_SymbolImportForeign,
132
+ AST_SymbolLambda,
133
+ AST_SymbolLet,
134
+ AST_SymbolMethod,
135
+ AST_SymbolRef,
136
+ AST_SymbolVar,
137
+ AST_TemplateSegment,
138
+ AST_TemplateString,
139
+ AST_This,
140
+ AST_Throw,
141
+ AST_Token,
142
+ AST_Toplevel,
143
+ AST_True,
144
+ AST_Try,
145
+ AST_Unary,
146
+ AST_UnaryPostfix,
147
+ AST_UnaryPrefix,
148
+ AST_Var,
149
+ AST_VarDef,
150
+ AST_While,
151
+ AST_With,
152
+ AST_Yield,
153
+ } from "./ast.js";
154
+
155
+ (function() {
156
+
157
+ var normalize_directives = function(body) {
158
+ var in_directive = true;
159
+
160
+ for (var i = 0; i < body.length; i++) {
161
+ if (in_directive && body[i] instanceof AST_Statement && body[i].body instanceof AST_String) {
162
+ body[i] = new AST_Directive({
163
+ start: body[i].start,
164
+ end: body[i].end,
165
+ value: body[i].body.value
166
+ });
167
+ } else if (in_directive && !(body[i] instanceof AST_Statement && body[i].body instanceof AST_String)) {
168
+ in_directive = false;
169
+ }
170
+ }
171
+
172
+ return body;
173
+ };
174
+
175
+ var MOZ_TO_ME = {
176
+ Program: function(M) {
177
+ return new AST_Toplevel({
178
+ start: my_start_token(M),
179
+ end: my_end_token(M),
180
+ body: normalize_directives(M.body.map(from_moz))
181
+ });
182
+ },
183
+ ArrayPattern: function(M) {
184
+ return new AST_Destructuring({
185
+ start: my_start_token(M),
186
+ end: my_end_token(M),
187
+ names: M.elements.map(function(elm) {
188
+ if (elm === null) {
189
+ return new AST_Hole();
190
+ }
191
+ return from_moz(elm);
192
+ }),
193
+ is_array: true
194
+ });
195
+ },
196
+ ObjectPattern: function(M) {
197
+ return new AST_Destructuring({
198
+ start: my_start_token(M),
199
+ end: my_end_token(M),
200
+ names: M.properties.map(from_moz),
201
+ is_array: false
202
+ });
203
+ },
204
+ AssignmentPattern: function(M) {
205
+ return new AST_DefaultAssign({
206
+ start: my_start_token(M),
207
+ end: my_end_token(M),
208
+ left: from_moz(M.left),
209
+ operator: "=",
210
+ right: from_moz(M.right)
211
+ });
212
+ },
213
+ SpreadElement: function(M) {
214
+ return new AST_Expansion({
215
+ start: my_start_token(M),
216
+ end: my_end_token(M),
217
+ expression: from_moz(M.argument)
218
+ });
219
+ },
220
+ RestElement: function(M) {
221
+ return new AST_Expansion({
222
+ start: my_start_token(M),
223
+ end: my_end_token(M),
224
+ expression: from_moz(M.argument)
225
+ });
226
+ },
227
+ TemplateElement: function(M) {
228
+ return new AST_TemplateSegment({
229
+ start: my_start_token(M),
230
+ end: my_end_token(M),
231
+ value: M.value.cooked,
232
+ raw: M.value.raw
233
+ });
234
+ },
235
+ TemplateLiteral: function(M) {
236
+ var segments = [];
237
+ for (var i = 0; i < M.quasis.length; i++) {
238
+ segments.push(from_moz(M.quasis[i]));
239
+ if (M.expressions[i]) {
240
+ segments.push(from_moz(M.expressions[i]));
241
+ }
242
+ }
243
+ return new AST_TemplateString({
244
+ start: my_start_token(M),
245
+ end: my_end_token(M),
246
+ segments: segments
247
+ });
248
+ },
249
+ TaggedTemplateExpression: function(M) {
250
+ return new AST_PrefixedTemplateString({
251
+ start: my_start_token(M),
252
+ end: my_end_token(M),
253
+ template_string: from_moz(M.quasi),
254
+ prefix: from_moz(M.tag)
255
+ });
256
+ },
257
+ FunctionDeclaration: function(M) {
258
+ return new AST_Defun({
259
+ start: my_start_token(M),
260
+ end: my_end_token(M),
261
+ name: from_moz(M.id),
262
+ argnames: M.params.map(from_moz),
263
+ is_generator: M.generator,
264
+ async: M.async,
265
+ body: normalize_directives(from_moz(M.body).body)
266
+ });
267
+ },
268
+ FunctionExpression: function(M) {
269
+ return new AST_Function({
270
+ start: my_start_token(M),
271
+ end: my_end_token(M),
272
+ name: from_moz(M.id),
273
+ argnames: M.params.map(from_moz),
274
+ is_generator: M.generator,
275
+ async: M.async,
276
+ body: normalize_directives(from_moz(M.body).body)
277
+ });
278
+ },
279
+ ArrowFunctionExpression: function(M) {
280
+ const body = M.body.type === "BlockStatement"
281
+ ? from_moz(M.body).body
282
+ : [make_node(AST_Return, {}, { value: from_moz(M.body) })];
283
+ return new AST_Arrow({
284
+ start: my_start_token(M),
285
+ end: my_end_token(M),
286
+ argnames: M.params.map(from_moz),
287
+ body,
288
+ async: M.async,
289
+ });
290
+ },
291
+ ExpressionStatement: function(M) {
292
+ return new AST_SimpleStatement({
293
+ start: my_start_token(M),
294
+ end: my_end_token(M),
295
+ body: from_moz(M.expression)
296
+ });
297
+ },
298
+ TryStatement: function(M) {
299
+ var handlers = M.handlers || [M.handler];
300
+ if (handlers.length > 1 || M.guardedHandlers && M.guardedHandlers.length) {
301
+ throw new Error("Multiple catch clauses are not supported.");
302
+ }
303
+ return new AST_Try({
304
+ start : my_start_token(M),
305
+ end : my_end_token(M),
306
+ body : from_moz(M.block).body,
307
+ bcatch : from_moz(handlers[0]),
308
+ bfinally : M.finalizer ? new AST_Finally(from_moz(M.finalizer)) : null
309
+ });
310
+ },
311
+ Property: function(M) {
312
+ var key = M.key;
313
+ var args = {
314
+ start : my_start_token(key || M.value),
315
+ end : my_end_token(M.value),
316
+ key : key.type == "Identifier" ? key.name : key.value,
317
+ value : from_moz(M.value)
318
+ };
319
+ if (M.computed) {
320
+ args.key = from_moz(M.key);
321
+ }
322
+ if (M.method) {
323
+ args.is_generator = M.value.generator;
324
+ args.async = M.value.async;
325
+ if (!M.computed) {
326
+ args.key = new AST_SymbolMethod({ name: args.key });
327
+ } else {
328
+ args.key = from_moz(M.key);
329
+ }
330
+ return new AST_ConciseMethod(args);
331
+ }
332
+ if (M.kind == "init") {
333
+ if (key.type != "Identifier" && key.type != "Literal") {
334
+ args.key = from_moz(key);
335
+ }
336
+ return new AST_ObjectKeyVal(args);
337
+ }
338
+ if (typeof args.key === "string" || typeof args.key === "number") {
339
+ args.key = new AST_SymbolMethod({
340
+ name: args.key
341
+ });
342
+ }
343
+ args.value = new AST_Accessor(args.value);
344
+ if (M.kind == "get") return new AST_ObjectGetter(args);
345
+ if (M.kind == "set") return new AST_ObjectSetter(args);
346
+ if (M.kind == "method") {
347
+ args.async = M.value.async;
348
+ args.is_generator = M.value.generator;
349
+ args.quote = M.computed ? "\"" : null;
350
+ return new AST_ConciseMethod(args);
351
+ }
352
+ },
353
+ MethodDefinition: function(M) {
354
+ var args = {
355
+ start : my_start_token(M),
356
+ end : my_end_token(M),
357
+ key : M.computed ? from_moz(M.key) : new AST_SymbolMethod({ name: M.key.name || M.key.value }),
358
+ value : from_moz(M.value),
359
+ static : M.static,
360
+ };
361
+ if (M.kind == "get") {
362
+ return new AST_ObjectGetter(args);
363
+ }
364
+ if (M.kind == "set") {
365
+ return new AST_ObjectSetter(args);
366
+ }
367
+ args.is_generator = M.value.generator;
368
+ args.async = M.value.async;
369
+ return new AST_ConciseMethod(args);
370
+ },
371
+ FieldDefinition: function(M) {
372
+ let key;
373
+ if (M.computed) {
374
+ key = from_moz(M.key);
375
+ } else {
376
+ if (M.key.type !== "Identifier") throw new Error("Non-Identifier key in FieldDefinition");
377
+ key = from_moz(M.key);
378
+ }
379
+ return new AST_ClassProperty({
380
+ start : my_start_token(M),
381
+ end : my_end_token(M),
382
+ key,
383
+ value : from_moz(M.value),
384
+ static : M.static,
385
+ });
386
+ },
387
+ ArrayExpression: function(M) {
388
+ return new AST_Array({
389
+ start : my_start_token(M),
390
+ end : my_end_token(M),
391
+ elements : M.elements.map(function(elem) {
392
+ return elem === null ? new AST_Hole() : from_moz(elem);
393
+ })
394
+ });
395
+ },
396
+ ObjectExpression: function(M) {
397
+ return new AST_Object({
398
+ start : my_start_token(M),
399
+ end : my_end_token(M),
400
+ properties : M.properties.map(function(prop) {
401
+ if (prop.type === "SpreadElement") {
402
+ return from_moz(prop);
403
+ }
404
+ prop.type = "Property";
405
+ return from_moz(prop);
406
+ })
407
+ });
408
+ },
409
+ SequenceExpression: function(M) {
410
+ return new AST_Sequence({
411
+ start : my_start_token(M),
412
+ end : my_end_token(M),
413
+ expressions: M.expressions.map(from_moz)
414
+ });
415
+ },
416
+ MemberExpression: function(M) {
417
+ return new (M.computed ? AST_Sub : AST_Dot)({
418
+ start : my_start_token(M),
419
+ end : my_end_token(M),
420
+ property : M.computed ? from_moz(M.property) : M.property.name,
421
+ expression : from_moz(M.object)
422
+ });
423
+ },
424
+ SwitchCase: function(M) {
425
+ return new (M.test ? AST_Case : AST_Default)({
426
+ start : my_start_token(M),
427
+ end : my_end_token(M),
428
+ expression : from_moz(M.test),
429
+ body : M.consequent.map(from_moz)
430
+ });
431
+ },
432
+ VariableDeclaration: function(M) {
433
+ return new (M.kind === "const" ? AST_Const :
434
+ M.kind === "let" ? AST_Let : AST_Var)({
435
+ start : my_start_token(M),
436
+ end : my_end_token(M),
437
+ definitions : M.declarations.map(from_moz)
438
+ });
439
+ },
440
+
441
+ ImportDeclaration: function(M) {
442
+ var imported_name = null;
443
+ var imported_names = null;
444
+ M.specifiers.forEach(function (specifier) {
445
+ if (specifier.type === "ImportSpecifier") {
446
+ if (!imported_names) { imported_names = []; }
447
+ imported_names.push(new AST_NameMapping({
448
+ start: my_start_token(specifier),
449
+ end: my_end_token(specifier),
450
+ foreign_name: from_moz(specifier.imported),
451
+ name: from_moz(specifier.local)
452
+ }));
453
+ } else if (specifier.type === "ImportDefaultSpecifier") {
454
+ imported_name = from_moz(specifier.local);
455
+ } else if (specifier.type === "ImportNamespaceSpecifier") {
456
+ if (!imported_names) { imported_names = []; }
457
+ imported_names.push(new AST_NameMapping({
458
+ start: my_start_token(specifier),
459
+ end: my_end_token(specifier),
460
+ foreign_name: new AST_SymbolImportForeign({ name: "*" }),
461
+ name: from_moz(specifier.local)
462
+ }));
463
+ }
464
+ });
465
+ return new AST_Import({
466
+ start : my_start_token(M),
467
+ end : my_end_token(M),
468
+ imported_name: imported_name,
469
+ imported_names : imported_names,
470
+ module_name : from_moz(M.source)
471
+ });
472
+ },
473
+ ExportAllDeclaration: function(M) {
474
+ return new AST_Export({
475
+ start: my_start_token(M),
476
+ end: my_end_token(M),
477
+ exported_names: [
478
+ new AST_NameMapping({
479
+ name: new AST_SymbolExportForeign({ name: "*" }),
480
+ foreign_name: new AST_SymbolExportForeign({ name: "*" })
481
+ })
482
+ ],
483
+ module_name: from_moz(M.source)
484
+ });
485
+ },
486
+ ExportNamedDeclaration: function(M) {
487
+ return new AST_Export({
488
+ start: my_start_token(M),
489
+ end: my_end_token(M),
490
+ exported_definition: from_moz(M.declaration),
491
+ exported_names: M.specifiers && M.specifiers.length ? M.specifiers.map(function (specifier) {
492
+ return new AST_NameMapping({
493
+ foreign_name: from_moz(specifier.exported),
494
+ name: from_moz(specifier.local)
495
+ });
496
+ }) : null,
497
+ module_name: from_moz(M.source)
498
+ });
499
+ },
500
+ ExportDefaultDeclaration: function(M) {
501
+ return new AST_Export({
502
+ start: my_start_token(M),
503
+ end: my_end_token(M),
504
+ exported_value: from_moz(M.declaration),
505
+ is_default: true
506
+ });
507
+ },
508
+ Literal: function(M) {
509
+ var val = M.value, args = {
510
+ start : my_start_token(M),
511
+ end : my_end_token(M)
512
+ };
513
+ var rx = M.regex;
514
+ if (rx && rx.pattern) {
515
+ // RegExpLiteral as per ESTree AST spec
516
+ args.value = {
517
+ source: rx.pattern,
518
+ flags: rx.flags
519
+ };
520
+ return new AST_RegExp(args);
521
+ } else if (rx) {
522
+ // support legacy RegExp
523
+ const rx_source = M.raw || val;
524
+ const match = rx_source.match(/^\/(.*)\/(\w*)$/);
525
+ if (!match) throw new Error("Invalid regex source " + rx_source);
526
+ const [_, source, flags] = match;
527
+ args.value = { source, flags };
528
+ return new AST_RegExp(args);
529
+ }
530
+ if (val === null) return new AST_Null(args);
531
+ switch (typeof val) {
532
+ case "string":
533
+ args.value = val;
534
+ return new AST_String(args);
535
+ case "number":
536
+ args.value = val;
537
+ return new AST_Number(args);
538
+ case "boolean":
539
+ return new (val ? AST_True : AST_False)(args);
540
+ }
541
+ },
542
+ MetaProperty: function(M) {
543
+ if (M.meta.name === "new" && M.property.name === "target") {
544
+ return new AST_NewTarget({
545
+ start: my_start_token(M),
546
+ end: my_end_token(M)
547
+ });
548
+ }
549
+ },
550
+ Identifier: function(M) {
551
+ var p = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 2];
552
+ return new ( p.type == "LabeledStatement" ? AST_Label
553
+ : p.type == "VariableDeclarator" && p.id === M ? (p.kind == "const" ? AST_SymbolConst : p.kind == "let" ? AST_SymbolLet : AST_SymbolVar)
554
+ : /Import.*Specifier/.test(p.type) ? (p.local === M ? AST_SymbolImport : AST_SymbolImportForeign)
555
+ : p.type == "ExportSpecifier" ? (p.local === M ? AST_SymbolExport : AST_SymbolExportForeign)
556
+ : p.type == "FunctionExpression" ? (p.id === M ? AST_SymbolLambda : AST_SymbolFunarg)
557
+ : p.type == "FunctionDeclaration" ? (p.id === M ? AST_SymbolDefun : AST_SymbolFunarg)
558
+ : p.type == "ArrowFunctionExpression" ? (p.params.includes(M)) ? AST_SymbolFunarg : AST_SymbolRef
559
+ : p.type == "ClassExpression" ? (p.id === M ? AST_SymbolClass : AST_SymbolRef)
560
+ : p.type == "Property" ? (p.key === M && p.computed || p.value === M ? AST_SymbolRef : AST_SymbolMethod)
561
+ : p.type == "FieldDefinition" ? (p.key === M && p.computed || p.value === M ? AST_SymbolRef : AST_SymbolClassProperty)
562
+ : p.type == "ClassDeclaration" ? (p.id === M ? AST_SymbolDefClass : AST_SymbolRef)
563
+ : p.type == "MethodDefinition" ? (p.computed ? AST_SymbolRef : AST_SymbolMethod)
564
+ : p.type == "CatchClause" ? AST_SymbolCatch
565
+ : p.type == "BreakStatement" || p.type == "ContinueStatement" ? AST_LabelRef
566
+ : AST_SymbolRef)({
567
+ start : my_start_token(M),
568
+ end : my_end_token(M),
569
+ name : M.name
570
+ });
571
+ },
572
+ BigIntLiteral(M) {
573
+ return new AST_BigInt({
574
+ start : my_start_token(M),
575
+ end : my_end_token(M),
576
+ value : M.value
577
+ });
578
+ }
579
+ };
580
+
581
+ MOZ_TO_ME.UpdateExpression =
582
+ MOZ_TO_ME.UnaryExpression = function To_Moz_Unary(M) {
583
+ var prefix = "prefix" in M ? M.prefix
584
+ : M.type == "UnaryExpression" ? true : false;
585
+ return new (prefix ? AST_UnaryPrefix : AST_UnaryPostfix)({
586
+ start : my_start_token(M),
587
+ end : my_end_token(M),
588
+ operator : M.operator,
589
+ expression : from_moz(M.argument)
590
+ });
591
+ };
592
+
593
+ MOZ_TO_ME.ClassDeclaration =
594
+ MOZ_TO_ME.ClassExpression = function From_Moz_Class(M) {
595
+ return new (M.type === "ClassDeclaration" ? AST_DefClass : AST_ClassExpression)({
596
+ start : my_start_token(M),
597
+ end : my_end_token(M),
598
+ name : from_moz(M.id),
599
+ extends : from_moz(M.superClass),
600
+ properties: M.body.body.map(from_moz)
601
+ });
602
+ };
603
+
604
+ map("EmptyStatement", AST_EmptyStatement);
605
+ map("BlockStatement", AST_BlockStatement, "body@body");
606
+ map("IfStatement", AST_If, "test>condition, consequent>body, alternate>alternative");
607
+ map("LabeledStatement", AST_LabeledStatement, "label>label, body>body");
608
+ map("BreakStatement", AST_Break, "label>label");
609
+ map("ContinueStatement", AST_Continue, "label>label");
610
+ map("WithStatement", AST_With, "object>expression, body>body");
611
+ map("SwitchStatement", AST_Switch, "discriminant>expression, cases@body");
612
+ map("ReturnStatement", AST_Return, "argument>value");
613
+ map("ThrowStatement", AST_Throw, "argument>value");
614
+ map("WhileStatement", AST_While, "test>condition, body>body");
615
+ map("DoWhileStatement", AST_Do, "test>condition, body>body");
616
+ map("ForStatement", AST_For, "init>init, test>condition, update>step, body>body");
617
+ map("ForInStatement", AST_ForIn, "left>init, right>object, body>body");
618
+ map("ForOfStatement", AST_ForOf, "left>init, right>object, body>body, await=await");
619
+ map("AwaitExpression", AST_Await, "argument>expression");
620
+ map("YieldExpression", AST_Yield, "argument>expression, delegate=is_star");
621
+ map("DebuggerStatement", AST_Debugger);
622
+ map("VariableDeclarator", AST_VarDef, "id>name, init>value");
623
+ map("CatchClause", AST_Catch, "param>argname, body%body");
624
+
625
+ map("ThisExpression", AST_This);
626
+ map("Super", AST_Super);
627
+ map("BinaryExpression", AST_Binary, "operator=operator, left>left, right>right");
628
+ map("LogicalExpression", AST_Binary, "operator=operator, left>left, right>right");
629
+ map("AssignmentExpression", AST_Assign, "operator=operator, left>left, right>right");
630
+ map("ConditionalExpression", AST_Conditional, "test>condition, consequent>consequent, alternate>alternative");
631
+ map("NewExpression", AST_New, "callee>expression, arguments@args");
632
+ map("CallExpression", AST_Call, "callee>expression, arguments@args");
633
+
634
+ def_to_moz(AST_Toplevel, function To_Moz_Program(M) {
635
+ return to_moz_scope("Program", M);
636
+ });
637
+
638
+ def_to_moz(AST_Expansion, function To_Moz_Spread(M) {
639
+ return {
640
+ type: to_moz_in_destructuring() ? "RestElement" : "SpreadElement",
641
+ argument: to_moz(M.expression)
642
+ };
643
+ });
644
+
645
+ def_to_moz(AST_PrefixedTemplateString, function To_Moz_TaggedTemplateExpression(M) {
646
+ return {
647
+ type: "TaggedTemplateExpression",
648
+ tag: to_moz(M.prefix),
649
+ quasi: to_moz(M.template_string)
650
+ };
651
+ });
652
+
653
+ def_to_moz(AST_TemplateString, function To_Moz_TemplateLiteral(M) {
654
+ var quasis = [];
655
+ var expressions = [];
656
+ for (var i = 0; i < M.segments.length; i++) {
657
+ if (i % 2 !== 0) {
658
+ expressions.push(to_moz(M.segments[i]));
659
+ } else {
660
+ quasis.push({
661
+ type: "TemplateElement",
662
+ value: {
663
+ raw: M.segments[i].raw,
664
+ cooked: M.segments[i].value
665
+ },
666
+ tail: i === M.segments.length - 1
667
+ });
668
+ }
669
+ }
670
+ return {
671
+ type: "TemplateLiteral",
672
+ quasis: quasis,
673
+ expressions: expressions
674
+ };
675
+ });
676
+
677
+ def_to_moz(AST_Defun, function To_Moz_FunctionDeclaration(M) {
678
+ return {
679
+ type: "FunctionDeclaration",
680
+ id: to_moz(M.name),
681
+ params: M.argnames.map(to_moz),
682
+ generator: M.is_generator,
683
+ async: M.async,
684
+ body: to_moz_scope("BlockStatement", M)
685
+ };
686
+ });
687
+
688
+ def_to_moz(AST_Function, function To_Moz_FunctionExpression(M, parent) {
689
+ var is_generator = parent.is_generator !== undefined ?
690
+ parent.is_generator : M.is_generator;
691
+ return {
692
+ type: "FunctionExpression",
693
+ id: to_moz(M.name),
694
+ params: M.argnames.map(to_moz),
695
+ generator: is_generator,
696
+ async: M.async,
697
+ body: to_moz_scope("BlockStatement", M)
698
+ };
699
+ });
700
+
701
+ def_to_moz(AST_Arrow, function To_Moz_ArrowFunctionExpression(M) {
702
+ var body = {
703
+ type: "BlockStatement",
704
+ body: M.body.map(to_moz)
705
+ };
706
+ return {
707
+ type: "ArrowFunctionExpression",
708
+ params: M.argnames.map(to_moz),
709
+ async: M.async,
710
+ body: body
711
+ };
712
+ });
713
+
714
+ def_to_moz(AST_Destructuring, function To_Moz_ObjectPattern(M) {
715
+ if (M.is_array) {
716
+ return {
717
+ type: "ArrayPattern",
718
+ elements: M.names.map(to_moz)
719
+ };
720
+ }
721
+ return {
722
+ type: "ObjectPattern",
723
+ properties: M.names.map(to_moz)
724
+ };
725
+ });
726
+
727
+ def_to_moz(AST_Directive, function To_Moz_Directive(M) {
728
+ return {
729
+ type: "ExpressionStatement",
730
+ expression: {
731
+ type: "Literal",
732
+ value: M.value,
733
+ raw: M.print_to_string()
734
+ },
735
+ directive: M.value
736
+ };
737
+ });
738
+
739
+ def_to_moz(AST_SimpleStatement, function To_Moz_ExpressionStatement(M) {
740
+ return {
741
+ type: "ExpressionStatement",
742
+ expression: to_moz(M.body)
743
+ };
744
+ });
745
+
746
+ def_to_moz(AST_SwitchBranch, function To_Moz_SwitchCase(M) {
747
+ return {
748
+ type: "SwitchCase",
749
+ test: to_moz(M.expression),
750
+ consequent: M.body.map(to_moz)
751
+ };
752
+ });
753
+
754
+ def_to_moz(AST_Try, function To_Moz_TryStatement(M) {
755
+ return {
756
+ type: "TryStatement",
757
+ block: to_moz_block(M),
758
+ handler: to_moz(M.bcatch),
759
+ guardedHandlers: [],
760
+ finalizer: to_moz(M.bfinally)
761
+ };
762
+ });
763
+
764
+ def_to_moz(AST_Catch, function To_Moz_CatchClause(M) {
765
+ return {
766
+ type: "CatchClause",
767
+ param: to_moz(M.argname),
768
+ guard: null,
769
+ body: to_moz_block(M)
770
+ };
771
+ });
772
+
773
+ def_to_moz(AST_Definitions, function To_Moz_VariableDeclaration(M) {
774
+ return {
775
+ type: "VariableDeclaration",
776
+ kind:
777
+ M instanceof AST_Const ? "const" :
778
+ M instanceof AST_Let ? "let" : "var",
779
+ declarations: M.definitions.map(to_moz)
780
+ };
781
+ });
782
+
783
+ def_to_moz(AST_Export, function To_Moz_ExportDeclaration(M) {
784
+ if (M.exported_names) {
785
+ if (M.exported_names[0].name.name === "*") {
786
+ return {
787
+ type: "ExportAllDeclaration",
788
+ source: to_moz(M.module_name)
789
+ };
790
+ }
791
+ return {
792
+ type: "ExportNamedDeclaration",
793
+ specifiers: M.exported_names.map(function (name_mapping) {
794
+ return {
795
+ type: "ExportSpecifier",
796
+ exported: to_moz(name_mapping.foreign_name),
797
+ local: to_moz(name_mapping.name)
798
+ };
799
+ }),
800
+ declaration: to_moz(M.exported_definition),
801
+ source: to_moz(M.module_name)
802
+ };
803
+ }
804
+ return {
805
+ type: M.is_default ? "ExportDefaultDeclaration" : "ExportNamedDeclaration",
806
+ declaration: to_moz(M.exported_value || M.exported_definition)
807
+ };
808
+ });
809
+
810
+ def_to_moz(AST_Import, function To_Moz_ImportDeclaration(M) {
811
+ var specifiers = [];
812
+ if (M.imported_name) {
813
+ specifiers.push({
814
+ type: "ImportDefaultSpecifier",
815
+ local: to_moz(M.imported_name)
816
+ });
817
+ }
818
+ if (M.imported_names && M.imported_names[0].foreign_name.name === "*") {
819
+ specifiers.push({
820
+ type: "ImportNamespaceSpecifier",
821
+ local: to_moz(M.imported_names[0].name)
822
+ });
823
+ } else if (M.imported_names) {
824
+ M.imported_names.forEach(function(name_mapping) {
825
+ specifiers.push({
826
+ type: "ImportSpecifier",
827
+ local: to_moz(name_mapping.name),
828
+ imported: to_moz(name_mapping.foreign_name)
829
+ });
830
+ });
831
+ }
832
+ return {
833
+ type: "ImportDeclaration",
834
+ specifiers: specifiers,
835
+ source: to_moz(M.module_name)
836
+ };
837
+ });
838
+
839
+ def_to_moz(AST_Sequence, function To_Moz_SequenceExpression(M) {
840
+ return {
841
+ type: "SequenceExpression",
842
+ expressions: M.expressions.map(to_moz)
843
+ };
844
+ });
845
+
846
+ def_to_moz(AST_PropAccess, function To_Moz_MemberExpression(M) {
847
+ var isComputed = M instanceof AST_Sub;
848
+ return {
849
+ type: "MemberExpression",
850
+ object: to_moz(M.expression),
851
+ computed: isComputed,
852
+ property: isComputed ? to_moz(M.property) : {type: "Identifier", name: M.property}
853
+ };
854
+ });
855
+
856
+ def_to_moz(AST_Unary, function To_Moz_Unary(M) {
857
+ return {
858
+ type: M.operator == "++" || M.operator == "--" ? "UpdateExpression" : "UnaryExpression",
859
+ operator: M.operator,
860
+ prefix: M instanceof AST_UnaryPrefix,
861
+ argument: to_moz(M.expression)
862
+ };
863
+ });
864
+
865
+ def_to_moz(AST_Binary, function To_Moz_BinaryExpression(M) {
866
+ if (M.operator == "=" && to_moz_in_destructuring()) {
867
+ return {
868
+ type: "AssignmentPattern",
869
+ left: to_moz(M.left),
870
+ right: to_moz(M.right)
871
+ };
872
+ }
873
+
874
+ const type = M.operator == "&&" || M.operator == "||" || M.operator === "??"
875
+ ? "LogicalExpression"
876
+ : "BinaryExpression";
877
+
878
+ return {
879
+ type,
880
+ left: to_moz(M.left),
881
+ operator: M.operator,
882
+ right: to_moz(M.right)
883
+ };
884
+ });
885
+
886
+ def_to_moz(AST_Array, function To_Moz_ArrayExpression(M) {
887
+ return {
888
+ type: "ArrayExpression",
889
+ elements: M.elements.map(to_moz)
890
+ };
891
+ });
892
+
893
+ def_to_moz(AST_Object, function To_Moz_ObjectExpression(M) {
894
+ return {
895
+ type: "ObjectExpression",
896
+ properties: M.properties.map(to_moz)
897
+ };
898
+ });
899
+
900
+ def_to_moz(AST_ObjectProperty, function To_Moz_Property(M, parent) {
901
+ var key = M.key instanceof AST_Node ? to_moz(M.key) : {
902
+ type: "Identifier",
903
+ value: M.key
904
+ };
905
+ if (typeof M.key === "number") {
906
+ key = {
907
+ type: "Literal",
908
+ value: Number(M.key)
909
+ };
910
+ }
911
+ if (typeof M.key === "string") {
912
+ key = {
913
+ type: "Identifier",
914
+ name: M.key
915
+ };
916
+ }
917
+ var kind;
918
+ var string_or_num = typeof M.key === "string" || typeof M.key === "number";
919
+ var computed = string_or_num ? false : !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef;
920
+ if (M instanceof AST_ObjectKeyVal) {
921
+ kind = "init";
922
+ computed = !string_or_num;
923
+ } else
924
+ if (M instanceof AST_ObjectGetter) {
925
+ kind = "get";
926
+ } else
927
+ if (M instanceof AST_ObjectSetter) {
928
+ kind = "set";
929
+ }
930
+ if (M instanceof AST_ClassProperty) {
931
+ return {
932
+ type: "FieldDefinition",
933
+ computed,
934
+ key,
935
+ value: to_moz(M.value),
936
+ static: M.static
937
+ };
938
+ }
939
+ if (parent instanceof AST_Class) {
940
+ return {
941
+ type: "MethodDefinition",
942
+ computed: computed,
943
+ kind: kind,
944
+ static: M.static,
945
+ key: to_moz(M.key),
946
+ value: to_moz(M.value)
947
+ };
948
+ }
949
+ return {
950
+ type: "Property",
951
+ computed: computed,
952
+ kind: kind,
953
+ key: key,
954
+ value: to_moz(M.value)
955
+ };
956
+ });
957
+
958
+ def_to_moz(AST_ConciseMethod, function To_Moz_MethodDefinition(M, parent) {
959
+ if (parent instanceof AST_Object) {
960
+ return {
961
+ type: "Property",
962
+ computed: !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef,
963
+ kind: "init",
964
+ method: true,
965
+ shorthand: false,
966
+ key: to_moz(M.key),
967
+ value: to_moz(M.value)
968
+ };
969
+ }
970
+ return {
971
+ type: "MethodDefinition",
972
+ computed: !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef,
973
+ kind: M.key === "constructor" ? "constructor" : "method",
974
+ static: M.static,
975
+ key: to_moz(M.key),
976
+ value: to_moz(M.value)
977
+ };
978
+ });
979
+
980
+ def_to_moz(AST_Class, function To_Moz_Class(M) {
981
+ var type = M instanceof AST_ClassExpression ? "ClassExpression" : "ClassDeclaration";
982
+ return {
983
+ type: type,
984
+ superClass: to_moz(M.extends),
985
+ id: M.name ? to_moz(M.name) : null,
986
+ body: {
987
+ type: "ClassBody",
988
+ body: M.properties.map(to_moz)
989
+ }
990
+ };
991
+ });
992
+
993
+ def_to_moz(AST_NewTarget, function To_Moz_MetaProperty() {
994
+ return {
995
+ type: "MetaProperty",
996
+ meta: {
997
+ type: "Identifier",
998
+ name: "new"
999
+ },
1000
+ property: {
1001
+ type: "Identifier",
1002
+ name: "target"
1003
+ }
1004
+ };
1005
+ });
1006
+
1007
+ def_to_moz(AST_Symbol, function To_Moz_Identifier(M, parent) {
1008
+ if (M instanceof AST_SymbolMethod && parent.quote) {
1009
+ return {
1010
+ type: "Literal",
1011
+ value: M.name
1012
+ };
1013
+ }
1014
+ var def = M.definition();
1015
+ return {
1016
+ type: "Identifier",
1017
+ name: def ? def.mangled_name || def.name : M.name
1018
+ };
1019
+ });
1020
+
1021
+ def_to_moz(AST_RegExp, function To_Moz_RegExpLiteral(M) {
1022
+ const pattern = M.value.source;
1023
+ const flags = M.value.flags;
1024
+ return {
1025
+ type: "Literal",
1026
+ value: null,
1027
+ raw: M.print_to_string(),
1028
+ regex: { pattern, flags }
1029
+ };
1030
+ });
1031
+
1032
+ def_to_moz(AST_Constant, function To_Moz_Literal(M) {
1033
+ var value = M.value;
1034
+ if (typeof value === "number" && (value < 0 || (value === 0 && 1 / value < 0))) {
1035
+ return {
1036
+ type: "UnaryExpression",
1037
+ operator: "-",
1038
+ prefix: true,
1039
+ argument: {
1040
+ type: "Literal",
1041
+ value: -value,
1042
+ raw: M.start.raw
1043
+ }
1044
+ };
1045
+ }
1046
+ return {
1047
+ type: "Literal",
1048
+ value: value,
1049
+ raw: M.start.raw
1050
+ };
1051
+ });
1052
+
1053
+ def_to_moz(AST_Atom, function To_Moz_Atom(M) {
1054
+ return {
1055
+ type: "Identifier",
1056
+ name: String(M.value)
1057
+ };
1058
+ });
1059
+
1060
+ def_to_moz(AST_BigInt, M => ({
1061
+ type: "BigIntLiteral",
1062
+ value: M.value
1063
+ }));
1064
+
1065
+ AST_Boolean.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
1066
+ AST_Null.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
1067
+ AST_Hole.DEFMETHOD("to_mozilla_ast", function To_Moz_ArrayHole() { return null; });
1068
+
1069
+ AST_Block.DEFMETHOD("to_mozilla_ast", AST_BlockStatement.prototype.to_mozilla_ast);
1070
+ AST_Lambda.DEFMETHOD("to_mozilla_ast", AST_Function.prototype.to_mozilla_ast);
1071
+
1072
+ /* -----[ tools ]----- */
1073
+
1074
+ function raw_token(moznode) {
1075
+ if (moznode.type == "Literal") {
1076
+ return moznode.raw != null ? moznode.raw : moznode.value + "";
1077
+ }
1078
+ }
1079
+
1080
+ function my_start_token(moznode) {
1081
+ var loc = moznode.loc, start = loc && loc.start;
1082
+ var range = moznode.range;
1083
+ return new AST_Token({
1084
+ file : loc && loc.source,
1085
+ line : start && start.line,
1086
+ col : start && start.column,
1087
+ pos : range ? range[0] : moznode.start,
1088
+ endline : start && start.line,
1089
+ endcol : start && start.column,
1090
+ endpos : range ? range[0] : moznode.start,
1091
+ raw : raw_token(moznode),
1092
+ });
1093
+ }
1094
+
1095
+ function my_end_token(moznode) {
1096
+ var loc = moznode.loc, end = loc && loc.end;
1097
+ var range = moznode.range;
1098
+ return new AST_Token({
1099
+ file : loc && loc.source,
1100
+ line : end && end.line,
1101
+ col : end && end.column,
1102
+ pos : range ? range[1] : moznode.end,
1103
+ endline : end && end.line,
1104
+ endcol : end && end.column,
1105
+ endpos : range ? range[1] : moznode.end,
1106
+ raw : raw_token(moznode),
1107
+ });
1108
+ }
1109
+
1110
+ function map(moztype, mytype, propmap) {
1111
+ var moz_to_me = "function From_Moz_" + moztype + "(M){\n";
1112
+ moz_to_me += "return new U2." + mytype.name + "({\n" +
1113
+ "start: my_start_token(M),\n" +
1114
+ "end: my_end_token(M)";
1115
+
1116
+ var me_to_moz = "function To_Moz_" + moztype + "(M){\n";
1117
+ me_to_moz += "return {\n" +
1118
+ "type: " + JSON.stringify(moztype);
1119
+
1120
+ if (propmap) propmap.split(/\s*,\s*/).forEach(function(prop) {
1121
+ var m = /([a-z0-9$_]+)([=@>%])([a-z0-9$_]+)/i.exec(prop);
1122
+ if (!m) throw new Error("Can't understand property map: " + prop);
1123
+ var moz = m[1], how = m[2], my = m[3];
1124
+ moz_to_me += ",\n" + my + ": ";
1125
+ me_to_moz += ",\n" + moz + ": ";
1126
+ switch (how) {
1127
+ case "@":
1128
+ moz_to_me += "M." + moz + ".map(from_moz)";
1129
+ me_to_moz += "M." + my + ".map(to_moz)";
1130
+ break;
1131
+ case ">":
1132
+ moz_to_me += "from_moz(M." + moz + ")";
1133
+ me_to_moz += "to_moz(M." + my + ")";
1134
+ break;
1135
+ case "=":
1136
+ moz_to_me += "M." + moz;
1137
+ me_to_moz += "M." + my;
1138
+ break;
1139
+ case "%":
1140
+ moz_to_me += "from_moz(M." + moz + ").body";
1141
+ me_to_moz += "to_moz_block(M)";
1142
+ break;
1143
+ default:
1144
+ throw new Error("Can't understand operator in propmap: " + prop);
1145
+ }
1146
+ });
1147
+
1148
+ moz_to_me += "\n})\n}";
1149
+ me_to_moz += "\n}\n}";
1150
+
1151
+ moz_to_me = new Function("U2", "my_start_token", "my_end_token", "from_moz", "return(" + moz_to_me + ")")(
1152
+ ast, my_start_token, my_end_token, from_moz
1153
+ );
1154
+ me_to_moz = new Function("to_moz", "to_moz_block", "to_moz_scope", "return(" + me_to_moz + ")")(
1155
+ to_moz, to_moz_block, to_moz_scope
1156
+ );
1157
+ MOZ_TO_ME[moztype] = moz_to_me;
1158
+ def_to_moz(mytype, me_to_moz);
1159
+ }
1160
+
1161
+ var FROM_MOZ_STACK = null;
1162
+
1163
+ function from_moz(node) {
1164
+ FROM_MOZ_STACK.push(node);
1165
+ var ret = node != null ? MOZ_TO_ME[node.type](node) : null;
1166
+ FROM_MOZ_STACK.pop();
1167
+ return ret;
1168
+ }
1169
+
1170
+ AST_Node.from_mozilla_ast = function(node) {
1171
+ var save_stack = FROM_MOZ_STACK;
1172
+ FROM_MOZ_STACK = [];
1173
+ var ast = from_moz(node);
1174
+ FROM_MOZ_STACK = save_stack;
1175
+ return ast;
1176
+ };
1177
+
1178
+ function set_moz_loc(mynode, moznode) {
1179
+ var start = mynode.start;
1180
+ var end = mynode.end;
1181
+ if (!(start && end)) {
1182
+ return moznode;
1183
+ }
1184
+ if (start.pos != null && end.endpos != null) {
1185
+ moznode.range = [start.pos, end.endpos];
1186
+ }
1187
+ if (start.line) {
1188
+ moznode.loc = {
1189
+ start: {line: start.line, column: start.col},
1190
+ end: end.endline ? {line: end.endline, column: end.endcol} : null
1191
+ };
1192
+ if (start.file) {
1193
+ moznode.loc.source = start.file;
1194
+ }
1195
+ }
1196
+ return moznode;
1197
+ }
1198
+
1199
+ function def_to_moz(mytype, handler) {
1200
+ mytype.DEFMETHOD("to_mozilla_ast", function(parent) {
1201
+ return set_moz_loc(this, handler(this, parent));
1202
+ });
1203
+ }
1204
+
1205
+ var TO_MOZ_STACK = null;
1206
+
1207
+ function to_moz(node) {
1208
+ if (TO_MOZ_STACK === null) { TO_MOZ_STACK = []; }
1209
+ TO_MOZ_STACK.push(node);
1210
+ var ast = node != null ? node.to_mozilla_ast(TO_MOZ_STACK[TO_MOZ_STACK.length - 2]) : null;
1211
+ TO_MOZ_STACK.pop();
1212
+ if (TO_MOZ_STACK.length === 0) { TO_MOZ_STACK = null; }
1213
+ return ast;
1214
+ }
1215
+
1216
+ function to_moz_in_destructuring() {
1217
+ var i = TO_MOZ_STACK.length;
1218
+ while (i--) {
1219
+ if (TO_MOZ_STACK[i] instanceof AST_Destructuring) {
1220
+ return true;
1221
+ }
1222
+ }
1223
+ return false;
1224
+ }
1225
+
1226
+ function to_moz_block(node) {
1227
+ return {
1228
+ type: "BlockStatement",
1229
+ body: node.body.map(to_moz)
1230
+ };
1231
+ }
1232
+
1233
+ function to_moz_scope(type, node) {
1234
+ var body = node.body.map(to_moz);
1235
+ if (node.body[0] instanceof AST_SimpleStatement && node.body[0].body instanceof AST_String) {
1236
+ body.unshift(to_moz(new AST_EmptyStatement(node.body[0])));
1237
+ }
1238
+ return {
1239
+ type: type,
1240
+ body: body
1241
+ };
1242
+ }
1243
+ })();