terser 5.41.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.
@@ -329,7 +329,6 @@ ALL_RESERVED_WORDS = makePredicate(ALL_RESERVED_WORDS);
329
329
 
330
330
  var OPERATOR_CHARS = makePredicate(characters("+-*&%=<>!?|~^"));
331
331
 
332
- var RE_NUM_LITERAL = /[0-9a-f]/i;
333
332
  var RE_HEX_NUMBER = /^0x[0-9a-f]+$/i;
334
333
  var RE_OCT_NUMBER = /^0[0-7]+$/;
335
334
  var RE_ES6_OCT_NUMBER = /^0o[0-7]+$/i;
@@ -694,15 +693,16 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
694
693
  return after_e;
695
694
  case (after_e = false, 46): // .
696
695
  return (!has_dot && !has_x && !has_e) ? (has_dot = true) : false;
697
- }
698
-
699
- if (ch === "n") {
696
+ case 110: // n
700
697
  is_big_int = true;
701
-
702
698
  return true;
703
699
  }
704
700
 
705
- return RE_NUM_LITERAL.test(ch);
701
+ return (
702
+ code >= 48 && code <= 57 // 0-9
703
+ || code >= 97 && code <= 102 // a-f
704
+ || code >= 65 && code <= 70 // A-F
705
+ );
706
706
  });
707
707
  if (prefix) num = prefix + num;
708
708
 
@@ -719,7 +719,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
719
719
  }
720
720
  num = num.replace(/_/g, "");
721
721
  }
722
- if (num.endsWith("n")) {
722
+ if (is_big_int) {
723
723
  const without_n = num.slice(0, -1);
724
724
  const allow_e = RE_HEX_NUMBER.test(without_n);
725
725
  const valid = parse_js_number(without_n, allow_e);
@@ -894,7 +894,24 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
894
894
  return next_token;
895
895
  });
896
896
 
