Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ignore IIFE's for no-loop-func rule #17426

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/rules/no-loop-func.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

"use strict";

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

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -144,6 +146,18 @@ function isSafe(loopNode, reference) {
return Boolean(variable) && variable.references.every(isSafeReference);
}

/**
* Determines if a given expression node is an IIFE
* @param {ASTNode} node The node to check
* @returns {boolean} `true` if the given node is an IIFE
*/
function isIIFE(node) {
const maybeCallNode = astUtils.skipChainExpression(node);

return maybeCallNode.type === "CallExpression" && (maybeCallNode.callee.type === "FunctionExpression" ||
maybeCallNode.callee.type === "ArrowFunctionExpression");
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -185,6 +199,10 @@ module.exports = {
return;
}

if (!node.async && !node.generator && isIIFE(node.parent)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a false negative:

/* eslint no-loop-func: "error" */

var arr = [];

for (var i = 0; i < 5; i++) {
    arr.push((f => f)(() => i)); // false negative, this is not a call of `() => i`
}

console.log(arr.map(f => f())); // [ 5, 5, 5, 5, 5 ]

Parent of () => i is a function call, but not a call of that function (() => i is an argument, not callee).

return;
}

const references = sourceCode.getScope(node).through;
const unsafeRefs = references.filter(r => r.resolved && !isSafe(loopNode, r)).map(r => r.identifier.name);

Expand Down
32 changes: 22 additions & 10 deletions tests/lib/rules/no-loop-func.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ ruleTester.run("no-loop-func", rule, {
].join("\n"),
parserOptions: { ecmaVersion: 6 }
},
{
code: "for (var i=0; (function() { i; })(), i<l; i++) { }",
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "FunctionExpression" }]
},

{
code: "for (let i = 0; i < 10; ++i) { (()=>{ i;})() }",
parserOptions: { ecmaVersion: 6 }
},
{
code: "for (let i = 0; i < 10; ++i) { (function a(){i;})() }",
parserOptions: { ecmaVersion: 6 }
},

/*
* These loops _look_ like they might be unsafe, but because i is undeclared, they're fine
Expand Down Expand Up @@ -149,13 +162,15 @@ ruleTester.run("no-loop-func", rule, {
code: "for (let i in {}) { i = 7; (function() { undeclared; }) }",
parserOptions: { ecmaVersion: 6 }
},

/* The references to loop variable are inside an IIFE so they are safe */
{
code: "for (const i of {}) { (function() { undeclared; }) }",
parserOptions: { ecmaVersion: 6 }
},
{
code: "for (let i = 0; i < 10; ++i) { for (let x in xs.filter(x => x != undeclared)) { } }",
parserOptions: { ecmaVersion: 6 }
code: "for (var i=0; i<l; (function() { i; })(), i++) { }",
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "FunctionExpression" }]
}

],
Expand Down Expand Up @@ -190,14 +205,6 @@ ruleTester.run("no-loop-func", rule, {
code: "for (var i=0; i < l; i++) { function a() { i; }; a(); }",
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "FunctionDeclaration" }]
},
{
code: "for (var i=0; (function() { i; })(), i<l; i++) { }",
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "FunctionExpression" }]
},
{
code: "for (var i=0; i<l; (function() { i; })(), i++) { }",
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "FunctionExpression" }]
},

// Warns functions which are using modified variables.
{
Expand Down Expand Up @@ -269,6 +276,11 @@ ruleTester.run("no-loop-func", rule, {
code: "let a; function foo() { a = 10; for (let x of xs) { (function() { a; }); } } foo();",
parserOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unsafeRefs", data: { varNames: "'a'" }, type: "FunctionExpression" }]
},
{
code: "let a; for (var i=0; i<l; i++) { (function* (){i;})() }",
parserOptions: { ecmaVersion: 6 },
errors: [{ messageId: "unsafeRefs", data: { varNames: "'i'" }, type: "FunctionExpression" }]
}
]
});