Skip to content

Commit

Permalink
fix: handle more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Jan 1, 2024
1 parent 0fd38e9 commit 7a9f721
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
26 changes: 12 additions & 14 deletions lib/rules/no-loop-func.js
Expand Up @@ -9,6 +9,15 @@
// Helpers
//------------------------------------------------------------------------------

/**
* Identifies is a node is a FunctionExpression which is part of an IIFE
* @param {ASTNode} node Node to test
* @returns {boolean} True if it's an IIFE
*/
function isIIFE(node) {
return (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") && node.parent && node.parent.type === "CallExpression" && node.parent.callee === node;
}

/**
* Gets the containing loop node of a specified node.
*
Expand Down Expand Up @@ -47,10 +56,10 @@ function getContainingLoopNode(node) {
case "ArrowFunctionExpression":
case "FunctionExpression":
case "FunctionDeclaration":

// We don't need to check nested functions.
if (isIIFE(node)) {
break;
}
return null;

default:
break;
}
Expand Down Expand Up @@ -144,17 +153,6 @@ function isSafe(loopNode, reference) {
return Boolean(variable) && variable.references.every(isSafeReference);
}

/**
* Identifies is a node is a FunctionExpression which is part of an IIFE
* @param {ASTNode} node Node to test
* @returns {boolean} True if it's an IIFE
*/
function isIIFE(node) {

// console.log("🚀 ~ isIIFE ~ node:", (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") && node.parent && node.parent.type === "CallExpression" && node.parent.callee === node)
return (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") && node.parent && node.parent.type === "CallExpression" && node.parent.callee === node;
}


//------------------------------------------------------------------------------
// Rule Definition
Expand Down
53 changes: 42 additions & 11 deletions tests/lib/rules/no-loop-func.js
Expand Up @@ -206,12 +206,8 @@ ruleTester.run("no-loop-func", rule, {
})());
}
`,
<<<<<<< HEAD
languageOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "ArrowFunctionExpression" }]
=======
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "ArrowFunctionExpression" }],
languageOptions: { ecmaVersion: 6 }

Check failure on line 210 in tests/lib/rules/no-loop-func.js

View workflow job for this annotation

GitHub Actions / Verify Files

The properties of a test case should be placed in a consistent order: [code, languageOptions, errors]
>>>>>>> 70f44c33f (refactor: remove false negatives)
}

],
Expand Down Expand Up @@ -411,8 +407,8 @@ ruleTester.run("no-loop-func", rule, {
})());
}
`,
languageOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "ArrowFunctionExpression" }]
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "ArrowFunctionExpression" }],
languageOptions: { ecmaVersion: 6 }
},
{
code: `
Expand All @@ -424,8 +420,8 @@ ruleTester.run("no-loop-func", rule, {
})());
}
`,
languageOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "ArrowFunctionExpression" }]
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "ArrowFunctionExpression" }],
languageOptions: { ecmaVersion: 6 }
},
{
code: `
Expand All @@ -437,8 +433,43 @@ ruleTester.run("no-loop-func", rule, {
})());
}
`,
languageOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "ArrowFunctionExpression" }]
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "ArrowFunctionExpression" }],
languageOptions: { ecmaVersion: 6 }
},
{
code: `
var arr = [];
for (var i = 0; i < 5; i ++) {
(() => {
arr.push((async () => {
await 1;
return i;
})());
})();
}
`,
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "ArrowFunctionExpression" }],
languageOptions: { ecmaVersion: 2022 }
},
{
code: `
var arr = [];
for (var i = 0; i < 5; i ++) {
(() => {
(function f() {
if (!arr.includes(f)) {
arr.push(f);
}
return i;
})();
})();
}
`,
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "FunctionExpression" }],
languageOptions: { ecmaVersion: 2022 }
}
]
});

0 comments on commit 7a9f721

Please sign in to comment.