diff --git a/packages/babel-traverse/src/path/evaluation.js b/packages/babel-traverse/src/path/evaluation.js index 67c96f6869..804c40ee6d 100644 --- a/packages/babel-traverse/src/path/evaluation.js +++ b/packages/babel-traverse/src/path/evaluation.js @@ -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; } }