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

Rule Change: [no-loop-func] add an option to ignore IIFEs #16902

Open
1 task done
bradzacher opened this issue Feb 17, 2023 · 6 comments · May be fixed by #17528
Open
1 task done

Rule Change: [no-loop-func] add an option to ignore IIFEs #16902

bradzacher opened this issue Feb 17, 2023 · 6 comments · May be fixed by #17528
Assignees
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules

Comments

@bradzacher
Copy link
Contributor

What rule do you want to change?

no-loop-func

What change to do you want to make?

Generate fewer warnings

How do you think the change should be implemented?

A new default behavior

Example code

/* eslint no-loop-func: 'error' */
let current = getStart();
while (current) {
  (() => {
    current;
    current.a;
    current.b;
    current.c;
    current.d;
  })();
  
  current = current.upper;
}

What does the rule currently do for this code?

the rule will report for this code even though this code is provably safe because the function is immediately invoked.

What will the rule do after it's changed?

the rule will not report for this code as it is provably safe

Participation

  • I am willing to submit a pull request to implement this change.

Additional comments

Playground

Also worth noting that the rule's error message is wonky here. For the example code it reports the same ID many times:

Function declared in a loop contains unsafe references to variable(s) 'current', 'current', 'current', 'current', 'current'. 
@bradzacher bradzacher added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules labels Feb 17, 2023
@mdjermanovic
Copy link
Member

I support the proposal to ignore IIFEs, and I think there's no need for an option (the issue title suggests adding an option).

However, named function expressions could be stored so perhaps we should additionally check if the function is referenced.

let current = getStart();
const arr = [];
while (current) {
  (function f() {
    current;
    arr.push(f); // <--
  })();
  
  current = current.upper;
}

@fasttime
Copy link
Member

I support the proposal to ignore IIFEs, and I think there's no need for an option (the issue title suggests adding an option).

However, named function expressions could be stored so perhaps we should additionally check if the function is referenced.

I think we should also keep reporting on IIFEs with async and generator functions, because their asynchronous state may become accessible from outside, even without a self-reference.

let current = getStart();
const arr = [];
while (current) {
  const p = (async () => {
    await someDelay();
    current;
  })();

  arr.push(p);
  current = current.upper;
}
await Promise.all(arr);

Other that this (and possibly other yet-to-be-discovered edge cases), I also like the proposal.

@github-actions
Copy link

Oops! It looks like we lost track of this issue. What do we want to do here? This issue will auto-close in 7 days without an update.

@github-actions github-actions bot added the Stale label Mar 20, 2023
@nzakas nzakas removed the Stale label Mar 23, 2023
@sam3k sam3k added the accepted There is consensus among the team that this change meets the criteria for inclusion label Mar 25, 2023
@sam3k
Copy link
Contributor

sam3k commented Mar 25, 2023

In the 2023-03-23 TSC meeting we have agreed to:

  1. Accept the proposal
  2. Not add an option and instead make this change a default behavior
  3. Continue warning in the async/generator case

Notes from this meeting will be published in this PR.

@Gautam-Arora24
Copy link
Contributor

Would love to work on this.

@Gautam-Arora24
Copy link
Contributor

Apparently, we should also ignore reporting for this rule, if the function is called (irrespective of its IIFE nature) anywhere inside the loop's scope.

@snitin315 snitin315 self-assigned this Aug 26, 2023
@snitin315 snitin315 linked a pull request Sep 2, 2023 that will close this issue
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
Status: Implementing
7 participants