source: frontend/node_modules/terser/lib/mozilla-ast.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 67.1 KB
Line 
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
44import { make_node } from "./utils/index.js";
45import {
46 AST_Accessor,
47 AST_Array,
48 AST_Arrow,
49 AST_Assign,
50 AST_Atom,
51 AST_Await,
52 AST_BigInt,
53 AST_Binary,
54 AST_Block,
55 AST_BlockStatement,
56 AST_Boolean,
57 AST_Break,
58 AST_Call,
59 AST_Case,
60 AST_Catch,
61 AST_Chain,
62 AST_Class,
63 AST_ClassStaticBlock,
64 AST_ClassExpression,
65 AST_ClassProperty,
66 AST_ClassPrivateProperty,
67 AST_ConciseMethod,
68 AST_Conditional,
69 AST_Const,
70 AST_Constant,
71 AST_Continue,
72 AST_Debugger,
73 AST_Default,
74 AST_DefaultAssign,
75 AST_DefClass,
76 AST_DefinitionsLike,
77 AST_Defun,
78 AST_Destructuring,
79 AST_Directive,
80 AST_Do,
81 AST_Dot,
82 AST_DotHash,
83 AST_EmptyStatement,
84 AST_Expansion,
85 AST_Export,
86 AST_False,
87 AST_Finally,
88 AST_For,
89 AST_ForIn,
90 AST_ForOf,
91 AST_Function,
92 AST_Hole,
93 AST_If,
94 AST_DynamicImport,
95 AST_Import,
96 AST_ImportMeta,
97 AST_Label,
98 AST_LabeledStatement,
99 AST_LabelRef,
100 AST_Lambda,
101 AST_Let,
102 AST_NameMapping,
103 AST_New,
104 AST_NewTarget,
105 AST_Node,
106 AST_Null,
107 AST_Number,
108 AST_Object,
109 AST_ObjectGetter,
110 AST_ObjectKeyVal,
111 AST_ObjectProperty,
112 AST_ObjectSetter,
113 AST_PrefixedTemplateString,
114 AST_PrivateGetter,
115 AST_PrivateMethod,
116 AST_PrivateSetter,
117 AST_PrivateIn,
118 AST_PropAccess,
119 AST_RegExp,
120 AST_Return,
121 AST_Sequence,
122 AST_SimpleStatement,
123 AST_Statement,
124 AST_String,
125 AST_Sub,
126 AST_Super,
127 AST_Switch,
128 AST_SwitchBranch,
129 AST_Symbol,
130 AST_SymbolCatch,
131 AST_SymbolClass,
132 AST_SymbolClassProperty,
133 AST_SymbolPrivateProperty,
134 AST_SymbolConst,
135 AST_SymbolDefClass,
136 AST_SymbolDefun,
137 AST_SymbolExport,
138 AST_SymbolExportForeign,
139 AST_SymbolFunarg,
140 AST_SymbolImport,
141 AST_SymbolImportForeign,
142 AST_SymbolLambda,
143 AST_SymbolLet,
144 AST_SymbolMethod,
145 AST_SymbolRef,
146 AST_SymbolVar,
147 AST_SymbolUsing,
148 AST_TemplateSegment,
149 AST_TemplateString,
150 AST_This,
151 AST_Throw,
152 AST_Token,
153 AST_Toplevel,
154 AST_True,
155 AST_Try,
156 AST_TryBlock,
157 AST_Unary,
158 AST_UnaryPostfix,
159 AST_UnaryPrefix,
160 AST_Using,
161 AST_UsingDef,
162 AST_Var,
163 AST_VarDef,
164 AST_VarDefLike,
165 AST_While,
166 AST_With,
167 AST_Yield,
168} from "./ast.js";
169import { is_basic_identifier_string } from "./parse.js";
170
171(function() {
172
173 var normalize_directives = function(body) {
174 for (var i = 0; i < body.length; i++) {
175 if (body[i] instanceof AST_Statement && body[i].body instanceof AST_String) {
176 body[i] = new AST_Directive({
177 start: body[i].start,
178 end: body[i].end,
179 quote: '"',
180 value: body[i].body.value
181 });
182 } else {
183 return body;
184 }
185 }
186
187 return body;
188 };
189
190 function import_attributes_from_moz(attributes) {
191 if (attributes && attributes.length > 0) {
192 return new AST_Object({
193 start: my_start_token(attributes),
194 end: my_end_token(attributes),
195 properties: attributes.map((attr) =>
196 new AST_ObjectKeyVal({
197 start: my_start_token(attr),
198 end: my_end_token(attr),
199 key: attr.key.name || attr.key.value,
200 value: from_moz(attr.value)
201 })
202 )
203 });
204 }
205 return null;
206 }
207
208 var MOZ_TO_ME = {
209 Program: function(M) {
210 return new AST_Toplevel({
211 start: my_start_token(M),
212 end: my_end_token(M),
213 body: normalize_directives(M.body.map(from_moz))
214 });
215 },
216
217 ArrayPattern: function(M) {
218 return new AST_Destructuring({
219 start: my_start_token(M),
220 end: my_end_token(M),
221 names: M.elements.map(function(elm) {
222 if (elm === null) {
223 return new AST_Hole();
224 }
225 return from_moz(elm);
226 }),
227 is_array: true
228 });
229 },
230
231 ObjectPattern: function(M) {
232 return new AST_Destructuring({
233 start: my_start_token(M),
234 end: my_end_token(M),
235 names: M.properties.map(from_moz),
236 is_array: false
237 });
238 },
239
240 AssignmentPattern: function(M) {
241 return new AST_DefaultAssign({
242 start: my_start_token(M),
243 end: my_end_token(M),
244 left: from_moz(M.left),
245 operator: "=",
246 right: from_moz(M.right)
247 });
248 },
249
250 SpreadElement: function(M) {
251 return new AST_Expansion({
252 start: my_start_token(M),
253 end: my_end_token(M),
254 expression: from_moz(M.argument)
255 });
256 },
257
258 RestElement: function(M) {
259 return new AST_Expansion({
260 start: my_start_token(M),
261 end: my_end_token(M),
262 expression: from_moz(M.argument)
263 });
264 },
265
266 TemplateElement: function(M) {
267 return new AST_TemplateSegment({
268 start: my_start_token(M),
269 end: my_end_token(M),
270 value: M.value.cooked,
271 raw: M.value.raw
272 });
273 },
274
275 TemplateLiteral: function(M) {
276 var segments = [];
277 for (var i = 0; i < M.quasis.length; i++) {
278 segments.push(from_moz(M.quasis[i]));
279 if (M.expressions[i]) {
280 segments.push(from_moz(M.expressions[i]));
281 }
282 }
283 return new AST_TemplateString({
284 start: my_start_token(M),
285 end: my_end_token(M),
286 segments: segments
287 });
288 },
289
290 TaggedTemplateExpression: function(M) {
291 return new AST_PrefixedTemplateString({
292 start: my_start_token(M),
293 end: my_end_token(M),
294 template_string: from_moz(M.quasi),
295 prefix: from_moz(M.tag)
296 });
297 },
298
299 FunctionDeclaration: function(M) {
300 return new AST_Defun({
301 start: my_start_token(M),
302 end: my_end_token(M),
303 name: M.id && from_moz_symbol(AST_SymbolDefun, M.id),
304 argnames: M.params.map(M => from_moz_pattern(M, AST_SymbolFunarg)),
305 is_generator: M.generator,
306 async: M.async,
307 body: normalize_directives(from_moz(M.body).body)
308 });
309 },
310
311 FunctionExpression: function(M) {
312 return from_moz_lambda(M, /*is_method=*/false);
313 },
314
315 ArrowFunctionExpression: function(M) {
316 const body = M.body.type === "BlockStatement"
317 ? from_moz(M.body).body
318 : [make_node(AST_Return, {}, { value: from_moz(M.body) })];
319 return new AST_Arrow({
320 start: my_start_token(M),
321 end: my_end_token(M),
322 argnames: M.params.map(p => from_moz_pattern(p, AST_SymbolFunarg)),
323 body,
324 async: M.async,
325 });
326 },
327
328 ExpressionStatement: function(M) {
329 return new AST_SimpleStatement({
330 start: my_start_token(M),
331 end: my_end_token(M),
332 body: from_moz(M.expression)
333 });
334 },
335
336 TryStatement: function(M) {
337 var handlers = M.handlers || [M.handler];
338 if (handlers.length > 1 || M.guardedHandlers && M.guardedHandlers.length) {
339 throw new Error("Multiple catch clauses are not supported.");
340 }
341 return new AST_Try({
342 start : my_start_token(M),
343 end : my_end_token(M),
344 body : new AST_TryBlock(from_moz(M.block)),
345 bcatch : from_moz(handlers[0]),
346 bfinally : M.finalizer ? new AST_Finally(from_moz(M.finalizer)) : null
347 });
348 },
349
350 Property: function(M) {
351 if (M.kind == "init" && !M.method) {
352 var args = {
353 start : my_start_token(M.key || M.value),
354 end : my_end_token(M.value),
355 key : M.computed
356 ? from_moz(M.key)
357 : M.key.name || String(M.key.value),
358 quote : from_moz_quote(M.key, M.computed),
359 static : false, // always an object
360 value : from_moz(M.value)
361 };
362
363 return new AST_ObjectKeyVal(args);
364 } else {
365 var value = from_moz_lambda(M.value, /*is_method=*/true);
366 var args = {
367 start : my_start_token(M.key || M.value),
368 end : my_end_token(M.value),
369 key : M.computed
370 ? from_moz(M.key)
371 : from_moz_symbol(AST_SymbolMethod, M.key),
372 quote : from_moz_quote(M.key, M.computed),
373 static : false, // always an object
374 value,
375 };
376
377 if (M.kind == "get") return new AST_ObjectGetter(args);
378 if (M.kind == "set") return new AST_ObjectSetter(args);
379 if (M.method) return new AST_ConciseMethod(args);
380 }
381 },
382
383 MethodDefinition: function(M) {
384 const is_private = M.key.type === "PrivateIdentifier";
385 const key = M.computed ? from_moz(M.key) : new AST_SymbolMethod({ name: M.key.name || String(M.key.value) });
386
387 var args = {
388 start : my_start_token(M),
389 end : my_end_token(M),
390 key,
391 quote : from_moz_quote(M.key, M.computed),
392 value : from_moz_lambda(M.value, /*is_method=*/true),
393 static : M.static,
394 };
395 if (M.kind == "get") {
396 return new (is_private ? AST_PrivateGetter : AST_ObjectGetter)(args);
397 }
398 if (M.kind == "set") {
399 return new (is_private ? AST_PrivateSetter : AST_ObjectSetter)(args);
400 }
401 return new (is_private ? AST_PrivateMethod : AST_ConciseMethod)(args);
402 },
403
404 FieldDefinition: function(M) {
405 let key;
406 if (M.computed) {
407 key = from_moz(M.key);
408 } else {
409 if (M.key.type !== "Identifier") throw new Error("Non-Identifier key in FieldDefinition");
410 key = from_moz(M.key);
411 }
412 return new AST_ClassProperty({
413 start : my_start_token(M),
414 end : my_end_token(M),
415 quote : from_moz_quote(M.key, M.computed),
416 key,
417 value : from_moz(M.value),
418 static : M.static,
419 });
420 },
421
422 PropertyDefinition: function(M) {
423 let key;
424 if (M.computed) {
425 key = from_moz(M.key);
426 } else if (M.key.type === "PrivateIdentifier") {
427 return new AST_ClassPrivateProperty({
428 start : my_start_token(M),
429 end : my_end_token(M),
430 key : from_moz(M.key),
431 value : from_moz(M.value),
432 static : M.static,
433 });
434 } else {
435 key = from_moz_symbol(AST_SymbolClassProperty, M.key);
436 }
437
438 return new AST_ClassProperty({
439 start : my_start_token(M),
440 end : my_end_token(M),
441 quote : from_moz_quote(M.key, M.computed),
442 key,
443 value : from_moz(M.value),
444 static : M.static,
445 });
446 },
447
448 PrivateIdentifier: function (M) {
449 return new AST_SymbolPrivateProperty({
450 start: my_start_token(M),
451 end: my_end_token(M),
452 name: M.name
453 });
454 },
455
456 StaticBlock: function(M) {
457 return new AST_ClassStaticBlock({
458 start : my_start_token(M),
459 end : my_end_token(M),
460 body : M.body.map(from_moz),
461 });
462 },
463
464 ArrayExpression: function(M) {
465 return new AST_Array({
466 start : my_start_token(M),
467 end : my_end_token(M),
468 elements : M.elements.map(function(elem) {
469 return elem === null ? new AST_Hole() : from_moz(elem);
470 })
471 });
472 },
473
474 ObjectExpression: function(M) {
475 return new AST_Object({
476 start : my_start_token(M),
477 end : my_end_token(M),
478 properties : M.properties.map(function(prop) {
479 if (prop.type === "SpreadElement") {
480 return from_moz(prop);
481 }
482 prop.type = "Property";
483 return from_moz(prop);
484 })
485 });
486 },
487
488 SequenceExpression: function(M) {
489 return new AST_Sequence({
490 start : my_start_token(M),
491 end : my_end_token(M),
492 expressions: M.expressions.map(from_moz)
493 });
494 },
495
496 MemberExpression: function(M) {
497 if (M.property.type === "PrivateIdentifier") {
498 return new AST_DotHash({
499 start : my_start_token(M),
500 end : my_end_token(M),
501 property : M.property.name,
502 expression : from_moz(M.object),
503 optional : M.optional || false
504 });
505 }
506 return new (M.computed ? AST_Sub : AST_Dot)({
507 start : my_start_token(M),
508 end : my_end_token(M),
509 property : M.computed ? from_moz(M.property) : M.property.name,
510 expression : from_moz(M.object),
511 optional : M.optional || false
512 });
513 },
514
515 ChainExpression: function(M) {
516 return new AST_Chain({
517 start : my_start_token(M),
518 end : my_end_token(M),
519 expression : from_moz(M.expression)
520 });
521 },
522
523 SwitchCase: function(M) {
524 return new (M.test ? AST_Case : AST_Default)({
525 start : my_start_token(M),
526 end : my_end_token(M),
527 expression : from_moz(M.test),
528 body : M.consequent.map(from_moz)
529 });
530 },
531
532 VariableDeclaration: function(M) {
533 let decl_type;
534 let defs_type = AST_VarDef;
535 let sym_type;
536 let await_using = false;
537 if (M.kind === "const") {
538 decl_type = AST_Const;
539 sym_type = AST_SymbolConst;
540 } else if (M.kind === "let") {
541 decl_type = AST_Let;
542 sym_type = AST_SymbolLet;
543 } else if (M.kind === "using") {
544 decl_type = AST_Using;
545 defs_type = AST_UsingDef;
546 sym_type = AST_SymbolUsing;
547 } else if (M.kind === "await using") {
548 decl_type = AST_Using;
549 defs_type = AST_UsingDef;
550 sym_type = AST_SymbolUsing;
551 await_using = true;
552 } else {
553 decl_type = AST_Var;
554 sym_type = AST_SymbolVar;
555 }
556 const definitions = M.declarations.map(M => {
557 return new defs_type({
558 start: my_start_token(M),
559 end: my_end_token(M),
560 name: from_moz_pattern(M.id, sym_type),
561 value: from_moz(M.init),
562 });
563 });
564 return new decl_type({
565 start : my_start_token(M),
566 end : my_end_token(M),
567 definitions : definitions,
568 await : await_using,
569 });
570 },
571
572 ImportDeclaration: function(M) {
573 var imported_name = null;
574 var imported_names = null;
575 M.specifiers.forEach(function (specifier) {
576 if (specifier.type === "ImportSpecifier" || specifier.type === "ImportNamespaceSpecifier") {
577 if (!imported_names) { imported_names = []; }
578 imported_names.push(from_moz(specifier));
579 } else if (specifier.type === "ImportDefaultSpecifier") {
580 imported_name = from_moz(specifier);
581 }
582 });
583 return new AST_Import({
584 start : my_start_token(M),
585 end : my_end_token(M),
586 imported_name: imported_name,
587 imported_names : imported_names,
588 module_name : from_moz(M.source),
589 attributes: import_attributes_from_moz(M.attributes || M.assertions),
590 phase: M.phase || null
591 });
592 },
593
594 ImportSpecifier: function(M) {
595 return new AST_NameMapping({
596 start: my_start_token(M),
597 end: my_end_token(M),
598 foreign_name: from_moz_symbol(AST_SymbolImportForeign, M.imported, M.imported.type === "Literal"),
599 name: from_moz_symbol(AST_SymbolImport, M.local)
600 });
601 },
602
603 ImportDefaultSpecifier: function(M) {
604 return from_moz_symbol(AST_SymbolImport, M.local);
605 },
606
607 ImportNamespaceSpecifier: function(M) {
608 return new AST_NameMapping({
609 start: my_start_token(M),
610 end: my_end_token(M),
611 foreign_name: new AST_SymbolImportForeign({ name: "*" }),
612 name: from_moz_symbol(AST_SymbolImport, M.local)
613 });
614 },
615
616 ImportExpression: function(M) {
617 const args = [from_moz(M.source)];
618 if (M.options) {
619 args.push(from_moz(M.options));
620 }
621 if (M.phase) {
622 return new AST_DynamicImport({
623 start: my_start_token(M),
624 end: my_end_token(M),
625 phase: M.phase,
626 args: args
627 });
628 }
629 return new AST_Call({
630 start: my_start_token(M),
631 end: my_end_token(M),
632 expression: from_moz({
633 type: "Identifier",
634 name: "import"
635 }),
636 optional: false,
637 args
638 });
639 },
640
641 ExportAllDeclaration: function(M) {
642 var foreign_name = M.exported == null ?
643 new AST_SymbolExportForeign({ name: "*" }) :
644 from_moz_symbol(AST_SymbolExportForeign, M.exported, M.exported.type === "Literal");
645 return new AST_Export({
646 start: my_start_token(M),
647 end: my_end_token(M),
648 exported_names: [
649 new AST_NameMapping({
650 start: my_start_token(M),
651 end: my_end_token(M),
652 name: new AST_SymbolExport({ name: "*" }),
653 foreign_name: foreign_name
654 })
655 ],
656 module_name: from_moz(M.source),
657 attributes: import_attributes_from_moz(M.attributes || M.assertions)
658 });
659 },
660
661 ExportNamedDeclaration: function(M) {
662 if (M.declaration) {
663 // export const, export function, ...
664 return new AST_Export({
665 start: my_start_token(M),
666 end: my_end_token(M),
667 exported_definition: from_moz(M.declaration),
668 exported_names: null,
669 module_name: null,
670 attributes: null,
671 });
672 } else {
673 return new AST_Export({
674 start: my_start_token(M),
675 end: my_end_token(M),
676 exported_definition: null,
677 exported_names: M.specifiers && M.specifiers.length ? M.specifiers.map(from_moz) : [],
678 module_name: from_moz(M.source),
679 attributes: import_attributes_from_moz(M.attributes || M.assertions),
680 });
681 }
682 },
683
684 ExportDefaultDeclaration: function(M) {
685 return new AST_Export({
686 start: my_start_token(M),
687 end: my_end_token(M),
688 exported_value: from_moz(M.declaration),
689 is_default: true
690 });
691 },
692
693 ExportSpecifier: function(M) {
694 return new AST_NameMapping({
695 start: my_start_token(M),
696 end: my_end_token(M),
697 foreign_name: from_moz_symbol(AST_SymbolExportForeign, M.exported, M.exported.type === "Literal"),
698 name: from_moz_symbol(AST_SymbolExport, M.local, M.local.type === "Literal"),
699 });
700 },
701
702 Literal: function(M) {
703 var val = M.value, args = {
704 start : my_start_token(M),
705 end : my_end_token(M)
706 };
707 var rx = M.regex;
708 if (rx && rx.pattern) {
709 // RegExpLiteral as per ESTree AST spec
710 args.value = {
711 source: rx.pattern,
712 flags: rx.flags
713 };
714 return new AST_RegExp(args);
715 } else if (rx) {
716 // support legacy RegExp
717 const rx_source = M.raw || val;
718 const match = rx_source.match(/^\/(.*)\/(\w*)$/);
719 if (!match) throw new Error("Invalid regex source " + rx_source);
720 const [_, source, flags] = match;
721 args.value = { source, flags };
722 return new AST_RegExp(args);
723 }
724 const bi = typeof M.value === "bigint" ? M.value.toString() : M.bigint;
725 if (typeof bi === "string") {
726 args.value = bi;
727 args.raw = M.raw;
728 return new AST_BigInt(args);
729 }
730 if (val === null) return new AST_Null(args);
731 switch (typeof val) {
732 case "string":
733 args.quote = "\"";
734 args.value = val;
735 return new AST_String(args);
736 case "number":
737 args.value = val;
738 args.raw = M.raw || val.toString();
739 return new AST_Number(args);
740 case "boolean":
741 return new (val ? AST_True : AST_False)(args);
742 }
743 },
744
745 MetaProperty: function(M) {
746 if (M.meta.name === "new" && M.property.name === "target") {
747 return new AST_NewTarget({
748 start: my_start_token(M),
749 end: my_end_token(M)
750 });
751 } else if (M.meta.name === "import" && M.property.name === "meta") {
752 return new AST_ImportMeta({
753 start: my_start_token(M),
754 end: my_end_token(M)
755 });
756 }
757 },
758
759 Identifier: function(M) {
760 return new AST_SymbolRef({
761 start : my_start_token(M),
762 end : my_end_token(M),
763 name : M.name
764 });
765 },
766
767 EmptyStatement: function(M) {
768 return new AST_EmptyStatement({
769 start: my_start_token(M),
770 end: my_end_token(M)
771 });
772 },
773
774 BlockStatement: function(M) {
775 return new AST_BlockStatement({
776 start: my_start_token(M),
777 end: my_end_token(M),
778 body: M.body.map(from_moz)
779 });
780 },
781
782 IfStatement: function(M) {
783 return new AST_If({
784 start: my_start_token(M),
785 end: my_end_token(M),
786 condition: from_moz(M.test),
787 body: from_moz(M.consequent),
788 alternative: from_moz(M.alternate)
789 });
790 },
791
792 LabeledStatement: function(M) {
793 try {
794 const label = from_moz_symbol(AST_Label, M.label);
795 FROM_MOZ_LABELS.push(label);
796
797 const stat = new AST_LabeledStatement({
798 start: my_start_token(M),
799 end: my_end_token(M),
800 label,
801 body: from_moz(M.body)
802 });
803
804 return stat;
805 } finally {
806 FROM_MOZ_LABELS.pop();
807 }
808 },
809
810 BreakStatement: function(M) {
811 return new AST_Break({
812 start: my_start_token(M),
813 end: my_end_token(M),
814 label: from_moz_label_ref(M.label),
815 });
816 },
817
818 ContinueStatement: function(M) {
819 return new AST_Continue({
820 start: my_start_token(M),
821 end: my_end_token(M),
822 label: from_moz_label_ref(M.label),
823 });
824 },
825
826 WithStatement: function(M) {
827 return new AST_With({
828 start: my_start_token(M),
829 end: my_end_token(M),
830 expression: from_moz(M.object),
831 body: from_moz(M.body)
832 });
833 },
834
835 SwitchStatement: function(M) {
836 return new AST_Switch({
837 start: my_start_token(M),
838 end: my_end_token(M),
839 expression: from_moz(M.discriminant),
840 body: M.cases.map(from_moz)
841 });
842 },
843
844 ReturnStatement: function(M) {
845 return new AST_Return({
846 start: my_start_token(M),
847 end: my_end_token(M),
848 value: from_moz(M.argument)
849 });
850 },
851
852 ThrowStatement: function(M) {
853 return new AST_Throw({
854 start: my_start_token(M),
855 end: my_end_token(M),
856 value: from_moz(M.argument)
857 });
858 },
859
860 WhileStatement: function(M) {
861 return new AST_While({
862 start: my_start_token(M),
863 end: my_end_token(M),
864 condition: from_moz(M.test),
865 body: from_moz(M.body)
866 });
867 },
868
869 DoWhileStatement: function(M) {
870 return new AST_Do({
871 start: my_start_token(M),
872 end: my_end_token(M),
873 condition: from_moz(M.test),
874 body: from_moz(M.body)
875 });
876 },
877
878 ForStatement: function(M) {
879 return new AST_For({
880 start: my_start_token(M),
881 end: my_end_token(M),
882 init: from_moz(M.init),
883 condition: from_moz(M.test),
884 step: from_moz(M.update),
885 body: from_moz(M.body)
886 });
887 },
888
889 ForInStatement: function(M) {
890 return new AST_ForIn({
891 start: my_start_token(M),
892 end: my_end_token(M),
893 init: from_moz(M.left),
894 object: from_moz(M.right),
895 body: from_moz(M.body)
896 });
897 },
898
899 ForOfStatement: function(M) {
900 return new AST_ForOf({
901 start: my_start_token(M),
902 end: my_end_token(M),
903 init: from_moz(M.left),
904 object: from_moz(M.right),
905 body: from_moz(M.body),
906 await: M.await
907 });
908 },
909
910 AwaitExpression: function(M) {
911 return new AST_Await({
912 start: my_start_token(M),
913 end: my_end_token(M),
914 expression: from_moz(M.argument)
915 });
916 },
917
918 YieldExpression: function(M) {
919 return new AST_Yield({
920 start: my_start_token(M),
921 end: my_end_token(M),
922 expression: from_moz(M.argument),
923 is_star: M.delegate
924 });
925 },
926
927 DebuggerStatement: function(M) {
928 return new AST_Debugger({
929 start: my_start_token(M),
930 end: my_end_token(M)
931 });
932 },
933
934 CatchClause: function(M) {
935 return new AST_Catch({
936 start: my_start_token(M),
937 end: my_end_token(M),
938 argname: M.param ? from_moz_pattern(M.param, AST_SymbolCatch) : null,
939 body: from_moz(M.body).body
940 });
941 },
942
943 ThisExpression: function(M) {
944 return new AST_This({
945 start: my_start_token(M),
946 name: "this",
947 end: my_end_token(M)
948 });
949 },
950
951 Super: function(M) {
952 return new AST_Super({
953 start: my_start_token(M),
954 end: my_end_token(M),
955 name: "super",
956 });
957 },
958
959 BinaryExpression: function(M) {
960 if (M.left.type === "PrivateIdentifier") {
961 return new AST_PrivateIn({
962 start: my_start_token(M),
963 end: my_end_token(M),
964 key: new AST_SymbolPrivateProperty({
965 start: my_start_token(M.left),
966 end: my_end_token(M.left),
967 name: M.left.name
968 }),
969 value: from_moz(M.right),
970 });
971 }
972 return new AST_Binary({
973 start: my_start_token(M),
974 end: my_end_token(M),
975 operator: M.operator,
976 left: from_moz(M.left),
977 right: from_moz(M.right)
978 });
979 },
980
981 LogicalExpression: function(M) {
982 return new AST_Binary({
983 start: my_start_token(M),
984 end: my_end_token(M),
985 operator: M.operator,
986 left: from_moz(M.left),
987 right: from_moz(M.right)
988 });
989 },
990
991 AssignmentExpression: function(M) {
992 return new AST_Assign({
993 start: my_start_token(M),
994 end: my_end_token(M),
995 operator: M.operator,
996 logical: M.operator === "??=" || M.operator === "&&=" || M.operator === "||=",
997 left: from_moz(M.left),
998 right: from_moz(M.right)
999 });
1000 },
1001
1002 ConditionalExpression: function(M) {
1003 return new AST_Conditional({
1004 start: my_start_token(M),
1005 end: my_end_token(M),
1006 condition: from_moz(M.test),
1007 consequent: from_moz(M.consequent),
1008 alternative: from_moz(M.alternate)
1009 });
1010 },
1011
1012 NewExpression: function(M) {
1013 return new AST_New({
1014 start: my_start_token(M),
1015 end: my_end_token(M),
1016 expression: from_moz(M.callee),
1017 args: M.arguments.map(from_moz)
1018 });
1019 },
1020
1021 CallExpression: function(M) {
1022 return new AST_Call({
1023 start: my_start_token(M),
1024 end: my_end_token(M),
1025 expression: from_moz(M.callee),
1026 optional: M.optional,
1027 args: M.arguments.map(from_moz)
1028 });
1029 }
1030 };
1031
1032 MOZ_TO_ME.UpdateExpression =
1033 MOZ_TO_ME.UnaryExpression = function To_Moz_Unary(M) {
1034 var prefix = "prefix" in M ? M.prefix
1035 : M.type == "UnaryExpression" ? true : false;
1036 return new (prefix ? AST_UnaryPrefix : AST_UnaryPostfix)({
1037 start : my_start_token(M),
1038 end : my_end_token(M),
1039 operator : M.operator,
1040 expression : from_moz(M.argument)
1041 });
1042 };
1043
1044 MOZ_TO_ME.ClassDeclaration =
1045 MOZ_TO_ME.ClassExpression = function From_Moz_Class(M) {
1046 return new (M.type === "ClassDeclaration" ? AST_DefClass : AST_ClassExpression)({
1047 start : my_start_token(M),
1048 end : my_end_token(M),
1049 name : M.id && from_moz_symbol(M.type === "ClassDeclaration" ? AST_SymbolDefClass : AST_SymbolClass, M.id),
1050 extends : from_moz(M.superClass),
1051 properties: M.body.body.map(from_moz)
1052 });
1053 };
1054
1055 def_to_moz(AST_EmptyStatement, function To_Moz_EmptyStatement() {
1056 return {
1057 type: "EmptyStatement"
1058 };
1059 });
1060 def_to_moz(AST_BlockStatement, function To_Moz_BlockStatement(M) {
1061 return {
1062 type: "BlockStatement",
1063 body: M.body.map(to_moz)
1064 };
1065 });
1066 def_to_moz(AST_If, function To_Moz_IfStatement(M) {
1067 return {
1068 type: "IfStatement",
1069 test: to_moz(M.condition),
1070 consequent: to_moz(M.body),
1071 alternate: to_moz(M.alternative)
1072 };
1073 });
1074 def_to_moz(AST_LabeledStatement, function To_Moz_LabeledStatement(M) {
1075 return {
1076 type: "LabeledStatement",
1077 label: to_moz(M.label),
1078 body: to_moz(M.body)
1079 };
1080 });
1081 def_to_moz(AST_Break, function To_Moz_BreakStatement(M) {
1082 return {
1083 type: "BreakStatement",
1084 label: to_moz(M.label)
1085 };
1086 });
1087 def_to_moz(AST_Continue, function To_Moz_ContinueStatement(M) {
1088 return {
1089 type: "ContinueStatement",
1090 label: to_moz(M.label)
1091 };
1092 });
1093 def_to_moz(AST_With, function To_Moz_WithStatement(M) {
1094 return {
1095 type: "WithStatement",
1096 object: to_moz(M.expression),
1097 body: to_moz(M.body)
1098 };
1099 });
1100 def_to_moz(AST_Switch, function To_Moz_SwitchStatement(M) {
1101 return {
1102 type: "SwitchStatement",
1103 discriminant: to_moz(M.expression),
1104 cases: M.body.map(to_moz)
1105 };
1106 });
1107 def_to_moz(AST_Return, function To_Moz_ReturnStatement(M) {
1108 return {
1109 type: "ReturnStatement",
1110 argument: to_moz(M.value)
1111 };
1112 });
1113 def_to_moz(AST_Throw, function To_Moz_ThrowStatement(M) {
1114 return {
1115 type: "ThrowStatement",
1116 argument: to_moz(M.value)
1117 };
1118 });
1119 def_to_moz(AST_While, function To_Moz_WhileStatement(M) {
1120 return {
1121 type: "WhileStatement",
1122 test: to_moz(M.condition),
1123 body: to_moz(M.body)
1124 };
1125 });
1126 def_to_moz(AST_Do, function To_Moz_DoWhileStatement(M) {
1127 return {
1128 type: "DoWhileStatement",
1129 test: to_moz(M.condition),
1130 body: to_moz(M.body)
1131 };
1132 });
1133 def_to_moz(AST_For, function To_Moz_ForStatement(M) {
1134 return {
1135 type: "ForStatement",
1136 init: to_moz(M.init),
1137 test: to_moz(M.condition),
1138 update: to_moz(M.step),
1139 body: to_moz(M.body)
1140 };
1141 });
1142 def_to_moz(AST_ForIn, function To_Moz_ForInStatement(M) {
1143 return {
1144 type: "ForInStatement",
1145 left: to_moz(M.init),
1146 right: to_moz(M.object),
1147 body: to_moz(M.body)
1148 };
1149 });
1150 def_to_moz(AST_ForOf, function To_Moz_ForOfStatement(M) {
1151 return {
1152 type: "ForOfStatement",
1153 left: to_moz(M.init),
1154 right: to_moz(M.object),
1155 body: to_moz(M.body),
1156 await: M.await
1157 };
1158 });
1159 def_to_moz(AST_Await, function To_Moz_AwaitExpression(M) {
1160 return {
1161 type: "AwaitExpression",
1162 argument: to_moz(M.expression)
1163 };
1164 });
1165 def_to_moz(AST_Yield, function To_Moz_YieldExpression(M) {
1166 return {
1167 type: "YieldExpression",
1168 argument: to_moz(M.expression),
1169 delegate: M.is_star
1170 };
1171 });
1172 def_to_moz(AST_Debugger, function To_Moz_DebuggerStatement() {
1173 return {
1174 type: "DebuggerStatement"
1175 };
1176 });
1177 def_to_moz(AST_VarDefLike, function To_Moz_VariableDeclarator(M) {
1178 return {
1179 type: "VariableDeclarator",
1180 id: to_moz(M.name),
1181 init: to_moz(M.value)
1182 };
1183 });
1184
1185 def_to_moz(AST_This, function To_Moz_ThisExpression() {
1186 return {
1187 type: "ThisExpression"
1188 };
1189 });
1190 def_to_moz(AST_Super, function To_Moz_Super() {
1191 return {
1192 type: "Super"
1193 };
1194 });
1195 def_to_moz(AST_Conditional, function To_Moz_ConditionalExpression(M) {
1196 return {
1197 type: "ConditionalExpression",
1198 test: to_moz(M.condition),
1199 consequent: to_moz(M.consequent),
1200 alternate: to_moz(M.alternative)
1201 };
1202 });
1203 def_to_moz(AST_New, function To_Moz_NewExpression(M) {
1204 return {
1205 type: "NewExpression",
1206 callee: to_moz(M.expression),
1207 arguments: M.args.map(to_moz)
1208 };
1209 });
1210 def_to_moz(AST_Call, function To_Moz_CallExpression(M) {
1211 if (M.expression instanceof AST_SymbolRef && M.expression.name === "import") {
1212 const [source, options] = M.args.map(to_moz);
1213 return {
1214 type: "ImportExpression",
1215 source,
1216 options: options || null
1217 };
1218 }
1219
1220 return {
1221 type: "CallExpression",
1222 callee: to_moz(M.expression),
1223 optional: M.optional,
1224 arguments: M.args.map(to_moz)
1225 };
1226 });
1227
1228 def_to_moz(AST_DynamicImport, function To_Moz_ImportExpression(M) {
1229 const [source, options] = M.args.map(to_moz);
1230 return {
1231 type: "ImportExpression",
1232 source,
1233 options: options || null,
1234 phase: M.phase
1235 };
1236 });
1237
1238 def_to_moz(AST_Toplevel, function To_Moz_Program(M) {
1239 return to_moz_scope("Program", M);
1240 });
1241
1242 def_to_moz(AST_Expansion, function To_Moz_Spread(M) {
1243 return {
1244 type: to_moz_in_destructuring() ? "RestElement" : "SpreadElement",
1245 argument: to_moz(M.expression)
1246 };
1247 });
1248
1249 def_to_moz(AST_PrefixedTemplateString, function To_Moz_TaggedTemplateExpression(M) {
1250 return {
1251 type: "TaggedTemplateExpression",
1252 tag: to_moz(M.prefix),
1253 quasi: to_moz(M.template_string)
1254 };
1255 });
1256
1257 def_to_moz(AST_TemplateString, function To_Moz_TemplateLiteral(M) {
1258 var quasis = [];
1259 var expressions = [];
1260 for (var i = 0; i < M.segments.length; i++) {
1261 if (i % 2 !== 0) {
1262 expressions.push(to_moz(M.segments[i]));
1263 } else {
1264 quasis.push({
1265 type: "TemplateElement",
1266 value: {
1267 raw: M.segments[i].raw,
1268 cooked: M.segments[i].value
1269 },
1270 tail: i === M.segments.length - 1
1271 });
1272 }
1273 }
1274 return {
1275 type: "TemplateLiteral",
1276 quasis: quasis,
1277 expressions: expressions
1278 };
1279 });
1280
1281 def_to_moz(AST_Defun, function To_Moz_FunctionDeclaration(M) {
1282 return {
1283 type: "FunctionDeclaration",
1284 id: to_moz(M.name),
1285 params: M.argnames.map(to_moz_pattern),
1286 generator: M.is_generator,
1287 async: M.async,
1288 body: to_moz_scope("BlockStatement", M)
1289 };
1290 });
1291
1292 def_to_moz(AST_Function, function To_Moz_FunctionExpression(M) {
1293 return {
1294 type: "FunctionExpression",
1295 id: to_moz(M.name),
1296 params: M.argnames.map(to_moz_pattern),
1297 generator: M.is_generator || false,
1298 async: M.async || false,
1299 body: to_moz_scope("BlockStatement", M)
1300 };
1301 });
1302
1303 def_to_moz(AST_Arrow, function To_Moz_ArrowFunctionExpression(M) {
1304 var body = M.body.length === 1 && M.body[0] instanceof AST_Return && M.body[0].value
1305 ? to_moz(M.body[0].value)
1306 : {
1307 type: "BlockStatement",
1308 body: M.body.map(to_moz)
1309 };
1310 return {
1311 type: "ArrowFunctionExpression",
1312 params: M.argnames.map(to_moz_pattern),
1313 async: M.async,
1314 body: body,
1315 };
1316 });
1317
1318 def_to_moz(AST_Destructuring, function To_Moz_ObjectPattern(M) {
1319 if (M.is_array) {
1320 return {
1321 type: "ArrayPattern",
1322 elements: M.names.map(
1323 M => M instanceof AST_Hole ? null : to_moz_pattern(M)
1324 ),
1325 };
1326 }
1327 return {
1328 type: "ObjectPattern",
1329 properties: M.names.map(M => {
1330 if (M instanceof AST_ObjectKeyVal) {
1331 var computed = M.computed_key();
1332 const [shorthand, key] = to_moz_property_key(M.key, computed, M.quote, M.value);
1333
1334 return {
1335 type: "Property",
1336 computed,
1337 kind: "init",
1338 key: key,
1339 method: false,
1340 shorthand,
1341 value: to_moz_pattern(M.value)
1342 };
1343 } else {
1344 return to_moz_pattern(M);
1345 }
1346 }),
1347 };
1348 });
1349
1350 def_to_moz(AST_DefaultAssign, function To_Moz_AssignmentExpression(M) {
1351 return {
1352 type: "AssignmentPattern",
1353 left: to_moz_pattern(M.left),
1354 right: to_moz(M.right),
1355 };
1356 });
1357
1358 def_to_moz(AST_Directive, function To_Moz_Directive(M) {
1359 return {
1360 type: "ExpressionStatement",
1361 expression: {
1362 type: "Literal",
1363 value: M.value,
1364 raw: M.print_to_string()
1365 },
1366 directive: M.value
1367 };
1368 });
1369
1370 def_to_moz(AST_SimpleStatement, function To_Moz_ExpressionStatement(M) {
1371 return {
1372 type: "ExpressionStatement",
1373 expression: to_moz(M.body)
1374 };
1375 });
1376
1377 def_to_moz(AST_SwitchBranch, function To_Moz_SwitchCase(M) {
1378 return {
1379 type: "SwitchCase",
1380 test: to_moz(M.expression),
1381 consequent: M.body.map(to_moz)
1382 };
1383 });
1384
1385 def_to_moz(AST_Try, function To_Moz_TryStatement(M) {
1386 return {
1387 type: "TryStatement",
1388 block: to_moz_block(M.body),
1389 handler: to_moz(M.bcatch),
1390 guardedHandlers: [],
1391 finalizer: to_moz(M.bfinally)
1392 };
1393 });
1394
1395 def_to_moz(AST_Catch, function To_Moz_CatchClause(M) {
1396 return {
1397 type: "CatchClause",
1398 param: M.argname != null ? to_moz_pattern(M.argname) : null,
1399 body: to_moz_block(M)
1400 };
1401 });
1402
1403 def_to_moz(AST_DefinitionsLike, function To_Moz_VariableDeclaration(M) {
1404 return {
1405 type: "VariableDeclaration",
1406 kind:
1407 M instanceof AST_Const ? "const" :
1408 M instanceof AST_Let ? "let" :
1409 M instanceof AST_Using ? (M.await ? "await using" : "using") :
1410 "var",
1411 declarations: M.definitions.map(to_moz)
1412 };
1413 });
1414
1415 function import_attributes_to_moz(attribute) {
1416 const import_attributes = [];
1417 if (attribute) {
1418 for (const { key, value } of attribute.properties) {
1419 const key_moz = is_basic_identifier_string(key)
1420 ? { type: "Identifier", name: key }
1421 : { type: "Literal", value: key, raw: JSON.stringify(key) };
1422 import_attributes.push({
1423 type: "ImportAttribute",
1424 key: key_moz,
1425 value: to_moz(value)
1426 });
1427 }
1428 }
1429 return import_attributes;
1430 }
1431
1432 def_to_moz(AST_Export, function To_Moz_ExportDeclaration(M) {
1433 if (M.exported_names) {
1434 var first_exported = M.exported_names[0];
1435 if (first_exported && first_exported.name.name === "*" && !first_exported.name.quote) {
1436 var foreign_name = first_exported.foreign_name;
1437 var exported = foreign_name.name === "*" && !foreign_name.quote
1438 ? null
1439 : to_moz(foreign_name);
1440 return {
1441 type: "ExportAllDeclaration",
1442 source: to_moz(M.module_name),
1443 exported: exported,
1444 attributes: import_attributes_to_moz(M.attributes)
1445 };
1446 }
1447 return {
1448 type: "ExportNamedDeclaration",
1449 specifiers: M.exported_names.map(function (name_mapping) {
1450 return {
1451 type: "ExportSpecifier",
1452 exported: to_moz(name_mapping.foreign_name),
1453 local: to_moz(name_mapping.name)
1454 };
1455 }),
1456 declaration: to_moz(M.exported_definition),
1457 source: to_moz(M.module_name),
1458 attributes: import_attributes_to_moz(M.attributes)
1459 };
1460 }
1461
1462 if (M.is_default) {
1463 return {
1464 type: "ExportDefaultDeclaration",
1465 declaration: to_moz(M.exported_value || M.exported_definition),
1466 };
1467 } else {
1468 return {
1469 type: "ExportNamedDeclaration",
1470 declaration: to_moz(M.exported_value || M.exported_definition),
1471 specifiers: [],
1472 source: null,
1473 };
1474 }
1475 });
1476
1477 def_to_moz(AST_Import, function To_Moz_ImportDeclaration(M) {
1478 var specifiers = [];
1479 if (M.imported_name) {
1480 specifiers.push({
1481 type: "ImportDefaultSpecifier",
1482 local: to_moz(M.imported_name)
1483 });
1484 }
1485 if (M.imported_names) {
1486 var first_imported_foreign_name = M.imported_names[0].foreign_name;
1487 if (first_imported_foreign_name.name === "*" && !first_imported_foreign_name.quote) {
1488 specifiers.push({
1489 type: "ImportNamespaceSpecifier",
1490 local: to_moz(M.imported_names[0].name)
1491 });
1492 } else {
1493 M.imported_names.forEach(function(name_mapping) {
1494 specifiers.push({
1495 type: "ImportSpecifier",
1496 local: to_moz(name_mapping.name),
1497 imported: to_moz(name_mapping.foreign_name)
1498 });
1499 });
1500 }
1501 }
1502 var moz = {
1503 type: "ImportDeclaration",
1504 specifiers: specifiers,
1505 source: to_moz(M.module_name),
1506 attributes: import_attributes_to_moz(M.attributes)
1507 };
1508 if (M.phase) moz.phase = M.phase;
1509 return moz;
1510 });
1511
1512 def_to_moz(AST_ImportMeta, function To_Moz_MetaProperty() {
1513 return {
1514 type: "MetaProperty",
1515 meta: {
1516 type: "Identifier",
1517 name: "import"
1518 },
1519 property: {
1520 type: "Identifier",
1521 name: "meta"
1522 }
1523 };
1524 });
1525
1526 def_to_moz(AST_Sequence, function To_Moz_SequenceExpression(M) {
1527 return {
1528 type: "SequenceExpression",
1529 expressions: M.expressions.map(to_moz)
1530 };
1531 });
1532
1533 def_to_moz(AST_DotHash, function To_Moz_PrivateMemberExpression(M) {
1534 return {
1535 type: "MemberExpression",
1536 object: to_moz(M.expression),
1537 computed: false,
1538 property: {
1539 type: "PrivateIdentifier",
1540 name: M.property
1541 },
1542 optional: M.optional
1543 };
1544 });
1545
1546 def_to_moz(AST_PropAccess, function To_Moz_MemberExpression(M) {
1547 var isComputed = M instanceof AST_Sub;
1548 return {
1549 type: "MemberExpression",
1550 object: to_moz(M.expression),
1551 computed: isComputed,
1552 property: isComputed ? to_moz(M.property) : {type: "Identifier", name: M.property},
1553 optional: M.optional
1554 };
1555 });
1556
1557 def_to_moz(AST_Chain, function To_Moz_ChainExpression(M) {
1558 return {
1559 type: "ChainExpression",
1560 expression: to_moz(M.expression)
1561 };
1562 });
1563
1564 def_to_moz(AST_Unary, function To_Moz_Unary(M) {
1565 return {
1566 type: M.operator == "++" || M.operator == "--" ? "UpdateExpression" : "UnaryExpression",
1567 operator: M.operator,
1568 prefix: M instanceof AST_UnaryPrefix,
1569 argument: to_moz(M.expression)
1570 };
1571 });
1572
1573 def_to_moz(AST_Binary, function To_Moz_BinaryExpression(M) {
1574 if (M.operator == "=" && to_moz_in_destructuring()) {
1575 return {
1576 type: "AssignmentPattern",
1577 left: to_moz(M.left),
1578 right: to_moz(M.right)
1579 };
1580 }
1581
1582 const type = M.operator == "&&" || M.operator == "||" || M.operator === "??"
1583 ? "LogicalExpression"
1584 : "BinaryExpression";
1585
1586 return {
1587 type,
1588 left: to_moz(M.left),
1589 operator: M.operator,
1590 right: to_moz(M.right)
1591 };
1592 });
1593
1594 def_to_moz(AST_Assign, function To_Moz_AssignmentExpression(M) {
1595 return {
1596 type: "AssignmentExpression",
1597 operator: M.operator,
1598 left: to_moz(M.left),
1599 right: to_moz(M.right)
1600 };
1601 });
1602
1603 def_to_moz(AST_PrivateIn, function To_Moz_BinaryExpression_PrivateIn(M) {
1604 return {
1605 type: "BinaryExpression",
1606 left: { type: "PrivateIdentifier", name: M.key.name },
1607 operator: "in",
1608 right: to_moz(M.value),
1609 };
1610 });
1611
1612 def_to_moz(AST_Array, function To_Moz_ArrayExpression(M) {
1613 return {
1614 type: "ArrayExpression",
1615 elements: M.elements.map(to_moz)
1616 };
1617 });
1618
1619 def_to_moz(AST_Object, function To_Moz_ObjectExpression(M) {
1620 return {
1621 type: "ObjectExpression",
1622 properties: M.properties.map(to_moz)
1623 };
1624 });
1625
1626 def_to_moz(AST_ObjectProperty, function To_Moz_Property(M, parent) {
1627 var computed = M.computed_key();
1628 const [shorthand, key] = to_moz_property_key(M.key, computed, M.quote, M.value);
1629
1630 var kind;
1631 if (M instanceof AST_ObjectGetter) {
1632 kind = "get";
1633 } else
1634 if (M instanceof AST_ObjectSetter) {
1635 kind = "set";
1636 }
1637 if (M instanceof AST_PrivateGetter || M instanceof AST_PrivateSetter) {
1638 const kind = M instanceof AST_PrivateGetter ? "get" : "set";
1639 return {
1640 type: "MethodDefinition",
1641 computed: false,
1642 kind: kind,
1643 static: M.static,
1644 key: {
1645 type: "PrivateIdentifier",
1646 name: M.key.name
1647 },
1648 value: to_moz(M.value)
1649 };
1650 }
1651 if (M instanceof AST_ClassPrivateProperty) {
1652 return {
1653 type: "PropertyDefinition",
1654 key: {
1655 type: "PrivateIdentifier",
1656 name: M.key.name
1657 },
1658 value: to_moz(M.value),
1659 computed: false,
1660 static: M.static
1661 };
1662 }
1663 if (M instanceof AST_ClassProperty) {
1664 return {
1665 type: "PropertyDefinition",
1666 key,
1667 value: to_moz(M.value),
1668 computed,
1669 static: M.static
1670 };
1671 }
1672 if (parent instanceof AST_Class) {
1673 return {
1674 type: "MethodDefinition",
1675 computed: computed,
1676 kind: kind,
1677 static: M.static,
1678 key: to_moz(M.key),
1679 value: to_moz(M.value)
1680 };
1681 }
1682 return {
1683 type: "Property",
1684 computed: computed,
1685 method: false,
1686 shorthand,
1687 kind: kind,
1688 key: key,
1689 value: to_moz(M.value)
1690 };
1691 });
1692
1693 def_to_moz(AST_ObjectKeyVal, function To_Moz_Property(M) {
1694 var computed = M.computed_key();
1695 const [shorthand, key] = to_moz_property_key(M.key, computed, M.quote, M.value);
1696
1697 return {
1698 type: "Property",
1699 computed: computed,
1700 shorthand: shorthand,
1701 method: false,
1702 kind: "init",
1703 key: key,
1704 value: to_moz(M.value)
1705 };
1706 });
1707
1708 def_to_moz(AST_ConciseMethod, function To_Moz_MethodDefinition(M, parent) {
1709 const computed = M.computed_key();
1710 const [_always_false, key] = to_moz_property_key(M.key, computed, M.quote, M.value);
1711
1712 if (parent instanceof AST_Object) {
1713 return {
1714 type: "Property",
1715 kind: "init",
1716 computed,
1717 method: true,
1718 shorthand: false,
1719 key,
1720 value: to_moz(M.value),
1721 };
1722 }
1723
1724 return {
1725 type: "MethodDefinition",
1726 kind: !computed && M.key.name === "constructor" ? "constructor" : "method",
1727 computed,
1728 key,
1729 value: to_moz(M.value),
1730 static: M.static,
1731 };
1732 });
1733
1734 def_to_moz(AST_PrivateMethod, function To_Moz_MethodDefinition(M) {
1735 return {
1736 type: "MethodDefinition",
1737 kind: "method",
1738 key: { type: "PrivateIdentifier", name: M.key.name },
1739 value: to_moz(M.value),
1740 computed: false,
1741 static: M.static,
1742 };
1743 });
1744
1745 def_to_moz(AST_Class, function To_Moz_Class(M) {
1746 var type = M instanceof AST_ClassExpression ? "ClassExpression" : "ClassDeclaration";
1747 return {
1748 type: type,
1749 superClass: to_moz(M.extends),
1750 id: M.name ? to_moz(M.name) : null,
1751 body: {
1752 type: "ClassBody",
1753 body: M.properties.map(to_moz)
1754 }
1755 };
1756 });
1757
1758 def_to_moz(AST_ClassStaticBlock, function To_Moz_StaticBlock(M) {
1759 return {
1760 type: "StaticBlock",
1761 body: M.body.map(to_moz),
1762 };
1763 });
1764
1765 def_to_moz(AST_NewTarget, function To_Moz_MetaProperty() {
1766 return {
1767 type: "MetaProperty",
1768 meta: {
1769 type: "Identifier",
1770 name: "new"
1771 },
1772 property: {
1773 type: "Identifier",
1774 name: "target"
1775 }
1776 };
1777 });
1778
1779 def_to_moz(AST_Symbol, function To_Moz_Identifier(M, parent) {
1780 if (
1781 (M instanceof AST_SymbolMethod && parent.quote) ||
1782 ((
1783 M instanceof AST_SymbolImportForeign ||
1784 M instanceof AST_SymbolExportForeign ||
1785 M instanceof AST_SymbolExport
1786 ) && M.quote)
1787 ) {
1788 return {
1789 type: "Literal",
1790 value: M.name
1791 };
1792 }
1793 var def = M.definition();
1794 return {
1795 type: "Identifier",
1796 name: def ? def.mangled_name || def.name : M.name
1797 };
1798 });
1799
1800 def_to_moz(AST_RegExp, function To_Moz_RegExpLiteral(M) {
1801 const pattern = M.value.source;
1802 const flags = M.value.flags;
1803 return {
1804 type: "Literal",
1805 value: null,
1806 raw: M.print_to_string(),
1807 regex: { pattern, flags }
1808 };
1809 });
1810
1811 def_to_moz(AST_Constant, function To_Moz_Literal(M) {
1812 var value = M.value;
1813 return {
1814 type: "Literal",
1815 value: value,
1816 raw: M.raw || M.print_to_string()
1817 };
1818 });
1819
1820 def_to_moz(AST_Atom, function To_Moz_Atom(M) {
1821 return {
1822 type: "Identifier",
1823 name: String(M.value)
1824 };
1825 });
1826
1827 def_to_moz(AST_BigInt, M => ({
1828 type: "Literal",
1829 // value cannot be represented natively
1830 // see: https://github.com/estree/estree/blob/master/es2020.md#bigintliteral
1831 value: null,
1832 // `M.value` is a string that may be a hex number representation.
1833 // but "bigint" property should have only decimal digits
1834 bigint: typeof BigInt === "function" ? BigInt(M.value).toString() : M.value,
1835 raw: M.raw,
1836 }));
1837
1838 AST_Boolean.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
1839 AST_Null.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
1840 AST_Hole.DEFMETHOD("to_mozilla_ast", function To_Moz_ArrayHole() { return null; });
1841
1842 AST_Block.DEFMETHOD("to_mozilla_ast", AST_BlockStatement.prototype.to_mozilla_ast);
1843 AST_Lambda.DEFMETHOD("to_mozilla_ast", AST_Function.prototype.to_mozilla_ast);
1844
1845 /* -----[ tools ]----- */
1846
1847 function my_start_token(moznode) {
1848 var loc = moznode.loc, start = loc && loc.start;
1849 var range = moznode.range;
1850 return new AST_Token(
1851 "",
1852 "",
1853 start && start.line || 0,
1854 start && start.column || 0,
1855 range ? range [0] : moznode.start,
1856 false,
1857 [],
1858 [],
1859 loc && loc.source,
1860 );
1861 }
1862
1863 function my_end_token(moznode) {
1864 var loc = moznode.loc, end = loc && loc.end;
1865 var range = moznode.range;
1866 return new AST_Token(
1867 "",
1868 "",
1869 end && end.line || 0,
1870 end && end.column || 0,
1871 range ? range [0] : moznode.end,
1872 false,
1873 [],
1874 [],
1875 loc && loc.source,
1876 );
1877 }
1878
1879 var FROM_MOZ_LABELS = null;
1880
1881 function from_moz(node) {
1882 if (node == null) return null;
1883 return MOZ_TO_ME[node.type](node);
1884 }
1885
1886 function from_moz_quote(moz_key, computed) {
1887 if (!computed && moz_key.type === "Literal" && typeof moz_key.value === "string") {
1888 return '"';
1889 } else {
1890 return "";
1891 }
1892 }
1893
1894 function from_moz_symbol(symbol_type, M, has_quote) {
1895 return new symbol_type({
1896 start: my_start_token(M),
1897 quote: has_quote ? '"' : undefined,
1898 name: M.type === "Identifier" ? M.name : String(M.value),
1899 end: my_end_token(M),
1900 });
1901 }
1902
1903 function from_moz_lambda(M, is_method) {
1904 return new (is_method ? AST_Accessor : AST_Function)({
1905 start: my_start_token(M),
1906 end: my_end_token(M),
1907 name: M.id && from_moz_symbol(is_method ? AST_SymbolMethod : AST_SymbolLambda, M.id),
1908 argnames: M.params.map(M => from_moz_pattern(M, AST_SymbolFunarg)),
1909 is_generator: M.generator,
1910 async: M.async,
1911 body: normalize_directives(from_moz(M.body).body)
1912 });
1913 }
1914
1915 function from_moz_pattern(M, sym_type) {
1916 switch (M.type) {
1917 case "ObjectPattern":
1918 return new AST_Destructuring({
1919 start: my_start_token(M),
1920 end: my_end_token(M),
1921 names: M.properties.map(p => from_moz_pattern(p, sym_type)),
1922 is_array: false
1923 });
1924
1925 case "Property":
1926 var key = M.key;
1927 var args = {
1928 start : my_start_token(key || M.value),
1929 end : my_end_token(M.value),
1930 key : key.type == "Identifier" ? key.name : String(key.value),
1931 quote : !M.computed && key.type === "Literal" && typeof key.value === "string"
1932 ? '"'
1933 : "",
1934 value : from_moz_pattern(M.value, sym_type)
1935 };
1936 if (M.computed) {
1937 args.key = from_moz(M.key);
1938 }
1939 return new AST_ObjectKeyVal(args);
1940
1941 case "ArrayPattern":
1942 return new AST_Destructuring({
1943 start: my_start_token(M),
1944 end: my_end_token(M),
1945 names: M.elements.map(function(elm) {
1946 if (elm === null) {
1947 return new AST_Hole();
1948 }
1949 return from_moz_pattern(elm, sym_type);
1950 }),
1951 is_array: true
1952 });
1953
1954 case "SpreadElement":
1955 case "RestElement":
1956 return new AST_Expansion({
1957 start: my_start_token(M),
1958 end: my_end_token(M),
1959 expression: from_moz_pattern(M.argument, sym_type),
1960 });
1961
1962 case "AssignmentPattern":
1963 return new AST_DefaultAssign({
1964 start : my_start_token(M),
1965 end : my_end_token(M),
1966 left : from_moz_pattern(M.left, sym_type),
1967 operator: "=",
1968 right : from_moz(M.right),
1969 });
1970
1971 case "Identifier":
1972 return new sym_type({
1973 start : my_start_token(M),
1974 end : my_end_token(M),
1975 name : M.name,
1976 });
1977
1978 default:
1979 throw new Error("Invalid node type for destructuring: " + M.type);
1980 }
1981 }
1982
1983 function from_moz_label_ref(m_label) {
1984 if (!m_label) return null;
1985
1986 const label = from_moz_symbol(AST_LabelRef, m_label);
1987
1988 let i = FROM_MOZ_LABELS.length;
1989 while (i--) {
1990 const label_origin = FROM_MOZ_LABELS[i];
1991
1992 if (label.name === label_origin.name) {
1993 label.thedef = label_origin;
1994 break;
1995 }
1996 }
1997
1998 return label;
1999 }
2000
2001 AST_Node.from_mozilla_ast = function(node) {
2002 var save_labels = FROM_MOZ_LABELS;
2003 FROM_MOZ_LABELS = [];
2004 var ast = from_moz(node);
2005 FROM_MOZ_LABELS = save_labels;
2006 return ast;
2007 };
2008
2009 function set_moz_loc(mynode, moznode) {
2010 var start = mynode.start;
2011 var end = mynode.end;
2012 if (!(start && end)) {
2013 return moznode;
2014 }
2015 if (start.pos != null && end.endpos != null) {
2016 moznode.range = [start.pos, end.endpos];
2017 }
2018 if (start.line) {
2019 moznode.loc = {
2020 start: {line: start.line, column: start.col},
2021 end: end.endline ? {line: end.endline, column: end.endcol} : null
2022 };
2023 if (start.file) {
2024 moznode.loc.source = start.file;
2025 }
2026 }
2027 return moznode;
2028 }
2029
2030 function def_to_moz(mytype, handler) {
2031 mytype.DEFMETHOD("to_mozilla_ast", function(parent) {
2032 return set_moz_loc(this, handler(this, parent));
2033 });
2034 }
2035
2036 var TO_MOZ_STACK = null;
2037
2038 function to_moz(node) {
2039 if (TO_MOZ_STACK === null) { TO_MOZ_STACK = []; }
2040 TO_MOZ_STACK.push(node);
2041 var ast = node != null ? node.to_mozilla_ast(TO_MOZ_STACK[TO_MOZ_STACK.length - 2]) : null;
2042 TO_MOZ_STACK.pop();
2043 if (TO_MOZ_STACK.length === 0) { TO_MOZ_STACK = null; }
2044 return ast;
2045 }
2046
2047 /** Object property keys can be number literals, string literals, or raw names. Additionally they can be shorthand. We decide that here. */
2048 function to_moz_property_key(key, computed = false, quote = false, value = null) {
2049 if (computed) {
2050 return [false, to_moz(key)];
2051 }
2052
2053 const key_name = typeof key === "string" ? key : key.name;
2054 let moz_key;
2055 if (quote) {
2056 moz_key = { type: "Literal", value: key_name, raw: JSON.stringify(key_name) };
2057 } else if ("" + +key_name === key_name && +key_name >= 0) {
2058 // representable as a number
2059 moz_key = { type: "Literal", value: +key_name, raw: JSON.stringify(+key_name) };
2060 } else {
2061 moz_key = { type: "Identifier", name: key_name };
2062 }
2063
2064 const shorthand =
2065 moz_key.type === "Identifier"
2066 && moz_key.name === key_name
2067 && (value instanceof AST_Symbol && value.name === key_name
2068 || value instanceof AST_DefaultAssign && value.left.name === key_name);
2069 return [shorthand, moz_key];
2070 }
2071
2072 function to_moz_pattern(node) {
2073 if (node instanceof AST_Expansion) {
2074 return {
2075 type: "RestElement",
2076 argument: to_moz_pattern(node.expression),
2077 };
2078 }
2079
2080 if ((
2081 node instanceof AST_Symbol
2082 || node instanceof AST_Destructuring
2083 || node instanceof AST_DefaultAssign
2084 || node instanceof AST_PropAccess
2085 )) {
2086 // Plain translation
2087 return to_moz(node);
2088 }
2089
2090 throw new Error(node.TYPE);
2091 }
2092
2093 function to_moz_in_destructuring() {
2094 var i = TO_MOZ_STACK.length;
2095 while (i--) {
2096 if (TO_MOZ_STACK[i] instanceof AST_Destructuring) {
2097 return true;
2098 }
2099 }
2100 return false;
2101 }
2102
2103 function to_moz_block(node) {
2104 return {
2105 type: "BlockStatement",
2106 body: node.body.map(to_moz)
2107 };
2108 }
2109
2110 function to_moz_scope(type, node) {
2111 var body = node.body.map(to_moz);
2112 if (node.body[0] instanceof AST_SimpleStatement && node.body[0].body instanceof AST_String) {
2113 body.unshift(to_moz(new AST_EmptyStatement(node.body[0])));
2114 }
2115 return {
2116 type: type,
2117 body: body
2118 };
2119 }
2120})();
Note: See TracBrowser for help on using the repository browser.