diff --git a/lib/rules/no-constant-binary-expression.js b/lib/rules/no-constant-binary-expression.js index dccfa2f5826..0fa0029c3d7 100644 --- a/lib/rules/no-constant-binary-expression.js +++ b/lib/rules/no-constant-binary-expression.js @@ -45,6 +45,12 @@ function hasConstantNullishness(scope, node) { return (functionName === "Boolean" || functionName === "String" || functionName === "Number") && isReferenceToGlobalVariable(scope, node.callee); } + case "LogicalExpression": { + const isLeftConstant = hasConstantNullishness(scope, node.left); + const isRightConstant = hasConstantNullishness(scope, node.right); + + return isLeftConstant || isRightConstant; + } case "AssignmentExpression": if (node.operator === "=") { return hasConstantNullishness(scope, node.right); diff --git a/tests/lib/rules/no-constant-binary-expression.js b/tests/lib/rules/no-constant-binary-expression.js index c430c7787fd..c44fe499120 100644 --- a/tests/lib/rules/no-constant-binary-expression.js +++ b/tests/lib/rules/no-constant-binary-expression.js @@ -308,6 +308,12 @@ ruleTester.run("no-constant-binary-expression", rule, { { code: "x === /[a-z]/", errors: [{ messageId: "alwaysNew" }] }, // It's not obvious what this does, but it compares the old value of `x` to the new object. - { code: "x === (x = {})", errors: [{ messageId: "alwaysNew" }] } + { code: "x === (x = {})", errors: [{ messageId: "alwaysNew" }] }, + + { code: "window.abc && false && anything", errors: [{ messageId: "constantShortCircuit" }] }, + { code: "window.abc || true || anything", errors: [{ messageId: "constantShortCircuit" }] }, + { code: "window.abc ?? 'non-nullish' ?? 'non-nullish'", errors: [{ messageId: "constantShortCircuit" }] }, + { code: "window.abc ?? undefined ?? 'non-nullish'", errors: [{ messageId: "constantShortCircuit" }] }, + { code: "window.abc ?? null ?? 'non-nullish'", errors: [{ messageId: "constantShortCircuit" }] } ] });