terser 5.4.0 → 5.6.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/lib/ast.js CHANGED
@@ -1007,7 +1007,7 @@ var AST_PropAccess = DEFNODE("PropAccess", "expression property optional", {
1007
1007
  $documentation: "Base class for property access expressions, i.e. `a.foo` or `a[\"foo\"]`",
1008
1008
  $propdoc: {
1009
1009
  expression: "[AST_Node] the “container” expression",
1010
- property: "[AST_Node|string] the property to access. For AST_Dot this is always a plain string, while for AST_Sub it's an arbitrary AST_Node",
1010
+ property: "[AST_Node|string] the property to access. For AST_Dot & AST_DotHash this is always a plain string, while for AST_Sub it's an arbitrary AST_Node",
1011
1011
 
1012
1012
  optional: "[boolean] whether this is an optional property access (IE ?.)"
1013
1013
  }
@@ -1028,6 +1028,18 @@ var AST_Dot = DEFNODE("Dot", "quote", {
1028
1028
  },
1029
1029
  }, AST_PropAccess);
1030
1030
 
1031
+ var AST_DotHash = DEFNODE("DotHash", "", {
1032
+ $documentation: "A dotted property access to a private property",
1033
+ _walk: function(visitor) {
1034
+ return visitor._visit(this, function() {
1035
+ this.expression._walk(visitor);
1036
+ });
1037
+ },
1038
+ _children_backwards(push) {
1039
+ push(this.expression);
1040
+ },
1041
+ }, AST_PropAccess);
1042
+
1031
1043
  var AST_Sub = DEFNODE("Sub", null, {
1032
1044
  $documentation: "Index-style property access, i.e. `a[\"foo\"]`",
1033
1045
  _walk: function(visitor) {
@@ -1045,7 +1057,7 @@ var AST_Sub = DEFNODE("Sub", null, {
1045
1057
  var AST_Chain = DEFNODE("Chain", "expression", {
1046
1058
  $documentation: "A chain expression like a?.b?.(c)?.[d]",
1047
1059
  $propdoc: {
1048
- expression: "[AST_Call|AST_Dot|AST_Sub] chain element."
1060
+ expression: "[AST_Call|AST_Dot|AST_DotHash|AST_Sub] chain element."
1049
1061
  },
1050
1062
  _walk: function (visitor) {
1051
1063
  return visitor._visit(this, function() {
@@ -1201,6 +1213,26 @@ var AST_ObjectKeyVal = DEFNODE("ObjectKeyVal", "quote", {
1201
1213
  }
1202
1214
  }, AST_ObjectProperty);
1203
1215
 
1216
+ var AST_PrivateSetter = DEFNODE("PrivateSetter", "static", {
1217
+ $propdoc: {
1218
+ static: "[boolean] whether this is a static private setter"
1219
+ },
1220
+ $documentation: "A private setter property",
1221
+ computed_key() {
1222
+ return false;
1223
+ }
1224
+ }, AST_ObjectProperty);
1225
+
1226
+ var AST_PrivateGetter = DEFNODE("PrivateGetter", "static", {
1227
+ $propdoc: {
1228
+ static: "[boolean] whether this is a static private getter"
1229
+ },
1230
+ $documentation: "A private getter property",
1231
+ computed_key() {
1232
+ return false;
1233
+ }
1234
+ }, AST_ObjectProperty);
1235
+
1204
1236
  var AST_ObjectSetter = DEFNODE("ObjectSetter", "quote static", {
1205
1237
  $propdoc: {
1206
1238
  quote: "[string|undefined] the original quote character, if any",
@@ -1236,6 +1268,10 @@ var AST_ConciseMethod = DEFNODE("ConciseMethod", "quote static is_generator asyn
1236
1268
  }
1237
1269
  }, AST_ObjectProperty);
1238
1270
 
1271
+ var AST_PrivateMethod = DEFNODE("PrivateMethod", "", {
1272
+ $documentation: "A private class method inside a class",
1273
+ }, AST_ConciseMethod);
1274
+
1239
1275
  var AST_Class = DEFNODE("Class", "name extends properties", {
1240
1276
  $propdoc: {
1241
1277
  name: "[AST_SymbolClass|AST_SymbolDefClass?] optional class name.",
@@ -1285,6 +1321,10 @@ var AST_ClassProperty = DEFNODE("ClassProperty", "static quote", {
1285
1321
  }
1286
1322
  }, AST_ObjectProperty);
1287
1323
 
1324
+ var AST_ClassPrivateProperty = DEFNODE("ClassProperty", "", {
1325
+ $documentation: "A class property for a private property",
1326
+ }, AST_ClassProperty);
1327
+
1288
1328
  var AST_DefClass = DEFNODE("DefClass", null, {
1289
1329
  $documentation: "A class definition",
1290
1330
  }, AST_Class);
@@ -1684,6 +1724,7 @@ export {
1684
1724
  AST_Chain,
1685
1725
  AST_Class,
1686
1726
  AST_ClassExpression,
1727
+ AST_ClassPrivateProperty,
1687
1728
  AST_ClassProperty,
1688
1729
  AST_ConciseMethod,
1689
1730
  AST_Conditional,
@@ -1700,6 +1741,7 @@ export {
1700
1741
  AST_Directive,
1701
1742
  AST_Do,
1702
1743
  AST_Dot,
1744
+ AST_DotHash,
1703
1745
  AST_DWLoop,
1704
1746
  AST_EmptyStatement,
1705
1747
  AST_Exit,
@@ -1737,6 +1779,9 @@ export {
1737
1779
  AST_ObjectProperty,
1738
1780
  AST_ObjectSetter,
1739
1781
  AST_PrefixedTemplateString,
1782
+ AST_PrivateGetter,
1783
+ AST_PrivateMethod,
1784
+ AST_PrivateSetter,
1740
1785
  AST_PropAccess,
1741
1786
  AST_RegExp,
1742
1787
  AST_Return,
package/lib/cli.js CHANGED
@@ -42,7 +42,7 @@ export async function run_cli({ program, packageJson, fs, path }) {
42
42
  program.option("-m, --mangle [options]", "Mangle names/specify mangler options.", parse_js());
43
43
  program.option("--mangle-props [options]", "Mangle properties/specify mangler options.", parse_js());
44
44
  program.option("-f, --format [options]", "Format options.", parse_js());
45
- program.option("-b, --beautify [options]", "Alias for --format beautify=true.", parse_js());
45
+ program.option("-b, --beautify [options]", "Alias for --format.", parse_js());
46
46
  program.option("-o, --output <file>", "Output file (default STDOUT).");
47
47
  program.option("--comments [filter]", "Preserve copyright comments in the output.");
48
48
  program.option("--config-file <file>", "Read minify() options from JSON file.");
@@ -93,19 +93,9 @@ export async function run_cli({ program, packageJson, fs, path }) {
93
93
  else
94
94
  options.ecma = ecma;
95
95
  }
96
- if (program.beautify || program.format) {
97
- if (program.beautify && program.format) {
98
- fatal("Please only specify one of --beautify or --format");
99
- }
100
- if (program.beautify) {
101
- options.format = typeof program.beautify == "object" ? program.beautify : {};
102
- if (!("beautify" in options.format)) {
103
- options.format.beautify = true;
104
- }
105
- }
106
- if (program.format) {
107
- options.format = typeof program.format == "object" ? program.format : {};
108
- }
96
+ if (program.format || program.beautify) {
97
+ const chosenOption = program.format || program.beautify;
98
+ options.format = typeof chosenOption === "object" ? chosenOption : {};
109
99
  }
110
100
  if (program.comments) {
111
101
  if (typeof options.format != "object") options.format = {};
@@ -3433,7 +3433,7 @@ const pure_prop_access_globals = new Set([
3433
3433
  def_has_side_effects(AST_ObjectProperty, function(compressor) {
3434
3434
  return (
3435
3435
  this.computed_key() && this.key.has_side_effects(compressor)
3436
- || this.value.has_side_effects(compressor)
3436
+ || this.value && this.value.has_side_effects(compressor)
3437
3437
  );
3438
3438
  });
3439
3439
  def_has_side_effects(AST_ClassProperty, function(compressor) {
@@ -3561,7 +3561,7 @@ const pure_prop_access_globals = new Set([
3561
3561
  });
3562
3562
  def_may_throw(AST_ObjectProperty, function(compressor) {
3563
3563
  // TODO key may throw too
3564
- return this.value.may_throw(compressor);
3564
+ return this.value ? this.value.may_throw(compressor) : false;
3565
3565
  });
3566
3566
  def_may_throw(AST_ClassProperty, function(compressor) {
3567
3567
  return (
@@ -3695,7 +3695,7 @@ const pure_prop_access_globals = new Set([
3695
3695
  return this.properties.every((l) => l.is_constant_expression());
3696
3696
  });
3697
3697
  def_is_constant_expression(AST_ObjectProperty, function() {
3698
- return !(this.key instanceof AST_Node) && this.value.is_constant_expression();
3698
+ return !!(!(this.key instanceof AST_Node) && this.value && this.value.is_constant_expression());
3699
3699
  });
3700
3700
  })(function(node, func) {
3701
3701
  node.DEFMETHOD("is_constant_expression", func);
@@ -4522,7 +4522,7 @@ AST_Scope.DEFMETHOD("hoist_properties", function(compressor) {
4522
4522
  def_drop_side_effect_free(AST_ObjectProperty, function(compressor, first_in_statement) {
4523
4523
  const computed_key = this instanceof AST_ObjectKeyVal && this.key instanceof AST_Node;
4524
4524
  const key = computed_key && this.key.drop_side_effect_free(compressor, first_in_statement);
4525
- const value = this.value.drop_side_effect_free(compressor, first_in_statement);
4525
+ const value = this.value && this.value.drop_side_effect_free(compressor, first_in_statement);
4526
4526
  if (key && value) {
4527
4527
  return make_sequence(this, [key, value]);
4528
4528
  }
@@ -6245,7 +6245,7 @@ def_optimize(AST_Binary, function(self, compressor) {
6245
6245
  var l = self.left;
6246
6246
  var r = self.right.evaluate(compressor);
6247
6247
  if (r != self.right) {
6248
- l.segments[l.segments.length - 1].value += r.toString();
6248
+ l.segments[l.segments.length - 1].value += String(r);
6249
6249
  return l;
6250
6250
  }
6251
6251
  }
@@ -6254,7 +6254,7 @@ def_optimize(AST_Binary, function(self, compressor) {
6254
6254
  var r = self.right;
6255
6255
  var l = self.left.evaluate(compressor);
6256
6256
  if (l != self.left) {
6257
- r.segments[0].value = l.toString() + r.segments[0].value ;
6257
+ r.segments[0].value = String(l) + r.segments[0].value;
6258
6258
  return r;
6259
6259
  }
6260
6260
  }
@@ -63,6 +63,7 @@ import {
63
63
  AST_Class,
64
64
  AST_ClassExpression,
65
65
  AST_ClassProperty,
66
+ AST_ClassPrivateProperty,
66
67
  AST_ConciseMethod,
67
68
  AST_Conditional,
68
69
  AST_Const,
@@ -78,6 +79,7 @@ import {
78
79
  AST_Directive,
79
80
  AST_Do,
80
81
  AST_Dot,
82
+ AST_DotHash,
81
83
  AST_EmptyStatement,
82
84
  AST_Expansion,
83
85
  AST_Export,
@@ -108,6 +110,9 @@ import {
108
110
  AST_ObjectProperty,
109
111
  AST_ObjectSetter,
110
112
  AST_PrefixedTemplateString,
113
+ AST_PrivateGetter,
114
+ AST_PrivateMethod,
115
+ AST_PrivateSetter,
111
116
  AST_PropAccess,
112
117
  AST_RegExp,
113
118
  AST_Return,
@@ -386,6 +391,23 @@ import {
386
391
  static : M.static,
387
392
  });
388
393
  },
394
+ PropertyDefinition: function(M) {
395
+ let key;
396
+ if (M.computed) {
397
+ key = from_moz(M.key);
398
+ } else {
399
+ if (M.key.type !== "Identifier") throw new Error("Non-Identifier key in PropertyDefinition");
400
+ key = from_moz(M.key);
401
+ }
402
+
403
+ return new AST_ClassProperty({
404
+ start : my_start_token(M),
405
+ end : my_end_token(M),
406
+ key,
407
+ value : from_moz(M.value),
408
+ static : M.static,
409
+ });
410
+ },
389
411
  ArrayExpression: function(M) {
390
412
  return new AST_Array({
391
413
  start : my_start_token(M),
@@ -574,7 +596,7 @@ import {
574
596
  : p.type == "ArrowFunctionExpression" ? (p.params.includes(M)) ? AST_SymbolFunarg : AST_SymbolRef
575
597
  : p.type == "ClassExpression" ? (p.id === M ? AST_SymbolClass : AST_SymbolRef)
576
598
  : p.type == "Property" ? (p.key === M && p.computed || p.value === M ? AST_SymbolRef : AST_SymbolMethod)
577
- : p.type == "FieldDefinition" ? (p.key === M && p.computed || p.value === M ? AST_SymbolRef : AST_SymbolClassProperty)
599
+ : p.type == "PropertyDefinition" || p.type === "FieldDefinition" ? (p.key === M && p.computed || p.value === M ? AST_SymbolRef : AST_SymbolClassProperty)
578
600
  : p.type == "ClassDeclaration" ? (p.id === M ? AST_SymbolDefClass : AST_SymbolRef)
579
601
  : p.type == "MethodDefinition" ? (p.computed ? AST_SymbolRef : AST_SymbolMethod)
580
602
  : p.type == "CatchClause" ? AST_SymbolCatch
@@ -873,6 +895,19 @@ import {
873
895
  };
874
896
  });
875
897
 
898
+ def_to_moz(AST_DotHash, function To_Moz_PrivateMemberExpression(M) {
899
+ return {
900
+ type: "MemberExpression",
901
+ object: to_moz(M.expression),
902
+ computed: false,
903
+ property: {
904
+ type: "PrivateIdentifier",
905
+ name: M.property
906
+ },
907
+ optional: M.optional
908
+ };
909
+ });
910
+
876
911
  def_to_moz(AST_PropAccess, function To_Moz_MemberExpression(M) {
877
912
  var isComputed = M instanceof AST_Sub;
878
913
  return {
@@ -965,12 +1000,38 @@ import {
965
1000
  if (M instanceof AST_ObjectSetter) {
966
1001
  kind = "set";
967
1002
  }
1003
+ if (M instanceof AST_PrivateGetter || M instanceof AST_PrivateSetter) {
1004
+ const kind = M instanceof AST_PrivateGetter ? "get" : "set";
1005
+ return {
1006
+ type: "MethodDefinition",
1007
+ computed: false,
1008
+ kind: kind,
1009
+ static: M.static,
1010
+ key: {
1011
+ type: "PrivateIdentifier",
1012
+ name: M.key.name
1013
+ },
1014
+ value: to_moz(M.value)
1015
+ };
1016
+ }
1017
+ if (M instanceof AST_ClassPrivateProperty) {
1018
+ return {
1019
+ type: "PropertyDefinition",
1020
+ key: {
1021
+ type: "PrivateIdentifier",
1022
+ name: M.key.name
1023
+ },
1024
+ value: to_moz(M.value),
1025
+ computed: false,
1026
+ static: M.static
1027
+ };
1028
+ }
968
1029
  if (M instanceof AST_ClassProperty) {
969
1030
  return {
970
- type: "FieldDefinition",
971
- computed,
1031
+ type: "PropertyDefinition",
972
1032
  key,
973
1033
  value: to_moz(M.value),
1034
+ computed,
974
1035
  static: M.static
975
1036
  };
976
1037
  }
@@ -1005,13 +1066,21 @@ import {
1005
1066
  value: to_moz(M.value)
1006
1067
  };
1007
1068
  }
1069
+
1070
+ const key = M instanceof AST_PrivateMethod
1071
+ ? {
1072
+ type: "PrivateIdentifier",
1073
+ name: M.key.name
1074
+ }
1075
+ : to_moz(M.key);
1076
+
1008
1077
  return {
1009
1078
  type: "MethodDefinition",
1010
- computed: !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef,
1011
1079
  kind: M.key === "constructor" ? "constructor" : "method",
1080
+ key,
1081
+ value: to_moz(M.value),
1082
+ computed: !(M.key instanceof AST_Symbol) || M.key instanceof AST_SymbolRef,
1012
1083
  static: M.static,
1013
- key: to_moz(M.key),
1014
- value: to_moz(M.value)
1015
1084
  };
1016
1085
  });
1017
1086
 
package/lib/output.js CHANGED
@@ -68,8 +68,12 @@ import {
68
68
  AST_Chain,
69
69
  AST_Class,
70
70
  AST_ClassExpression,
71
+ AST_ClassPrivateProperty,
71
72
  AST_ClassProperty,
72
73
  AST_ConciseMethod,
74
+ AST_PrivateGetter,
75
+ AST_PrivateMethod,
76
+ AST_PrivateSetter,
73
77
  AST_Conditional,
74
78
  AST_Const,
75
79
  AST_Constant,
@@ -83,6 +87,7 @@ import {
83
87
  AST_Directive,
84
88
  AST_Do,
85
89
  AST_Dot,
90
+ AST_DotHash,
86
91
  AST_EmptyStatement,
87
92
  AST_Exit,
88
93
  AST_Expansion,
@@ -239,7 +244,7 @@ function OutputStream(options) {
239
244
  let printed_comments = new Set();
240
245
 
241
246
  var to_utf8 = options.ascii_only ? function(str, identifier) {
242
- if (options.ecma >= 2015) {
247
+ if (options.ecma >= 2015 && !options.safari10) {
243
248
  str = str.replace(/[\ud800-\udbff][\udc00-\udfff]/g, function(ch) {
244
249
  var code = get_full_char_code(ch, 0).toString(16);
245
250
  return "\\u{" + code + "}";
@@ -1779,7 +1784,10 @@ function OutputStream(options) {
1779
1784
  var prop = self.property;
1780
1785
  var print_computed = RESERVED_WORDS.has(prop)
1781
1786
  ? output.option("ie8")
1782
- : !is_identifier_string(prop, output.option("ecma") >= 2015);
1787
+ : !is_identifier_string(
1788
+ prop,
1789
+ output.option("ecma") >= 2015 || output.option("safari10")
1790
+ );
1783
1791
 
1784
1792
  if (self.optional) output.print("?.");
1785
1793
 
@@ -1800,6 +1808,15 @@ function OutputStream(options) {
1800
1808
  output.print_name(prop);
1801
1809
  }
1802
1810
  });
1811
+ DEFPRINT(AST_DotHash, function(self, output) {
1812
+ var expr = self.expression;
1813
+ expr.print(output);
1814
+ var prop = self.property;
1815
+
1816
+ if (self.optional) output.print("?");
1817
+ output.print(".#");
1818
+ output.print_name(prop);
1819
+ });
1803
1820
  DEFPRINT(AST_Sub, function(self, output) {
1804
1821
  self.expression.print(output);
1805
1822
  if (self.optional) output.print("?.");
@@ -1949,7 +1966,7 @@ function OutputStream(options) {
1949
1966
  var print_string = RESERVED_WORDS.has(key)
1950
1967
  ? output.option("ie8")
1951
1968
  : (
1952
- output.option("ecma") < 2015
1969
+ output.option("ecma") < 2015 || output.option("safari10")
1953
1970
  ? !is_basic_identifier_string(key)
1954
1971
  : !is_identifier_string(key, true)
1955
1972
  );
@@ -1968,7 +1985,10 @@ function OutputStream(options) {
1968
1985
  var allowShortHand = output.option("shorthand");
1969
1986
  if (allowShortHand &&
1970
1987
  self.value instanceof AST_Symbol &&
1971
- is_identifier_string(self.key, output.option("ecma") >= 2015) &&
1988
+ is_identifier_string(
1989
+ self.key,
1990
+ output.option("ecma") >= 2015 || output.option("safari10")
1991
+ ) &&
1972
1992
  get_name(self.value) === self.key &&
1973
1993
  !RESERVED_WORDS.has(self.key)
1974
1994
  ) {
@@ -1977,7 +1997,10 @@ function OutputStream(options) {
1977
1997
  } else if (allowShortHand &&
1978
1998
  self.value instanceof AST_DefaultAssign &&
1979
1999
  self.value.left instanceof AST_Symbol &&
1980
- is_identifier_string(self.key, output.option("ecma") >= 2015) &&
2000
+ is_identifier_string(
2001
+ self.key,
2002
+ output.option("ecma") >= 2015 || output.option("safari10")
2003
+ ) &&
1981
2004
  get_name(self.value.left) === self.key
1982
2005
  ) {
1983
2006
  print_property_name(self.key, self.quote, output);
@@ -1997,6 +2020,23 @@ function OutputStream(options) {
1997
2020
  self.value.print(output);
1998
2021
  }
1999
2022
  });
2023
+ DEFPRINT(AST_ClassPrivateProperty, (self, output) => {
2024
+ if (self.static) {
2025
+ output.print("static");
2026
+ output.space();
2027
+ }
2028
+
2029
+ output.print("#");
2030
+
2031
+ print_property_name(self.key.name, self.quote, output);
2032
+
2033
+ if (self.value) {
2034
+ output.print("=");
2035
+ self.value.print(output);
2036
+ }
2037
+
2038
+ output.semicolon();
2039
+ });
2000
2040
  DEFPRINT(AST_ClassProperty, (self, output) => {
2001
2041
  if (self.static) {
2002
2042
  output.print("static");
@@ -2018,7 +2058,7 @@ function OutputStream(options) {
2018
2058
 
2019
2059
  output.semicolon();
2020
2060
  });
2021
- AST_ObjectProperty.DEFMETHOD("_print_getter_setter", function(type, output) {
2061
+ AST_ObjectProperty.DEFMETHOD("_print_getter_setter", function(type, is_private, output) {
2022
2062
  var self = this;
2023
2063
  if (self.static) {
2024
2064
  output.print("static");
@@ -2029,6 +2069,7 @@ function OutputStream(options) {
2029
2069
  output.space();
2030
2070
  }
2031
2071
  if (self.key instanceof AST_SymbolMethod) {
2072
+ if (is_private) output.print("#");
2032
2073
  print_property_name(self.key.name, self.quote, output);
2033
2074
  } else {
2034
2075
  output.with_square(function() {
@@ -2038,10 +2079,27 @@ function OutputStream(options) {
2038
2079
  self.value._do_print(output, true);
2039
2080
  });
2040
2081
  DEFPRINT(AST_ObjectSetter, function(self, output) {
2041
- self._print_getter_setter("set", output);
2082
+ self._print_getter_setter("set", false, output);
2042
2083
  });
2043
2084
  DEFPRINT(AST_ObjectGetter, function(self, output) {
2044
- self._print_getter_setter("get", output);
2085
+ self._print_getter_setter("get", false, output);
2086
+ });
2087
+ DEFPRINT(AST_PrivateSetter, function(self, output) {
2088
+ self._print_getter_setter("set", true, output);
2089
+ });
2090
+ DEFPRINT(AST_PrivateGetter, function(self, output) {
2091
+ self._print_getter_setter("get", true, output);
2092
+ });
2093
+ DEFPRINT(AST_PrivateMethod, function(self, output) {
2094
+ var type;
2095
+ if (self.is_generator && self.async) {
2096
+ type = "async*";
2097
+ } else if (self.is_generator) {
2098
+ type = "*";
2099
+ } else if (self.async) {
2100
+ type = "async";
2101
+ }
2102
+ self._print_getter_setter(type, true, output);
2045
2103
  });
2046
2104
  DEFPRINT(AST_ConciseMethod, function(self, output) {
2047
2105
  var type;
@@ -2052,7 +2110,7 @@ function OutputStream(options) {
2052
2110
  } else if (self.async) {
2053
2111
  type = "async";
2054
2112
  }
2055
- self._print_getter_setter(type, output);
2113
+ self._print_getter_setter(type, false, output);
2056
2114
  });
2057
2115
  AST_Symbol.DEFMETHOD("_do_print", function(output) {
2058
2116
  var def = this.definition();