Skip to content

Commit

Permalink
boolean_expression ? true : false --> boolean_expression
Browse files Browse the repository at this point in the history
  • Loading branch information
kzc authored and Richard van Velzen committed Feb 22, 2016
1 parent 5486b68 commit 11b0efd
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -2651,6 +2651,10 @@ merge(Compressor.prototype, {

// y?true:false --> !!y
if (is_true(consequent) && is_false(alternative)) {
if (self.condition.is_boolean()) {
// boolean_expression ? true : false --> boolean_expression
return self.condition;
}
self.condition = self.condition.negate(compressor);
return make_node(AST_UnaryPrefix, self.condition, {
operator: "!",
Expand Down
74 changes: 74 additions & 0 deletions test/compress/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,3 +738,77 @@ conditional_or: {
a = condition + 3 || null;
}
}

trivial_boolean_ternary_expressions : {
options = {
conditionals: true,
evaluate : true,
booleans : true
};
input: {
f('foo' in m ? true : false);
f('foo' in m ? false : true);

f(g ? true : false);
f(foo() ? true : false);
f("bar" ? true : false);
f(5 ? true : false);
f(5.7 ? true : false);
f(x - y ? true : false);

f(x == y ? true : false);
f(x === y ? !0 : !1);
f(x < y ? !0 : false);
f(x <= y ? true : false);
f(x > y ? true : !1);
f(x >= y ? !0 : !1);

f(g ? false : true);
f(foo() ? false : true);
f("bar" ? false : true);
f(5 ? false : true);
f(5.7 ? false : true);
f(x - y ? false : true);

f(x == y ? !1 : !0);
f(x === y ? false : true);

f(x < y ? false : true);
f(x <= y ? false : !0);
f(x > y ? !1 : true);
f(x >= y ? !1 : !0);
}
expect: {
f('foo' in m);
f(!('foo' in m));

f(!!g);
f(!!foo());
f(!0);
f(!0);
f(!0);
f(!!(x - y));

f(x == y);
f(x === y);
f(x < y);
f(x <= y);
f(x > y);
f(x >= y);

f(!g);
f(!foo());
f(!1);
f(!1);
f(!1);
f(!(x - y));

f(x != y);
f(x !== y);

f(!(x < y));
f(!(x <= y));
f(!(x > y));
f(!(x >= y));
}
}

0 comments on commit 11b0efd

Please sign in to comment.