897
- var read_name = with_eof_error("Unterminated identifier name", function() {
897
+ var read_name = function () {
898
+ let start = S.pos, end = start - 1, ch = "c";
899
+
900
+ while (
901
+ (ch = S.text.charAt(++end))
902
+ && (ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z")
903
+ );
904
+
905
+ if (end > start + 1 && ch && ch !== "\\" && !is_identifier_char(ch)) {
906
+ S.pos += end - start;
907
+ S.col += end - start;
908
+ return S.text.slice(start, S.pos);
909
+ }
910
+
911
+ return read_name_hard();
912
+ };
913
+
914
+ var read_name_hard = with_eof_error("Unterminated identifier name", function() {
898
915
  var name = [], ch, escaped = false;
899
916
  var read_escaped_identifier_char = function() {
900
917
  escaped = true;
@@ -7181,6 +7198,7 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7181
7198
  body[i] = new AST_Directive({
7182
7199
  start: body[i].start,
7183
7200
  end: body[i].end,
7201
+ quote: '"',
7184
7202
  value: body[i].body.value
7185
7203
  });
7186
7204
  } else {
@@ -7304,8 +7322,8 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7304
7322
  return new AST_Defun({
7305
7323
  start: my_start_token(M),
7306
7324
  end: my_end_token(M),
7307
- name: from_moz(M.id),
7308
- argnames: M.params.map(from_moz),
7325
+ name: M.id && from_moz_symbol(AST_SymbolDefun, M.id),
7326
+ argnames: M.params.map(M => from_moz_pattern(M, AST_SymbolFunarg)),
7309
7327
  is_generator: M.generator,
7310
7328
  async: M.async,
7311
7329
  body: normalize_directives(from_moz(M.body).body)
@@ -7313,15 +7331,7 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7313
7331
  },
7314
7332
 
7315
7333
  FunctionExpression: function(M) {
7316
- return new AST_Function({
7317
- start: my_start_token(M),
7318
- end: my_end_token(M),
7319
- name: from_moz(M.id),
7320
- argnames: M.params.map(from_moz),
7321
- is_generator: M.generator,
7322
- async: M.async,
7323
- body: normalize_directives(from_moz(M.body).body)
7324
- });
7334
+ return from_moz_lambda(M, /*is_method=*/false);
7325
7335
  },
7326
7336
 
7327
7337
  ArrowFunctionExpression: function(M) {
@@ -7331,7 +7341,7 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7331
7341
  return new AST_Arrow({
7332
7342
  start: my_start_token(M),
7333
7343
  end: my_end_token(M),
7334
- argnames: M.params.map(from_moz),
7344
+ argnames: M.params.map(p => from_moz_pattern(p, AST_SymbolFunarg)),
7335
7345
  body,
7336
7346
  async: M.async,
7337
7347
  });
@@ -7360,59 +7370,50 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7360
7370
  },
7361
7371
 
7362
7372
  Property: function(M) {
7363
- var key = M.key;
7364
- var args = {
7365
- start : my_start_token(key || M.value),
7366
- end : my_end_token(M.value),
7367
- key : key.type == "Identifier" ? key.name : key.value,
7368
- quote : !key.computed && key.type === "Literal" && typeof key.value === "string"
7369
- ? '"'
7370
- : null,
7371
- value : from_moz(M.value)
7372
- };
7373
- if (M.computed) {
7374
- args.key = from_moz(M.key);
7375
- }
7376
- if (M.method) {
7377
- args.is_generator = M.value.generator;
7378
- args.async = M.value.async;
7379
- if (!M.computed) {
7380
- args.key = new AST_SymbolMethod({ name: args.key });
7381
- } else {
7382
- args.key = from_moz(M.key);
7383
- }
7384
- return new AST_ConciseMethod(args);
7385
- }
7386
- if (M.kind == "init") {
7387
- if (key.type != "Identifier" && key.type != "Literal") {
7388
- args.key = from_moz(key);
7389
- }
7373
+ if (M.kind == "init" && !M.method) {
7374
+ var args = {
7375
+ start : my_start_token(M.key || M.value),
7376
+ end : my_end_token(M.value),
7377
+ key : M.computed
7378
+ ? from_moz(M.key)
7379
+ : M.key.name || String(M.key.value),
7380
+ quote : from_moz_quote(M.key, M.computed),
7381
+ static : false, // always an object
7382
+ value : from_moz(M.value)
7383
+ };
7384
+
7390
7385
  return new AST_ObjectKeyVal(args);
7391
- }
7392
- if (typeof args.key === "string" || typeof args.key === "number") {
7393
- args.key = new AST_SymbolMethod({
7394
- name: args.key
7395
- });
7396
- }
7397
- args.value = new AST_Accessor(args.value);
7398
- if (M.kind == "get") return new AST_ObjectGetter(args);
7399
- if (M.kind == "set") return new AST_ObjectSetter(args);
7400
- if (M.kind == "method") {
7401
- args.async = M.value.async;
7402
- args.is_generator = M.value.generator;
7403
- return new AST_ConciseMethod(args);
7386
+ } else {
7387
+ var value = from_moz_lambda(M.value, /*is_method=*/true);
7388
+ var args = {
7389
+ start : my_start_token(M.key || M.value),
7390
+ end : my_end_token(M.value),
7391
+ key : M.computed
7392
+ ? from_moz(M.key)
7393
+ : from_moz_symbol(AST_SymbolMethod, M.key),
7394
+ quote : from_moz_quote(M.key, M.computed),
7395
+ is_generator: value.is_generator,
7396
+ async : value.async,
7397
+ static : false, // always an object
7398
+ value,
7399
+ };
7400
+
7401
+ if (M.kind == "get") return new AST_ObjectGetter(args);
7402
+ if (M.kind == "set") return new AST_ObjectSetter(args);
7403
+ if (M.method) return new AST_ConciseMethod(args);
7404
7404
  }
7405
7405
  },
7406
7406
 
7407
7407
  MethodDefinition: function(M) {
7408
7408
  const is_private = M.key.type === "PrivateIdentifier";
7409
- const key = M.computed ? from_moz(M.key) : new AST_SymbolMethod({ name: M.key.name || M.key.value });
7409
+ const key = M.computed ? from_moz(M.key) : new AST_SymbolMethod({ name: M.key.name || String(M.key.value) });
7410
7410
 
7411
7411
  var args = {
7412
7412
  start : my_start_token(M),
7413
7413
  end : my_end_token(M),
7414
7414
  key,
7415
- value : from_moz(M.value),
7415
+ quote : from_moz_quote(M.key, M.computed),
7416
+ value : from_moz_lambda(M.value, /*is_method=*/true),
7416
7417
  static : M.static,
7417
7418
  };
7418
7419
  if (M.kind == "get") {
@@ -7437,6 +7438,7 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7437
7438
  return new AST_ClassProperty({
7438
7439
  start : my_start_token(M),
7439
7440
  end : my_end_token(M),
7441
+ quote : from_moz_quote(M.key, M.computed),
7440
7442
  key,
7441
7443
  value : from_moz(M.value),
7442
7444
  static : M.static,
@@ -7456,15 +7458,13 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7456
7458
  static : M.static,
7457
7459
  });
7458
7460
  } else {
7459
- if (M.key.type !== "Identifier") {
7460
- throw new Error("Non-Identifier key in PropertyDefinition");
7461
- }
7462
- key = from_moz(M.key);
7461
+ key = from_moz_symbol(AST_SymbolClassProperty, M.key);
7463
7462
  }
7464
7463
 
7465
7464
  return new AST_ClassProperty({
7466
7465
  start : my_start_token(M),
7467
7466
  end : my_end_token(M),
7467
+ quote : from_moz_quote(M.key, M.computed),
7468
7468
  key,
7469
7469
  value : from_moz(M.value),
7470
7470
  static : M.static,
@@ -7556,11 +7556,30 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7556
7556
  },
7557
7557
 
7558
7558
  VariableDeclaration: function(M) {
7559
- return new (M.kind === "const" ? AST_Const :
7560
- M.kind === "let" ? AST_Let : AST_Var)({
7559
+ let decl_type;
7560
+ let sym_type;
7561
+ if (M.kind === "const") {
7562
+ decl_type = AST_Const;
7563
+ sym_type = AST_SymbolConst;
7564
+ } else if (M.kind === "let") {
7565
+ decl_type = AST_Let;
7566
+ sym_type = AST_SymbolLet;
7567
+ } else {
7568
+ decl_type = AST_Var;
7569
+ sym_type = AST_SymbolVar;
7570
+ }
7571
+ const definitions = M.declarations.map(M => {
7572
+ return new AST_VarDef({
7573
+ start: my_start_token(M),
7574
+ end: my_end_token(M),
7575
+ name: from_moz_pattern(M.id, sym_type),
7576
+ value: from_moz(M.init),
7577
+ });
7578
+ });
7579
+ return new decl_type({
7561
7580
  start : my_start_token(M),
7562
7581
  end : my_end_token(M),
7563
- definitions : M.declarations.map(from_moz)
7582
+ definitions : definitions,
7564
7583
  });
7565
7584
  },
7566
7585
 
@@ -7589,13 +7608,13 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7589
7608
  return new AST_NameMapping({
7590
7609
  start: my_start_token(M),
7591
7610
  end: my_end_token(M),
7592
- foreign_name: from_moz(M.imported),
7593
- name: from_moz(M.local)
7611
+ foreign_name: from_moz_symbol(AST_SymbolImportForeign, M.imported, M.imported.type === "Literal"),
7612
+ name: from_moz_symbol(AST_SymbolImport, M.local)
7594
7613
  });
7595
7614
  },
7596
7615
 
7597
7616
  ImportDefaultSpecifier: function(M) {
7598
- return from_moz(M.local);
7617
+ return from_moz_symbol(AST_SymbolImport, M.local);
7599
7618
  },
7600
7619
 
7601
7620
  ImportNamespaceSpecifier: function(M) {
@@ -7603,7 +7622,7 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7603
7622
  start: my_start_token(M),
7604
7623
  end: my_end_token(M),
7605
7624
  foreign_name: new AST_SymbolImportForeign({ name: "*" }),
7606
- name: from_moz(M.local)
7625
+ name: from_moz_symbol(AST_SymbolImport, M.local)
7607
7626
  });
7608
7627
  },
7609
7628
 
@@ -7625,15 +7644,17 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7625
7644
  },
7626
7645
 
7627
7646
  ExportAllDeclaration: function(M) {
7628
- var foreign_name = M.exported == null ?
7647
+ var foreign_name = M.exported == null ?
7629
7648
  new AST_SymbolExportForeign({ name: "*" }) :
7630
- from_moz(M.exported);
7649
+ from_moz_symbol(AST_SymbolExportForeign, M.exported, M.exported.type === "Literal");
7631
7650
  return new AST_Export({
7632
7651
  start: my_start_token(M),
7633
7652
  end: my_end_token(M),
7634
7653
  exported_names: [
7635
7654
  new AST_NameMapping({
7636
- name: new AST_SymbolExportForeign({ name: "*" }),
7655
+ start: my_start_token(M),
7656
+ end: my_end_token(M),
7657
+ name: new AST_SymbolExport({ name: "*" }),
7637
7658
  foreign_name: foreign_name
7638
7659
  })
7639
7660
  ],
@@ -7676,8 +7697,10 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7676
7697
 
7677
7698
  ExportSpecifier: function(M) {
7678
7699
  return new AST_NameMapping({
7679
- foreign_name: from_moz(M.exported),
7680
- name: from_moz(M.local)
7700
+ start: my_start_token(M),
7701
+ end: my_end_token(M),
7702
+ foreign_name: from_moz_symbol(AST_SymbolExportForeign, M.exported, M.exported.type === "Literal"),
7703
+ name: from_moz_symbol(AST_SymbolExport, M.local, M.local.type === "Literal"),
7681
7704
  });
7682
7705
  },
7683
7706
 
@@ -7706,27 +7729,13 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7706
7729
  const bi = typeof M.value === "bigint" ? M.value.toString() : M.bigint;
7707
7730
  if (typeof bi === "string") {
7708
7731
  args.value = bi;
7732
+ args.raw = M.raw;
7709
7733
  return new AST_BigInt(args);
7710
7734
  }
7711
7735
  if (val === null) return new AST_Null(args);
7712
7736
  switch (typeof val) {
7713
7737
  case "string":
7714
7738
  args.quote = "\"";
7715
- var p = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 2];
7716
- if (p.type == "ImportSpecifier") {
7717
- args.name = val;
7718
- return new AST_SymbolImportForeign(args);
7719
- } else if (p.type == "ExportSpecifier") {
7720
- args.name = val;
7721
- if (M == p.exported) {
7722
- return new AST_SymbolExportForeign(args);
7723
- } else {
7724
- return new AST_SymbolExport(args);
7725
- }
7726
- } else if (p.type == "ExportAllDeclaration" && M == p.exported) {
7727
- args.name = val;
7728
- return new AST_SymbolExportForeign(args);
7729
- }
7730
7739
  args.value = val;
7731
7740
  return new AST_String(args);
7732
7741
  case "number":
@@ -7753,27 +7762,11 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7753
7762
  },
7754
7763
 
7755
7764
  Identifier: function(M) {
7756
- var p = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 2];
7757
- var q = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 3];
7758
- return new ( p.type == "LabeledStatement" ? AST_Label
7759
- : p.type == "VariableDeclarator" && p.id === M ? (q.kind == "const" ? AST_SymbolConst : q.kind == "let" ? AST_SymbolLet : AST_SymbolVar)
7760
- : /Import.*Specifier/.test(p.type) ? (p.local === M ? AST_SymbolImport : AST_SymbolImportForeign)
7761
- : p.type == "ExportSpecifier" ? (p.local === M ? AST_SymbolExport : AST_SymbolExportForeign)
7762
- : p.type == "FunctionExpression" ? (p.id === M ? AST_SymbolLambda : AST_SymbolFunarg)
7763
- : p.type == "FunctionDeclaration" ? (p.id === M ? AST_SymbolDefun : AST_SymbolFunarg)
7764
- : p.type == "ArrowFunctionExpression" ? (p.params.includes(M)) ? AST_SymbolFunarg : AST_SymbolRef
7765
- : p.type == "ClassExpression" ? (p.id === M ? AST_SymbolClass : AST_SymbolRef)
7766
- : p.type == "Property" ? (p.key === M && p.computed || p.value === M ? AST_SymbolRef : AST_SymbolMethod)
7767
- : p.type == "PropertyDefinition" || p.type === "FieldDefinition" ? (p.key === M && p.computed || p.value === M ? AST_SymbolRef : AST_SymbolClassProperty)
7768
- : p.type == "ClassDeclaration" ? (p.id === M ? AST_SymbolDefClass : AST_SymbolRef)
7769
- : p.type == "MethodDefinition" ? (p.computed ? AST_SymbolRef : AST_SymbolMethod)
7770
- : p.type == "CatchClause" ? AST_SymbolCatch
7771
- : p.type == "BreakStatement" || p.type == "ContinueStatement" ? AST_LabelRef
7772
- : AST_SymbolRef)({
7773
- start : my_start_token(M),
7774
- end : my_end_token(M),
7775
- name : M.name
7776
- });
7765
+ return new AST_SymbolRef({
7766
+ start : my_start_token(M),
7767
+ end : my_end_token(M),
7768
+ name : M.name
7769
+ });
7777
7770
  },
7778
7771
 
7779
7772
  EmptyStatement: function(M) {
@@ -7802,19 +7795,28 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7802
7795
  },
7803
7796
 
7804
7797
  LabeledStatement: function(M) {
7805
- return new AST_LabeledStatement({
7806
- start: my_start_token(M),
7807
- end: my_end_token(M),
7808
- label: from_moz(M.label),
7809
- body: from_moz(M.body)
7810
- });
7798
+ try {
7799
+ const label = from_moz_symbol(AST_Label, M.label);
7800
+ FROM_MOZ_LABELS.push(label);
7801
+
7802
+ const stat = new AST_LabeledStatement({
7803
+ start: my_start_token(M),
7804
+ end: my_end_token(M),
7805
+ label,
7806
+ body: from_moz(M.body)
7807
+ });
7808
+
7809
+ return stat;
7810
+ } finally {
7811
+ FROM_MOZ_LABELS.pop();
7812
+ }
7811
7813
  },
7812
7814
 
7813
7815
  BreakStatement: function(M) {
7814
7816
  return new AST_Break({
7815
7817
  start: my_start_token(M),
7816
7818
  end: my_end_token(M),
7817
- label: from_moz(M.label)
7819
+ label: from_moz_label_ref(M.label),
7818
7820
  });
7819
7821
  },
7820
7822
 
@@ -7822,7 +7824,7 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7822
7824
  return new AST_Continue({
7823
7825
  start: my_start_token(M),
7824
7826
  end: my_end_token(M),
7825
- label: from_moz(M.label)
7827
+ label: from_moz_label_ref(M.label),
7826
7828
  });
7827
7829
  },
7828
7830
 
@@ -7934,20 +7936,11 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7934
7936
  });
7935
7937
  },
7936
7938
 
7937
- VariableDeclarator: function(M) {
7938
- return new AST_VarDef({
7939
- start: my_start_token(M),
7940
- end: my_end_token(M),
7941
- name: from_moz(M.id),
7942
- value: from_moz(M.init)
7943
- });
7944
- },
7945
-
7946
7939
  CatchClause: function(M) {
7947
7940
  return new AST_Catch({
7948
7941
  start: my_start_token(M),
7949
7942
  end: my_end_token(M),
7950
- argname: from_moz(M.param),
7943
+ argname: M.param ? from_moz_pattern(M.param, AST_SymbolCatch) : null,
7951
7944
  body: from_moz(M.body).body
7952
7945
  });
7953
7946
  },
@@ -7955,6 +7948,7 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7955
7948
  ThisExpression: function(M) {
7956
7949
  return new AST_This({
7957
7950
  start: my_start_token(M),
7951
+ name: "this",
7958
7952
  end: my_end_token(M)
7959
7953
  });
7960
7954
  },
@@ -7962,7 +7956,8 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7962
7956
  Super: function(M) {
7963
7957
  return new AST_Super({
7964
7958
  start: my_start_token(M),
7965
- end: my_end_token(M)
7959
+ end: my_end_token(M),
7960
+ name: "super",
7966
7961
  });
7967
7962
  },
7968
7963
 
@@ -8003,6 +7998,7 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
8003
7998
  start: my_start_token(M),
8004
7999
  end: my_end_token(M),
8005
8000
  operator: M.operator,
8001
+ logical: M.operator === "??=" || M.operator === "&&=" || M.operator === "||=",
8006
8002
  left: from_moz(M.left),
8007
8003
  right: from_moz(M.right)
8008
8004
  });
@@ -8055,7 +8051,7 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
8055
8051
  return new (M.type === "ClassDeclaration" ? AST_DefClass : AST_ClassExpression)({
8056
8052
  start : my_start_token(M),
8057
8053
  end : my_end_token(M),
8058
- name : from_moz(M.id),
8054
+ name : M.id && from_moz_symbol(M.type === "ClassDeclaration" ? AST_SymbolDefClass : AST_SymbolClass, M.id),
8059
8055
  extends : from_moz(M.superClass),
8060
8056
  properties: M.body.body.map(from_moz)
8061
8057
  });
@@ -8190,13 +8186,6 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
8190
8186
  init: to_moz(M.value)
8191
8187
  };
8192
8188
  });
