diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-03-06 14:49:39 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-06 22:49:39 +0800 |
commit | ad903e924050067a96a33f2eb5a0b9ab3e5454c1 (patch) | |
tree | 4729dfc4dd369cf9bb8d8f8eed8c9b1e276cb5f1 | |
parent | 83c3838b07ed9ff32a83a39d5c1cbf40155c46f2 (diff) | |
download | tracifyjs-ad903e924050067a96a33f2eb5a0b9ab3e5454c1.tar.gz tracifyjs-ad903e924050067a96a33f2eb5a0b9ab3e5454c1.zip |
fix corner cases with `export` (#4743)
fixes #4742
-rw-r--r-- | lib/compress.js | 5 | ||||
-rw-r--r-- | lib/scope.js | 11 | ||||
-rw-r--r-- | test/compress/exports.js | 59 |
3 files changed, 70 insertions, 5 deletions
diff --git a/lib/compress.js b/lib/compress.js index 20558ba8..be586a88 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3425,6 +3425,7 @@ merge(Compressor.prototype, { if (parent instanceof AST_For && parent.init === node) return null; return in_list ? List.skip : make_node(AST_EmptyStatement, node); } + if (node instanceof AST_ExportDeclaration) return node; if (node instanceof AST_Scope) return node; if (!is_statement(node)) return node; })); @@ -6167,7 +6168,9 @@ merge(Compressor.prototype, { } var old_def; if (!value && !(node instanceof AST_Let)) { - if (drop_sym && var_defs[sym.id] > 1) { + if (parent instanceof AST_ExportDeclaration) { + flush(); + } else if (drop_sym && var_defs[sym.id] > 1) { AST_Node.info("Dropping declaration of variable {name} [{file}:{line},{col}]", template(def.name)); var_defs[sym.id]--; sym.eliminated++; diff --git a/lib/scope.js b/lib/scope.js index b22f77a2..870ebf38 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -198,9 +198,10 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) { } else if (node instanceof AST_SymbolConst) { var def = scope.def_variable(node); def.defun = defun; - def.exported = exported; + if (exported) def.exported = true; } else if (node instanceof AST_SymbolDefun) { - defun.def_function(node, tw.parent()).exported = exported; + var def = defun.def_function(node, tw.parent()); + if (exported) def.exported = true; entangle(defun, scope); } else if (node instanceof AST_SymbolFunarg) { defun.def_variable(node); @@ -209,9 +210,11 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) { var def = defun.def_function(node, node.name == "arguments" ? undefined : defun); if (options.ie8) def.defun = defun.parent_scope.resolve(); } else if (node instanceof AST_SymbolLet) { - scope.def_variable(node).exported = exported; + var def = scope.def_variable(node); + if (exported) def.exported = true; } else if (node instanceof AST_SymbolVar) { - defun.def_variable(node, node instanceof AST_SymbolImport ? undefined : null).exported = exported; + var def = defun.def_variable(node, node instanceof AST_SymbolImport ? undefined : null); + if (exported) def.exported = true; entangle(defun, scope); } diff --git a/test/compress/exports.js b/test/compress/exports.js index 2da1ee33..a7239148 100644 --- a/test/compress/exports.js +++ b/test/compress/exports.js @@ -382,3 +382,62 @@ single_use_class_default: { A.prototype.p = "PASS"; } } + +issue_4742_join_vars_1: { + options = { + join_vars: true, + } + input: { + var a = 42; + export var a; + } + expect: { + var a = 42; + export var a; + } +} + +issue_4742_join_vars_2: { + options = { + join_vars: true, + } + input: { + export var a = "foo"; + var b; + b = "bar"; + } + expect: { + export var a = "foo"; + var b, b = "bar"; + } +} + +issue_4742_unused_1: { + options = { + unused: true, + } + input: { + var a = 42; + export var a; + } + expect: { + var a = 42; + export var a; + } +} + +issue_4742_unused_2: { + options = { + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + export var a = "foo"; + var a = "bar"; + } + expect: { + export var a = "foo"; + a = "bar"; + } +} |