[Fix] --define replaces SymbolRefs in LHS of assignments
authorChristopher Chedeau <vjeux@fb.com>
Mon, 10 Aug 2015 18:22:36 +0000 (11:22 -0700)
committerChristopher Chedeau <vjeux@fb.com>
Mon, 10 Aug 2015 18:22:36 +0000 (11:22 -0700)
See #208 for context

lib/compress.js
test/compress/issue-208.js [new file with mode: 0644]

index 401a1c7..ac306fc 100644 (file)
@@ -2277,7 +2277,15 @@ merge(Compressor.prototype, {
     });
 
     OPT(AST_SymbolRef, function(self, compressor){
-        if (self.undeclared()) {
+        function isLHS(symbol, parent) {
+            return (
+                parent instanceof AST_Binary &&
+                parent.operator === '=' &&
+                parent.left === symbol
+            );
+        }
+
+        if (self.undeclared() && !isLHS(self, compressor.parent())) {
             var defines = compressor.option("global_defs");
             if (defines && defines.hasOwnProperty(self.name)) {
                 return make_node_from_constant(compressor, defines[self.name], self);
diff --git a/test/compress/issue-208.js b/test/compress/issue-208.js
new file mode 100644 (file)
index 0000000..14752b8
--- /dev/null
@@ -0,0 +1,11 @@
+do_not_update_lhs: {
+    options = { global_defs: { DEBUG: false } };
+    input: { DEBUG = false; }
+    expect: { DEBUG = false; }
+}
+
+do_update_rhs: {
+    options = { global_defs: { DEBUG: false } };
+    input: { MY_DEBUG = DEBUG; }
+    expect: { MY_DEBUG = false; }
+}