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: Treat Class/New Expressions as truthy in no-constant-condition #15326

Merged
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
8 changes: 8 additions & 0 deletions docs/rules/no-constant-condition.md
Expand Up @@ -32,6 +32,14 @@ if (x &&= false) {
doSomethingNever();
}

if (class {}) {
doSomethingAlways();
}

if (new Boolean(x)) {
doSomethingAlways();
}

if (x ||= true) {
doSomethingAlways();
}
Expand Down
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 inBooleanPosition;
case "AssignmentExpression":
if (node.operator === "=") {
return isConstant(node.right, inBooleanPosition);
Expand Down
15 changes: 13 additions & 2 deletions tests/lib/rules/no-constant-condition.js
Expand Up @@ -174,7 +174,8 @@ ruleTester.run("no-constant-condition", rule, {
"function* foo() {for (; ; yield) {}}",
"function* foo() {while (true) {function* foo() {yield;}yield;}}",
"function* foo() { for (let x = yield; x < 10; x++) {yield;}yield;}",
"function* foo() { for (let x = yield; ; x++) { yield; }}"
"function* foo() { for (let x = yield; ; x++) { yield; }}",
"if (new Number(x) + 1 === 2) {}"
],
invalid: [
{ code: "for(;true;);", errors: [{ messageId: "unexpected", type: "Literal" }] },
Expand Down Expand Up @@ -387,6 +388,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" }] }

]
});