terser 5.40.0 → 5.42.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.
@@ -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: from_moz(M.id),
298
- argnames: M.params.map(from_moz),
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 new AST_Function({
307
- start: my_start_token(M),
308
- end: my_end_token(M),
309
- name: from_moz(M.id),
310
- argnames: M.params.map(from_moz),
311
- is_generator: M.generator,
312
- async: M.async,
313
- body: normalize_directives(from_moz(M.body).body)
314
- });
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(from_moz),
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,50 @@ import { is_basic_identifier_string } from "./parse.js";
350
343
  },
351
344
 
352
345
  Property: function(M) {
353
- var key = M.key;
354
- var args = {
355
- start : my_start_token(key || M.value),
356
- end : my_end_token(M.value),
357
- key : key.type == "Identifier" ? key.name : key.value,
358
- quote : !key.computed && key.type === "Literal" && typeof key.value === "string"
359
- ? '"'
360
- : null,
361
- value : from_moz(M.value)
362
- };
363
- if (M.computed) {
364
- args.key = from_moz(M.key);
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
- if (typeof args.key === "string" || typeof args.key === "number") {
383
- args.key = new AST_SymbolMethod({
384
- name: args.key
385
- });
386
- }
387
- args.value = new AST_Accessor(args.value);
388
- if (M.kind == "get") return new AST_ObjectGetter(args);
389
- if (M.kind == "set") return new AST_ObjectSetter(args);
390
- if (M.kind == "method") {
391
- args.async = M.value.async;
392
- args.is_generator = M.value.generator;
393
- return new AST_ConciseMethod(args);
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
+ is_generator: value.is_generator,
369
+ async : value.async,
370
+ static : false, // always an object
371
+ value,
372
+ };
373
+
374
+ if (M.kind == "get") return new AST_ObjectGetter(args);
375
+ if (M.kind == "set") return new AST_ObjectSetter(args);
376
+ if (M.method) return new AST_ConciseMethod(args);
394
377
  }
395
378
  },
396
379
 
397
380
  MethodDefinition: function(M) {
398
381
  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 });
382
+ const key = M.computed ? from_moz(M.key) : new AST_SymbolMethod({ name: M.key.name || String(M.key.value) });
400
383
 
401
384
  var args = {
402
385
  start : my_start_token(M),
403
386
  end : my_end_token(M),
404
387
  key,
405
- value : from_moz(M.value),
388
+ quote : from_moz_quote(M.key, M.computed),
389
+ value : from_moz_lambda(M.value, /*is_method=*/true),
406
390
  static : M.static,
407
391
  };
408
392
  if (M.kind == "get") {
@@ -427,6 +411,7 @@ import { is_basic_identifier_string } from "./parse.js";
427
411
  return new AST_ClassProperty({
428
412
  start : my_start_token(M),
429
413
  end : my_end_token(M),
414
+ quote : from_moz_quote(M.key, M.computed),
430
415
  key,
431
416
  value : from_moz(M.value),
432
417
  static : M.static,
@@ -446,15 +431,13 @@ import { is_basic_identifier_string } from "./parse.js";
446
431
  static : M.static,
447
432
  });
448
433
  } else {
449
- if (M.key.type !== "Identifier") {
450
- throw new Error("Non-Identifier key in PropertyDefinition");
451
- }
452
- key = from_moz(M.key);
434
+ key = from_moz_symbol(AST_SymbolClassProperty, M.key);
453
435
  }
454
436
 
