Fix evaluation bug with ||

This commit is contained in:
Amjad Masad
2015-12-22 20:48:50 -08:00
parent 76eae9bcd8
commit 6ce463399d

View File

@@ -195,27 +195,35 @@ export function evaluate(): { confident: boolean; value: any } {
}
if (path.isLogicalExpression()) {
// If we are confident that one side of an && is false, or one side of
// an || is true, we can be confident about the entire expression
// If we are confident that one side of an && is false, or the left
// side of an || is true, we can be confident about the entire expression
let wasConfident = confident;
let left = evaluate(path.get("left"));
let leftConfident = confident;
confident = wasConfident;
let right = evaluate(path.get("right"));
let rightConfident = confident;
let uncertain = leftConfident !== rightConfident;
confident = leftConfident && rightConfident;
switch (node.operator) {
case "||":
if ((left || right) && uncertain) {
// TODO consider having a "truthy type" that doesn't bail on
// left uncertainity but can still evaluate to truthy.
if (left && leftConfident) {
confident = true;
return left;
}
if (!confident) return;
return left || right;
case "&&":
if ((!left && leftConfident) || (!right && rightConfident)) {
confident = true;
}
if (!confident) return;
return left && right;
}
}