Skip to content

Commit

Permalink
Update: WIP support generators in no constant condition (Fixes eslint…
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorHom committed Jun 14, 2017
1 parent c61194f commit 88c9af6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
27 changes: 26 additions & 1 deletion lib/rules/no-constant-condition.js
Expand Up @@ -5,6 +5,8 @@

"use strict";

const astUtils = require("../ast-utils");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -73,6 +75,9 @@ module.exports = {
* @private
*/
function isConstant(node, inBooleanPosition) {
console.log(node)

// console.log("text", sourceCode.getText(node));
switch (node.type) {
case "Literal":
case "ArrowFunctionExpression":
Expand Down Expand Up @@ -121,6 +126,20 @@ module.exports = {
* @private
*/
function checkConstantCondition(node) {
console.log(Object.keys(node))
const sourceCode = context.getSourceCode();
// determine if it is a generator function
// console.log()
const upperFunction = astUtils.getUpperFunction(node);
if (upperFunction && upperFunction.generator) {
// isConstant(node.test, true)
// sourceCode.getTokensAfter(node.test, countOptions)
// locate position of the yield

console.log(sourceCode.getTokensAfter(node.test, {count: 3}))
console.log("hello")
}
// console.log(node.body)
if (node.test && isConstant(node.test, true)) {
context.report({ node, message: "Unexpected constant condition." });
}
Expand All @@ -138,6 +157,11 @@ module.exports = {
}
}

function checkFunction(node) {
console.log("whatttt")
console.log(node);
}

//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
Expand All @@ -147,7 +171,8 @@ module.exports = {
IfStatement: checkConstantCondition,
WhileStatement: checkLoop,
DoWhileStatement: checkLoop,
ForStatement: checkLoop
ForStatement: checkLoop,
GeneratorFunction: checkFunction,
};

}
Expand Down
9 changes: 8 additions & 1 deletion tests/lib/rules/no-constant-condition.js
Expand Up @@ -78,6 +78,7 @@ ruleTester.run("no-constant-condition", rule, {
{ 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(() => {});", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Unexpected constant condition.", type: "WhileStatement" }] },

// #5228 , typeof conditions
Expand All @@ -103,6 +104,12 @@ ruleTester.run("no-constant-condition", rule, {
{ 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(typeof abc==='str' || true){}", errors: [{ message: "Unexpected constant condition.", type: "IfStatement" }] },

// #2375
{ code: "function* foo(){while(true){} yield 'foo';}", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Unexpected constant condition.", type: "GeneratorFunction" }] },
{ code: "function* foo(){while(true){yield 'foo';}}", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Unexpected constant condition.", type: "GeneratorFunction" }] },
{ code: "var a = function* foo(){while(true){} yield 'foo';}", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Unexpected constant condition.", type: "GeneratorFunction" }] },

]
});

0 comments on commit 88c9af6

Please sign in to comment.