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

Fix: no-constant-condition doesn't introspect arrays (fixes #12225) #12307

Merged
merged 4 commits into from Jan 27, 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
13 changes: 12 additions & 1 deletion lib/rules/no-constant-condition.js
Expand Up @@ -90,14 +90,25 @@ module.exports = {
* @private
*/
function isConstant(node, inBooleanPosition) {

// node.elements can return null values in the case of sparse arrays ex. [,]
if (!node) {
return true;
}
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
switch (node.type) {
case "Literal":
case "ArrowFunctionExpression":
case "FunctionExpression":
case "ObjectExpression":
case "ArrayExpression":
return true;

case "ArrayExpression": {
if (node.parent.type === "BinaryExpression" && node.parent.operator === "+") {
return node.elements.every(element => isConstant(element, false));
}
return true;
}

case "UnaryExpression":
if (node.operator === "void") {
return true;
Expand Down
43 changes: 43 additions & 0 deletions tests/lib/rules/no-constant-condition.js
Expand Up @@ -73,6 +73,15 @@ ruleTester.run("no-constant-condition", rule, {
"if ((key || 'k') in obj) {}",
"if ((foo || {}) instanceof obj) {}",

// #12225
"if ('' + [y] === '' + [ty]) {}",
"if ('a' === '' + [ty]) {}",
"if ('' + [y, m, d] === 'a') {}",
"if ('' + [y, 'm'] === '' + [ty, 'tm']) {}",
"if ('' + [y, 'm'] === '' + ['ty']) {}",
"if ([,] in\n\n($2))\n ;\nelse\n ;",
"if ([...x]+'' === 'y'){}",

// { checkLoops: false }
{ code: "while(true);", options: [{ checkLoops: false }] },
{ code: "for(;true;);", options: [{ checkLoops: false }] },
Expand Down Expand Up @@ -183,6 +192,40 @@ ruleTester.run("no-constant-condition", rule, {
{
code: "function* foo() { for (let foo = 1 + 2 + 3 + (yield); true; baz) {}}",
errors: [{ messageId: "unexpected", type: "Literal" }]
},

// #12225
{
code: "if([a]) {}",
errors: [{ messageId: "unexpected", type: "ArrayExpression" }]
},
{
code: "if([]) {}",
errors: [{ messageId: "unexpected", type: "ArrayExpression" }]
},
{
code: "if(''+['a']) {}",
errors: [{ messageId: "unexpected", type: "BinaryExpression" }]
},
{
code: "if(''+[]) {}",
errors: [{ messageId: "unexpected", type: "BinaryExpression" }]
},
{
code: "if([a]==[a]) {}",
errors: [{ messageId: "unexpected", type: "BinaryExpression" }]
},
{
code: "if([a] - '') {}",
errors: [{ messageId: "unexpected", type: "BinaryExpression" }]
},
{
code: "if(+1) {}",
Copy link
Member

Choose a reason for hiding this comment

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

I think this was meant to be +[a]? (a test to ensure that the change in this PR applies only to binary +)

errors: [{ messageId: "unexpected", type: "UnaryExpression" }]
},
{
code: "if ([,] + ''){}",
errors: [{ messageId: "unexpected", type: "BinaryExpression" }]
}
]
});