From 33df81565fcee3c85afdf165e7fd11fa4ad68ef6 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Mon, 17 Jun 2024 08:33:30 +0300 Subject: [PATCH] fix corner case in `inline` fixes #5851 --- lib/compress.js | 4 ++- test/compress/functions.js | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index 26dc1505dfd..33cbe574a64 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -10928,9 +10928,11 @@ Compressor.prototype.compress = function(node) { && (value = can_flatten_body(stat))) { var replacing = exp === fn || def.single_use && def.references.length - def.replaced == 1; if (can_substitute_directly()) { + self._optimized = true; + var retValue = value.optimize(compressor).clone(true); var args = self.args.slice(); var refs = []; - var retValue = value.optimize(compressor).clone(true).transform(new TreeTransformer(function(node) { + retValue = retValue.transform(new TreeTransformer(function(node) { if (node instanceof AST_SymbolRef) { var def = node.definition(); if (fn.variables.get(node.name) !== def) { diff --git a/test/compress/functions.js b/test/compress/functions.js index 53406b0d1fd..04c42031f30 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -8917,3 +8917,57 @@ issue_5841_2: { "foo", ] } + +issue_5851_1: { + options = { + inline: true, + reduce_vars: true, + toplevel: true, + } + input: { + console.log("PASS") && f(); + function f() { + return f(); + } + f; + } + expect: { + console.log("PASS") && f(); + function f() { + return f(); + } + f; + } + expect_stdout: "PASS" +} + +issue_5851_2: { + options = { + conditionals: true, + inline: true, + passes: 2, + reduce_vars: true, + side_effects: true, + toplevel: true, + unused: true, + } + input: { + var a = f(); + f(); + function f() { + if (console.log("foo")) + console && f(); + } + } + expect: { + f(); + f(); + function f() { + console.log("foo") && console && f(); + } + } + expect_stdout: [ + "foo", + "foo", + ] +}