deduplicate declarations regardless of `toplevel` (#2393)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 22 Oct 2017 17:00:50 +0000 (01:00 +0800)
committerGitHub <noreply@github.com>
Sun, 22 Oct 2017 17:00:50 +0000 (01:00 +0800)
lib/compress.js
test/compress/collapse_vars.js

index eb0e201..7085fcb 100644 (file)
@@ -2240,7 +2240,6 @@ merge(Compressor.prototype, {
         if (self.uses_eval || self.uses_with) return;
         var drop_funcs = !(self instanceof AST_Toplevel) || compressor.toplevel.funcs;
         var drop_vars = !(self instanceof AST_Toplevel) || compressor.toplevel.vars;
-        if (!drop_funcs && !drop_vars) return;
         var assign_as_unused = /keep_assign/.test(compressor.option("unused")) ? return_false : function(node) {
             if (node instanceof AST_Assign && (node.write_only || node.operator == "=")) {
                 return node.left;
@@ -2375,7 +2374,8 @@ merge(Compressor.prototype, {
                     }
                     return node;
                 }
-                if (drop_vars && node instanceof AST_Definitions && !(tt.parent() instanceof AST_ForIn && tt.parent().init === node)) {
+                var parent = tt.parent();
+                if (node instanceof AST_Definitions && !(parent instanceof AST_ForIn && parent.init === node)) {
                     // place uninitialized names at the start
                     var body = [], head = [], tail = [];
                     // for unused names whose initialization has
@@ -2385,7 +2385,7 @@ merge(Compressor.prototype, {
                     node.definitions.forEach(function(def) {
                         if (def.value) def.value = def.value.transform(tt);
                         var sym = def.name.definition();
-                        if (sym.id in in_use_ids) {
+                        if (!drop_vars || sym.id in in_use_ids) {
                             if (def.name instanceof AST_SymbolVar) {
                                 var var_defs = var_defs_by_id.get(sym.id);
                                 if (var_defs.length > 1 && !def.value) {
@@ -2467,7 +2467,7 @@ merge(Compressor.prototype, {
                         && !((def = def.definition()).id in in_use_ids)
                         && self.variables.get(def.name) === def) {
                         if (node instanceof AST_Assign) {
-                            return maintain_this_binding(tt.parent(), node, node.right.transform(tt));
+                            return maintain_this_binding(parent, node, node.right.transform(tt));
                         }
                         return make_node(AST_Number, node, {
                             value: 0
index 1f702ad..b5b97d2 100644 (file)
@@ -2051,7 +2051,7 @@ inner_lvalues: {
         console.log(null, a, b);
     }
     expect: {
-        var a, b = 10;
+        var b = 10;
         var a = (--b || a || 3).toString(), c = --b + -a;
         console.log(null, a, b);
     }