455
437
  return new AST_ClassProperty({
456
438
  start : my_start_token(M),
457
439
  end : my_end_token(M),
440
+ quote : from_moz_quote(M.key, M.computed),
458
441
  key,
459
442
  value : from_moz(M.value),
460
443
  static : M.static,
@@ -546,11 +529,30 @@ import { is_basic_identifier_string } from "./parse.js";
546
529
  },
547
530
 
548
531
  VariableDeclaration: function(M) {
549
- return new (M.kind === "const" ? AST_Const :
550
- M.kind === "let" ? AST_Let : AST_Var)({
532
+ let decl_type;
533
+ let sym_type;
534
+ if (M.kind === "const") {
535
+ decl_type = AST_Const;
536
+ sym_type = AST_SymbolConst;
537
+ } else if (M.kind === "let") {
538
+ decl_type = AST_Let;
539
+ sym_type = AST_SymbolLet;
540
+ } else {
541
+ decl_type = AST_Var;
542
+ sym_type = AST_SymbolVar;
543
+ }
544
+ const definitions = M.declarations.map(M => {
545
+ return new AST_VarDef({
546
+ start: my_start_token(M),
547
+ end: my_end_token(M),
548
+ name: from_moz_pattern(M.id, sym_type),
549
+ value: from_moz(M.init),
550
+ });
551
+ });
552
+ return new decl_type({
551
553
  start : my_start_token(M),
552
554
  end : my_end_token(M),
553
- definitions : M.declarations.map(from_moz)
555
+ definitions : definitions,
554
556
  });
555
557
  },
556
558
 
@@ -579,13 +581,13 @@ import { is_basic_identifier_string } from "./parse.js";
579
581
  return new AST_NameMapping({
580
582
  start: my_start_token(M),
581
583
  end: my_end_token(M),
582
- foreign_name: from_moz(M.imported),
583
- name: from_moz(M.local)
584
+ foreign_name: from_moz_symbol(AST_SymbolImportForeign, M.imported, M.imported.type === "Literal"),
585
+ name: from_moz_symbol(AST_SymbolImport, M.local)
584
586
  });
585
587
  },
586
588
 
587
589
  ImportDefaultSpecifier: function(M) {
588
- return from_moz(M.local);
590
+ return from_moz_symbol(AST_SymbolImport, M.local);
589
591
  },
590
592
 
591
593
  ImportNamespaceSpecifier: function(M) {
@@ -593,7 +595,7 @@ import { is_basic_identifier_string } from "./parse.js";
593
595
  start: my_start_token(M),
594
596
  end: my_end_token(M),
595
597
  foreign_name: new AST_SymbolImportForeign({ name: "*" }),
596
- name: from_moz(M.local)
598
+ name: from_moz_symbol(AST_SymbolImport, M.local)
597
599
  });
598
600
  },
599
601
 
@@ -615,15 +617,17 @@ import { is_basic_identifier_string } from "./parse.js";
615
617
  },
616
618
 
617
619
  ExportAllDeclaration: function(M) {
618
- var foreign_name = M.exported == null ?
620
+ var foreign_name = M.exported == null ?
619
621
  new AST_SymbolExportForeign({ name: "*" }) :
620
- from_moz(M.exported);
622
+ from_moz_symbol(AST_SymbolExportForeign, M.exported, M.exported.type === "Literal");
621
623
  return new AST_Export({
622
624
  start: my_start_token(M),
623
625
  end: my_end_token(M),
624
626
  exported_names: [
625
627
  new AST_NameMapping({
626
- name: new AST_SymbolExportForeign({ name: "*" }),
628
+ start: my_start_token(M),
629
+ end: my_end_token(M),
630
+ name: new AST_SymbolExport({ name: "*" }),
627
631
  foreign_name: foreign_name
628
632
  })
629
633
  ],
@@ -666,8 +670,10 @@ import { is_basic_identifier_string } from "./parse.js";
666
670
 
667
671
  ExportSpecifier: function(M) {
668
672
  return new AST_NameMapping({
669
- foreign_name: from_moz(M.exported),
670
- name: from_moz(M.local)
673
+ start: my_start_token(M),
674
+ end: my_end_token(M),
675
+ foreign_name: from_moz_symbol(AST_SymbolExportForeign, M.exported, M.exported.type === "Literal"),
676
+ name: from_moz_symbol(AST_SymbolExport, M.local, M.local.type === "Literal"),
671
677
  });
672
678
  },
673
679
 
@@ -696,27 +702,13 @@ import { is_basic_identifier_string } from "./parse.js";
696
702
  const bi = typeof M.value === "bigint" ? M.value.toString() : M.bigint;
697
703
  if (typeof bi === "string") {
698
704
  args.value = bi;
705
+ args.raw = M.raw;
699
706
  return new AST_BigInt(args);
700
707
  }
701
708
  if (val === null) return new AST_Null(args);
702
709
  switch (typeof val) {
703
710
  case "string":
704
711
  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
712
  args.value = val;
721
713
  return new AST_String(args);
722
714
  case "number":
@@ -743,27 +735,11 @@ import { is_basic_identifier_string } from "./parse.js";
743
735
  },
744
736
 
745
737
  Identifier: function(M) {
746
- var p = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 2];
747
- var q = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 3];
748
- return new ( p.type == "LabeledStatement" ? AST_Label
749
- : p.type == "VariableDeclarator" && p.id === M ? (q.kind == "const" ? AST_SymbolConst : q.kind == "let" ? AST_SymbolLet : AST_SymbolVar)
750
- : /Import.*Specifier/.test(p.type) ? (p.local === M ? AST_SymbolImport : AST_SymbolImportForeign)
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
- });
738
+ return new AST_SymbolRef({
739
+ start : my_start_token(M),
740
+ end : my_end_token(M),
741
+ name : M.name
742
+ });
767
743
  },
