Permalink
Browse files

Fix: false positive in no-constant-condition (fixes #11306) (#11308)

  • Loading branch information...
g-plane authored and btmills committed Feb 1, 2019
1 parent 6567c4f commit 5b14ad1003c7df9a37621dea55c6d6d0484adc05
Showing with 31 additions and 1 deletion.
  1. +19 −1 lib/rules/no-constant-condition.js
  2. +12 −0 tests/lib/rules/no-constant-condition.js
@@ -5,6 +5,13 @@

"use strict";

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

const EQUALITY_OPERATORS = ["===", "!==", "==", "!="];
const RELATIONAL_OPERATORS = [">", "<", ">=", "<=", "in", "instanceof"];

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
@@ -110,7 +117,18 @@ module.exports = {
const isRightShortCircuit = (isRightConstant && isLogicalIdentity(node.right, node.operator));

return (isLeftConstant && isRightConstant) ||
(node.operator === "||" && isRightConstant && node.right.value) || // in the case of an "OR", we need to know if the right constant value is truthy
(

// in the case of an "OR", we need to know if the right constant value is truthy
node.operator === "||" &&
isRightConstant &&
node.right.value &&
(
!node.parent ||
node.parent.type !== "BinaryExpression" ||
!(EQUALITY_OPERATORS.includes(node.parent.operator) || RELATIONAL_OPERATORS.includes(node.parent.operator))
)
) ||
isLeftShortCircuit ||
isRightShortCircuit;
}
@@ -61,6 +61,18 @@ ruleTester.run("no-constant-condition", rule, {
"if(a && 'str'){}",
"if('str' || abc==='str'){}",

// #11306
"if ((foo || 'bar') === 'baz') {}",
"if ((foo || 'bar') !== 'baz') {}",
"if ((foo || 'bar') == 'baz') {}",
"if ((foo || 'bar') != 'baz') {}",
"if ((foo || 233) > 666) {}",
"if ((foo || 233) < 666) {}",
"if ((foo || 233) >= 666) {}",
"if ((foo || 233) <= 666) {}",
"if ((key || 'k') in obj) {}",
"if ((foo || {}) instanceof obj) {}",

// { checkLoops: false }
{ code: "while(true);", options: [{ checkLoops: false }] },
{ code: "for(;true;);", options: [{ checkLoops: false }] },

0 comments on commit 5b14ad1

Please sign in to comment.