Skip to content

Commit

Permalink
fix: Expression x === 'y' && '' should not evaluate to undefined.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyp committed Oct 16, 2018
1 parent 5fa3628 commit 96e01e2
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions packages/babel-traverse/src/path/evaluation.js
Expand Up @@ -257,33 +257,25 @@ function _evaluate(path, state) {
}

if (path.isLogicalExpression()) {
// If we are confident that one side of an && is false, or the left
// If we are confident that the left side of an && is false, or the left
// side of an || is true, we can be confident about the entire expression
const wasConfident = state.confident;
const left = evaluateCached(path.get("left"), state);
const leftConfident = state.confident;
state.confident = wasConfident;
const right = evaluateCached(path.get("right"), state);
const rightConfident = state.confident;
state.confident = leftConfident && rightConfident;

switch (node.operator) {
case "||":
// TODO consider having a "truthy type" that doesn't bail on
// left uncertainty but can still evaluate to truthy.
if (left && leftConfident) {
state.confident = true;
return left;
}

state.confident = leftConfident && (!!left || rightConfident);
if (!state.confident) return;

return left || right;
case "&&":
if ((!left && leftConfident) || (!right && rightConfident)) {
state.confident = true;
}
state.confident = leftConfident && (!left || rightConfident);
if (!state.confident) return;
return left && right;
Expand Down

0 comments on commit 96e01e2

Please sign in to comment.