768
744
 
769
745
  EmptyStatement: function(M) {
@@ -792,19 +768,28 @@ import { is_basic_identifier_string } from "./parse.js";
792
768
  },
793
769
 
794
770
  LabeledStatement: function(M) {
795
- return new AST_LabeledStatement({
796
- start: my_start_token(M),
797
- end: my_end_token(M),
798
- label: from_moz(M.label),
799
- body: from_moz(M.body)
800
- });
771
+ try {
772
+ const label = from_moz_symbol(AST_Label, M.label);
773
+ FROM_MOZ_LABELS.push(label);
774
+
775
+ const stat = new AST_LabeledStatement({
776
+ start: my_start_token(M),
777
+ end: my_end_token(M),
778
+ label,
779
+ body: from_moz(M.body)
780
+ });
781
+
782
+ return stat;
783
+ } finally {
784
+ FROM_MOZ_LABELS.pop();
785
+ }
801
786
  },
802
787
 
803
788
  BreakStatement: function(M) {
804
789
  return new AST_Break({
805
790
  start: my_start_token(M),
806
791
  end: my_end_token(M),
807
- label: from_moz(M.label)
792
+ label: from_moz_label_ref(M.label),
808
793
  });
809
794
  },
810
795
 
@@ -812,7 +797,7 @@ import { is_basic_identifier_string } from "./parse.js";
812
797
  return new AST_Continue({
813
798
  start: my_start_token(M),
814
799
  end: my_end_token(M),
815
- label: from_moz(M.label)
800
+ label: from_moz_label_ref(M.label),
816
801
  });
817
802
  },
818
803
 
@@ -924,20 +909,11 @@ import { is_basic_identifier_string } from "./parse.js";
924
909
  });
925
910
  },
926
911
 
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
912
  CatchClause: function(M) {
937
913
  return new AST_Catch({
938
914
  start: my_start_token(M),
939
915
  end: my_end_token(M),
940
- argname: from_moz(M.param),
916
+ argname: M.param ? from_moz_pattern(M.param, AST_SymbolCatch) : null,
941
917
  body: from_moz(M.body).body
942
918
  });
943
919
  },
@@ -945,6 +921,7 @@ import { is_basic_identifier_string } from "./parse.js";
945
921
  ThisExpression: function(M) {
946
922
  return new AST_This({
947
923
  start: my_start_token(M),
924
+ name: "this",
948
925
  end: my_end_token(M)
949
926
  });
950
927
  },
@@ -952,7 +929,8 @@ import { is_basic_identifier_string } from "./parse.js";
952
929
  Super: function(M) {
953
930
  return new AST_Super({
954
931
  start: my_start_token(M),
955
- end: my_end_token(M)
932
+ end: my_end_token(M),
933
+ name: "super",
956
934
  });
957
935
  },
958
936
 
@@ -993,6 +971,7 @@ import { is_basic_identifier_string } from "./parse.js";
993
971
  start: my_start_token(M),
994
972
  end: my_end_token(M),
995
973
  operator: M.operator,
974
+ logical: M.operator === "??=" || M.operator === "&&=" || M.operator === "||=",
996
975
  left: from_moz(M.left),
997
976
  right: from_moz(M.right)
998
977
  });
@@ -1045,7 +1024,7 @@ import { is_basic_identifier_string } from "./parse.js";
1045
1024
  return new (M.type === "ClassDeclaration" ? AST_DefClass : AST_ClassExpression)({
1046
1025
  start : my_start_token(M),
1047
1026
  end : my_end_token(M),
1048
- name : from_moz(M.id),
1027
+ name : M.id && from_moz_symbol(M.type === "ClassDeclaration" ? AST_SymbolDefClass : AST_SymbolClass, M.id),
1049
1028
  extends : from_moz(M.superClass),
1050
1029
  properties: M.body.body.map(from_moz)
1051
1030
  });