8193
- def_to_moz(AST_Catch, function To_Moz_CatchClause(M) {
8194
- return {
8195
- type: "CatchClause",
8196
- param: to_moz(M.argname),
8197
- body: to_moz_block(M)
8198
- };
8199
- });
8200
8189
 
8201
8190
  def_to_moz(AST_This, function To_Moz_ThisExpression() {
8202
8191
  return {
@@ -8229,7 +8218,7 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
8229
8218
  return {
8230
8219
  type: "ImportExpression",
8231
8220
  source,
8232
- options
8221
+ options: options || null
8233
8222
  };
8234
8223
  }
8235
8224
 
@@ -8288,7 +8277,7 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
8288
8277
  return {
8289
8278
  type: "FunctionDeclaration",
8290
8279
  id: to_moz(M.name),
8291
- params: M.argnames.map(to_moz),
8280
+ params: M.argnames.map(to_moz_pattern),
8292
8281
  generator: M.is_generator,
8293
8282
  async: M.async,
8294
8283
  body: to_moz_scope("BlockStatement", M)
@@ -8296,28 +8285,31 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
8296
8285
  });
8297
8286
 
8298
8287
  def_to_moz(AST_Function, function To_Moz_FunctionExpression(M, parent) {
8299
- var is_generator = parent.is_generator !== undefined ?
8300
- parent.is_generator : M.is_generator;
8288
+ var is_generator = parent.is_generator !== undefined
8289
+ ? parent.is_generator
8290
+ : M.is_generator;
8301
8291
  return {
8302
8292
  type: "FunctionExpression",
8303
8293
  id: to_moz(M.name),
8304
- params: M.argnames.map(to_moz),
8305
- generator: is_generator,
8306
- async: M.async,
8294
+ params: M.argnames.map(to_moz_pattern),
8295
+ generator: is_generator || false,
8296
+ async: M.async || false,
8307
8297
  body: to_moz_scope("BlockStatement", M)
8308
8298
  };
8309
8299
  });
8310
8300
 
8311
8301
  def_to_moz(AST_Arrow, function To_Moz_ArrowFunctionExpression(M) {
8312
- var body = {
8313
- type: "BlockStatement",
8314
- body: M.body.map(to_moz)
8315
- };
8302
+ var body = M.body.length === 1 && M.body[0] instanceof AST_Return && M.body[0].value
8303
+ ? to_moz(M.body[0].value)
8304
+ : {
8305
+ type: "BlockStatement",
8306
+ body: M.body.map(to_moz)
8307
+ };
8316
8308
  return {
8317
8309
  type: "ArrowFunctionExpression",
8318
- params: M.argnames.map(to_moz),
8310
+ params: M.argnames.map(to_moz_pattern),
8319
8311
  async: M.async,
8320
- body: body
8312
+ body: body,
8321
8313
  };
8322
8314
  });
8323
8315
 
@@ -8325,19 +8317,38 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
8325
8317
  if (M.is_array) {
8326
8318
  return {
8327
8319
  type: "ArrayPattern",
8328
- elements: M.names.map(to_moz)
8320
+ elements: M.names.map(
8321
+ M => M instanceof AST_Hole ? null : to_moz_pattern(M)
8322
+ ),
8329
8323
  };
8330
8324
  }
8331
8325
  return {
8332
8326
  type: "ObjectPattern",
8333
- properties: M.names.map(to_moz)
8327
+ properties: M.names.map(M => {
8328
+ if (M instanceof AST_ObjectKeyVal) {
8329
+ var computed = M.computed_key();
8330
+ const [shorthand, key] = to_moz_property_key(M.key, computed, M.quote, M.value);
8331
+
8332
+ return {
8333
+ type: "Property",
8334
+ computed,
8335
+ kind: "init",
8336
+ key: key,
8337
+ method: false,
8338
+ shorthand,
8339
+ value: to_moz_pattern(M.value)
8340
+ };
8341
+ } else {
8342
+ return to_moz_pattern(M);
8343
+ }
8344
+ }),
8334
8345
  };
8335
8346
  });
