fix `AST_PropAccess` in `collapse_vars` (take 2) (#2372)
authorAlex Lam S.L <alexlamsl@gmail.com>
Tue, 17 Oct 2017 14:59:15 +0000 (22:59 +0800)
committerGitHub <noreply@github.com>
Tue, 17 Oct 2017 14:59:15 +0000 (22:59 +0800)
fixes #2364

lib/compress.js
test/compress/collapse_vars.js

index 3909c65..488cc6f 100644 (file)
@@ -839,8 +839,7 @@ merge(Compressor.prototype, {
                         if (node instanceof AST_Call
                             || node instanceof AST_Exit
                             || node instanceof AST_PropAccess
-                                && (node.has_side_effects(compressor)
-                                    || get_symbol(node).name in lvalues)
+                                && (side_effects || node.has_side_effects(compressor))
                             || node instanceof AST_SymbolRef
                                && (lvalues[node.name]
                                    || side_effects && !references_in_scope(node.definition()))
index baa18ea..c115763 100644 (file)
@@ -2711,3 +2711,75 @@ issue_2364_2: {
         }
     }
 }
+
+issue_2364_3: {
+    options = {
+        collapse_vars: true,
+        pure_getters: true,
+    }
+    input: {
+        function inc(obj) {
+            return obj.count++;
+        }
+        function foo(bar) {
+            var result = inc(bar);
+            return foo.amount = bar.count, result;
+        }
+        var data = {
+            count: 0,
+        };
+        var answer = foo(data);
+        console.log(foo.amount, answer);
+    }
+    expect: {
+        function inc(obj) {
+            return obj.count++;
+        }
+        function foo(bar) {
+            var result = inc(bar);
+            return foo.amount = bar.count, result;
+        }
+        var data = {
+            count: 0,
+        };
+        var answer = foo(data);
+        console.log(foo.amount, answer);
+    }
+    expect_stdout: "1 0"
+}
+
+issue_2364_4: {
+    options = {
+        collapse_vars: true,
+        pure_getters: true,
+    }
+    input: {
+        function inc(obj) {
+            return obj.count++;
+        }
+        function foo(bar, baz) {
+            var result = inc(bar);
+            return foo.amount = baz.count, result;
+        }
+        var data = {
+            count: 0,
+        };
+        var answer = foo(data, data);
+        console.log(foo.amount, answer);
+    }
+    expect: {
+        function inc(obj) {
+            return obj.count++;
+        }
+        function foo(bar, baz) {
+            var result = inc(bar);
+            return foo.amount = baz.count, result;
+        }
+        var data = {
+            count: 0,
+        };
+        var answer = foo(data, data);
+        console.log(foo.amount, answer);
+    }
+    expect_stdout: "1 0"
+}