@@ -1180,13 +1159,6 @@ import { is_basic_identifier_string } from "./parse.js";
1180
1159
  init: to_moz(M.value)
1181
1160
  };
1182
1161
  });
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
1162
 
1191
1163
  def_to_moz(AST_This, function To_Moz_ThisExpression() {
1192
1164
  return {
@@ -1219,7 +1191,7 @@ import { is_basic_identifier_string } from "./parse.js";
1219
1191
  return {
1220
1192
  type: "ImportExpression",
1221
1193
  source,
1222
- options
1194
+ options: options || null
1223
1195
  };
1224
1196
  }
1225
1197
 
@@ -1278,7 +1250,7 @@ import { is_basic_identifier_string } from "./parse.js";
1278
1250
  return {
1279
1251
  type: "FunctionDeclaration",
1280
1252
  id: to_moz(M.name),
1281
- params: M.argnames.map(to_moz),
1253
+ params: M.argnames.map(to_moz_pattern),
1282
1254
  generator: M.is_generator,
1283
1255
  async: M.async,
1284
1256
  body: to_moz_scope("BlockStatement", M)
@@ -1286,28 +1258,31 @@ import { is_basic_identifier_string } from "./parse.js";
1286
1258
  });
1287
1259
 
1288
1260
  def_to_moz(AST_Function, function To_Moz_FunctionExpression(M, parent) {
1289
- var is_generator = parent.is_generator !== undefined ?
1290
- parent.is_generator : M.is_generator;
1261
+ var is_generator = parent.is_generator !== undefined
1262
+ ? parent.is_generator
1263
+ : M.is_generator;
1291
1264
  return {
1292
1265
  type: "FunctionExpression",
1293
1266
  id: to_moz(M.name),
1294
- params: M.argnames.map(to_moz),
1295
- generator: is_generator,
1296
- async: M.async,
1267
+ params: M.argnames.map(to_moz_pattern),
1268
+ generator: is_generator || false,
1269
+ async: M.async || false,
1297
1270
  body: to_moz_scope("BlockStatement", M)
1298
1271
  };
1299
1272
  });
1300
1273
 
1301
1274
  def_to_moz(AST_Arrow, function To_Moz_ArrowFunctionExpression(M) {
1302
- var body = {
1303
- type: "BlockStatement",
1304
- body: M.body.map(to_moz)
1305
- };
1275
+ var body = M.body.length === 1 && M.body[0] instanceof AST_Return && M.body[0].value
1276
+ ? to_moz(M.body[0].value)
1277
+ : {
1278
+ type: "BlockStatement",
1279
+ body: M.body.map(to_moz)
1280
+ };
1306
1281
  return {
1307
1282
  type: "ArrowFunctionExpression",
1308
- params: M.argnames.map(to_moz),
1283
+ params: M.argnames.map(to_moz_pattern),
1309
1284
  async: M.async,
1310
- body: body
1285
+ body: body,
1311
1286
  };
1312
1287
  });
1313
1288
 
@@ -1315,19 +1290,38 @@ import { is_basic_identifier_string } from "./parse.js";
1315
1290
  if (M.is_array) {
1316
1291
  return {
1317
1292
  type: "ArrayPattern",
1318
- elements: M.names.map(to_moz)
1293
+ elements: M.names.map(
1294
+ M => M instanceof AST_Hole ? null : to_moz_pattern(M)
1295
+ ),
1319
1296
  };
1320
1297
  }
1321
1298
  return {
1322
1299
  type: "ObjectPattern",
1323
- properties: M.names.map(to_moz)
1300
+ properties: M.names.map(M => {
1301
+ if (M instanceof AST_ObjectKeyVal) {
1302
+ var computed = M.computed_key();
1303
+ const [shorthand, key] = to_moz_property_key(M.key, computed, M.quote, M.value);
1304
+
1305
+ return {
1306
+ type: "Property",
1307
+ computed,
1308
+ kind: "init",
1309
+ key: key,
1310
+ method: false,
1311
+ shorthand,
1312
+ value: to_moz_pattern(M.value)
1313
+ };
1314
+ } else {
1315
+ return to_moz_pattern(M);
1316
+ }
1317
+ }),
1324
1318
  };
1325
1319
  });
1326
1320
 
