Permalink
Browse files

Fix: change err report in constant condition (fixes #9398) (#9436)

* Fix: change err report in constant condition (fixes #9398)

* kick off the pr checks

* kick off the pr checks
  • Loading branch information...
VictorHom committed Oct 17, 2017
1 parent da77eb4 commit eb8cfb1cc738753a1e8175926f5ea240fcf51b07
Showing with 50 additions and 50 deletions.
  1. +2 −2 lib/rules/no-constant-condition.js
  2. +48 −48 tests/lib/rules/no-constant-condition.js
@@ -138,7 +138,7 @@ module.exports = {
function checkConstantConditionLoopInSet(node) {
if (loopsInCurrentScope.has(node)) {
loopsInCurrentScope.delete(node);
context.report({ node, message: "Unexpected constant condition." });
context.report({ node: node.test, message: "Unexpected constant condition." });
}
}
@@ -150,7 +150,7 @@ module.exports = {
*/
function reportIfConstant(node) {
if (node.test && isConstant(node.test, true)) {
context.report({ node, message: "Unexpected constant condition." });
context.report({ node: node.test, message: "Unexpected constant condition." });
}
}
@@ -71,90 +71,90 @@ ruleTester.run("no-constant-condition", rule, {
"function* foo() { for (let x = yield; ; x++) { yield; }}"
],
invalid: [
{ code: "for(;true;);", errors: [{ message: "Unexpected constant condition.", type: "ForStatement" }] },
{ code: "do{}while(true)", errors: [{ message: "Unexpected constant condition.", type: "DoWhileStatement" }] },
{ code: "do{}while(t = -2)", errors: [{ message: "Unexpected constant condition.", type: "DoWhileStatement" }] },
{ code: "true ? 1 : 2;", errors: [{ message: "Unexpected constant condition.", type: "ConditionalExpression" }] },
{ code: "q = 0 ? 1 : 2;", errors: [{ message: "Unexpected constant condition.", type: "ConditionalExpression" }] },
{ code: "(q = 0) ? 1 : 2;", errors: [{ message: "Unexpected constant condition.", type: "ConditionalExpression" }] },
{ code: "if(-2);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(true);", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if({});", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ 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" }] },
{ code: "while(function(){});", errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }] },
{ code: "while(true);", errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }] },
{ code: "while(() => {});", errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }] },
{ code: "for(;true;);", errors: [{ message: "Unexpected constant condition.", type: "Literal" }] },
{ code: "do{}while(true)", errors: [{ message: "Unexpected constant condition.", type: "Literal" }] },
{ code: "do{}while(t = -2)", errors: [{ message: "Unexpected constant condition.", type: "AssignmentExpression" }] },
{ code: "true ? 1 : 2;", errors: [{ message: "Unexpected constant condition.", type: "Literal" }] },
{ code: "q = 0 ? 1 : 2;", errors: [{ message: "Unexpected constant condition.", type: "Literal" }] },
{ code: "(q = 0) ? 1 : 2;", errors: [{ message: "Unexpected constant condition.", type: "AssignmentExpression" }] },
{ code: "if(-2);", errors: [{ message: "Unexpected constant condition.", type: "UnaryExpression" }] },
{ code: "if(true);", errors: [{ message: "Unexpected constant condition.", type: "Literal" }] },
{ code: "if({});", errors: [{ message: "Unexpected constant condition.", type: "ObjectExpression" }] },
{ code: "if(0 < 1);", errors: [{ message: "Unexpected constant condition.", type: "BinaryExpression" }] },
{ code: "if(0 || 1);", errors: [{ message: "Unexpected constant condition.", type: "LogicalExpression" }] },
{ code: "if(a, 1);", errors: [{ message: "Unexpected constant condition.", type: "SequenceExpression" }] },
{ code: "while([]);", errors: [{ message: "Unexpected constant condition.", type: "ArrayExpression" }] },
{ code: "while(~!0);", errors: [{ message: "Unexpected constant condition.", type: "UnaryExpression" }] },
{ code: "while(x = 1);", errors: [{ message: "Unexpected constant condition.", type: "AssignmentExpression" }] },
{ code: "while(function(){});", errors: [{ message: "Unexpected constant condition.", type: "FunctionExpression" }] },
{ code: "while(true);", errors: [{ message: "Unexpected constant condition.", type: "Literal" }] },
{ code: "while(() => {});", errors: [{ message: "Unexpected constant condition.", type: "ArrowFunctionExpression" }] },
// #5228 , typeof conditions
{ code: "if(typeof x){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ 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: "while(typeof x){}", errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }] },
{ code: "if(typeof x){}", errors: [{ message: "Unexpected constant condition.", type: "UnaryExpression" }] },
{ code: "if(typeof 'abc' === 'string'){}", errors: [{ message: "Unexpected constant condition.", type: "BinaryExpression" }] },
{ code: "if(a = typeof b){}", errors: [{ message: "Unexpected constant condition.", type: "AssignmentExpression" }] },
{ code: "if(a, typeof b){}", errors: [{ message: "Unexpected constant condition.", type: "SequenceExpression" }] },
{ code: "if(typeof 'a' == 'string' || typeof 'b' == 'string'){}", errors: [{ message: "Unexpected constant condition.", type: "LogicalExpression" }] },
{ code: "while(typeof x){}", errors: [{ message: "Unexpected constant condition.", type: "UnaryExpression" }] },
// #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" }] },
{ code: "if(1 || void x);", errors: [{ message: "Unexpected constant condition.", type: "LogicalExpression" }] },
{ code: "if(void x);", errors: [{ message: "Unexpected constant condition.", type: "UnaryExpression" }] },
{ code: "if(y = void x);", errors: [{ message: "Unexpected constant condition.", type: "AssignmentExpression" }] },
{ code: "if(x, void x);", errors: [{ message: "Unexpected constant condition.", type: "SequenceExpression" }] },
{ code: "if(void x === void y);", errors: [{ message: "Unexpected constant condition.", type: "BinaryExpression" }] },
{ code: "if(void x && a);", errors: [{ message: "Unexpected constant condition.", type: "LogicalExpression" }] },
{ code: "if(a && void x);", errors: [{ message: "Unexpected constant condition.", type: "LogicalExpression" }] },
// #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" }] },
{ code: "if(abc==='str' || true){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(abc==='str' || true || def ==='str'){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(false || true){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(typeof abc==='str' || true){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },
{ code: "if(false && abc==='str'){}", errors: [{ message: "Unexpected constant condition.", type: "LogicalExpression" }] },
{ code: "if(true || abc==='str'){}", errors: [{ message: "Unexpected constant condition.", type: "LogicalExpression" }] },
{ code: "if(abc==='str' || true){}", errors: [{ message: "Unexpected constant condition.", type: "LogicalExpression" }] },
{ code: "if(abc==='str' || true || def ==='str'){}", errors: [{ message: "Unexpected constant condition.", type: "LogicalExpression" }] },
{ code: "if(false || true){}", errors: [{ message: "Unexpected constant condition.", type: "LogicalExpression" }] },
{ code: "if(typeof abc==='str' || true){}", errors: [{ message: "Unexpected constant condition.", type: "LogicalExpression" }] },
{
code: "function* foo(){while(true){} yield 'foo';}",
errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }]
errors: [{ message: "Unexpected constant condition.", type: "Literal" }]
},
{
code: "function* foo(){while(true){if (true) {yield 'foo';}}}",
errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }]
errors: [{ message: "Unexpected constant condition.", type: "Literal" }]
},
{
code: "function* foo(){while(true){yield 'foo';} while(true) {}}",
errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }]
errors: [{ message: "Unexpected constant condition.", type: "Literal" }]
},
{
code: "var a = function* foo(){while(true){} yield 'foo';}",
errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }]
errors: [{ message: "Unexpected constant condition.", type: "Literal" }]
},
{
code: "while (true) { function* foo() {yield;}}",
errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }]
errors: [{ message: "Unexpected constant condition.", type: "Literal" }]
},
{
code: "function* foo(){if (true) {yield 'foo';}}",
errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }]
errors: [{ message: "Unexpected constant condition.", type: "Literal" }]
},
{
code: "function* foo() {for (let foo = yield; true;) {}}",
errors: [{ message: "Unexpected constant condition.", type: "ForStatement" }]
errors: [{ message: "Unexpected constant condition.", type: "Literal" }]
},
{
code: "function* foo() {for (foo = yield; true;) {}}",
errors: [{ message: "Unexpected constant condition.", type: "ForStatement" }]
errors: [{ message: "Unexpected constant condition.", type: "Literal" }]
},
{
code: "function foo() {while (true) {function* bar() {while (true) {yield;}}}}",
errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }]
errors: [{ message: "Unexpected constant condition.", type: "Literal" }]
},
{
code: "function* foo() { for (let foo = 1 + 2 + 3 + (yield); true; baz) {}}",
errors: [{ message: "Unexpected constant condition.", type: "ForStatement" }]
errors: [{ message: "Unexpected constant condition.", type: "Literal" }]
}
]
});

0 comments on commit eb8cfb1

Please sign in to comment.