8336
8347
 
8337
8348
  def_to_moz(AST_DefaultAssign, function To_Moz_AssignmentExpression(M) {
8338
8349
  return {
8339
8350
  type: "AssignmentPattern",
8340
- left: to_moz(M.left),
8351
+ left: to_moz_pattern(M.left),
8341
8352
  right: to_moz(M.right),
8342
8353
  };
8343
8354
  });
@@ -8382,8 +8393,7 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
8382
8393
  def_to_moz(AST_Catch, function To_Moz_CatchClause(M) {
8383
8394
  return {
8384
8395
  type: "CatchClause",
8385
- param: to_moz(M.argname),
8386
- guard: null,
8396
+ param: M.argname != null ? to_moz_pattern(M.argname) : null,
8387
8397
  body: to_moz_block(M)
8388
8398
  };
8389
8399
  });
@@ -8444,10 +8454,20 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
8444
8454
  attributes: import_attributes_to_moz(M.attributes)
8445
8455
  };
8446
8456
  }
8447
- return {
8448
- type: M.is_default ? "ExportDefaultDeclaration" : "ExportNamedDeclaration",
8449
- declaration: to_moz(M.exported_value || M.exported_definition)
8450
- };
8457
+
8458
+ if (M.is_default) {
8459
+ return {
8460
+ type: "ExportDefaultDeclaration",
8461
+ declaration: to_moz(M.exported_value || M.exported_definition),
8462
+ };
8463
+ } else {
8464
+ return {
8465
+ type: "ExportNamedDeclaration",
8466
+ declaration: to_moz(M.exported_value || M.exported_definition),
8467
+ specifiers: [],
8468
+ source: null,
8469
+ };
8470
+ }
8451
8471
  });
