terser 5.16.1 → 5.16.3

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.
@@ -529,24 +529,11 @@ import { is_basic_identifier_string } from "./parse.js";
529
529
  var imported_name = null;
530
530
  var imported_names = null;
531
531
  M.specifiers.forEach(function (specifier) {
532
- if (specifier.type === "ImportSpecifier") {
532
+ if (specifier.type === "ImportSpecifier" || specifier.type === "ImportNamespaceSpecifier") {
533
533
  if (!imported_names) { imported_names = []; }
534
- imported_names.push(new AST_NameMapping({
535
- start: my_start_token(specifier),
536
- end: my_end_token(specifier),
537
- foreign_name: from_moz(specifier.imported),
538
- name: from_moz(specifier.local)
539
- }));
534
+ imported_names.push(from_moz(specifier));
540
535
  } else if (specifier.type === "ImportDefaultSpecifier") {
541
- imported_name = from_moz(specifier.local);
542
- } else if (specifier.type === "ImportNamespaceSpecifier") {
543
- if (!imported_names) { imported_names = []; }
544
- imported_names.push(new AST_NameMapping({
545
- start: my_start_token(specifier),
546
- end: my_end_token(specifier),
547
- foreign_name: new AST_SymbolImportForeign({ name: "*" }),
548
- name: from_moz(specifier.local)
549
- }));
536
+ imported_name = from_moz(specifier);
550
537
  }
551
538
  });
552
539
  return new AST_Import({
@@ -559,14 +546,39 @@ import { is_basic_identifier_string } from "./parse.js";
559
546
  });
560
547
  },
561
548
 
549
+ ImportSpecifier: function(M) {
550
+ return new AST_NameMapping({
551
+ start: my_start_token(M),
552
+ end: my_end_token(M),
553
+ foreign_name: from_moz(M.imported),
554
+ name: from_moz(M.local)
555
+ });
556
+ },
557
+
558
+ ImportDefaultSpecifier: function(M) {
559
+ return from_moz(M.local);
560
+ },
561
+
562
+ ImportNamespaceSpecifier: function(M) {
563
+ return new AST_NameMapping({
564
+ start: my_start_token(M),
565
+ end: my_end_token(M),
566
+ foreign_name: new AST_SymbolImportForeign({ name: "*" }),
567
+ name: from_moz(M.local)
568
+ });
569
+ },
570
+
562
571
  ExportAllDeclaration: function(M) {
572
+ var foreign_name = M.exported == null ?
573
+ new AST_SymbolExportForeign({ name: "*" }) :
574
+ from_moz(M.exported);
563
575
  return new AST_Export({
564
576
  start: my_start_token(M),
565
577
  end: my_end_token(M),
566
578
  exported_names: [
567
579
  new AST_NameMapping({
568
580
  name: new AST_SymbolExportForeign({ name: "*" }),
569
- foreign_name: new AST_SymbolExportForeign({ name: "*" })
581
+ foreign_name: foreign_name
570
582
  })
571
583
  ],
572
584
  module_name: from_moz(M.source),
@@ -580,10 +592,7 @@ import { is_basic_identifier_string } from "./parse.js";
580
592
  end: my_end_token(M),
581
593
  exported_definition: from_moz(M.declaration),
582
594
  exported_names: M.specifiers && M.specifiers.length ? M.specifiers.map(function (specifier) {
583
- return new AST_NameMapping({
584
- foreign_name: from_moz(specifier.exported),
585
- name: from_moz(specifier.local)
586
- });
595
+ return from_moz(specifier);
587
596
  }) : null,
588
597
  module_name: from_moz(M.source),
589
598
  assert_clause: assert_clause_from_moz(M.assertions)
@@ -599,6 +608,13 @@ import { is_basic_identifier_string } from "./parse.js";
599
608
  });
600
609
  },
601
610
 
