source: trip-planner-front/node_modules/terser/lib/mozilla-ast.js@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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