fix corner case in unsafe_math (#3548)

fixes #3547
This commit is contained in:
Alex Lam S.L
2019-10-29 17:06:57 +08:00
committed by GitHub
parent bad664c632
commit 22a09ea7c5
2 changed files with 158 additions and 15 deletions

View File

@@ -6024,9 +6024,12 @@ merge(Compressor.prototype, {
if (self.right instanceof AST_Constant if (self.right instanceof AST_Constant
&& self.left instanceof AST_Binary && self.left instanceof AST_Binary
&& self.left.operator != "%" && self.left.operator != "%"
&& PRECEDENCE[self.left.operator] == PRECEDENCE[self.operator]) { && PRECEDENCE[self.left.operator] == PRECEDENCE[self.operator]
&& self.left.is_number(compressor)) {
if (self.left.left instanceof AST_Constant if (self.left.left instanceof AST_Constant
&& (self.left.operator != "+" || self.left.right.is_number(compressor))) { && (self.operator != "+"
|| self.left.left.is_boolean(compressor)
|| self.left.left.is_number(compressor))) {
self = make_node(AST_Binary, self, { self = make_node(AST_Binary, self, {
operator: self.left.operator, operator: self.left.operator,
left: make_node(AST_Binary, self.left, { left: make_node(AST_Binary, self.left, {
@@ -6038,19 +6041,23 @@ merge(Compressor.prototype, {
}), }),
right: self.left.right right: self.left.right
}); });
} else if (self.left.right instanceof AST_Constant } else if (self.left.right instanceof AST_Constant) {
&& (self.left.operator != "+" || self.left.left.is_number(compressor))) { var op = align(self.left.operator, self.operator);
self = make_node(AST_Binary, self, { if (op != "+"
operator: self.left.operator, || self.left.right.is_boolean(compressor)
left: self.left.left, || self.left.right.is_number(compressor)) {
right: make_node(AST_Binary, self.left, { self = make_node(AST_Binary, self, {
operator: align(self.left.operator, self.operator), operator: self.left.operator,
left: self.left.right, left: self.left.left,
right: self.right, right: make_node(AST_Binary, self.left, {
start: self.left.right.start, operator: op,
end: self.right.end left: self.left.right,
}) right: self.right,
}); start: self.left.right.start,
end: self.right.end
})
});
}
} }
} }
break; break;

View File

@@ -781,3 +781,139 @@ issue_3539: {
} }
expect_stdout: "NaN -Infinity Infinity" expect_stdout: "NaN -Infinity Infinity"
} }
issue_3547_1: {
options = {
evaluate: true,
unsafe_math: true,
}
input: {
[
1/0 + "1" + 0,
1/0 + "1" - 0,
1/0 - "1" + 0,
1/0 - "1" - 0,
].forEach(function(n) {
console.log(typeof n, n);
});
}
expect: {
[
1/0 + "10",
NaN,
1/0,
1/0,
].forEach(function(n) {
console.log(typeof n, n);
});
}
expect_stdout: [
"string Infinity10",
"number NaN",
"number Infinity",
"number Infinity",
]
}
issue_3547_2: {
options = {
evaluate: true,
unsafe_math: true,
}
input: {
[
"1" + 1/0 + 0,
"1" + 1/0 - 0,
"1" - 1/0 + 0,
"1" - 1/0 - 0,
].forEach(function(n) {
console.log(typeof n, n);
});
}
expect: {
[
"1" + 1/0 + 0,
NaN,
-1/0,
-1/0,
].forEach(function(n) {
console.log(typeof n, n);
});
}
expect_stdout: [
"string 1Infinity0",
"number NaN",
"number -Infinity",
"number -Infinity",
]
}
issue_3547_3: {
options = {
evaluate: true,
unsafe_math: true,
}
input: {
var a = "3";
[
a + "2" + 1,
a + "2" - 1,
a - "2" + 1,
a - "2" - 1,
].forEach(function(n) {
console.log(typeof n, n);
});
}
expect: {
var a = "3";
[
a + "21",
a + "2" - 1,
a - 1,
a - "2" - 1,
].forEach(function(n) {
console.log(typeof n, n);
});
}
expect_stdout: [
"string 321",
"number 31",
"number 2",
"number 0",
]
}
issue_3547_4: {
options = {
evaluate: true,
unsafe_math: true,
}
input: {
var a = "2";
[
"3" + a + 1,
"3" + a - 1,
"3" - a + 1,
"3" - a - 1,
].forEach(function(n) {
console.log(typeof n, n);
});
}
expect: {
var a = "2";
[
"3" + a + 1,
"3" + a - 1,
"3" - a + 1,
2 - a,
].forEach(function(n) {
console.log(typeof n, n);
});
}
expect_stdout: [
"string 321",
"number 31",
"number 2",
"number 0",
]
}