Skip to content

Commit

Permalink
feat: Treat ClassExpressions NewExpression as always truthy in no-con…
Browse files Browse the repository at this point in the history
…stant-condition
  • Loading branch information
captbaritone committed Nov 17, 2021
1 parent 388eb7e commit a7fd1fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/rules/no-constant-condition.js
Expand Up @@ -139,6 +139,7 @@ module.exports = {
case "ArrowFunctionExpression":
case "FunctionExpression":
case "ObjectExpression":
case "ClassExpression":
return true;
case "TemplateLiteral":
return (inBooleanPosition && node.quasis.some(quasi => quasi.value.cooked.length)) ||
Expand Down Expand Up @@ -180,7 +181,8 @@ module.exports = {
isLeftShortCircuit ||
isRightShortCircuit;
}

case "NewExpression":
return true;
case "AssignmentExpression":
if (node.operator === "=") {
return isConstant(node.right, inBooleanPosition);
Expand Down
12 changes: 11 additions & 1 deletion tests/lib/rules/no-constant-condition.js
Expand Up @@ -387,6 +387,16 @@ ruleTester.run("no-constant-condition", rule, {
{ code: "if(0b1n);", parserOptions: { ecmaVersion: 11 }, errors: [{ messageId: "unexpected", type: "Literal" }] },
{ code: "if(0o1n);", parserOptions: { ecmaVersion: 11 }, errors: [{ messageId: "unexpected", type: "Literal" }] },
{ code: "if(0x1n);", parserOptions: { ecmaVersion: 11 }, errors: [{ messageId: "unexpected", type: "Literal" }] },
{ code: "if(0x1n || foo);", parserOptions: { ecmaVersion: 11 }, errors: [{ messageId: "unexpected", type: "LogicalExpression" }] }
{ code: "if(0x1n || foo);", parserOptions: { ecmaVersion: 11 }, errors: [{ messageId: "unexpected", type: "LogicalExpression" }] },

// Classes and instances are always truthy
{ code: "if(class {}) {}", errors: [{ messageId: "unexpected" }] },
{ code: "if(new Foo()) {}", errors: [{ messageId: "unexpected" }] },

// Boxed primitives are always truthy
{ code: "if(new Boolean(foo)) {}", errors: [{ messageId: "unexpected" }] },
{ code: "if(new String(foo)) {}", errors: [{ messageId: "unexpected" }] },
{ code: "if(new Number(foo)) {}", errors: [{ messageId: "unexpected" }] }

]
});

0 comments on commit a7fd1fd

Please sign in to comment.