8452
8472
 
8453
8473
  def_to_moz(AST_Import, function To_Moz_ImportDeclaration(M) {
@@ -8598,35 +8618,10 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
8598
8618
  });
8599
8619
 
8600
8620
  def_to_moz(AST_ObjectProperty, function To_Moz_Property(M, parent) {
8601
- var key = M.key instanceof AST_Node ? to_moz(M.key) : {
8602
- type: "Identifier",
8603
- value: M.key,
8604
- };
8605
- if (typeof M.key === "number") {
8606
- key = {
8607
- type: "Literal",
8608
- value: Number(M.key)
8609
- };
8610
- }
8611
- if (typeof M.key === "string") {
8612
- key = M.quote
8613
- ? {
8614
- type: "Literal",
8615
- value: M.key,
8616
- raw: JSON.stringify(M.key),
8617
- }
8618
- : {
8619
- type: "Identifier",
8620
- name: M.key,
8621
- };
8622
- }
8621
+ var computed = M.computed_key();
8622
+ const [shorthand, key] = to_moz_property_key(M.key, computed, M.quote, M.value);
8623
+
8623
8624
  var kind;
8624
- var string_or_num = typeof M.key === "string" || typeof M.key === "number";
8625
- var computed = string_or_num ? false : !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef;
8626
- if (M instanceof AST_ObjectKeyVal) {
8627
- kind = "init";
8628
- computed = !string_or_num;
8629
- } else
8630
8625
  if (M instanceof AST_ObjectGetter) {
8631
8626
  kind = "get";
8632
8627
  } else
@@ -8681,31 +8676,51 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
8681
8676
  return {
8682
8677
  type: "Property",
8683
8678
  computed: computed,
8679
+ method: false,
8680
+ shorthand,
8684
8681
  kind: kind,
8685
8682
  key: key,
8686
8683
  value: to_moz(M.value)
8687
8684
  };
8688
8685
  });