1327
1321
  def_to_moz(AST_DefaultAssign, function To_Moz_AssignmentExpression(M) {
1328
1322
  return {
1329
1323
  type: "AssignmentPattern",
1330
- left: to_moz(M.left),
1324
+ left: to_moz_pattern(M.left),
1331
1325
  right: to_moz(M.right),
1332
1326
  };
1333
1327
  });
@@ -1372,8 +1366,7 @@ import { is_basic_identifier_string } from "./parse.js";
1372
1366
  def_to_moz(AST_Catch, function To_Moz_CatchClause(M) {
1373
1367
  return {
1374
1368
  type: "CatchClause",
1375
- param: to_moz(M.argname),
1376
- guard: null,
1369
+ param: M.argname != null ? to_moz_pattern(M.argname) : null,
1377
1370
  body: to_moz_block(M)
1378
1371
  };
1379
1372
  });
@@ -1434,10 +1427,20 @@ import { is_basic_identifier_string } from "./parse.js";
1434
1427
  attributes: import_attributes_to_moz(M.attributes)
1435
1428
  };
1436
1429
  }
1437
- return {
1438
- type: M.is_default ? "ExportDefaultDeclaration" : "ExportNamedDeclaration",
1439
- declaration: to_moz(M.exported_value || M.exported_definition)
1440
- };
1430
+
1431
+ if (M.is_default) {
1432
+ return {
1433
+ type: "ExportDefaultDeclaration",
1434
+ declaration: to_moz(M.exported_value || M.exported_definition),
1435
+ };
1436
+ } else {
1437
+ return {
1438
+ type: "ExportNamedDeclaration",
1439
+ declaration: to_moz(M.exported_value || M.exported_definition),
1440
+ specifiers: [],
1441
+ source: null,
1442
+ };
1443
+ }
1441
1444
  });
1442
1445
 
1443
1446
  def_to_moz(AST_Import, function To_Moz_ImportDeclaration(M) {
@@ -1588,35 +1591,10 @@ import { is_basic_identifier_string } from "./parse.js";
1588
1591
  });
1589
1592
 
1590
1593
  def_to_moz(AST_ObjectProperty, function To_Moz_Property(M, parent) {
1591
- var key = M.key instanceof AST_Node ? to_moz(M.key) : {
1592
- type: "Identifier",
1593
- value: M.key,
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
- }
1594
+ var computed = M.computed_key();
1595
+ const [shorthand, key] = to_moz_property_key(M.key, computed, M.quote, M.value);
1596
+
1613
1597
  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
1598
  if (M instanceof AST_ObjectGetter) {
1621
1599
  kind = "get";
1622
1600
  } else
@@ -1671,31 +1649,51 @@ import { is_basic_identifier_string } from "./parse.js";
1671
1649
  return {
1672
1650
  type: "Property",
1673
1651
  computed: computed,
1652
+ method: false,
1653
+ shorthand,
1674
1654
  kind: kind,
1675
1655
  key: key,
1676
1656
  value: to_moz(M.value)
1677
1657
  };
1678
1658
  });
1679
1659
 
1660
+ def_to_moz(AST_ObjectKeyVal, function To_Moz_Property(M) {
1661
+ var computed = M.computed_key();
1662
+ const [shorthand, key] = to_moz_property_key(M.key, computed, M.quote, M.value);
1663
+
1664
+ return {
1665
+ type: "Property",
1666
+ computed: computed,
1667
+ shorthand: shorthand,
1668
+ method: false,
1669
+ kind: "init",
1670
+ key: key,
1671
+ value: to_moz(M.value)
1672
+ };
1673
+ });
1674
+
1680
1675
  def_to_moz(AST_ConciseMethod, function To_Moz_MethodDefinition(M, parent) {
1676
+ const computed = M.computed_key();
1677
+ const [_always_false, key] = to_moz_property_key(M.key, computed, M.quote, M.value);
1678
+
1681
1679
  if (parent instanceof AST_Object) {
1682
1680
  return {
1683
1681
  type: "Property",
1684
- computed: !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef,
1685
1682
  kind: "init",
1683
+ computed,
1686
1684
  method: true,
1687
1685
  shorthand: false,
1688
- key: to_moz(M.key),
1689
- value: to_moz(M.value)
1686
+ key,
1687
+ value: to_moz(M.value),
1690
1688
  };
1691
1689
  }
1692
1690
 
1693
1691
  return {
1694
1692
  type: "MethodDefinition",
1695
- kind: M.key === "constructor" ? "constructor" : "method",
1696
- key: to_moz(M.key),
1693
+ kind: !computed && M.key.name === "constructor" ? "constructor" : "method",
1694
+ computed,
1695
+ key,
1697
1696
  value: to_moz(M.value),
1698
- computed: !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef,
1699
1697
  static: M.static,
1700
1698
  };
1701
1699
  });
