fix corner case in `hoist_props` (#4024)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 26 Jul 2020 01:27:34 +0000 (02:27 +0100)
committerGitHub <noreply@github.com>
Sun, 26 Jul 2020 01:27:34 +0000 (09:27 +0800)
fixes #4023

lib/compress.js
test/compress/hoist_props.js

index 1da4995..3405253 100644 (file)
@@ -5071,6 +5071,7 @@ merge(Compressor.prototype, {
             }
         }));
         self.transform(new TreeTransformer(function(node, descend) {
+            if (node instanceof AST_Binary) return replace("right");
             if (node instanceof AST_PropAccess) {
                 if (!(node.expression instanceof AST_SymbolRef)) return;
                 var defs = defs_by_id[node.expression.definition().id];
@@ -5086,10 +5087,15 @@ merge(Compressor.prototype, {
             }
             if (node instanceof AST_Unary) {
                 if (unary_side_effects[node.operator]) return;
-                if (!(node.expression instanceof AST_SymbolRef)) return;
-                if (!(node.expression.definition().id in defs_by_id)) return;
+                return replace("expression");
+            }
+
+            function replace(prop) {
+                var sym = node[prop];
+                if (!(sym instanceof AST_SymbolRef)) return;
+                if (!(sym.definition().id in defs_by_id)) return;
                 var opt = node.clone();
-                opt.expression = make_node(AST_Object, node, {
+                opt[prop] = make_node(AST_Object, sym, {
                     properties: []
                 });
                 return opt;
index 692dc67..09137cc 100644 (file)
@@ -1016,3 +1016,28 @@ issue_3945_2: {
     }
     expect_stdout: "undefined"
 }
+
+issue_4023: {
+    options = {
+        comparisons: true,
+        hoist_props: true,
+        inline: true,
+        reduce_vars: true,
+        toplevel: true,
+        typeofs: true,
+        unused: true,
+    }
+    input: {
+        function f() {
+            var a = function() {
+                return { p: 0 };
+            }();
+            return console.log("undefined" != typeof a);
+        }
+        f();
+    }
+    expect: {
+        console.log(void 0 !== {});
+    }
+    expect_stdout: "true"
+}