8689
8686
 
8687
+ def_to_moz(AST_ObjectKeyVal, function To_Moz_Property(M) {
8688
+ var computed = M.computed_key();
8689
+ const [shorthand, key] = to_moz_property_key(M.key, computed, M.quote, M.value);
8690
+
8691
+ return {
8692
+ type: "Property",
8693
+ computed: computed,
8694
+ shorthand: shorthand,
8695
+ method: false,
8696
+ kind: "init",
8697
+ key: key,
8698
+ value: to_moz(M.value)
8699
+ };
8700
+ });
8701
+
8690
8702
  def_to_moz(AST_ConciseMethod, function To_Moz_MethodDefinition(M, parent) {
8703
+ const computed = M.computed_key();
8704
+ const [_always_false, key] = to_moz_property_key(M.key, computed, M.quote, M.value);
8705
+
8691
8706
  if (parent instanceof AST_Object) {
8692
8707
  return {
8693
8708
  type: "Property",
8694
- computed: !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef,
8695
8709
  kind: "init",
8710
+ computed,
8696
8711
  method: true,
8697
8712
  shorthand: false,
8698
- key: to_moz(M.key),
8699
- value: to_moz(M.value)
8713
+ key,
8714
+ value: to_moz(M.value),
8700
8715
  };
8701
8716
  }
8702
8717
 
8703
8718
  return {
8704
8719
  type: "MethodDefinition",
8705
- kind: M.key === "constructor" ? "constructor" : "method",
8706
- key: to_moz(M.key),
8720
+ kind: !computed && M.key.name === "constructor" ? "constructor" : "method",
8721
+ computed,
8722
+ key,
8707
8723
  value: to_moz(M.value),
8708
- computed: !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef,
8709
8724
  static: M.static,
8710
8725
  };
8711
8726
  });
