Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: report more cases with ?? in no-constant-binary-expression #16826

Merged
merged 8 commits into from Feb 13, 2023
6 changes: 6 additions & 0 deletions lib/rules/no-constant-binary-expression.js
Expand Up @@ -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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These would be false positives, as the left-hand sides of these ?? do not have constant nullishness so the expressions neither always nor never short-circuit:

const first = (foo && true) ?? bar;
//             ^^^^^^^^^^^ Unexpected constant nullishness on the left-hand side of a `??` expression.

const second = null ?? foo ?? bar;
//             ^^^^^^^^^^^ Unexpected constant nullishness on the left-hand side of a `??` expression.

const third = foo ?? null ?? bar;
//            ^^^^^^^^^^^ Unexpected constant nullishness on the left-hand side of a `??` expression.

We should check if the operator is ?? and if its right side is non-nullish.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for review! I tried to fix in 7c91ba8.

case "AssignmentExpression":
if (node.operator === "=") {
return hasConstantNullishness(scope, node.right);
Expand Down
8 changes: 7 additions & 1 deletion tests/lib/rules/no-constant-binary-expression.js
Expand Up @@ -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" }] }
]
});