terser 5.31.5 → 5.32.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 CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## v5.32.0
4
+
5
+ - `import("module")` can now be input and output from ESTree AST (#1557)
6
+ - `BigInt` literals can now be input and output from ESTree AST (#1555)
7
+ - `typeof` an object or array (`typeof {}` and `typeof []`) can now be statically evaluated. (#1546)
8
+
9
+ ## v5.31.6
10
+ - Retain side effects in a `case` when the expression is a sequence (comma) expression
11
+
3
12
  ## v5.31.5
4
13
  - Revert v5.31.4, which created mysterious issues #1548, #1549
5
14
 
@@ -2,7 +2,7 @@
2
2
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@jridgewell/source-map')) :
3
3
  typeof define === 'function' && define.amd ? define(['exports', '@jridgewell/source-map'], factory) :
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Terser = {}, global.sourceMap));
5
- })(this, (function (exports, sourceMap) { 'use strict';
5
+ }(this, (function (exports, sourceMap) { 'use strict';
6
6
 
7
7
  /***********************************************************************
8
8
 
@@ -7571,6 +7571,19 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7571
7571
  });
7572
7572
  },
7573
7573
 
7574
+ ImportExpression: function(M) {
7575
+ return new AST_Call({
7576
+ start: my_start_token(M),
7577
+ end: my_end_token(M),
7578
+ expression: from_moz({
7579
+ type: "Identifier",
7580
+ name: "import"
7581
+ }),
7582
+ optional: false,
7583
+ args: [from_moz(M.source)]
7584
+ });
7585
+ },
7586
+
7574
7587
  ExportAllDeclaration: function(M) {
7575
7588
  var foreign_name = M.exported == null ?
7576
7589
  new AST_SymbolExportForeign({ name: "*" }) :
@@ -7638,6 +7651,11 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7638
7651
  args.value = { source, flags };
7639
7652
  return new AST_RegExp(args);
7640
7653
  }
7654
+ const bi = typeof M.value === "bigint" ? M.value.toString() : M.bigint;
7655
+ if (typeof bi === "string") {
7656
+ args.value = bi;
7657
+ return new AST_BigInt(args);
7658
+ }
7641
7659
  if (val === null) return new AST_Null(args);
7642
7660
  switch (typeof val) {
7643
7661
  case "string":
@@ -7705,14 +7723,6 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
7705
7723
  });
7706
7724
  },
7707
7725
 
7708
- BigIntLiteral(M) {
7709
- return new AST_BigInt({
7710
- start : my_start_token(M),
7711
- end : my_end_token(M),
7712
- value : M.value
7713
- });
7714
- },
7715
-
7716
7726
  EmptyStatement: function(M) {
7717
7727
  return new AST_EmptyStatement({
7718
7728
  start: my_start_token(M),
@@ -8185,6 +8195,14 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
8185
8195
  };
8186
8196
  });
8187
8197
  def_to_moz(AST_Call, function To_Moz_CallExpression(M) {
8198
+ if (M.expression instanceof AST_SymbolRef && M.expression.name === "import") {
8199
+ const [source] = M.args.map(to_moz);
8200
+ return {
8201
+ type: "ImportExpression",
8202
+ source,
8203
+ };
8204
+ }
8205
+
8188
8206
  return {
8189
8207
  type: "CallExpression",
8190
8208
  callee: to_moz(M.expression),
@@ -8730,8 +8748,13 @@ def_transform(AST_PrefixedTemplateString, function(self, tw) {
8730
8748
  });
8731
8749
 
8732
8750
  def_to_moz(AST_BigInt, M => ({
8733
- type: "BigIntLiteral",
8734
- value: M.value
8751
+ type: "Literal",
8752
+ // value cannot be represented natively
8753
+ // see: https://github.com/estree/estree/blob/master/es2020.md#bigintliteral
8754
+ value: null,
8755
+ // `M.value` is a string that may be a hex number representation.
8756
+ // but "bigint" property should have only decimal digits
8757
+ bigint: typeof BigInt === "function" ? BigInt(M.value).toString() : M.value,
8735
8758
  }));
8736
8759
 
8737
8760
  AST_Boolean.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
@@ -13836,7 +13859,7 @@ function is_nullish(node, compressor) {
13836
13859
  return any(this.definitions, compressor);
13837
13860
  });
13838
13861
  def_has_side_effects(AST_VarDef, function() {
13839
- return this.value;
13862
+ return this.value != null;
13840
13863
  });
13841
13864
  def_has_side_effects(AST_TemplateSegment, return_false);
13842
13865
  def_has_side_effects(AST_TemplateString, function(compressor) {
@@ -14579,14 +14602,25 @@ def_eval(AST_Object, function (compressor, depth) {
14579
14602
  var non_converting_unary = makePredicate("! typeof void");
14580
14603
  def_eval(AST_UnaryPrefix, function (compressor, depth) {
14581
14604
  var e = this.expression;
14582
- // Function would be evaluated to an array and so typeof would
14583
- // incorrectly return 'object'. Hence making is a special case.
14584
14605
  if (compressor.option("typeofs")
14585
- && this.operator == "typeof"
14586
- && (e instanceof AST_Lambda
14606
+ && this.operator == "typeof") {
14607
+ // Function would be evaluated to an array and so typeof would
14608
+ // incorrectly return 'object'. Hence making is a special case.
14609
+ if (e instanceof AST_Lambda
14587
14610
  || e instanceof AST_SymbolRef
14588
- && e.fixed_value() instanceof AST_Lambda)) {
14589
- return typeof function () { };
14611
+ && e.fixed_value() instanceof AST_Lambda) {
14612
+ return typeof function () { };
14613
+ }
14614
+ if (
14615
+ (e instanceof AST_Object
14616
+ || e instanceof AST_Array
14617
+ || (e instanceof AST_SymbolRef
14618
+ && (e.fixed_value() instanceof AST_Object
14619
+ || e.fixed_value() instanceof AST_Array)))
14620
+ && !e.has_side_effects(compressor)
14621
+ ) {
14622
+ return typeof {};
14623
+ }
14590
14624
  }
14591
14625
  if (!non_converting_unary.has(this.operator))
14592
14626
  depth++;
@@ -19588,7 +19622,9 @@ def_optimize(AST_Switch, function(self, compressor) {
19588
19622
  eliminate_branch(branch, body[body.length - 1]);
19589
19623
  continue;
19590
19624
  }
19591
- if (exp instanceof AST_Node) exp = branch.expression.tail_node().evaluate(compressor);
19625
+ if (exp instanceof AST_Node && !exp.has_side_effects(compressor)) {
19626
+ exp = branch.expression.tail_node().evaluate(compressor);
19627
+ }
19592
19628
  if (exp === value) {
19593
19629
  exact_match = branch;
19594
19630
  if (default_branch) {
@@ -19770,12 +19806,9 @@ def_optimize(AST_Switch, function(self, compressor) {
19770
19806
  break DEFAULT;
19771
19807
  }
19772
19808
 
19773
- let sideEffect = body.find(branch => {
19774
- return (
19775
- branch !== default_or_exact
19776
- && branch.expression.has_side_effects(compressor)
19777
- );
19778
- });
19809
+ let sideEffect = body.find(
19810
+ branch => branch !== default_or_exact && branch.expression.has_side_effects(compressor)
19811
+ );
19779
19812
  // If no cases cause a side-effect, we can eliminate the switch entirely.
19780
19813
  if (!sideEffect) {
19781
19814
  return make_node(AST_BlockStatement, self, {
@@ -19883,9 +19916,10 @@ def_optimize(AST_Switch, function(self, compressor) {
19883
19916
  right: branch.expression,
19884
19917
  }),
19885
19918
  body: consequent,
19886
- alternative: null
19887
- })
19888
- ].concat(always)
19919
+ alternative: null,
19920
+ }),
19921
+ always,
19922
+ ],
19889
19923
  }).optimize(compressor);
19890
19924
  }
19891
19925
  return self;
@@ -19908,13 +19942,12 @@ def_optimize(AST_Switch, function(self, compressor) {
19908
19942
  let pblock = make_node(AST_BlockStatement, prev, { body: pbody });
19909
19943
  return bblock.equivalent_to(pblock);
19910
19944
  }
19911
- function statement(expression) {
19912
- return make_node(AST_SimpleStatement, expression, {
19913
- body: expression
19914
- });
19945
+ function statement(body) {
19946
+ return make_node(AST_SimpleStatement, body, { body });
19915
19947
  }
19916
19948
  function has_nested_break(root) {
19917
19949
  let has_break = false;
19950
+
19918
19951
  let tw = new TreeWalker(node => {
19919
19952
  if (has_break) return true;
19920
19953
  if (node instanceof AST_Lambda) return true;
@@ -32547,4 +32580,4 @@ exports._run_cli = run_cli;
32547
32580
  exports.minify = minify;
32548
32581
  exports.minify_sync = minify_sync;
32549
32582
 
32550
- }));
32583
+ })));
@@ -218,14 +218,25 @@ def_eval(AST_Object, function (compressor, depth) {
218
218
  var non_converting_unary = makePredicate("! typeof void");
219
219
  def_eval(AST_UnaryPrefix, function (compressor, depth) {
220
220
  var e = this.expression;
221
- // Function would be evaluated to an array and so typeof would
222
- // incorrectly return 'object'. Hence making is a special case.
223
221
  if (compressor.option("typeofs")
224
- && this.operator == "typeof"
225
- && (e instanceof AST_Lambda
222
+ && this.operator == "typeof") {
223
+ // Function would be evaluated to an array and so typeof would
224
+ // incorrectly return 'object'. Hence making is a special case.
225
+ if (e instanceof AST_Lambda
226
226
  || e instanceof AST_SymbolRef
227
- && e.fixed_value() instanceof AST_Lambda)) {
228
- return typeof function () { };
227
+ && e.fixed_value() instanceof AST_Lambda) {
228
+ return typeof function () { };
229
+ }
230
+ if (
231
+ (e instanceof AST_Object
232
+ || e instanceof AST_Array
233
+ || (e instanceof AST_SymbolRef
234
+ && (e.fixed_value() instanceof AST_Object
235
+ || e.fixed_value() instanceof AST_Array)))
236
+ && !e.has_side_effects(compressor)
237
+ ) {
238
+ return typeof {};
239
+ }
229
240
  }
230
241
  if (!non_converting_unary.has(this.operator))
231
242
  depth++;
@@ -1233,7 +1233,9 @@ def_optimize(AST_Switch, function(self, compressor) {
1233
1233
  eliminate_branch(branch, body[body.length - 1]);
1234
1234
  continue;
1235
1235
  }
1236
- if (exp instanceof AST_Node) exp = branch.expression.tail_node().evaluate(compressor);
1236
+ if (exp instanceof AST_Node && !exp.has_side_effects(compressor)) {
1237
+ exp = branch.expression.tail_node().evaluate(compressor);
1238
+ }
1237
1239
  if (exp === value) {
1238
1240
  exact_match = branch;
1239
1241
  if (default_branch) {
@@ -1415,12 +1417,9 @@ def_optimize(AST_Switch, function(self, compressor) {
1415
1417
  break DEFAULT;
1416
1418
  }
1417
1419
 
1418
- let sideEffect = body.find(branch => {
1419
- return (
1420
- branch !== default_or_exact
1421
- && branch.expression.has_side_effects(compressor)
1422
- );
1423
- });
1420
+ let sideEffect = body.find(
1421
+ branch => branch !== default_or_exact && branch.expression.has_side_effects(compressor)
1422
+ );
1424
1423
  // If no cases cause a side-effect, we can eliminate the switch entirely.
1425
1424
  if (!sideEffect) {
1426
1425
  return make_node(AST_BlockStatement, self, {
@@ -1528,9 +1527,10 @@ def_optimize(AST_Switch, function(self, compressor) {
1528
1527
  right: branch.expression,
1529
1528
  }),
1530
1529
  body: consequent,
1531
- alternative: null
1532
- })
1533
- ].concat(always)
1530
+ alternative: null,
1531
+ }),
1532
+ always,
1533
+ ],
1534
1534
  }).optimize(compressor);
1535
1535
  }
1536
1536
  return self;
@@ -1553,13 +1553,12 @@ def_optimize(AST_Switch, function(self, compressor) {
1553
1553
  let pblock = make_node(AST_BlockStatement, prev, { body: pbody });
1554
1554
  return bblock.equivalent_to(pblock);
1555
1555
  }
1556
- function statement(expression) {
1557
- return make_node(AST_SimpleStatement, expression, {
1558
- body: expression
1559
- });
1556
+ function statement(body) {
1557
+ return make_node(AST_SimpleStatement, body, { body });
1560
1558
  }
1561
1559
  function has_nested_break(root) {
1562
1560
  let has_break = false;
1561
+
1563
1562
  let tw = new TreeWalker(node => {
1564
1563
  if (has_break) return true;
1565
1564
  if (node instanceof AST_Lambda) return true;
@@ -424,7 +424,7 @@ export function is_nullish(node, compressor) {
424
424
  return any(this.definitions, compressor);
425
425
  });
426
426
  def_has_side_effects(AST_VarDef, function() {
427
- return this.value;
427
+ return this.value != null;
428
428
  });
429
429
  def_has_side_effects(AST_TemplateSegment, return_false);
430
430
  def_has_side_effects(AST_TemplateString, function(compressor) {
@@ -595,6 +595,19 @@ import { is_basic_identifier_string } from "./parse.js";
595
595
  });
596
596
  },
597
597
 
598
+ ImportExpression: function(M) {
599
+ return new AST_Call({
600
+ start: my_start_token(M),
601
+ end: my_end_token(M),
602
+ expression: from_moz({
603
+ type: "Identifier",
604
+ name: "import"
605
+ }),
606
+ optional: false,
607
+ args: [from_moz(M.source)]
608
+ });
609
+ },
610
+
598
611
  ExportAllDeclaration: function(M) {
599
612
  var foreign_name = M.exported == null ?
600
613
  new AST_SymbolExportForeign({ name: "*" }) :
@@ -662,6 +675,11 @@ import { is_basic_identifier_string } from "./parse.js";
662
675
  args.value = { source, flags };
663
676
  return new AST_RegExp(args);
664
677
  }
678
+ const bi = typeof M.value === "bigint" ? M.value.toString() : M.bigint;
679
+ if (typeof bi === "string") {
680
+ args.value = bi;
681
+ return new AST_BigInt(args);
682
+ }
665
683
  if (val === null) return new AST_Null(args);
666
684
  switch (typeof val) {
667
685
  case "string":
@@ -729,14 +747,6 @@ import { is_basic_identifier_string } from "./parse.js";
729
747
  });
730
748
  },
731
749
 
732
- BigIntLiteral(M) {
733
- return new AST_BigInt({
734
- start : my_start_token(M),
735
- end : my_end_token(M),
736
- value : M.value
737
- });
738
- },
739
-
740
750
  EmptyStatement: function(M) {
741
751
  return new AST_EmptyStatement({
742
752
  start: my_start_token(M),
@@ -1209,6 +1219,14 @@ import { is_basic_identifier_string } from "./parse.js";
1209
1219
  };
1210
1220
  });
1211
1221
  def_to_moz(AST_Call, function To_Moz_CallExpression(M) {
1222
+ if (M.expression instanceof AST_SymbolRef && M.expression.name === "import") {
1223
+ const [source] = M.args.map(to_moz);
1224
+ return {
1225
+ type: "ImportExpression",
1226
+ source,
1227
+ };
1228
+ }
1229
+
1212
1230
  return {
1213
1231
  type: "CallExpression",
1214
1232
  callee: to_moz(M.expression),
@@ -1754,8 +1772,13 @@ import { is_basic_identifier_string } from "./parse.js";
1754
1772
  });
1755
1773
 
1756
1774
  def_to_moz(AST_BigInt, M => ({
1757
- type: "BigIntLiteral",
1758
- value: M.value
1775
+ type: "Literal",
1776
+ // value cannot be represented natively
1777
+ // see: https://github.com/estree/estree/blob/master/es2020.md#bigintliteral
1778
+ value: null,
1779
+ // `M.value` is a string that may be a hex number representation.
1780
+ // but "bigint" property should have only decimal digits
1781
+ bigint: typeof BigInt === "function" ? BigInt(M.value).toString() : M.value,
1759
1782
  }));
1760
1783
 
1761
1784
  AST_Boolean.DEFMETHOD("to_mozilla_ast", AST_Constant.prototype.to_mozilla_ast);
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "homepage": "https://terser.org",
5
5
  "author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
6
6
  "license": "BSD-2-Clause",
7
- "version": "5.31.5",
7
+ "version": "5.32.0",
8
8
  "engines": {
9
9
  "node": ">=10"
10
10
  },