convert to number under boolean context (#2545)

This commit is contained in:
Alex Lam S.L
2017-12-01 12:52:36 +08:00
committed by GitHub
parent 5a1e99d713
commit 09b320e8a5
8 changed files with 15 additions and 12 deletions

View File

@@ -4699,6 +4699,9 @@ merge(Compressor.prototype, {
}); });
OPT(AST_Boolean, function(self, compressor){ OPT(AST_Boolean, function(self, compressor){
if (compressor.in_boolean_context()) return make_node(AST_Number, self, {
value: +self.value
});
if (compressor.option("booleans")) { if (compressor.option("booleans")) {
var p = compressor.parent(); var p = compressor.parent();
if (p instanceof AST_Binary && (p.operator == "==" if (p instanceof AST_Binary && (p.operator == "=="

View File

@@ -1037,10 +1037,10 @@ issue_2535: {
expect: { expect: {
y(); y();
x() && y(); x() && y();
(x(), 0) || y(); (x(), 1) && y();
x() && y(); x() && y();
x() && y(); x() && y();
x() && y(); x() && y();
(x(), 1) || y(); (x(), 0) && y();
} }
} }

View File

@@ -26,7 +26,7 @@ issue_1639_1: {
} }
expect: { expect: {
for (var a = 100, b = 10, L1 = 5; --L1 > 0;) for (var a = 100, b = 10, L1 = 5; --L1 > 0;)
if (--b, !1) var ignore = 0; if (--b, 0) var ignore = 0;
console.log(a, b); console.log(a, b);
} }
expect_stdout: true expect_stdout: true
@@ -57,7 +57,7 @@ issue_1639_2: {
expect: { expect: {
var a = 100, b = 10; var a = 100, b = 10;
function f19() { function f19() {
++a, 1; ++a, 0;
} }
f19(), f19(),
console.log(a, b); console.log(a, b);

View File

@@ -17,6 +17,6 @@ wrongly_optimized: {
foo(); foo();
} }
// TODO: optimize to `func(), bar()` // TODO: optimize to `func(), bar()`
(func(), 0) || bar(); (func(), 1) && bar();
} }
} }

View File

@@ -100,7 +100,7 @@ wrongly_optimized: {
foo(); foo();
} }
// TODO: optimize to `func(), bar()` // TODO: optimize to `func(), bar()`
if (func(), !0) bar(); if (func(), 1) bar();
} }
} }

View File

@@ -2347,7 +2347,7 @@ booleans: {
} }
expect: { expect: {
console.log(function(a) { console.log(function(a) {
if (!1); if (0);
switch (!1) { switch (!1) {
case 0: case 0:
return "FAIL"; return "FAIL";

View File

@@ -45,9 +45,9 @@ condition_evaluate: {
if (void 0 == null); if (void 0 == null);
} }
expect: { expect: {
while (!1); while (0);
for (; !0;); for (; 1;);
if (!0); if (1);
} }
} }

View File

@@ -44,7 +44,7 @@ typeof_in_boolean_context: {
function f2() { return g(), "Yes"; } function f2() { return g(), "Yes"; }
foo(); foo();
console.log(1); console.log(1);
var a = !(console.log(2), !0); var a = !(console.log(2), 1);
foo(); foo();
} }
} }
@@ -57,6 +57,6 @@ issue_1668: {
if (typeof bar); if (typeof bar);
} }
expect: { expect: {
if (!0); if (1);
} }
} }