Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: check template literal in no-constant-condition (fixes #12815) (
#12816)

* Fix: check template literal in no-constant-condition (fixes #12815)

* add test cases

* add tagged template test cases

* treats truthy cases

* check template literal truthy cases

* fix typo, add test cases for cooked
  • Loading branch information
yeonjuan authored and kaicataldo committed Jan 27, 2020
1 parent 80309c3 commit 0460748
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/rules/no-constant-condition.js
Expand Up @@ -101,6 +101,9 @@ module.exports = {
case "FunctionExpression":
case "ObjectExpression":
return true;
case "TemplateLiteral":
return (inBooleanPosition && node.quasis.some(quasi => quasi.value.cooked.length)) ||
node.expressions.every(exp => isConstant(exp, inBooleanPosition));

case "ArrayExpression": {
if (node.parent.type === "BinaryExpression" && node.parent.operator === "+") {
Expand Down
42 changes: 42 additions & 0 deletions tests/lib/rules/no-constant-condition.js
Expand Up @@ -25,19 +25,37 @@ ruleTester.run("no-constant-condition", rule, {
"if(a = f());",
"if(1, a);",
"if ('every' in []);",
"if (`\\\n${a}`) {}",
"if (`${a}`);",
"if (`${foo()}`);",
"if (`${a === 'b' && b==='a'}`);",
"if (`foo${a}` === 'fooa');",
"if (tag`a`);",
"if (tag`${a}`);",
"while(~!a);",
"while(a = b);",
"while(`${a}`);",
"for(;x < 10;);",
"for(;;);",
"for(;`${a}`;);",
"do{ }while(x)",
"q > 0 ? 1 : 2;",
"`${a}` === a ? 1 : 2",
"`foo${a}` === a ? 1 : 2",
"tag`a` === a ? 1 : 2",
"tag`${a}` === a ? 1 : 2",
"while(x += 3) {}",
"while(tag`a`) {}",
"while(tag`${a}`) {}",
"while(`\\\n${a}`) {}",

// #5228, typeof conditions
"if(typeof x === 'undefined'){}",
"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);",
Expand Down Expand Up @@ -99,27 +117,51 @@ ruleTester.run("no-constant-condition", rule, {
],
invalid: [
{ code: "for(;true;);", errors: [{ messageId: "unexpected", type: "Literal" }] },
{ code: "for(;``;);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "for(;`foo`;);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "for(;`foo${bar}`;);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "do{}while(true)", errors: [{ messageId: "unexpected", type: "Literal" }] },
{ code: "do{}while(t = -2)", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] },
{ code: "do{}while(``)", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "do{}while(`foo`)", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "do{}while(`foo${bar}`)", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "true ? 1 : 2;", errors: [{ messageId: "unexpected", type: "Literal" }] },
{ code: "q = 0 ? 1 : 2;", errors: [{ messageId: "unexpected", type: "Literal" }] },
{ code: "(q = 0) ? 1 : 2;", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] },
{ code: "`` ? 1 : 2;", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "`foo` ? 1 : 2;", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "`foo${bar}` ? 1 : 2;", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "if(-2);", errors: [{ messageId: "unexpected", type: "UnaryExpression" }] },
{ code: "if(true);", errors: [{ messageId: "unexpected", type: "Literal" }] },
{ code: "if({});", errors: [{ messageId: "unexpected", type: "ObjectExpression" }] },
{ code: "if(0 < 1);", errors: [{ messageId: "unexpected", type: "BinaryExpression" }] },
{ code: "if(0 || 1);", errors: [{ messageId: "unexpected", type: "LogicalExpression" }] },
{ code: "if(a, 1);", errors: [{ messageId: "unexpected", type: "SequenceExpression" }] },
{ code: "if(`foo`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "if(``);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "if(`\\\n`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "if(`${'bar'}`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "if(`${'bar' + `foo`}`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "if(`foo${false || true}`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "if(`foo${bar}`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "if(`${bar}foo`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },


{ code: "while([]);", errors: [{ messageId: "unexpected", type: "ArrayExpression" }] },
{ code: "while(~!0);", errors: [{ messageId: "unexpected", type: "UnaryExpression" }] },
{ code: "while(x = 1);", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] },
{ code: "while(function(){});", errors: [{ messageId: "unexpected", type: "FunctionExpression" }] },
{ code: "while(true);", errors: [{ messageId: "unexpected", type: "Literal" }] },
{ code: "while(() => {});", errors: [{ messageId: "unexpected", type: "ArrowFunctionExpression" }] },
{ code: "while(`foo`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "while(``);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "while(`${'foo'}`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "while(`${'foo' + 'bar'}`);", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },

// #5228 , typeof conditions
{ code: "if(typeof x){}", errors: [{ messageId: "unexpected", type: "UnaryExpression" }] },
{ code: "if(`${typeof x}`){}", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "if(`${''}${typeof x}`){}", errors: [{ messageId: "unexpected", type: "TemplateLiteral" }] },
{ code: "if(typeof 'abc' === 'string'){}", errors: [{ messageId: "unexpected", type: "BinaryExpression" }] },
{ code: "if(a = typeof b){}", errors: [{ messageId: "unexpected", type: "AssignmentExpression" }] },
{ code: "if(a, typeof b){}", errors: [{ messageId: "unexpected", type: "SequenceExpression" }] },
Expand Down

0 comments on commit 0460748

Please sign in to comment.