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

Update: Fix && vs || short-circuiting false negatives (fixes #13634) #13769

Merged
merged 2 commits into from Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions lib/rules/constructor-super.js
Expand Up @@ -79,6 +79,17 @@ function isPossibleConstructor(node) {
return false;

case "LogicalExpression":

/*
* If the && operator short-circuits, the left side was falsy and therefore not a constructor, and if
* it doesn't short-circuit, it takes the value from the right side, so the right side must always be a
* possible constructor. A future improvement could verify that the left side could be truthy by
* excluding falsy literals.
*/
if (node.operator === "&&") {
return isPossibleConstructor(node.right);
}

return (
isPossibleConstructor(node.left) ||
isPossibleConstructor(node.right)
Expand Down
11 changes: 11 additions & 0 deletions lib/rules/utils/ast-utils.js
Expand Up @@ -1611,6 +1611,17 @@ module.exports = {
}

case "LogicalExpression":

/*
* If the && operator short-circuits, the left side was falsy and therefore not an error, and if it
* doesn't short-circuit, it takes the value from the right side, so the right side must always be
* a plausible error. A future improvement could verify that the left side could be truthy by
* excluding falsy literals.
*/
if (node.operator === "&&") {
return module.exports.couldBeError(node.right);
}

return module.exports.couldBeError(node.left) || module.exports.couldBeError(node.right);

case "ConditionalExpression":
Expand Down
8 changes: 7 additions & 1 deletion tests/lib/rules/constructor-super.js
Expand Up @@ -43,8 +43,10 @@ ruleTester.run("constructor-super", rule, {
"class A extends (B ||= 5) { constructor() { super(); } }",
"class A extends (B ??= 5) { constructor() { super(); } }",
"class A extends (B || C) { constructor() { super(); } }",
"class A extends (B && 5) { constructor() { super(); } }",
"class A extends (5 && B) { constructor() { super(); } }",

// A future improvement could detect the left side as statically falsy, making this invalid.
"class A extends (false && B) { constructor() { super(); } }",
"class A extends (B || 5) { constructor() { super(); } }",
"class A extends (B ?? 5) { constructor() { super(); } }",

Expand Down Expand Up @@ -126,6 +128,10 @@ ruleTester.run("constructor-super", rule, {
code: "class A extends (B = 5) { constructor() { super(); } }",
errors: [{ messageId: "badSuper", type: "CallExpression" }]
},
{
code: "class A extends (B && 5) { constructor() { super(); } }",
errors: [{ messageId: "badSuper", type: "CallExpression" }]
},
{

// `B &&= 5` evaluates either to a falsy value of `B` (which, then, cannot be a constructor), or to '5'
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/no-throw-literal.js
Expand Up @@ -152,6 +152,13 @@ ruleTester.run("no-throw-literal", rule, {
type: "ThrowStatement"
}]
},
{
code: "throw foo && 'literal'", // evaluates either to a falsy value of `foo` (which, then, cannot be an Error object), or to 'literal'
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},

// ConditionalExpression
{
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/prefer-promise-reject-errors.js
Expand Up @@ -117,6 +117,7 @@ ruleTester.run("prefer-promise-reject-errors", rule, {
"Promise.reject(foo &= new Error())",

// evaluates either to a falsy value of `foo` (which, then, cannot be an Error object), or to `5`
"Promise.reject(foo && 5)",
"Promise.reject(foo &&= 5)"

].map(invalidCase => {
Expand Down
5 changes: 4 additions & 1 deletion tests/lib/rules/utils/ast-utils.js
Expand Up @@ -1099,7 +1099,10 @@ describe("ast-utils", () => {
"(1, 2, foo)": true,
"1 && 2": false,
"1 && foo": true,
"foo && 2": true,
"foo && 2": false,

// A future improvement could detect the left side as statically falsy, making this false.
"false && foo": true,
Comment on lines +1104 to +1105
Copy link
Member

Choose a reason for hiding this comment

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

👍

We are already doing that in no-constant-condition for some basic cases here, and it's going to be improved by #13245.

"foo &&= 2": false,
"foo.bar ??= 2": true,
"foo[bar] ||= 2": true,
Expand Down