From 328641fa02df6d57317ade8871ad6ade43e260d6 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Tue, 16 Jul 2024 01:08:29 +0300 Subject: [PATCH] fix corner case in `booleans` fixes #5885 --- lib/compress.js | 6 +++++- test/compress/functions.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index 275e9ee034..b85bd8bb02 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -12119,7 +12119,11 @@ Compressor.prototype.compress = function(node) { var exprs = []; if (self.left.evaluate(compressor) instanceof AST_Node) exprs.push(self.left); if (self.right.evaluate(compressor) instanceof AST_Node) exprs.push(self.right); - if (exprs.length < 2) { + switch (exprs.length) { + case 0: + return make_node(AST_True, self).optimize(compressor); + case 1: + exprs[0] = exprs[0].clone(); exprs.push(make_node(AST_True, self)); return make_sequence(self, exprs).optimize(compressor); } diff --git a/test/compress/functions.js b/test/compress/functions.js index 26d9d00c30..7c4742257a 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -8968,3 +8968,32 @@ issue_5851_2: { "foo", ] } + +issue_5885: { + options = { + booleans: true, + evaluate: true, + inline: true, + reduce_vars: true, + side_effects: true, + toplevel: true, + unused: true, + } + input: { + var a; + f(); + function f() { + return ++a + "foo"; + } + console.log(a = f()); + } + expect: { + var a; + f(); + function f() { + return ++a + "foo"; + } + console.log(a = f()); + } + expect_stdout: "NaNfoo" +}