Merge pull request #3198 from babel/eval-bug-2

Eval bug with lack of confidence considered falsy
This commit is contained in:
Amjad Masad 2015-12-22 22:57:32 -08:00
commit a2030e5804
2 changed files with 19 additions and 4 deletions

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;
}
}

View File

@ -22,5 +22,12 @@ suite("evaluation", function () {
undefined
);
});
test("it should not mistake lack of confidence for falsy", function () {
assert.strictEqual(
getPath("foo || 'bar'").get("body")[0].evaluate().value,
undefined
);
});
});
});