@@ -1801,6 +1799,7 @@ import { is_basic_identifier_string } from "./parse.js";
1801
1799
  // `M.value` is a string that may be a hex number representation.
1802
1800
  // but "bigint" property should have only decimal digits
1803
1801
  bigint: typeof BigInt === "function" ? BigInt(M.value).toString() : M.value,
1802
+ raw: M.raw,
1804
1803
  }));
1805
1804
 
1806
1805
  AST_Boolean.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
@@ -1844,20 +1843,133 @@ import { is_basic_identifier_string } from "./parse.js";
1844
1843
  );
1845
1844
  }
1846
1845
 
1847
- var FROM_MOZ_STACK = null;
1846
+ var FROM_MOZ_LABELS = null;
1848
1847
 
1849
1848
  function from_moz(node) {
1850
- FROM_MOZ_STACK.push(node);
1851
- var ret = node != null ? MOZ_TO_ME[node.type](node) : null;
1852
- FROM_MOZ_STACK.pop();
1853
- return ret;
1849
+ if (node == null) return null;
1850
+ return MOZ_TO_ME[node.type](node);
1851
+ }
1852
+
1853
+ function from_moz_quote(moz_key, computed) {
1854
+ if (!computed && moz_key.type === "Literal" && typeof moz_key.value === "string") {
1855
+ return '"';
1856
+ } else {
1857
+ return "";
1858
+ }
1859
+ }
1860
+
1861
+ function from_moz_symbol(symbol_type, M, has_quote) {
1862
+ return new symbol_type({
1863
+ start: my_start_token(M),
1864
+ quote: has_quote ? '"' : undefined,
1865
+ name: M.type === "Identifier" ? M.name : String(M.value),
1866
+ end: my_end_token(M),
1867
+ });
1868
+ }
1869
+
1870
+ function from_moz_lambda(M, is_method) {
1871
+ return new (is_method ? AST_Accessor : AST_Function)({
1872
+ start: my_start_token(M),
1873
+ end: my_end_token(M),
1874
+ name: M.id && from_moz_symbol(is_method ? AST_SymbolMethod : AST_SymbolLambda, M.id),
1875
+ argnames: M.params.map(M => from_moz_pattern(M, AST_SymbolFunarg)),
1876
+ is_generator: M.generator,
1877
+ async: M.async,
1878
+ body: normalize_directives(from_moz(M.body).body)
1879
+ });
1880
+ }
1881
+
1882
+ function from_moz_pattern(M, sym_type) {
1883
+ switch (M.type) {
1884
+ case "ObjectPattern":
1885
+ return new AST_Destructuring({
1886
+ start: my_start_token(M),
1887
+ end: my_end_token(M),
1888
+ names: M.properties.map(p => from_moz_pattern(p, sym_type)),
1889
+ is_array: false
1890
+ });
1891
+
1892
+ case "Property":
1893
+ var key = M.key;
1894
+ var args = {
1895
+ start : my_start_token(key || M.value),
1896
+ end : my_end_token(M.value),
1897
+ key : key.type == "Identifier" ? key.name : String(key.value),
1898
+ quote : !M.computed && key.type === "Literal" && typeof key.value === "string"
1899
+ ? '"'
1900
+ : "",
1901
+ value : from_moz_pattern(M.value, sym_type)
1902
+ };
1903
+ if (M.computed) {
1904
+ args.key = from_moz(M.key);
1905
+ }
1906
+ return new AST_ObjectKeyVal(args);
1907
+
1908
+ case "ArrayPattern":
1909
+ return new AST_Destructuring({
1910
+ start: my_start_token(M),
1911
+ end: my_end_token(M),
1912
+ names: M.elements.map(function(elm) {
1913
+ if (elm === null) {
1914
+ return new AST_Hole();
1915
+ }
1916
+ return from_moz_pattern(elm, sym_type);
1917
+ }),
1918
+ is_array: true
1919
+ });
1920
+
1921
+ case "SpreadElement":
1922
+ case "RestElement":
1923
+ return new AST_Expansion({
1924
+ start: my_start_token(M),
1925
+ end: my_end_token(M),
1926
+ expression: from_moz_pattern(M.argument, sym_type),
1927
+ });
1928
+
1929
+ case "AssignmentPattern":
1930
+ return new AST_DefaultAssign({
1931
+ start : my_start_token(M),
1932
+ end : my_end_token(M),
1933
+ left : from_moz_pattern(M.left, sym_type),
1934
+ operator: "=",
1935
+ right : from_moz(M.right),
1936
+ });
1937
+
1938
+ case "Identifier":
1939
+ return new sym_type({
1940
+ start : my_start_token(M),
1941
+ end : my_end_token(M),
1942
+ name : M.name,
1943
+ });
1944
+
1945
+ default:
1946
+ throw new Error("Invalid node type for destructuring: " + M.type);
1947
+ }
1948
+ }
1949
+
1950
+ function from_moz_label_ref(m_label) {
1951
+ if (!m_label) return null;
1952
+
1953
+ const label = from_moz_symbol(AST_LabelRef, m_label);
1954
+
1955
+ let i = FROM_MOZ_LABELS.length;
1956
+ while (i--) {
1957
+ const label_origin = FROM_MOZ_LABELS[i];
1958
+
1959
+ if (label.name === label_origin.name) {
1960
+ label.thedef = label_origin;
1961
+ break;
1962
+ }
1963
+ }
1964
+
1965
+ return label;
1854
1966
  }