@@ -8811,6 +8826,7 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
8811
8826
  // `M.value` is a string that may be a hex number representation.
8812
8827
  // but "bigint" property should have only decimal digits
8813
8828
  bigint: typeof BigInt === "function" ? BigInt(M.value).toString() : M.value,
8829
+ raw: M.raw,
8814
8830
  }));
8815
8831
 
8816
8832
  AST_Boolean.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
@@ -8854,20 +8870,133 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
8854
8870
  );
8855
8871
  }
8856
8872
 
8857
- var FROM_MOZ_STACK = null;
8873
+ var FROM_MOZ_LABELS = null;
8858
8874
 
8859
8875
  function from_moz(node) {
8860
- FROM_MOZ_STACK.push(node);
8861
- var ret = node != null ? MOZ_TO_ME[node.type](node) : null;
8862
- FROM_MOZ_STACK.pop();
8863
- return ret;
8876
+ if (node == null) return null;
8877
+ return MOZ_TO_ME[node.type](node);
8878
+ }
8879
+
8880
+ function from_moz_quote(moz_key, computed) {
8881
+ if (!computed && moz_key.type === "Literal" && typeof moz_key.value === "string") {
8882
+ return '"';
8883
+ } else {
8884
+ return "";
8885
+ }
8886
+ }
8887
+
8888
+ function from_moz_symbol(symbol_type, M, has_quote) {
8889
+ return new symbol_type({
8890
+ start: my_start_token(M),
8891
+ quote: has_quote ? '"' : undefined,
8892
+ name: M.type === "Identifier" ? M.name : String(M.value),
8893
+ end: my_end_token(M),
8894
+ });
8895
+ }
8896
+
8897
+ function from_moz_lambda(M, is_method) {
8898
+ return new (is_method ? AST_Accessor : AST_Function)({
8899
+ start: my_start_token(M),
8900
+ end: my_end_token(M),
8901
+ name: M.id && from_moz_symbol(is_method ? AST_SymbolMethod : AST_SymbolLambda, M.id),
8902
+ argnames: M.params.map(M => from_moz_pattern(M, AST_SymbolFunarg)),
8903
+ is_generator: M.generator,
8904
+ async: M.async,
8905
+ body: normalize_directives(from_moz(M.body).body)
8906
+ });
8907
+ }
8908
+
8909
+ function from_moz_pattern(M, sym_type) {
8910
+ switch (M.type) {
8911
+ case "ObjectPattern":
8912
+ return new AST_Destructuring({
8913
+ start: my_start_token(M),
8914
+ end: my_end_token(M),
8915
+ names: M.properties.map(p => from_moz_pattern(p, sym_type)),
8916
+ is_array: false
8917
+ });
8918
+
8919
+ case "Property":
8920
+ var key = M.key;
8921
+ var args = {
8922
+ start : my_start_token(key || M.value),
8923
+ end : my_end_token(M.value),
8924
+ key : key.type == "Identifier" ? key.name : String(key.value),
8925
+ quote : !M.computed && key.type === "Literal" && typeof key.value === "string"
8926
+ ? '"'
8927
+ : "",
8928
+ value : from_moz_pattern(M.value, sym_type)
8929
+ };
8930
+ if (M.computed) {
8931
+ args.key = from_moz(M.key);
8932
+ }
8933
+ return new AST_ObjectKeyVal(args);
8934
+
8935
+ case "ArrayPattern":
8936
+ return new AST_Destructuring({
8937
+ start: my_start_token(M),
8938
+ end: my_end_token(M),
8939
+ names: M.elements.map(function(elm) {
8940
+ if (elm === null) {
8941
+ return new AST_Hole();
8942
+ }
8943
+ return from_moz_pattern(elm, sym_type);
8944
+ }),
8945
+ is_array: true
8946
+ });
8947
+
8948
+ case "SpreadElement":
8949
+ case "RestElement":
8950
+ return new AST_Expansion({
8951
+ start: my_start_token(M),
8952
+ end: my_end_token(M),
8953
+ expression: from_moz_pattern(M.argument, sym_type),
8954
+ });
8955
+
8956
+ case "AssignmentPattern":
8957
+ return new AST_DefaultAssign({
8958
+ start : my_start_token(M),
8959
+ end : my_end_token(M),
8960
+ left : from_moz_pattern(M.left, sym_type),
8961
+ operator: "=",
8962
+ right : from_moz(M.right),
8963
+ });
8964
+
8965
+ case "Identifier":
8966
+ return new sym_type({
8967
+ start : my_start_token(M),
8968
+ end : my_end_token(M),
8969
+ name : M.name,
8970
+ });
8971
+
8972
+ default:
8973
+ throw new Error("Invalid node type for destructuring: " + M.type);
8974
+ }
8975
+ }
8976
+
8977
+ function from_moz_label_ref(m_label) {
8978
+ if (!m_label) return null;
8979
+
8980
+ const label = from_moz_symbol(AST_LabelRef, m_label);
8981
+
8982
+ let i = FROM_MOZ_LABELS.length;
8983
+ while (i--) {
8984
+ const label_origin = FROM_MOZ_LABELS[i];
8985
+
8986
+ if (label.name === label_origin.name) {
8987
+ label.thedef = label_origin;
8988
+ break;
8989
+ }
8990
+ }
8991
+
8992
+ return label;
8864
8993
  }
