Skip to content

Commit

Permalink
Fix: no-constant-condition doesn't introspect arrays (fixes #12225) (#…
Browse files Browse the repository at this point in the history
…12307)

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

* Update: no-constant-condition allows for narrower scope of varriable arrays

* Update: no-constant condition now requires direct parent to be binary expression

* Update: no-contant-contidtion now checks parent type before operator, unit tests include more paths
  • Loading branch information
tickct authored and kaicataldo committed Jan 27, 2020
1 parent 10a79a6 commit 80309c3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
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;
}
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) {}",
errors: [{ messageId: "unexpected", type: "UnaryExpression" }]
},
{
code: "if ([,] + ''){}",
errors: [{ messageId: "unexpected", type: "BinaryExpression" }]
}
]
});

0 comments on commit 80309c3

Please sign in to comment.