terser 5.41.0 → 5.43.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.
- package/CHANGELOG.md +10 -0
- package/README.md +1 -1
- package/dist/bundle.min.js +437 -239
- package/lib/ast.js +27 -8
- package/lib/compress/index.js +4 -6
- package/lib/compress/inline.js +15 -0
- package/lib/equivalent-to.js +2 -2
- package/lib/mozilla-ast.js +351 -200
- package/lib/output.js +7 -7
- package/lib/parse.js +30 -15
- package/lib/size.js +1 -1
- package/package.json +1 -1
package/lib/mozilla-ast.js
CHANGED
@@ -171,6 +171,7 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
171
171
|
body[i] = new AST_Directive({
|
172
172
|
start: body[i].start,
|
173
173
|
end: body[i].end,
|
174
|
+
quote: '"',
|
174
175
|
value: body[i].body.value
|
175
176
|
});
|
176
177
|
} else {
|
@@ -294,8 +295,8 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
294
295
|
return new AST_Defun({
|
295
296
|
start: my_start_token(M),
|
296
297
|
end: my_end_token(M),
|
297
|
-
name:
|
298
|
-
argnames: M.params.map(
|
298
|
+
name: M.id && from_moz_symbol(AST_SymbolDefun, M.id),
|
299
|
+
argnames: M.params.map(M => from_moz_pattern(M, AST_SymbolFunarg)),
|
299
300
|
is_generator: M.generator,
|
300
301
|
async: M.async,
|
301
302
|
body: normalize_directives(from_moz(M.body).body)
|
@@ -303,15 +304,7 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
303
304
|
},
|
304
305
|
|
305
306
|
FunctionExpression: function(M) {
|
306
|
-
return
|
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
|
-
});
|
307
|
+
return from_moz_lambda(M, /*is_method=*/false);
|
315
308
|
},
|
316
309
|
|
317
310
|
ArrowFunctionExpression: function(M) {
|
@@ -321,7 +314,7 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
321
314
|
return new AST_Arrow({
|
322
315
|
start: my_start_token(M),
|
323
316
|
end: my_end_token(M),
|
324
|
-
argnames: M.params.map(
|
317
|
+
argnames: M.params.map(p => from_moz_pattern(p, AST_SymbolFunarg)),
|
325
318
|
body,
|
326
319
|
async: M.async,
|
327
320
|
});
|
@@ -350,59 +343,48 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
350
343
|
},
|
351
344
|
|
352
345
|
Property: function(M) {
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
}
|
366
|
-
if (M.method) {
|
367
|
-
args.is_generator = M.value.generator;
|
368
|
-
args.async = M.value.async;
|
369
|
-
if (!M.computed) {
|
370
|
-
args.key = new AST_SymbolMethod({ name: args.key });
|
371
|
-
} else {
|
372
|
-
args.key = from_moz(M.key);
|
373
|
-
}
|
374
|
-
return new AST_ConciseMethod(args);
|
375
|
-
}
|
376
|
-
if (M.kind == "init") {
|
377
|
-
if (key.type != "Identifier" && key.type != "Literal") {
|
378
|
-
args.key = from_moz(key);
|
379
|
-
}
|
346
|
+
if (M.kind == "init" && !M.method) {
|
347
|
+
var args = {
|
348
|
+
start : my_start_token(M.key || M.value),
|
349
|
+
end : my_end_token(M.value),
|
350
|
+
key : M.computed
|
351
|
+
? from_moz(M.key)
|
352
|
+
: M.key.name || String(M.key.value),
|
353
|
+
quote : from_moz_quote(M.key, M.computed),
|
354
|
+
static : false, // always an object
|
355
|
+
value : from_moz(M.value)
|
356
|
+
};
|
357
|
+
|
380
358
|
return new AST_ObjectKeyVal(args);
|
381
|
-
}
|
382
|
-
|
383
|
-
args
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
359
|
+
} else {
|
360
|
+
var value = from_moz_lambda(M.value, /*is_method=*/true);
|
361
|
+
var args = {
|
362
|
+
start : my_start_token(M.key || M.value),
|
363
|
+
end : my_end_token(M.value),
|
364
|
+
key : M.computed
|
365
|
+
? from_moz(M.key)
|
366
|
+
: from_moz_symbol(AST_SymbolMethod, M.key),
|
367
|
+
quote : from_moz_quote(M.key, M.computed),
|
368
|
+
static : false, // always an object
|
369
|
+
value,
|
370
|
+
};
|
371
|
+
|
372
|
+
if (M.kind == "get") return new AST_ObjectGetter(args);
|
373
|
+
if (M.kind == "set") return new AST_ObjectSetter(args);
|
374
|
+
if (M.method) return new AST_ConciseMethod(args);
|
394
375
|
}
|
395
376
|
},
|
396
377
|
|
397
378
|
MethodDefinition: function(M) {
|
398
379
|
const is_private = M.key.type === "PrivateIdentifier";
|
399
|
-
const key = M.computed ? from_moz(M.key) : new AST_SymbolMethod({ name: M.key.name || M.key.value });
|
380
|
+
const key = M.computed ? from_moz(M.key) : new AST_SymbolMethod({ name: M.key.name || String(M.key.value) });
|
400
381
|
|
401
382
|
var args = {
|
402
383
|
start : my_start_token(M),
|
403
384
|
end : my_end_token(M),
|
404
385
|
key,
|
405
|
-
|
386
|
+
quote : from_moz_quote(M.key, M.computed),
|
387
|
+
value : from_moz_lambda(M.value, /*is_method=*/true),
|
406
388
|
static : M.static,
|
407
389
|
};
|
408
390
|
if (M.kind == "get") {
|
@@ -411,8 +393,6 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
411
393
|
if (M.kind == "set") {
|
412
394
|
return new (is_private ? AST_PrivateSetter : AST_ObjectSetter)(args);
|
413
395
|
}
|
414
|
-
args.is_generator = M.value.generator;
|
415
|
-
args.async = M.value.async;
|
416
396
|
return new (is_private ? AST_PrivateMethod : AST_ConciseMethod)(args);
|
417
397
|
},
|
418
398
|
|
@@ -427,6 +407,7 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
427
407
|
return new AST_ClassProperty({
|
428
408
|
start : my_start_token(M),
|
429
409
|
end : my_end_token(M),
|
410
|
+
quote : from_moz_quote(M.key, M.computed),
|
430
411
|
key,
|
431
412
|
value : from_moz(M.value),
|
432
413
|
static : M.static,
|
@@ -446,15 +427,13 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
446
427
|
static : M.static,
|
447
428
|
});
|
448
429
|
} else {
|
449
|
-
|
450
|
-
throw new Error("Non-Identifier key in PropertyDefinition");
|
451
|
-
}
|
452
|
-
key = from_moz(M.key);
|
430
|
+
key = from_moz_symbol(AST_SymbolClassProperty, M.key);
|
453
431
|
}
|
454
432
|
|
455
433
|
return new AST_ClassProperty({
|
456
434
|
start : my_start_token(M),
|
457
435
|
end : my_end_token(M),
|
436
|
+
quote : from_moz_quote(M.key, M.computed),
|
458
437
|
key,
|
459
438
|
value : from_moz(M.value),
|
460
439
|
static : M.static,
|
@@ -546,11 +525,30 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
546
525
|
},
|
547
526
|
|
548
527
|
VariableDeclaration: function(M) {
|
549
|
-
|
550
|
-
|
528
|
+
let decl_type;
|
529
|
+
let sym_type;
|
530
|
+
if (M.kind === "const") {
|
531
|
+
decl_type = AST_Const;
|
532
|
+
sym_type = AST_SymbolConst;
|
533
|
+
} else if (M.kind === "let") {
|
534
|
+
decl_type = AST_Let;
|
535
|
+
sym_type = AST_SymbolLet;
|
536
|
+
} else {
|
537
|
+
decl_type = AST_Var;
|
538
|
+
sym_type = AST_SymbolVar;
|
539
|
+
}
|
540
|
+
const definitions = M.declarations.map(M => {
|
541
|
+
return new AST_VarDef({
|
542
|
+
start: my_start_token(M),
|
543
|
+
end: my_end_token(M),
|
544
|
+
name: from_moz_pattern(M.id, sym_type),
|
545
|
+
value: from_moz(M.init),
|
546
|
+
});
|
547
|
+
});
|
548
|
+
return new decl_type({
|
551
549
|
start : my_start_token(M),
|
552
550
|
end : my_end_token(M),
|
553
|
-
definitions :
|
551
|
+
definitions : definitions,
|
554
552
|
});
|
555
553
|
},
|
556
554
|
|
@@ -579,13 +577,13 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
579
577
|
return new AST_NameMapping({
|
580
578
|
start: my_start_token(M),
|
581
579
|
end: my_end_token(M),
|
582
|
-
foreign_name:
|
583
|
-
name:
|
580
|
+
foreign_name: from_moz_symbol(AST_SymbolImportForeign, M.imported, M.imported.type === "Literal"),
|
581
|
+
name: from_moz_symbol(AST_SymbolImport, M.local)
|
584
582
|
});
|
585
583
|
},
|
586
584
|
|
587
585
|
ImportDefaultSpecifier: function(M) {
|
588
|
-
return
|
586
|
+
return from_moz_symbol(AST_SymbolImport, M.local);
|
589
587
|
},
|
590
588
|
|
591
589
|
ImportNamespaceSpecifier: function(M) {
|
@@ -593,7 +591,7 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
593
591
|
start: my_start_token(M),
|
594
592
|
end: my_end_token(M),
|
595
593
|
foreign_name: new AST_SymbolImportForeign({ name: "*" }),
|
596
|
-
name:
|
594
|
+
name: from_moz_symbol(AST_SymbolImport, M.local)
|
597
595
|
});
|
598
596
|
},
|
599
597
|
|
@@ -615,15 +613,17 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
615
613
|
},
|
616
614
|
|
617
615
|
ExportAllDeclaration: function(M) {
|
618
|
-
var foreign_name = M.exported == null ?
|
616
|
+
var foreign_name = M.exported == null ?
|
619
617
|
new AST_SymbolExportForeign({ name: "*" }) :
|
620
|
-
|
618
|
+
from_moz_symbol(AST_SymbolExportForeign, M.exported, M.exported.type === "Literal");
|
621
619
|
return new AST_Export({
|
622
620
|
start: my_start_token(M),
|
623
621
|
end: my_end_token(M),
|
624
622
|
exported_names: [
|
625
623
|
new AST_NameMapping({
|
626
|
-
|
624
|
+
start: my_start_token(M),
|
625
|
+
end: my_end_token(M),
|
626
|
+
name: new AST_SymbolExport({ name: "*" }),
|
627
627
|
foreign_name: foreign_name
|
628
628
|
})
|
629
629
|
],
|
@@ -666,8 +666,10 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
666
666
|
|
667
667
|
ExportSpecifier: function(M) {
|
668
668
|
return new AST_NameMapping({
|
669
|
-
|
670
|
-
|
669
|
+
start: my_start_token(M),
|
670
|
+
end: my_end_token(M),
|
671
|
+
foreign_name: from_moz_symbol(AST_SymbolExportForeign, M.exported, M.exported.type === "Literal"),
|
672
|
+
name: from_moz_symbol(AST_SymbolExport, M.local, M.local.type === "Literal"),
|
671
673
|
});
|
672
674
|
},
|
673
675
|
|
@@ -696,27 +698,13 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
696
698
|
const bi = typeof M.value === "bigint" ? M.value.toString() : M.bigint;
|
697
699
|
if (typeof bi === "string") {
|
698
700
|
args.value = bi;
|
701
|
+
args.raw = M.raw;
|
699
702
|
return new AST_BigInt(args);
|
700
703
|
}
|
701
704
|
if (val === null) return new AST_Null(args);
|
702
705
|
switch (typeof val) {
|
703
706
|
case "string":
|
704
707
|
args.quote = "\"";
|
705
|
-
var p = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 2];
|
706
|
-
if (p.type == "ImportSpecifier") {
|
707
|
-
args.name = val;
|
708
|
-
return new AST_SymbolImportForeign(args);
|
709
|
-
} else if (p.type == "ExportSpecifier") {
|
710
|
-
args.name = val;
|
711
|
-
if (M == p.exported) {
|
712
|
-
return new AST_SymbolExportForeign(args);
|
713
|
-
} else {
|
714
|
-
return new AST_SymbolExport(args);
|
715
|
-
}
|
716
|
-
} else if (p.type == "ExportAllDeclaration" && M == p.exported) {
|
717
|
-
args.name = val;
|
718
|
-
return new AST_SymbolExportForeign(args);
|
719
|
-
}
|
720
708
|
args.value = val;
|
721
709
|
return new AST_String(args);
|
722
710
|
case "number":
|
@@ -743,27 +731,11 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
743
731
|
},
|
744
732
|
|
745
733
|
Identifier: function(M) {
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
: p.type == "ExportSpecifier" ? (p.local === M ? AST_SymbolExport : AST_SymbolExportForeign)
|
752
|
-
: p.type == "FunctionExpression" ? (p.id === M ? AST_SymbolLambda : AST_SymbolFunarg)
|
753
|
-
: p.type == "FunctionDeclaration" ? (p.id === M ? AST_SymbolDefun : AST_SymbolFunarg)
|
754
|
-
: p.type == "ArrowFunctionExpression" ? (p.params.includes(M)) ? AST_SymbolFunarg : AST_SymbolRef
|
755
|
-
: p.type == "ClassExpression" ? (p.id === M ? AST_SymbolClass : AST_SymbolRef)
|
756
|
-
: p.type == "Property" ? (p.key === M && p.computed || p.value === M ? AST_SymbolRef : AST_SymbolMethod)
|
757
|
-
: p.type == "PropertyDefinition" || p.type === "FieldDefinition" ? (p.key === M && p.computed || p.value === M ? AST_SymbolRef : AST_SymbolClassProperty)
|
758
|
-
: p.type == "ClassDeclaration" ? (p.id === M ? AST_SymbolDefClass : AST_SymbolRef)
|
759
|
-
: p.type == "MethodDefinition" ? (p.computed ? AST_SymbolRef : AST_SymbolMethod)
|
760
|
-
: p.type == "CatchClause" ? AST_SymbolCatch
|
761
|
-
: p.type == "BreakStatement" || p.type == "ContinueStatement" ? AST_LabelRef
|
762
|
-
: AST_SymbolRef)({
|
763
|
-
start : my_start_token(M),
|
764
|
-
end : my_end_token(M),
|
765
|
-
name : M.name
|
766
|
-
});
|
734
|
+
return new AST_SymbolRef({
|
735
|
+
start : my_start_token(M),
|
736
|
+
end : my_end_token(M),
|
737
|
+
name : M.name
|
738
|
+
});
|
767
739
|
},
|
768
740
|
|
769
741
|
EmptyStatement: function(M) {
|
@@ -792,19 +764,28 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
792
764
|
},
|
793
765
|
|
794
766
|
LabeledStatement: function(M) {
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
|
767
|
+
try {
|
768
|
+
const label = from_moz_symbol(AST_Label, M.label);
|
769
|
+
FROM_MOZ_LABELS.push(label);
|
770
|
+
|
771
|
+
const stat = new AST_LabeledStatement({
|
772
|
+
start: my_start_token(M),
|
773
|
+
end: my_end_token(M),
|
774
|
+
label,
|
775
|
+
body: from_moz(M.body)
|
776
|
+
});
|
777
|
+
|
778
|
+
return stat;
|
779
|
+
} finally {
|
780
|
+
FROM_MOZ_LABELS.pop();
|
781
|
+
}
|
801
782
|
},
|
802
783
|
|
803
784
|
BreakStatement: function(M) {
|
804
785
|
return new AST_Break({
|
805
786
|
start: my_start_token(M),
|
806
787
|
end: my_end_token(M),
|
807
|
-
label:
|
788
|
+
label: from_moz_label_ref(M.label),
|
808
789
|
});
|
809
790
|
},
|
810
791
|
|
@@ -812,7 +793,7 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
812
793
|
return new AST_Continue({
|
813
794
|
start: my_start_token(M),
|
814
795
|
end: my_end_token(M),
|
815
|
-
label:
|
796
|
+
label: from_moz_label_ref(M.label),
|
816
797
|
});
|
817
798
|
},
|
818
799
|
|
@@ -924,20 +905,11 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
924
905
|
});
|
925
906
|
},
|
926
907
|
|
927
|
-
VariableDeclarator: function(M) {
|
928
|
-
return new AST_VarDef({
|
929
|
-
start: my_start_token(M),
|
930
|
-
end: my_end_token(M),
|
931
|
-
name: from_moz(M.id),
|
932
|
-
value: from_moz(M.init)
|
933
|
-
});
|
934
|
-
},
|
935
|
-
|
936
908
|
CatchClause: function(M) {
|
937
909
|
return new AST_Catch({
|
938
910
|
start: my_start_token(M),
|
939
911
|
end: my_end_token(M),
|
940
|
-
argname:
|
912
|
+
argname: M.param ? from_moz_pattern(M.param, AST_SymbolCatch) : null,
|
941
913
|
body: from_moz(M.body).body
|
942
914
|
});
|
943
915
|
},
|
@@ -945,6 +917,7 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
945
917
|
ThisExpression: function(M) {
|
946
918
|
return new AST_This({
|
947
919
|
start: my_start_token(M),
|
920
|
+
name: "this",
|
948
921
|
end: my_end_token(M)
|
949
922
|
});
|
950
923
|
},
|
@@ -952,7 +925,8 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
952
925
|
Super: function(M) {
|
953
926
|
return new AST_Super({
|
954
927
|
start: my_start_token(M),
|
955
|
-
end: my_end_token(M)
|
928
|
+
end: my_end_token(M),
|
929
|
+
name: "super",
|
956
930
|
});
|
957
931
|
},
|
958
932
|
|
@@ -993,6 +967,7 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
993
967
|
start: my_start_token(M),
|
994
968
|
end: my_end_token(M),
|
995
969
|
operator: M.operator,
|
970
|
+
logical: M.operator === "??=" || M.operator === "&&=" || M.operator === "||=",
|
996
971
|
left: from_moz(M.left),
|
997
972
|
right: from_moz(M.right)
|
998
973
|
});
|
@@ -1045,7 +1020,7 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
1045
1020
|
return new (M.type === "ClassDeclaration" ? AST_DefClass : AST_ClassExpression)({
|
1046
1021
|
start : my_start_token(M),
|
1047
1022
|
end : my_end_token(M),
|
1048
|
-
name :
|
1023
|
+
name : M.id && from_moz_symbol(M.type === "ClassDeclaration" ? AST_SymbolDefClass : AST_SymbolClass, M.id),
|
1049
1024
|
extends : from_moz(M.superClass),
|
1050
1025
|
properties: M.body.body.map(from_moz)
|
1051
1026
|
});
|
@@ -1180,13 +1155,6 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
1180
1155
|
init: to_moz(M.value)
|
1181
1156
|
};
|
1182
1157
|
});
|
1183
|
-
def_to_moz(AST_Catch, function To_Moz_CatchClause(M) {
|
1184
|
-
return {
|
1185
|
-
type: "CatchClause",
|
1186
|
-
param: to_moz(M.argname),
|
1187
|
-
body: to_moz_block(M)
|
1188
|
-
};
|
1189
|
-
});
|
1190
1158
|
|
1191
1159
|
def_to_moz(AST_This, function To_Moz_ThisExpression() {
|
1192
1160
|
return {
|
@@ -1219,7 +1187,7 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
1219
1187
|
return {
|
1220
1188
|
type: "ImportExpression",
|
1221
1189
|
source,
|
1222
|
-
options
|
1190
|
+
options: options || null
|
1223
1191
|
};
|
1224
1192
|
}
|
1225
1193
|
|
@@ -1278,36 +1246,36 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
1278
1246
|
return {
|
1279
1247
|
type: "FunctionDeclaration",
|
1280
1248
|
id: to_moz(M.name),
|
1281
|
-
params: M.argnames.map(
|
1249
|
+
params: M.argnames.map(to_moz_pattern),
|
1282
1250
|
generator: M.is_generator,
|
1283
1251
|
async: M.async,
|
1284
1252
|
body: to_moz_scope("BlockStatement", M)
|
1285
1253
|
};
|
1286
1254
|
});
|
1287
1255
|
|
1288
|
-
def_to_moz(AST_Function, function To_Moz_FunctionExpression(M
|
1289
|
-
var is_generator = parent.is_generator !== undefined ?
|
1290
|
-
parent.is_generator : M.is_generator;
|
1256
|
+
def_to_moz(AST_Function, function To_Moz_FunctionExpression(M) {
|
1291
1257
|
return {
|
1292
1258
|
type: "FunctionExpression",
|
1293
1259
|
id: to_moz(M.name),
|
1294
|
-
params: M.argnames.map(
|
1295
|
-
generator: is_generator,
|
1296
|
-
async: M.async,
|
1260
|
+
params: M.argnames.map(to_moz_pattern),
|
1261
|
+
generator: M.is_generator || false,
|
1262
|
+
async: M.async || false,
|
1297
1263
|
body: to_moz_scope("BlockStatement", M)
|
1298
1264
|
};
|
1299
1265
|
});
|
1300
1266
|
|
1301
1267
|
def_to_moz(AST_Arrow, function To_Moz_ArrowFunctionExpression(M) {
|
1302
|
-
var body =
|
1303
|
-
|
1304
|
-
|
1305
|
-
|
1268
|
+
var body = M.body.length === 1 && M.body[0] instanceof AST_Return && M.body[0].value
|
1269
|
+
? to_moz(M.body[0].value)
|
1270
|
+
: {
|
1271
|
+
type: "BlockStatement",
|
1272
|
+
body: M.body.map(to_moz)
|
1273
|
+
};
|
1306
1274
|
return {
|
1307
1275
|
type: "ArrowFunctionExpression",
|
1308
|
-
params: M.argnames.map(
|
1276
|
+
params: M.argnames.map(to_moz_pattern),
|
1309
1277
|
async: M.async,
|
1310
|
-
body: body
|
1278
|
+
body: body,
|
1311
1279
|
};
|
1312
1280
|
});
|
1313
1281
|
|
@@ -1315,19 +1283,38 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
1315
1283
|
if (M.is_array) {
|
1316
1284
|
return {
|
1317
1285
|
type: "ArrayPattern",
|
1318
|
-
elements: M.names.map(
|
1286
|
+
elements: M.names.map(
|
1287
|
+
M => M instanceof AST_Hole ? null : to_moz_pattern(M)
|
1288
|
+
),
|
1319
1289
|
};
|
1320
1290
|
}
|
1321
1291
|
return {
|
1322
1292
|
type: "ObjectPattern",
|
1323
|
-
properties: M.names.map(
|
1293
|
+
properties: M.names.map(M => {
|
1294
|
+
if (M instanceof AST_ObjectKeyVal) {
|
1295
|
+
var computed = M.computed_key();
|
1296
|
+
const [shorthand, key] = to_moz_property_key(M.key, computed, M.quote, M.value);
|
1297
|
+
|
1298
|
+
return {
|
1299
|
+
type: "Property",
|
1300
|
+
computed,
|
1301
|
+
kind: "init",
|
1302
|
+
key: key,
|
1303
|
+
method: false,
|
1304
|
+
shorthand,
|
1305
|
+
value: to_moz_pattern(M.value)
|
1306
|
+
};
|
1307
|
+
} else {
|
1308
|
+
return to_moz_pattern(M);
|
1309
|
+
}
|
1310
|
+
}),
|
1324
1311
|
};
|
1325
1312
|
});
|
1326
1313
|
|
1327
1314
|
def_to_moz(AST_DefaultAssign, function To_Moz_AssignmentExpression(M) {
|
1328
1315
|
return {
|
1329
1316
|
type: "AssignmentPattern",
|
1330
|
-
left:
|
1317
|
+
left: to_moz_pattern(M.left),
|
1331
1318
|
right: to_moz(M.right),
|
1332
1319
|
};
|
1333
1320
|
});
|
@@ -1372,8 +1359,7 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
1372
1359
|
def_to_moz(AST_Catch, function To_Moz_CatchClause(M) {
|
1373
1360
|
return {
|
1374
1361
|
type: "CatchClause",
|
1375
|
-
param:
|
1376
|
-
guard: null,
|
1362
|
+
param: M.argname != null ? to_moz_pattern(M.argname) : null,
|
1377
1363
|
body: to_moz_block(M)
|
1378
1364
|
};
|
1379
1365
|
});
|
@@ -1434,10 +1420,20 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
1434
1420
|
attributes: import_attributes_to_moz(M.attributes)
|
1435
1421
|
};
|
1436
1422
|
}
|
1437
|
-
|
1438
|
-
|
1439
|
-
|
1440
|
-
|
1423
|
+
|
1424
|
+
if (M.is_default) {
|
1425
|
+
return {
|
1426
|
+
type: "ExportDefaultDeclaration",
|
1427
|
+
declaration: to_moz(M.exported_value || M.exported_definition),
|
1428
|
+
};
|
1429
|
+
} else {
|
1430
|
+
return {
|
1431
|
+
type: "ExportNamedDeclaration",
|
1432
|
+
declaration: to_moz(M.exported_value || M.exported_definition),
|
1433
|
+
specifiers: [],
|
1434
|
+
source: null,
|
1435
|
+
};
|
1436
|
+
}
|
1441
1437
|
});
|
1442
1438
|
|
1443
1439
|
def_to_moz(AST_Import, function To_Moz_ImportDeclaration(M) {
|
@@ -1588,35 +1584,10 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
1588
1584
|
});
|
1589
1585
|
|
1590
1586
|
def_to_moz(AST_ObjectProperty, function To_Moz_Property(M, parent) {
|
1591
|
-
var
|
1592
|
-
|
1593
|
-
|
1594
|
-
};
|
1595
|
-
if (typeof M.key === "number") {
|
1596
|
-
key = {
|
1597
|
-
type: "Literal",
|
1598
|
-
value: Number(M.key)
|
1599
|
-
};
|
1600
|
-
}
|
1601
|
-
if (typeof M.key === "string") {
|
1602
|
-
key = M.quote
|
1603
|
-
? {
|
1604
|
-
type: "Literal",
|
1605
|
-
value: M.key,
|
1606
|
-
raw: JSON.stringify(M.key),
|
1607
|
-
}
|
1608
|
-
: {
|
1609
|
-
type: "Identifier",
|
1610
|
-
name: M.key,
|
1611
|
-
};
|
1612
|
-
}
|
1587
|
+
var computed = M.computed_key();
|
1588
|
+
const [shorthand, key] = to_moz_property_key(M.key, computed, M.quote, M.value);
|
1589
|
+
|
1613
1590
|
var kind;
|
1614
|
-
var string_or_num = typeof M.key === "string" || typeof M.key === "number";
|
1615
|
-
var computed = string_or_num ? false : !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef;
|
1616
|
-
if (M instanceof AST_ObjectKeyVal) {
|
1617
|
-
kind = "init";
|
1618
|
-
computed = !string_or_num;
|
1619
|
-
} else
|
1620
1591
|
if (M instanceof AST_ObjectGetter) {
|
1621
1592
|
kind = "get";
|
1622
1593
|
} else
|
@@ -1671,31 +1642,51 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
1671
1642
|
return {
|
1672
1643
|
type: "Property",
|
1673
1644
|
computed: computed,
|
1645
|
+
method: false,
|
1646
|
+
shorthand,
|
1674
1647
|
kind: kind,
|
1675
1648
|
key: key,
|
1676
1649
|
value: to_moz(M.value)
|
1677
1650
|
};
|
1678
1651
|
});
|
1679
1652
|
|
1653
|
+
def_to_moz(AST_ObjectKeyVal, function To_Moz_Property(M) {
|
1654
|
+
var computed = M.computed_key();
|
1655
|
+
const [shorthand, key] = to_moz_property_key(M.key, computed, M.quote, M.value);
|
1656
|
+
|
1657
|
+
return {
|
1658
|
+
type: "Property",
|
1659
|
+
computed: computed,
|
1660
|
+
shorthand: shorthand,
|
1661
|
+
method: false,
|
1662
|
+
kind: "init",
|
1663
|
+
key: key,
|
1664
|
+
value: to_moz(M.value)
|
1665
|
+
};
|
1666
|
+
});
|
1667
|
+
|
1680
1668
|
def_to_moz(AST_ConciseMethod, function To_Moz_MethodDefinition(M, parent) {
|
1669
|
+
const computed = M.computed_key();
|
1670
|
+
const [_always_false, key] = to_moz_property_key(M.key, computed, M.quote, M.value);
|
1671
|
+
|
1681
1672
|
if (parent instanceof AST_Object) {
|
1682
1673
|
return {
|
1683
1674
|
type: "Property",
|
1684
|
-
computed: !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef,
|
1685
1675
|
kind: "init",
|
1676
|
+
computed,
|
1686
1677
|
method: true,
|
1687
1678
|
shorthand: false,
|
1688
|
-
key
|
1689
|
-
value: to_moz(M.value)
|
1679
|
+
key,
|
1680
|
+
value: to_moz(M.value),
|
1690
1681
|
};
|
1691
1682
|
}
|
1692
1683
|
|
1693
1684
|
return {
|
1694
1685
|
type: "MethodDefinition",
|
1695
|
-
kind: M.key === "constructor" ? "constructor" : "method",
|
1696
|
-
|
1686
|
+
kind: !computed && M.key.name === "constructor" ? "constructor" : "method",
|
1687
|
+
computed,
|
1688
|
+
key,
|
1697
1689
|
value: to_moz(M.value),
|
1698
|
-
computed: !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef,
|
1699
1690
|
static: M.static,
|
1700
1691
|
};
|
1701
1692
|
});
|
@@ -1801,6 +1792,7 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
1801
1792
|
// `M.value` is a string that may be a hex number representation.
|
1802
1793
|
// but "bigint" property should have only decimal digits
|
1803
1794
|
bigint: typeof BigInt === "function" ? BigInt(M.value).toString() : M.value,
|
1795
|
+
raw: M.raw,
|
1804
1796
|
}));
|
1805
1797
|
|
1806
1798
|
AST_Boolean.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
|
@@ -1844,20 +1836,133 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
1844
1836
|
);
|
1845
1837
|
}
|
1846
1838
|
|
1847
|
-
var
|
1839
|
+
var FROM_MOZ_LABELS = null;
|
1848
1840
|
|
1849
1841
|
function from_moz(node) {
|
1850
|
-
|
1851
|
-
|
1852
|
-
|
1853
|
-
|
1842
|
+
if (node == null) return null;
|
1843
|
+
return MOZ_TO_ME[node.type](node);
|
1844
|
+
}
|
1845
|
+
|
1846
|
+
function from_moz_quote(moz_key, computed) {
|
1847
|
+
if (!computed && moz_key.type === "Literal" && typeof moz_key.value === "string") {
|
1848
|
+
return '"';
|
1849
|
+
} else {
|
1850
|
+
return "";
|
1851
|
+
}
|
1852
|
+
}
|
1853
|
+
|
1854
|
+
function from_moz_symbol(symbol_type, M, has_quote) {
|
1855
|
+
return new symbol_type({
|
1856
|
+
start: my_start_token(M),
|
1857
|
+
quote: has_quote ? '"' : undefined,
|
1858
|
+
name: M.type === "Identifier" ? M.name : String(M.value),
|
1859
|
+
end: my_end_token(M),
|
1860
|
+
});
|
1861
|
+
}
|
1862
|
+
|
1863
|
+
function from_moz_lambda(M, is_method) {
|
1864
|
+
return new (is_method ? AST_Accessor : AST_Function)({
|
1865
|
+
start: my_start_token(M),
|
1866
|
+
end: my_end_token(M),
|
1867
|
+
name: M.id && from_moz_symbol(is_method ? AST_SymbolMethod : AST_SymbolLambda, M.id),
|
1868
|
+
argnames: M.params.map(M => from_moz_pattern(M, AST_SymbolFunarg)),
|
1869
|
+
is_generator: M.generator,
|
1870
|
+
async: M.async,
|
1871
|
+
body: normalize_directives(from_moz(M.body).body)
|
1872
|
+
});
|
1873
|
+
}
|
1874
|
+
|
1875
|
+
function from_moz_pattern(M, sym_type) {
|
1876
|
+
switch (M.type) {
|
1877
|
+
case "ObjectPattern":
|
1878
|
+
return new AST_Destructuring({
|
1879
|
+
start: my_start_token(M),
|
1880
|
+
end: my_end_token(M),
|
1881
|
+
names: M.properties.map(p => from_moz_pattern(p, sym_type)),
|
1882
|
+
is_array: false
|
1883
|
+
});
|
1884
|
+
|
1885
|
+
case "Property":
|
1886
|
+
var key = M.key;
|
1887
|
+
var args = {
|
1888
|
+
start : my_start_token(key || M.value),
|
1889
|
+
end : my_end_token(M.value),
|
1890
|
+
key : key.type == "Identifier" ? key.name : String(key.value),
|
1891
|
+
quote : !M.computed && key.type === "Literal" && typeof key.value === "string"
|
1892
|
+
? '"'
|
1893
|
+
: "",
|
1894
|
+
value : from_moz_pattern(M.value, sym_type)
|
1895
|
+
};
|
1896
|
+
if (M.computed) {
|
1897
|
+
args.key = from_moz(M.key);
|
1898
|
+
}
|
1899
|
+
return new AST_ObjectKeyVal(args);
|
1900
|
+
|
1901
|
+
case "ArrayPattern":
|
1902
|
+
return new AST_Destructuring({
|
1903
|
+
start: my_start_token(M),
|
1904
|
+
end: my_end_token(M),
|
1905
|
+
names: M.elements.map(function(elm) {
|
1906
|
+
if (elm === null) {
|
1907
|
+
return new AST_Hole();
|
1908
|
+
}
|
1909
|
+
return from_moz_pattern(elm, sym_type);
|
1910
|
+
}),
|
1911
|
+
is_array: true
|
1912
|
+
});
|
1913
|
+
|
1914
|
+
case "SpreadElement":
|
1915
|
+
case "RestElement":
|
1916
|
+
return new AST_Expansion({
|
1917
|
+
start: my_start_token(M),
|
1918
|
+
end: my_end_token(M),
|
1919
|
+
expression: from_moz_pattern(M.argument, sym_type),
|
1920
|
+
});
|
1921
|
+
|
1922
|
+
case "AssignmentPattern":
|
1923
|
+
return new AST_DefaultAssign({
|
1924
|
+
start : my_start_token(M),
|
1925
|
+
end : my_end_token(M),
|
1926
|
+
left : from_moz_pattern(M.left, sym_type),
|
1927
|
+
operator: "=",
|
1928
|
+
right : from_moz(M.right),
|
1929
|
+
});
|
1930
|
+
|
1931
|
+
case "Identifier":
|
1932
|
+
return new sym_type({
|
1933
|
+
start : my_start_token(M),
|
1934
|
+
end : my_end_token(M),
|
1935
|
+
name : M.name,
|
1936
|
+
});
|
1937
|
+
|
1938
|
+
default:
|
1939
|
+
throw new Error("Invalid node type for destructuring: " + M.type);
|
1940
|
+
}
|
1941
|
+
}
|
1942
|
+
|
1943
|
+
function from_moz_label_ref(m_label) {
|
1944
|
+
if (!m_label) return null;
|
1945
|
+
|
1946
|
+
const label = from_moz_symbol(AST_LabelRef, m_label);
|
1947
|
+
|
1948
|
+
let i = FROM_MOZ_LABELS.length;
|
1949
|
+
while (i--) {
|
1950
|
+
const label_origin = FROM_MOZ_LABELS[i];
|
1951
|
+
|
1952
|
+
if (label.name === label_origin.name) {
|
1953
|
+
label.thedef = label_origin;
|
1954
|
+
break;
|
1955
|
+
}
|
1956
|
+
}
|
1957
|
+
|
1958
|
+
return label;
|
1854
1959
|
}
|
1855
1960
|
|
1856
1961
|
AST_Node.from_mozilla_ast = function(node) {
|
1857
|
-
var
|
1858
|
-
|
1962
|
+
var save_labels = FROM_MOZ_LABELS;
|
1963
|
+
FROM_MOZ_LABELS = [];
|
1859
1964
|
var ast = from_moz(node);
|
1860
|
-
|
1965
|
+
FROM_MOZ_LABELS = save_labels;
|
1861
1966
|
return ast;
|
1862
1967
|
};
|
1863
1968
|
|
@@ -1899,6 +2004,52 @@ import { is_basic_identifier_string } from "./parse.js";
|
|
1899
2004
|
return ast;
|
1900
2005
|
}
|
1901
2006
|
|
2007
|
+
/** Object property keys can be number literals, string literals, or raw names. Additionally they can be shorthand. We decide that here. */
|
2008
|
+
function to_moz_property_key(key, computed = false, quote = false, value = null) {
|
2009
|
+
if (computed) {
|
2010
|
+
return [false, to_moz(key)];
|
2011
|
+
}
|
2012
|
+
|
2013
|
+
const key_name = typeof key === "string" ? key : key.name;
|
2014
|
+
let moz_key;
|
2015
|
+
if (quote) {
|
2016
|
+
moz_key = { type: "Literal", value: key_name, raw: JSON.stringify(key_name) };
|
2017
|
+
} else if ("" + +key_name === key_name && +key_name >= 0) {
|
2018
|
+
// representable as a number
|
2019
|
+
moz_key = { type: "Literal", value: +key_name, raw: JSON.stringify(+key_name) };
|
2020
|
+
} else {
|
2021
|
+
moz_key = { type: "Identifier", name: key_name };
|
2022
|
+
}
|
2023
|
+
|
2024
|
+
const shorthand =
|
2025
|
+
moz_key.type === "Identifier"
|
2026
|
+
&& moz_key.name === key_name
|
2027
|
+
&& (value instanceof AST_Symbol && value.name === key_name
|
2028
|
+
|| value instanceof AST_DefaultAssign && value.left.name === key_name);
|
2029
|
+
return [shorthand, moz_key];
|
2030
|
+
}
|
2031
|
+
|
2032
|
+
function to_moz_pattern(node) {
|
2033
|
+
if (node instanceof AST_Expansion) {
|
2034
|
+
return {
|
2035
|
+
type: "RestElement",
|
2036
|
+
argument: to_moz_pattern(node.expression),
|
2037
|
+
};
|
2038
|
+
}
|
2039
|
+
|
2040
|
+
if ((
|
2041
|
+
node instanceof AST_Symbol
|
2042
|
+
|| node instanceof AST_Destructuring
|
2043
|
+
|| node instanceof AST_DefaultAssign
|
2044
|
+
|| node instanceof AST_PropAccess
|
2045
|
+
)) {
|
2046
|
+
// Plain translation
|
2047
|
+
return to_moz(node);
|
2048
|
+
}
|
2049
|
+
|
2050
|
+
throw new Error(node.TYPE);
|
2051
|
+
}
|
2052
|
+
|
1902
2053
|
function to_moz_in_destructuring() {
|
1903
2054
|
var i = TO_MOZ_STACK.length;
|
1904
2055
|
while (i--) {
|