Skip to content

Commit

Permalink
fix corner case in sign propagation (#3696)
Browse files Browse the repository at this point in the history
- migrate de-facto functionality to `evaluate`

fixes #3695
  • Loading branch information
alexlamsl committed Jan 28, 2020
1 parent b499e03 commit 87119e4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
19 changes: 11 additions & 8 deletions lib/compress.js
Expand Up @@ -6062,6 +6062,8 @@ merge(Compressor.prototype, {
return self;
});

var SIGN_OPS = makePredicate("+ -");
var MULTIPLICATIVE_OPS = makePredicate("* / %");
OPT(AST_UnaryPrefix, function(self, compressor) {
var e = self.expression;
if (compressor.option("evaluate")
Expand Down Expand Up @@ -6112,12 +6114,12 @@ merge(Compressor.prototype, {
])).optimize(compressor);
}
}
if (self.operator == "-" && e instanceof AST_Infinity) {
e = e.transform(compressor);
}
if (e instanceof AST_Binary
&& (self.operator == "+" || self.operator == "-")
&& (e.operator == "*" || e.operator == "/" || e.operator == "%")) {
if (self.operator == "-" && e instanceof AST_Infinity) e = e.transform(compressor);
if (compressor.option("evaluate")
&& e instanceof AST_Binary
&& SIGN_OPS[self.operator]
&& MULTIPLICATIVE_OPS[e.operator]
&& (e.left.is_constant() || !e.right.has_side_effects(compressor))) {
return make_node(AST_Binary, self, {
operator: e.operator,
left: make_node(AST_UnaryPrefix, e.left, {
Expand Down Expand Up @@ -6387,7 +6389,8 @@ merge(Compressor.prototype, {
if (self.right instanceof AST_Binary
&& self.operator == self.right.operator
&& (self.left.is_string(compressor) && self.right.is_string(compressor)
|| self.right.left.is_string(compressor) && !self.right.right.has_side_effects(compressor))) {
|| self.right.left.is_string(compressor)
&& (self.left.is_constant() || !self.right.right.has_side_effects(compressor)))) {
swap_chain();
}
}
Expand Down Expand Up @@ -7084,7 +7087,7 @@ merge(Compressor.prototype, {
return reachable;
}

var ASSIGN_OPS = makePredicate("+ - / * % >> << >>> | ^ &");
var ASSIGN_OPS = makePredicate("+ - * / % >> << >>> | ^ &");
var ASSIGN_OPS_COMMUTATIVE = makePredicate("* | ^ &");
OPT(AST_Assign, function(self, compressor) {
if (compressor.option("dead_code")) {
Expand Down
2 changes: 1 addition & 1 deletion test/compress/issue-1770.js
Expand Up @@ -46,7 +46,7 @@ mangle_props: {
obj[1/0],
obj["Infinity"],
obj[-1/0],
obj[-1/0],
obj[-(1/0)],
obj["-Infinity"],
obj[null],
obj["null"]
Expand Down
18 changes: 18 additions & 0 deletions test/compress/numbers.js
Expand Up @@ -669,6 +669,9 @@ issue_1710: {
}

unary_binary_parenthesis: {
options = {
evaluate: true,
}
input: {
var v = [ 0, 1, NaN, Infinity, null, undefined, true, false, "", "foo", /foo/ ];
v.forEach(function(x) {
Expand Down Expand Up @@ -1233,3 +1236,18 @@ issue_3684: {
"Infinity",
]
}

issue_3695: {
options = {
evaluate: true,
}
input: {
var a = [];
console.log(+(a * (a[0] = false)));
}
expect: {
var a = [];
console.log(+(a * (a[0] = false)));
}
expect_stdout: "NaN"
}

0 comments on commit 87119e4

Please sign in to comment.