Skip to content

Commit

Permalink
Updated pass so that left and right values of logical operators in pr…
Browse files Browse the repository at this point in the history
…esence of

multiple identical operators do not have to be pureBooleanValues for folding to occur.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=161527912
  • Loading branch information
bellashim authored and Tyler Breisacher committed Jul 11, 2017
1 parent fa8e6a1 commit 7bd9db6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/com/google/javascript/jscomp/PeepholeFoldConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,12 @@ private Node tryFoldAndOr(Node n, Node left, Node right) {
result = IR.comma(left, right);
}
} else if (n.getParent().getToken() == type) {
if ((right.isFalse() && type == Token.OR)
|| (right.isTrue() && type == Token.AND)) {
result = left;
TernaryValue rightValue = NodeUtil.getImpureBooleanValue(right);
if (!mayHaveSideEffects(right)) {
if ((rightValue == TernaryValue.FALSE && type == Token.OR)
|| (rightValue == TernaryValue.TRUE && type == Token.AND)) {
result = left;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,17 +531,16 @@ public void testFoldLogicalOp() {
fold("x = foo() || true || bar()", "x = foo() || true");
fold("x = foo() && false && bar()", "x = foo() && false");
fold("x = foo() && 0 && bar()", "x = foo() && 0");
foldSame("x = foo() && 1 && bar()");
foldSame("x = foo() || 0 || bar()");
fold("x = foo() && 1 && bar()", "x = foo() && bar()");
fold("x = foo() || 0 || bar()", "x = foo() || bar()");
fold("x = foo() || 1 || bar()", "x = foo() || 1");
foldSame("x = foo() || bar() || baz()");
foldSame("x = foo() && bar() && baz()");

fold ("0 || b()", "b()");
fold("1 && b()", "b()");
fold("a() && (1 && b())", "a() && b()");
// TODO(johnlenz): Consider folding the following to:
// "(a(),1) && b();
foldSame("(a() && 1) && b()");
fold("(a() && 1) && b()", "a() && b()");

// Really not foldable, because it would change the type of the
// expression if foo() returns something truthy but not true.
Expand Down

0 comments on commit 7bd9db6

Please sign in to comment.