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
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/src/rules/no-constant-binary-expression.md
Expand Up @@ -53,6 +53,12 @@ const value4 = new Boolean(foo) === true;
const objIsEmpty = someObj === {};

const arrIsEmpty = someArr === [];

const shortCircuit1 = condition1 && false && condition2;

const shortCircuit2 = condition1 || true || condition2;

const shortCircuit3 = condition1 && "non-nullish" && condition2;
nissy-dev marked this conversation as resolved.
Show resolved Hide resolved
```

:::
Expand Down
38 changes: 20 additions & 18 deletions lib/rules/no-constant-binary-expression.js
Expand Up @@ -14,6 +14,23 @@ const NUMERIC_OR_STRING_BINARY_OPERATORS = new Set(["+", "-", "*", "/", "%", "|"
// Helpers
//------------------------------------------------------------------------------

/**
* Checks whether or not a node is `null` or `undefined`. Similar to the one
* found in ast-utils.js, but this one correctly handles the edge case that
* `undefined` has been redefined.
* @param {Scope} scope Scope in which the expression was found.
* @param {ASTNode} node A node to check.
* @returns {boolean} Whether or not the node is a `null` or `undefined`.
* @public
*/
function isNullOrUndefined(scope, node) {
return (
isNullLiteral(node) ||
(node.type === "Identifier" && node.name === "undefined" && isReferenceToGlobalVariable(scope, node)) ||
(node.type === "UnaryExpression" && node.operator === "void")
);
}

/**
* Test if an AST node has a statically knowable constant nullishness. Meaning,
* it will always resolve to a constant value of either: `null`, `undefined`
Expand Down Expand Up @@ -45,6 +62,9 @@ function hasConstantNullishness(scope, node) {
return (functionName === "Boolean" || functionName === "String" || functionName === "Number") &&
isReferenceToGlobalVariable(scope, node.callee);
}
case "LogicalExpression": {
return node.operator === "??" && (hasConstantNullishness(scope, node.right) && !isNullOrUndefined(scope, node.right));
}
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:

/*eslint no-constant-binary-expression: "error"*/

const foo = a ?? (doSomething(), undefined) ?? b;
//          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

const bar = a ?? (something = null) ?? b;
//          ^^^^^^^^^^^^^^^^^^^^^^^

case "AssignmentExpression":
if (node.operator === "=") {
return hasConstantNullishness(scope, node.right);
Expand Down Expand Up @@ -378,24 +398,6 @@ function isAlwaysNew(scope, node) {
}
}

/**
* Checks whether or not a node is `null` or `undefined`. Similar to the one
* found in ast-utils.js, but this one correctly handles the edge case that
* `undefined` has been redefined.
* @param {Scope} scope Scope in which the expression was found.
* @param {ASTNode} node A node to check.
* @returns {boolean} Whether or not the node is a `null` or `undefined`.
* @public
*/
function isNullOrUndefined(scope, node) {
return (
isNullLiteral(node) ||
(node.type === "Identifier" && node.name === "undefined" && isReferenceToGlobalVariable(scope, node)) ||
(node.type === "UnaryExpression" && node.operator === "void")
);
}


/**
* Checks if one operand will cause the result to be constant.
* @param {Scope} scope Scope in which the expression was found.
Expand Down
10 changes: 8 additions & 2 deletions tests/lib/rules/no-constant-binary-expression.js
Expand Up @@ -59,7 +59,9 @@ ruleTester.run("no-constant-binary-expression", rule, {
"function foo(undefined) { undefined === true;}",
"[...arr, 1] == true",
"[,,,] == true",
{ code: "new Foo() === bar;", globals: { Foo: "writable" } }
{ code: "new Foo() === bar;", globals: { Foo: "writable" } },
"(foo && true) ?? bar",
"foo ?? null ?? bar"
],
invalid: [

Expand Down Expand Up @@ -308,6 +310,10 @@ 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" }] }
nissy-dev marked this conversation as resolved.
Show resolved Hide resolved
]
});