fix corner case in `merge_vars` (#4113)
authorAlex Lam S.L <alexlamsl@gmail.com>
Wed, 16 Sep 2020 14:18:28 +0000 (15:18 +0100)
committerGitHub <noreply@github.com>
Wed, 16 Sep 2020 14:18:28 +0000 (22:18 +0800)
fixes #4112

lib/compress.js
test/compress/merge_vars.js

index dbf4e09..a79b909 100644 (file)
@@ -4468,20 +4468,23 @@ merge(Compressor.prototype, {
                     skipped.unshift(tail);
                     continue;
                 }
+                var orig = [], refs = [];
                 if (id in declarations) declarations[id].forEach(function(sym) {
                     sym.thedef = def;
                     sym.name = def.name;
-                    def.orig.push(sym);
+                    orig.push(sym);
                 });
                 references[id].forEach(function(sym) {
                     sym.thedef = def;
                     sym.name = def.name;
                     if (sym instanceof AST_SymbolRef) {
-                        def.references.push(sym);
+                        refs.push(sym);
                     } else {
-                        def.orig.push(sym);
+                        orig.push(sym);
                     }
                 });
+                def.orig = orig.concat(def.orig);
+                def.references = refs.concat(def.references);
                 def.fixed = tail.definition.fixed && def.fixed;
                 merged[id] = def;
                 break;
index f83643e..fd650f9 100644 (file)
@@ -450,3 +450,39 @@ issue_4111: {
     }
     expect_stdout: "2"
 }
+
+issue_4112: {
+    options = {
+        functions: true,
+        merge_vars: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        console.log(typeof function() {
+            try {
+                throw 42;
+            } catch (e) {
+                var o = e;
+                for (e in o);
+                var a = function() {};
+                console.log;
+                return a;
+            }
+        }());
+    }
+    expect: {
+        console.log(typeof function() {
+            try {
+                throw 42;
+            } catch (e) {
+                var a = e;
+                for (e in a);
+                a = function() {};
+                console.log;
+                return a;
+            }
+        }());
+    }
+    expect_stdout: "function"
+}