fix corner case in `inline` (#4266)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 8 Nov 2020 10:50:08 +0000 (10:50 +0000)
committerGitHub <noreply@github.com>
Sun, 8 Nov 2020 10:50:08 +0000 (18:50 +0800)
fixes #4265

lib/compress.js
test/compress/functions.js

index 9333cdf..47eba90 100644 (file)
@@ -7166,7 +7166,7 @@ merge(Compressor.prototype, {
                         return node;
                     }
                 }
-                var child, in_loop, scope;
+                var insert, in_loop, scope;
                 if (replacing && can_inject_symbols()) {
                     fn._squeezed = true;
                     if (exp !== fn) fn.parent_scope = exp.scope;
@@ -7336,7 +7336,7 @@ merge(Compressor.prototype, {
 
         function can_inject_symbols() {
             var defined = Object.create(null);
-            var level = 0;
+            var level = 0, child;
             scope = compressor.self();
             while (!(scope instanceof AST_Scope)) {
                 if (scope.variables) scope.variables.each(function(def) {
@@ -7357,6 +7357,8 @@ merge(Compressor.prototype, {
                     if (scope.fixed_value() instanceof AST_Scope) return false;
                 }
             }
+            insert = scope.body.indexOf(child) + 1;
+            if (!insert) return false;
             var safe_to_inject = (!(scope instanceof AST_Toplevel) || compressor.toplevel.vars)
                 && (exp !== fn || fn.parent_scope.resolve() === compressor.find_parent(AST_Scope));
             var inline = compressor.option("inline");
@@ -7459,7 +7461,7 @@ merge(Compressor.prototype, {
                     return true;
                 }
             });
-            args.unshift(scope.body.indexOf(child) + 1, 0);
+            args.unshift(insert, 0);
             if (decls.length) args.push(make_node(AST_Var, fn, {
                 definitions: decls
             }));
index 3b57fb2..686e7a5 100644 (file)
@@ -5183,3 +5183,34 @@ issue_4261: {
     }
     expect_stdout: true
 }
+
+issue_4265: {
+    options = {
+        conditionals: true,
+        dead_code: true,
+        inline: true,
+        sequences: true,
+    }
+    input: {
+        function f() {
+            console;
+            if ([ function() {
+                return this + console.log(a);
+                a;
+                var a;
+            }() ]);
+            return 0;
+        }
+        f();
+    }
+    expect: {
+        function f() {
+            return console, function() {
+                return console.log(a);
+                var a;
+            }(), 0;
+        }
+        f();
+    }
+    expect_stdout: "undefined"
+}