8865
8994
 
8866
8995
  AST_Node.from_mozilla_ast = function(node) {
8867
- var save_stack = FROM_MOZ_STACK;
8868
- FROM_MOZ_STACK = [];
8996
+ var save_labels = FROM_MOZ_LABELS;
8997
+ FROM_MOZ_LABELS = [];
8869
8998
  var ast = from_moz(node);
8870
- FROM_MOZ_STACK = save_stack;
8999
+ FROM_MOZ_LABELS = save_labels;
8871
9000
  return ast;
8872
9001
  };
8873
9002
 
@@ -8909,6 +9038,52 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
8909
9038
  return ast;
8910
9039
  }
8911
9040
 
9041
+ /** Object property keys can be number literals, string literals, or raw names. Additionally they can be shorthand. We decide that here. */
9042
+ function to_moz_property_key(key, computed = false, quote = false, value = null) {
9043
+ if (computed) {
9044
+ return [false, to_moz(key)];
9045
+ }
9046
+
9047
+ const key_name = typeof key === "string" ? key : key.name;
9048
+ let moz_key;
9049
+ if (quote) {
9050
+ moz_key = { type: "Literal", value: key_name, raw: JSON.stringify(key_name) };
9051
+ } else if ("" + +key_name === key_name && +key_name >= 0) {
9052
+ // representable as a number
9053
+ moz_key = { type: "Literal", value: +key_name, raw: JSON.stringify(+key_name) };
9054
+ } else {
9055
+ moz_key = { type: "Identifier", name: key_name };
9056
+ }
9057
+
9058
+ const shorthand =
9059
+ moz_key.type === "Identifier"
9060
+ && moz_key.name === key_name
9061
+ && (value instanceof AST_Symbol && value.name === key_name
9062
+ || value instanceof AST_DefaultAssign && value.left.name === key_name);
9063
+ return [shorthand, moz_key];
9064
+ }
9065
+
9066
+ function to_moz_pattern(node) {
9067
+ if (node instanceof AST_Expansion) {
9068
+ return {
9069
+ type: "RestElement",
9070
+ argument: to_moz_pattern(node.expression),
9071
+ };
9072
+ }
9073
+
9074
+ if ((
9075
+ node instanceof AST_Symbol
9076
+ || node instanceof AST_Destructuring
9077
+ || node instanceof AST_DefaultAssign
9078
+ || node instanceof AST_PropAccess
9079
+ )) {
9080
+ // Plain translation
9081
+ return to_moz(node);
9082
+ }
9083
+
9084
+ throw new Error(node.TYPE);
9085
+ }
9086
+
8912
9087
  function to_moz_in_destructuring() {
8913
9088
  var i = TO_MOZ_STACK.length;
8914
9089
  while (i--) {