Skip to content

Commit

Permalink
Update: validate void operator in no-constant-condition (Fixes eslint…
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorbal committed Apr 1, 2016
1 parent e54598a commit 9c8f211
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion docs/rules/no-constant-condition.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ if (false) {
doSomethingUnfinished();
}

if (void x) {
doSomethingUnfinished();
}

for (;true;) {
doSomethingForever();
}
Expand All @@ -34,7 +38,7 @@ while (-2) {
doSomethingForever();
}

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

Expand Down
3 changes: 3 additions & 0 deletions lib/rules/no-constant-condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ module.exports = function(context) {
return true;

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

case "BinaryExpression":
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/no-constant-condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ ruleTester.run("no-constant-condition", rule, {
"if(a = f());",
"if(1, a);",
"if ('every' in []);",
"if (a || void a);",
"while(~!a);",
"while(a = b);",
"for(;x < 10;);",
Expand All @@ -47,6 +48,11 @@ 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: "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: "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 Down

0 comments on commit 9c8f211

Please sign in to comment.