Skip to content

Commit 0e29ad5

Browse files
authored
fix corner case in evaluate (#3569)
1 parent 0f2687e commit 0e29ad5

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/compress.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2881,6 +2881,7 @@ merge(Compressor.prototype, {
28812881
if (!non_converting_binary[this.operator]) depth++;
28822882
var left = this.left._eval(compressor, cached, depth);
28832883
if (left === this.left) return this;
2884+
if (this.operator == (left ? "||" : "&&")) return left;
28842885
var right = this.right._eval(compressor, cached, depth);
28852886
if (right === this.right) return this;
28862887
var result;
@@ -3049,7 +3050,10 @@ merge(Compressor.prototype, {
30493050
cached.push(node);
30503051
});
30513052
});
3052-
return stat.value ? stat.value._eval(compressor, cached, depth) : undefined;
3053+
if (!stat.value) return undefined;
3054+
var val = stat.value._eval(compressor, cached, depth);
3055+
if (val === stat.value) return this;
3056+
return val;
30533057
} else if (compressor.option("unsafe") && exp instanceof AST_PropAccess) {
30543058
var key = exp.property;
30553059
if (key instanceof AST_Node) {

test/compress/evaluate.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,3 +1876,27 @@ issue_3558: {
18761876
}
18771877
expect_stdout: "1 0"
18781878
}
1879+
1880+
issue_3568: {
1881+
options = {
1882+
evaluate: true,
1883+
reduce_vars: true,
1884+
toplevel: true,
1885+
unsafe: true,
1886+
}
1887+
input: {
1888+
var a = 0;
1889+
function f(b) {
1890+
return b && b.p;
1891+
}
1892+
console.log(f(++a + f()));
1893+
}
1894+
expect: {
1895+
var a = 0;
1896+
function f(b) {
1897+
return b && b.p;
1898+
}
1899+
console.log(NaN);
1900+
}
1901+
expect_stdout: "NaN"
1902+
}

0 commit comments

Comments
 (0)