From 001f6f971913d28d9c77a5ff6602ae06d3780ccf Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 17 Sep 2022 01:54:54 +0100 Subject: [PATCH] fix corner case in `inline` (#5664) fixes #5662 --- lib/compress.js | 17 ++++++++++------- test/compress/classes.js | 33 +++++++++++++++++++++++++++++++++ test/reduce.js | 2 +- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 0bcd468e0e9..99543fc984e 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -6278,21 +6278,24 @@ Compressor.prototype.compress = function(node) { var call = stat.value; if (!call || call.TYPE != "Call") break; if (call.is_expr_pure(compressor)) break; - var fn = call.expression; - if (fn instanceof AST_SymbolRef) { - if (self.name && self.name.definition() === fn.definition()) break; - fn = fn.fixed_value(); + var exp = call.expression, fn; + if (!(exp instanceof AST_SymbolRef)) { + fn = exp; + } else if (self.name && self.name.definition() === exp.definition()) { + break; + } else { + fn = exp.fixed_value(); } if (!(fn instanceof AST_Defun || fn instanceof AST_Function)) break; if (fn.rest) break; if (fn.uses_arguments) break; - if (fn === call.expression) { + if (fn === exp) { if (fn.parent_scope !== self) break; if (!all(fn.enclosed, function(def) { return def.scope !== self; })) break; } - if (fn.name + if ((fn !== exp || fn.name) && (parent instanceof AST_ClassMethod || parent instanceof AST_ObjectMethod) && parent.value === compressor.self()) break; if (fn.contains_this()) break; @@ -6321,7 +6324,7 @@ Compressor.prototype.compress = function(node) { fn.argnames.push(fn.make_var(AST_SymbolFunarg, fn, "argument_" + len)); } while (++len < self.argnames.length); } - return call.expression; + return exp; } break; } diff --git a/test/compress/classes.js b/test/compress/classes.js index 32c4d74eebd..f2e6e1860b6 100644 --- a/test/compress/classes.js +++ b/test/compress/classes.js @@ -3586,3 +3586,36 @@ issue_5531_3: { expect_stdout: "foo" node_version: ">=16" } + +issue_5662: { + options = { + inline: true, + reduce_vars: true, + } + input: { + console.log(new (function() { + var g = function(a) { + return a; + }; + return class { + h(b) { + return g(b); + } + }; + }())().h("PASS")); + } + expect: { + console.log(new (function() { + var g = function(a) { + return a; + }; + return class { + h(b) { + return g(b); + } + }; + }())().h("PASS")); + } + expect_stdout: "PASS" + node_version: ">=6" +} diff --git a/test/reduce.js b/test/reduce.js index 696914bd8ee..d415e71f1c5 100644 --- a/test/reduce.js +++ b/test/reduce.js @@ -466,7 +466,7 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options) } } else if (node instanceof U.AST_VarDef) { - if (node.value && !(parent instanceof U.AST_Const)) { + if (node.value && !(node.name instanceof U.AST_Destructured || parent instanceof U.AST_Const)) { node.start._permute++; CHANGED = true; return new U.AST_VarDef({