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

[no-loop-func] New false positive in 4.0.0 when using a global type in a function in a loop #2448

Closed
3 tasks done
lencioni opened this issue Aug 31, 2020 · 1 comment · Fixed by #2490
Closed
3 tasks done
Assignees
Labels
enhancement: new base rule extension New base rule extension required to handle a TS specific case has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@lencioni
Copy link

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "rules": {
    "no-loop-func": "error"
  }
}
let someArray: MyType[] = [];
for (let i = 0; i < 10; i += 1) {
  someArray = someArray.filter((item: MyType) => !!item);
}

It is important here that the type is defined globally for the project in a different file. If the type is defined in the same file, the problem does not appear.

Expected Result

no-loop-func does not report an error

Actual Result

no-loop-func reports the following error:

Function declared in a loop contains unsafe references to variable(s) 'MyType'.

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 4.0.1
@typescript-eslint/parser 4.0.1
TypeScript 3.9.3
ESLint 7.7.0
node 12.16.2

I suspect this could be related to #2039 maybe?

@lencioni lencioni added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Aug 31, 2020
@bradzacher bradzacher added bug Something isn't working enhancement: new base rule extension New base rule extension required to handle a TS specific case and removed triage Waiting for maintainers to take a look labels Aug 31, 2020
@bradzacher
Copy link
Member

bradzacher commented Aug 31, 2020

I didn't even know that rule used scope analysis! I missed it during my analysis of the ESLint codebase.

This looks like a problem because the base rule assumes that global variables are mutable.
We can extend this rule into this project and add support for ignoring type locations.

Doing a quick test, the only case that I can see wherein this rule will false-positive is for global types. Types with a declaration in the file (imported or otherwise) will correctly be identified as readonly and thus the rule will ignore them.

@bradzacher bradzacher removed the bug Something isn't working label Sep 5, 2020
@bradzacher bradzacher self-assigned this Sep 5, 2020
@bradzacher bradzacher added the has pr there is a PR raised to close this label Sep 5, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new base rule extension New base rule extension required to handle a TS specific case has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants