From 82e8ebd77d812619f4aa516c9e575dfa0447fc7e Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Wed, 16 Feb 2022 00:28:49 +0800 Subject: [PATCH] fix corner case in `evaluate` (#5357) fixes #5356 --- lib/compress.js | 10 ++++++++-- test/compress/arrows.js | 16 ++++++++++++++++ test/compress/evaluate.js | 21 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 91c057f5d6..ab6629495a 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4404,9 +4404,15 @@ Compressor.prototype.compress = function(node) { def(AST_Sequence, function(compressor) { return this.tail_node().is_number(compressor); }); - def(AST_SymbolRef, function(compressor) { + def(AST_SymbolRef, function(compressor, keep_unary) { var fixed = this.fixed_value(); if (!fixed) return false; + if (keep_unary + && fixed instanceof AST_UnaryPrefix + && fixed.operator == "+" + && fixed.expression.equivalent_to(this)) { + return false; + } this.is_number = return_false; var result = fixed.is_number(compressor); delete this.is_number; @@ -10772,7 +10778,7 @@ Compressor.prototype.compress = function(node) { } switch (op) { case "+": - if (compressor.option("evaluate") && exp.is_number(compressor)) return exp; + if (compressor.option("evaluate") && exp.is_number(compressor, true)) return exp; break; case "-": if (exp instanceof AST_Infinity) exp = exp.transform(compressor); diff --git a/test/compress/arrows.js b/test/compress/arrows.js index d044d613c1..d1de06a213 100644 --- a/test/compress/arrows.js +++ b/test/compress/arrows.js @@ -1002,3 +1002,19 @@ issue_5342_2: { expect_stdout: "undefined" node_version: ">=4" } + +issue_5356: { + options = { + evaluate: true, + reduce_vars: true, + unused: true, + } + input: { + console.log((a => a++)(console)); + } + expect: { + console.log((a => +a)(console)); + } + expect_stdout: "NaN" + node_version: ">=4" +} diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js index 858869e377..18527fb09e 100644 --- a/test/compress/evaluate.js +++ b/test/compress/evaluate.js @@ -3273,3 +3273,24 @@ issue_5354: { } expect_stdout: "number string number" } + +issue_5356: { + options = { + evaluate: true, + inline: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + console.log(function() { + return a++; + var a = a; + }()); + } + expect: { + console.log(+a); + var a; + } + expect_stdout: "NaN" +}