source: imaps-frontend/node_modules/terser/lib/mozilla-ast.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

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