Skip to content

Commit

Permalink
fix corner case in evaluate (#5357)
Browse files Browse the repository at this point in the history
fixes #5356
  • Loading branch information
alexlamsl committed Feb 15, 2022
1 parent 0b50880 commit 82e8ebd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/compress.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions test/compress/arrows.js
Expand Up @@ -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"
}
21 changes: 21 additions & 0 deletions test/compress/evaluate.js
Expand Up @@ -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"
}

0 comments on commit 82e8ebd

Please sign in to comment.