terser 3.14.0 → 3.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of terser might be problematic. Click here for more details.

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