Skip to content

Commit

Permalink
Update: validate void operator in no-constant-condition (fixes #5726) (
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorbal authored and ilyavolodin committed Aug 9, 2016
1 parent 5ef839e commit bf0afcb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
8 changes: 6 additions & 2 deletions docs/rules/no-constant-condition.md
Expand Up @@ -24,6 +24,10 @@ if (false) {
doSomethingUnfinished();
}

if (void x) {
doSomethingUnfinished();
}

for (;-2;) {
doSomethingForever();
}
Expand All @@ -32,7 +36,7 @@ while (typeof x) {
doSomethingForever();
}

do{
do {
doSomethingForever();
} while (x = -1);

Expand All @@ -56,7 +60,7 @@ while (typeof x === "undefined") {
doSomething();
}

do{
do {
doSomething();
} while (x);

Expand Down
10 changes: 9 additions & 1 deletion lib/rules/no-constant-condition.js
Expand Up @@ -51,11 +51,15 @@ module.exports = {
case "Literal":
return (operator === "||" && node.value === true) ||
(operator === "&&" && node.value === false);

case "UnaryExpression":
return (operator === "&&" && node.operator === "void");

case "LogicalExpression":
return isLogicalIdentity(node.left, node.operator) ||
isLogicalIdentity(node.right, node.operator);

// no default
// no default
}
return false;
}
Expand All @@ -78,6 +82,10 @@ module.exports = {
return true;

case "UnaryExpression":
if (node.operator === "void") {
return true;
}

return (node.operator === "typeof" && inBooleanPosition) ||
isConstant(node.argument, true);

Expand Down
18 changes: 16 additions & 2 deletions tests/lib/rules/no-constant-condition.js
Expand Up @@ -37,7 +37,11 @@ ruleTester.run("no-constant-condition", rule, {
"if(typeof x === 'undefined'){}",
"if(a === 'str' && typeof b){}",
"typeof a == typeof b",
"typeof 'a'==='string'|| typeof b ==='string'",
"typeof 'a' === 'string'|| typeof b === 'string'",

// #5726, void conditions
"if (void a || a);",
"if (a || void a);",

// #5693
"if(xyz === 'str1' && abc==='str2'){}",
Expand Down Expand Up @@ -69,6 +73,7 @@ ruleTester.run("no-constant-condition", rule, {
{ code: "if(0 < 1);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement"}] },
{ code: "if(0 || 1);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement"}] },
{ code: "if(a, 1);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement"}] },

{ code: "while([]);", errors: [{ message: "Unexpected constant condition.", type: "WhileStatement"}] },
{ code: "while(~!0);", errors: [{ message: "Unexpected constant condition.", type: "WhileStatement"}] },
{ code: "while(x = 1);", errors: [{ message: "Unexpected constant condition.", type: "WhileStatement"}] },
Expand All @@ -80,9 +85,18 @@ ruleTester.run("no-constant-condition", rule, {
{ code: "if(typeof 'abc' === 'string'){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement"}] },
{ code: "if(a = typeof b){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement"}] },
{ code: "if(a, typeof b){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement"}] },
{ code: "if(typeof 'a' =='string' || typeof 'b' =='string'){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement"}] },
{ code: "if(typeof 'a' == 'string' || typeof 'b' == 'string'){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement"}] },
{ code: "while(typeof x){}", errors: [{ message: "Unexpected constant condition.", type: "WhileStatement"}] },

// #5726, void conditions
{ code: "if(1 || void x);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement"}] },
{ code: "if(void x);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement"}] },
{ code: "if(y = void x);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement"}] },
{ code: "if(x, void x);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement"}] },
{ code: "if(void x === void y);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement"}] },
{ code: "if(void x && a);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement"}] },
{ code: "if(a && void x);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement"}] },

// #5693
{ code: "if(false && abc==='str'){}", errors: [{message: "Unexpected constant condition.", type: "IfStatement"}]},
{ code: "if(true || abc==='str'){}", errors: [{message: "Unexpected constant condition.", type: "IfStatement"}]},
Expand Down

0 comments on commit bf0afcb

Please sign in to comment.