account for side-effects from `AST_This` in `collapse_vars` (#2365)
authorAlex Lam S.L <alexlamsl@gmail.com>
Mon, 16 Oct 2017 17:18:55 +0000 (01:18 +0800)
committerGitHub <noreply@github.com>
Mon, 16 Oct 2017 17:18:55 +0000 (01:18 +0800)
lib/compress.js
test/compress/collapse_vars.js

index c3876f9..5e391ab 100644 (file)
@@ -950,9 +950,11 @@ merge(Compressor.prototype, {
                         scope = save_scope;
                         return true;
                     }
-                    if (node instanceof AST_SymbolRef || node instanceof AST_PropAccess) {
+                    if (node instanceof AST_PropAccess
+                        || node instanceof AST_SymbolRef
+                        || node instanceof AST_This) {
                         var sym = get_symbol(node);
-                        if (sym instanceof AST_SymbolRef) {
+                        if (sym instanceof AST_SymbolRef || node instanceof AST_This) {
                             lvalues[sym.name] = lvalues[sym.name] || is_lhs(node, tw.parent());
                         }
                     }
index 7d66f7c..ec94e9f 100644 (file)
@@ -2600,3 +2600,55 @@ prop_side_effects_2: {
         "2",
     ]
 }
+
+issue_2364: {
+    options = {
+        collapse_vars: true,
+        pure_getters: true,
+    }
+    input: {
+        console.log(function(a) {
+            var b = a.f;
+            a.f++;
+            return b;
+        }({ f: 1 }));
+        console.log(function() {
+            var a = { f: 1 }, b = a.f;
+            a.f++;
+            return b;
+        }());
+        console.log({
+            f: 1,
+            g: function() {
+                var b = this.f;
+                this.f++;
+                return b;
+            }
+        }.g());
+    }
+    expect: {
+        console.log(function(a) {
+            var b = a.f;
+            a.f++;
+            return b;
+        }({ f: 1 }));
+        console.log(function() {
+            var a = { f: 1 }, b = a.f;
+            a.f++;
+            return b;
+        }());
+        console.log({
+            f: 1,
+            g: function() {
+                var b = this.f;
+                this.f++;
+                return b;
+            }
+        }.g());
+    }
+    expect_stdout: [
+        "1",
+        "1",
+        "1",
+    ]
+}