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

Conversation

Gautam-Arora24
Copy link
Contributor

@Gautam-Arora24 Gautam-Arora24 commented Jul 27, 2023

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[X] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)

The change proposes to ignore IIFE's inside the loop's block. However, if the function inside the loop is an async or a generator function, the rule still continues to report error.

Fixes: #16902

Is there anything you'd like reviewers to focus on?

@Gautam-Arora24 Gautam-Arora24 requested a review from a team as a code owner July 27, 2023 18:10
@eslint-github-bot eslint-github-bot bot added the feature This change adds a new feature to ESLint label Jul 27, 2023
@netlify
Copy link

netlify bot commented Jul 27, 2023

Deploy Preview for docs-eslint ready!

Name Link
🔨 Latest commit 6f2fb43
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/64c2b303dc39740008c5cf58
😎 Deploy Preview https://deploy-preview-17426--docs-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@aladdin-add aladdin-add added the accepted There is consensus among the team that this change meets the criteria for inclusion label Jul 28, 2023
@mdjermanovic mdjermanovic added the rule Relates to ESLint's core rules label Jul 28, 2023
@@ -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).

@mdjermanovic
Copy link
Member

Also, references in nested functions should still be considered unsafe, unless perhaps they're also immediately executed.

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

var arr = [];

for (var i = 0; i < 5; i++) {
    arr.push((() => {
        return () => i; // false negative
    })());
}

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

@mdjermanovic
Copy link
Member

Also, we should check if the function is referenced (#16902 (comment)).

Another example:

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

var arr = [];

for (var i = 0; i < 5; i++) {
    (function fun () {
        if (arr.includes(fun)) return i; // false negative
        else arr.push(fun);
    })();
}

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

@Gautam-Arora24
Copy link
Contributor Author

Also, we should check if the function is referenced (#16902 (comment)).

Another example:

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

var arr = [];

for (var i = 0; i < 5; i++) {
    (function fun () {
        if (arr.includes(fun)) return i; // false negative
        else arr.push(fun);
    })();
}

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

Makes sense, probably we can maintain a functionStack to keep track of currently running functions and use it to find if an IIFE is being referenced inside it or not.

@github-actions
Copy link

github-actions bot commented Aug 8, 2023

Hi everyone, it looks like we lost track of this pull request. Please review and see what the next steps are. This pull request will auto-close in 7 days without an update.

@github-actions github-actions bot added the Stale label Aug 8, 2023
@Rec0iL99
Copy link
Member

Hi @Gautam-Arora24, are you still working on this?

@Rec0iL99 Rec0iL99 removed the Stale label Aug 10, 2023
@Gautam-Arora24
Copy link
Contributor Author

Hey @Rec0iL99! Apologies, not getting enough time to work on the PR.

@snitin315
Copy link
Contributor

@Gautam-Arora24 No worries, I'll finish the PR.

@snitin315 snitin315 self-assigned this Aug 19, 2023
@snitin315
Copy link
Contributor

Thanks for the PR, closing in favor of #17528

@snitin315 snitin315 closed this Sep 2, 2023
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Mar 1, 2024
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Mar 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion contributor pool feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

Rule Change: [no-loop-func] add an option to ignore IIFEs
5 participants