From 96e01e24b2badc6a25a0d21154709e68b9924e6f Mon Sep 17 00:00:00 2001 From: Cyp Date: Tue, 16 Oct 2018 16:01:46 +0200 Subject: [PATCH] fix: Expression x === 'y' && '' should not evaluate to undefined. Fixes https://github.com/babel/minify/issues/908. --- packages/babel-traverse/src/path/evaluation.js | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/packages/babel-traverse/src/path/evaluation.js b/packages/babel-traverse/src/path/evaluation.js index 90029cbe1e3c..a970bd9aee73 100644 --- a/packages/babel-traverse/src/path/evaluation.js +++ b/packages/babel-traverse/src/path/evaluation.js @@ -257,7 +257,7 @@ 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); @@ -265,25 +265,17 @@ function _evaluate(path, state) { 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;