diff --git a/lib/rules/no-constant-condition.js b/lib/rules/no-constant-condition.js index a88fefeadba..5e583864102 100644 --- a/lib/rules/no-constant-condition.js +++ b/lib/rules/no-constant-condition.js @@ -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 === "+") { diff --git a/tests/lib/rules/no-constant-condition.js b/tests/lib/rules/no-constant-condition.js index 2315853735d..91c690883df 100644 --- a/tests/lib/rules/no-constant-condition.js +++ b/tests/lib/rules/no-constant-condition.js @@ -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);", @@ -99,17 +117,35 @@ 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" }] }, @@ -117,9 +153,15 @@ ruleTester.run("no-constant-condition", rule, { { 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" }] },