611
+ ExportSpecifier: function(M) {
612
+ return new AST_NameMapping({
613
+ foreign_name: from_moz(M.exported),
614
+ name: from_moz(M.local)
615
+ });
616
+ },
617
+
602
618
  Literal: function(M) {
603
619
  var val = M.value, args = {
604
620
  start : my_start_token(M),
@@ -624,6 +640,22 @@ import { is_basic_identifier_string } from "./parse.js";
624
640
  if (val === null) return new AST_Null(args);
625
641
  switch (typeof val) {
626
642
  case "string":
643
+ args.quote = "\"";
644
+ var p = FROM_MOZ_STACK[FROM_MOZ_STACK.length - 2];
645
+ if (p.type == "ImportSpecifier") {
646
+ args.name = val;
647
+ return new AST_SymbolImportForeign(args);
648
+ } else if (p.type == "ExportSpecifier") {
649
+ args.name = val;
650
+ if (M == p.exported) {
651
+ return new AST_SymbolExportForeign(args);
652
+ } else {
653
+ return new AST_SymbolExport(args);
654
+ }
655
+ } else if (p.type == "ExportAllDeclaration" && M == p.exported) {
656
+ args.name = val;
657
+ return new AST_SymbolExportForeign(args);
658
+ }
627
659
  args.value = val;
628
660
  return new AST_String(args);
629
661
  case "number":
@@ -1328,10 +1360,17 @@ import { is_basic_identifier_string } from "./parse.js";
1328
1360
 
1329
1361
  def_to_moz(AST_Export, function To_Moz_ExportDeclaration(M) {
1330
1362
  if (M.exported_names) {
1331
- if (M.exported_names[0].name.name === "*") {
1363
+ var first_exported = M.exported_names[0];
1364
+ var first_exported_name = first_exported.name;
1365
+ if (first_exported_name.name === "*" && !first_exported_name.quote) {
1366
+ var foreign_name = first_exported.foreign_name;
1367
+ var exported = foreign_name.name === "*" && !foreign_name.quote
1368
+ ? null
1369
+ : to_moz(foreign_name);
1332
1370
  return {
1333
1371
  type: "ExportAllDeclaration",
1334
1372
  source: to_moz(M.module_name),
1373
+ exported: exported,
1335
1374
  assertions: assert_clause_to_moz(M.assert_clause)
1336
1375
  };
1337
1376
  }
@@ -1363,19 +1402,22 @@ import { is_basic_identifier_string } from "./parse.js";
1363
1402
  local: to_moz(M.imported_name)
1364
1403
  });
1365
1404
  }
1366
- if (M.imported_names && M.imported_names[0].foreign_name.name === "*") {
1367
- specifiers.push({
1368
- type: "ImportNamespaceSpecifier",
1369
- local: to_moz(M.imported_names[0].name)
1370
- });
1371
- } else if (M.imported_names) {
1372
- M.imported_names.forEach(function(name_mapping) {
1405
+ if (M.imported_names) {
1406
+ var first_imported_foreign_name = M.imported_names[0].foreign_name;
1407
+ if (first_imported_foreign_name.name === "*" && !first_imported_foreign_name.quote) {
1373
1408
  specifiers.push({
1374
- type: "ImportSpecifier",
1375
- local: to_moz(name_mapping.name),
1376
- imported: to_moz(name_mapping.foreign_name)
1409
+ type: "ImportNamespaceSpecifier",
1410
+ local: to_moz(M.imported_names[0].name)
1377
1411
  });
1378
- });
1412
+ } else {
1413
+ M.imported_names.forEach(function(name_mapping) {
1414
+ specifiers.push({
1415
+ type: "ImportSpecifier",
1416
+ local: to_moz(name_mapping.name),
1417
+ imported: to_moz(name_mapping.foreign_name)
1418
+ });
1419
+ });
1420
+ }
1379
1421
  }
1380
1422
  return {
1381
1423
  type: "ImportDeclaration",
@@ -1639,7 +1681,14 @@ import { is_basic_identifier_string } from "./parse.js";
1639
1681
  });
1640
1682
 
1641
1683
  def_to_moz(AST_Symbol, function To_Moz_Identifier(M, parent) {
1642
- if (M instanceof AST_SymbolMethod && parent.quote) {
1684
+ if (
1685
+ (M instanceof AST_SymbolMethod && parent.quote) ||
1686
+ ((
1687
+ M instanceof AST_SymbolImportForeign ||
1688
+ M instanceof AST_SymbolExportForeign ||
1689
+ M instanceof AST_SymbolExport
1690
+ ) && M.quote)
1691
+ ) {
1643
1692
  return {
1644
1693
  type: "Literal",
1645
1694
  value: M.name
package/lib/output.js CHANGED
@@ -1225,7 +1225,7 @@ function OutputStream(options) {
1225
1225
  }
1226
1226
 
1227
1227
  AST_StatementWithBody.DEFMETHOD("_do_print_body", function(output) {
1228
- force_statement(this.body, output);
1228
+ print_maybe_braced_body(this.body, output);
1229
1229
  });
1230
1230
 
1231
1231
  DEFPRINT(AST_Statement, function(self, output) {
@@ -1554,7 +1554,7 @@ function OutputStream(options) {
1554
1554
  b = b.body;
1555
1555
  } else break;
1556
1556
  }
1557
- force_statement(self.body, output);
1557
+ print_maybe_braced_body(self.body, output);
1558
1558
  }
1559
1559
  DEFPRINT(AST_If, function(self, output) {
1560
1560
  output.print("if");
@@ -1571,7 +1571,7 @@ function OutputStream(options) {
1571
1571
  if (self.alternative instanceof AST_If)
1572
1572
  self.alternative.print(output);
1573
1573
  else
1574
- force_statement(self.alternative, output);
1574
+ print_maybe_braced_body(self.alternative, output);
1575
1575
  } else {
1576
1576
  self._do_print_body(output);
1577
1577
  }
@@ -1681,7 +1681,9 @@ function OutputStream(options) {
1681
1681
  output.space();
1682
1682
  }
1683
1683
  if (self.imported_names) {
1684
- if (self.imported_names.length === 1 && self.imported_names[0].foreign_name.name === "*") {
1684
+ if (self.imported_names.length === 1 &&
1685
+ self.imported_names[0].foreign_name.name === "*" &&
1686
+ !self.imported_names[0].foreign_name.quote) {
1685
1687
  self.imported_names[0].print(output);
1686
1688
  } else {
1687
1689
  output.print("{");
@@ -1715,14 +1717,31 @@ function OutputStream(options) {
1715
1717
  DEFPRINT(AST_NameMapping, function(self, output) {
1716
1718
  var is_import = output.parent() instanceof AST_Import;
1717
1719
  var definition = self.name.definition();
1720
+ var foreign_name = self.foreign_name;
1718
1721
  var names_are_different =
1719
1722
  (definition && definition.mangled_name || self.name.name) !==
1720
- self.foreign_name.name;
1723
+ foreign_name.name;
1724
+ if (!names_are_different &&
1725
+ foreign_name.name === "*" &&
1726
+ foreign_name.quote != self.name.quote) {
1727
+ // export * as "*"
1728
+ names_are_different = true;
1729
+ }
1730
+ var foreign_name_is_name = foreign_name.quote == null;
1721
1731
  if (names_are_different) {
1722
1732
  if (is_import) {
1723
- output.print(self.foreign_name.name);
1733
+ if (foreign_name_is_name) {
1734
+ output.print(foreign_name.name);
1735
+ } else {
1736
+ output.print_string(foreign_name.name, foreign_name.quote);
1737
+ }
1724
1738
  } else {
1725
- self.name.print(output);
1739
+ if (self.name.quote == null) {
1740
+ self.name.print(output);
1741
+ } else {
1742
+ output.print_string(self.name.name, self.name.quote);
1743
+ }
1744
+
1726
1745
  }
1727
1746
  output.space();
1728
1747
  output.print("as");
@@ -1730,10 +1749,18 @@ function OutputStream(options) {
1730
1749
  if (is_import) {
1731
1750
  self.name.print(output);
1732
1751
  } else {
1733
- output.print(self.foreign_name.name);
1752
+ if (foreign_name_is_name) {
1753
+ output.print(foreign_name.name);
1754
+ } else {
1755
+ output.print_string(foreign_name.name, foreign_name.quote);
1756
+ }
1734
1757
  }
1735
1758
  } else {
1736
- self.name.print(output);
1759
+ if (self.name.quote == null) {
1760
+ self.name.print(output);
1761
+ } else {
1762
+ output.print_string(self.name.name, self.name.quote);
1763
+ }
1737
1764
  }
1738
1765
  });
1739
1766
 
@@ -1745,8 +1772,10 @@ function OutputStream(options) {
1745
1772
  output.space();
1746
1773
  }
1747
1774
  if (self.exported_names) {
1748
- if (self.exported_names.length === 1 && self.exported_names[0].name.name === "*") {
1749
- self.exported_names[0].print(output);
1775
+ if (self.exported_names.length === 1 &&
1776
+ self.exported_names[0].name.name === "*" &&
1777
+ !self.exported_names[0].name.quote) {
1778
+ self.exported_names[0].print(output);
1750
1779
  } else {
1751
1780
  output.print("{");
1752
1781
  self.exported_names.forEach(function(name_export, i) {
@@ -2266,12 +2295,15 @@ function OutputStream(options) {
2266
2295
  }
2267
2296
  });
2268
2297
 
2269
- function force_statement(stat, output) {
2298
+ /** if, for, while, may or may not have braces surrounding its body */
2299
+ function print_maybe_braced_body(stat, output) {
2270
2300
  if (output.option("braces")) {
2271
2301
  make_block(stat, output);
2272
2302
  } else {
2273
2303
  if (!stat || stat instanceof AST_EmptyStatement)
2274
2304
  output.force_semicolon();
2305
+ else if (stat instanceof AST_Let || stat instanceof AST_Const || stat instanceof AST_Class)
2306
+ make_block(stat, output);
2275
2307
  else
2276
2308
  stat.print(output);
2277
2309
  }
package/lib/parse.js CHANGED
@@ -2089,14 +2089,20 @@ function parse($TEXT, options) {
2089
2089
  });
2090
2090
  }
2091
2091
 
2092
+ /**
2093
+ * var
2094
+ * vardef1 = 2,
2095
+ * vardef2 = 3;
2096
+ */
2092
2097
  function vardefs(no_in, kind) {
2093
- var a = [];
2098
+ var var_defs = [];
2094
2099
  var def;
2095
2100
  for (;;) {
2096
2101
  var sym_type =
2097
2102
  kind === "var" ? AST_SymbolVar :
2098
2103
  kind === "const" ? AST_SymbolConst :
2099
2104
  kind === "let" ? AST_SymbolLet : null;
2105
+ // var { a } = b
2100
2106
  if (is("punc", "{") || is("punc", "[")) {
2101
2107
  def = new AST_VarDef({
2102
2108
  start: S.token,
@@ -2116,12 +2122,12 @@ function parse($TEXT, options) {
2116
2122
  });
2117
2123
  if (def.name.name == "import") croak("Unexpected token: import");
2118
2124
  }
2119
- a.push(def);
2125
+ var_defs.push(def);
2120
2126
  if (!is("punc", ","))
2121
2127
  break;
2122
2128
  next();
2123
2129
  }
2124
- return a;
2130
+ return var_defs;
2125
2131
  }
2126
2132
 
2127
2133
  var var_ = function(no_in) {
@@ -2788,9 +2794,10 @@ function parse($TEXT, options) {
2788
2794
  }
2789
2795
 
2790
2796
  function map_name(is_import) {
2791
- function make_symbol(type) {
2797
+ function make_symbol(type, quote) {
2792
2798
  return new type({
2793
2799
  name: as_property_name(),
2800
+ quote: quote || undefined,
2794
2801
  start: prev(),
2795
2802
  end: prev()
2796
2803
  });
@@ -2803,16 +2810,16 @@ function parse($TEXT, options) {
2803
2810
  var name;
2804
2811
 
2805
2812
  if (is_import) {
2806
- foreign_name = make_symbol(foreign_type);
2813
+ foreign_name = make_symbol(foreign_type, start.quote);
2807
2814
  } else {
2808
- name = make_symbol(type);
2815
+ name = make_symbol(type, start.quote);
2809
2816
  }
2810
2817
  if (is("name", "as")) {
2811
2818
  next(); // The "as" word
2812
2819
  if (is_import) {
2813
2820
  name = make_symbol(type);
2814
2821
  } else {
2815
- foreign_name = make_symbol(foreign_type);
2822
+ foreign_name = make_symbol(foreign_type, S.token.quote);
2816
2823
  }
2817
2824
  } else if (is_import) {
2818
2825
  name = new type(foreign_name);
@@ -2828,20 +2835,26 @@ function parse($TEXT, options) {
2828
2835
  });
2829
2836
  }
2830
2837
 
2831
- function map_nameAsterisk(is_import, name) {
2838
+ function map_nameAsterisk(is_import, import_or_export_foreign_name) {
2832
2839
  var foreign_type = is_import ? AST_SymbolImportForeign : AST_SymbolExportForeign;
2833
2840
  var type = is_import ? AST_SymbolImport : AST_SymbolExport;
2834
2841
  var start = S.token;
2835
- var foreign_name;
2842
+ var name, foreign_name;
2836
2843
  var end = prev();
2837
2844
 
2845
+ if (is_import) {
2846
+ name = import_or_export_foreign_name;
2847
+ } else {
2848
+ foreign_name = import_or_export_foreign_name;
2849
+ }
2850
+
2838
2851
  name = name || new type({
2839
2852
  start: start,
2840
2853
  name: "*",
2841
2854
  end: end,
2842
2855
  });
2843
2856
 
2844
- foreign_name = new foreign_type({
2857
+ foreign_name = foreign_name || new foreign_type({
2845
2858
  start: start,
2846
2859
  name: "*",
2847
2860
  end: end,
@@ -2870,9 +2883,9 @@ function parse($TEXT, options) {
2870
2883
  } else if (is("operator", "*")) {
2871
2884
  var name;
2872
2885
  next();
2873
- if (is_import && is("name", "as")) {
2886
+ if (is("name", "as")) {
2874
2887
  next(); // The "as" word
2875
- name = as_symbol(is_import ? AST_SymbolImport : AST_SymbolExportForeign);
2888
+ name = is_import ? as_symbol(AST_SymbolImport) : as_symbol_or_string(AST_SymbolExportForeign);
2876
2889
  }
2877
2890
  names = [map_nameAsterisk(is_import, name)];
2878
2891
  }
@@ -3037,6 +3050,27 @@ function parse($TEXT, options) {
3037
3050
  return sym;
3038
3051
  }
3039
3052
 
3053
+ function as_symbol_or_string(type) {
3054
+ if (!is("name")) {
3055
+ if (!is("string")) {
3056
+ croak("Name or string expected");
3057
+ }
3058
+ var tok = S.token;
3059
+ var ret = new type({
3060
+ start : tok,
3061
+ end : tok,
3062
+ name : tok.value,
3063
+ quote : tok.quote
3064
+ });
3065
+ next();
3066
+ return ret;
3067
+ }
3068
+ var sym = _make_symbol(type);
3069
+ _verify_symbol(sym);
3070
+ next();
3071
+ return sym;
3072
+ }
3073
+
3040
3074
  // Annotate AST_Call, AST_Lambda or AST_New with the special comments
3041
3075
  function annotate(node) {
3042
3076
  var start = node.start;
package/lib/scope.js CHANGED
@@ -69,6 +69,7 @@ import {
69
69
  AST_Export,
70
70
  AST_For,
71
71
  AST_ForIn,
72
+ AST_ForOf,
72
73
  AST_Function,
73
74
  AST_Import,
74
75
  AST_IterationStatement,
@@ -230,8 +231,9 @@ AST_Scope.DEFMETHOD("figure_out_scope", function(options, { parent_scope = null,
230
231
  scope.init_scope_vars(parent_scope);
231
232
  scope.uses_with = save_scope.uses_with;
232
233
  scope.uses_eval = save_scope.uses_eval;
234
+
233
235
  if (options.safari10) {
234
- if (node instanceof AST_For || node instanceof AST_ForIn) {
236
+ if (node instanceof AST_For || node instanceof AST_ForIn || node instanceof AST_ForOf) {
235
237
  for_scopes.push(scope);
236
238
  }
237
239
  }
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.16.1",
7
+ "version": "5.16.3",
8
8
  "engines": {
9
9
  "node": ">=10"
10
10
  },
package/tools/terser.d.ts CHANGED
@@ -204,6 +204,7 @@ export interface SourceMapOptions {
204
204
  includeSources?: boolean;
205
205
  filename?: string;
206
206
  root?: string;
207
+ asObject?: boolean;
207
208
  url?: string | 'inline';
208
209
  }
209
210
 
package/bin/terser.mjs DELETED
@@ -1,21 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- "use strict";
4
-
5
- import "../tools/exit.cjs";
6
-
7
- import fs from "fs"
8
- import path from "path"
9
- import program from "commander"
10
-
11
- import { run_cli } from "../lib/cli.js"
12
-
13
- const packageJson = {
14
- name: "terser",
15
- version: "experimental module CLI"
16
- }
17
-
18
- run_cli({ program, packageJson, fs, path }).catch((error) => {
19
- console.error(error);
20
- process.exitCode = 1;
21
- });