1855
1967
 
1856
1968
  AST_Node.from_mozilla_ast = function(node) {
1857
- var save_stack = FROM_MOZ_STACK;
1858
- FROM_MOZ_STACK = [];
1969
+ var save_labels = FROM_MOZ_LABELS;
1970
+ FROM_MOZ_LABELS = [];
1859
1971
  var ast = from_moz(node);
1860
- FROM_MOZ_STACK = save_stack;
1972
+ FROM_MOZ_LABELS = save_labels;
1861
1973
  return ast;
1862
1974
  };
1863
1975
 
@@ -1899,6 +2011,52 @@ import { is_basic_identifier_string } from "./parse.js";
1899
2011
  return ast;
1900
2012
  }
1901
2013
 
2014
+ /** Object property keys can be number literals, string literals, or raw names. Additionally they can be shorthand. We decide that here. */
2015
+ function to_moz_property_key(key, computed = false, quote = false, value = null) {
2016
+ if (computed) {
2017
+ return [false, to_moz(key)];
2018
+ }
2019
+
2020
+ const key_name = typeof key === "string" ? key : key.name;
2021
+ let moz_key;
2022
+ if (quote) {
2023
+ moz_key = { type: "Literal", value: key_name, raw: JSON.stringify(key_name) };
2024
+ } else if ("" + +key_name === key_name && +key_name >= 0) {
2025
+ // representable as a number
2026
+ moz_key = { type: "Literal", value: +key_name, raw: JSON.stringify(+key_name) };
2027
+ } else {
2028
+ moz_key = { type: "Identifier", name: key_name };
2029
+ }
2030
+
2031
+ const shorthand =
2032
+ moz_key.type === "Identifier"
2033
+ && moz_key.name === key_name
2034
+ && (value instanceof AST_Symbol && value.name === key_name
2035
+ || value instanceof AST_DefaultAssign && value.left.name === key_name);
2036
+ return [shorthand, moz_key];
2037
+ }
2038
+
2039
+ function to_moz_pattern(node) {
2040
+ if (node instanceof AST_Expansion) {
2041
+ return {
2042
+ type: "RestElement",
2043
+ argument: to_moz_pattern(node.expression),
2044
+ };
2045
+ }
2046
+
2047
+ if ((
2048
+ node instanceof AST_Symbol
2049
+ || node instanceof AST_Destructuring
2050
+ || node instanceof AST_DefaultAssign
2051
+ || node instanceof AST_PropAccess
2052
+ )) {
2053
+ // Plain translation
2054
+ return to_moz(node);
2055
+ }
2056
+
2057
+ throw new Error(node.TYPE);
2058
+ }
2059
+
1902
2060
  function to_moz_in_destructuring() {
1903
2061
  var i = TO_MOZ_STACK.length;
1904
2062
  while (i--) {