sweepline / eslint-plugin-unused-imports

Package to separate no-unused-vars and no-unused-imports for eslint as well as providing an autofixer for the latter.
MIT License
448 stars 18 forks source link

False Positives unused-imports/no-unused-imports for arguments with @typescript-eslint/eslint-plugin@~7.8 #81

Open bahag-streckerj opened 1 month ago

bahag-streckerj commented 1 month ago

function(_unused) gets reported as unused import

I am getting false positive errors on function arguments as if they were imports when I @typescript-eslint/eslint-plugin to >7.7.

The Package.json

...
"@typescript-eslint/eslint-plugin": "~7.8",
"@typescript-eslint/parser": "~7.7",
"eslint-plugin-unused-imports": "^3.2.0",
...

The rule config:

"unused-imports/no-unused-imports": "error",
"unused-imports/no-unused-vars": [
  "warn",
  { "vars": "all", "varsIgnorePattern": "^_", "args": "after-used", "argsIgnorePattern": "^_" }
],

The unexpected outout

/filename.ts
  54:49  error  '_state' is defined but never used  unused-imports/no-unused-imports

for that 7th line in the following excerpt:

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
  providers: [
    {
      provide: 'externalUrlRedirectResolver',
      useValue: (route: ActivatedRouteSnapshot, _state: RouterStateSnapshot) => {
        window.location.href = (route.data as any).externalUrl;
      }
    }
  ]
})

The problem is, that it is the wrong rule (unused-imports/no-unused-imports) "saying" that there is a false positive: An unused import.

The problem does not occur with this package.json

...
"@typescript-eslint/eslint-plugin": "~7.7", <---- 7.7 instead of 7.8
"@typescript-eslint/parser": "~7.7",
"eslint-plugin-unused-imports": "^3.2.0",
...

Further information:

There was a 7.8.0 release two weeks ago: https://github.com/typescript-eslint/typescript-eslint/releases/tag/v7.8.0 with this feature (https://github.com/typescript-eslint/typescript-eslint/pull/8640) in it, that might have to do with eslint-plugin-unused-imports.

andrey59412678 commented 1 month ago

I had the same problem. Solved it by messing with versions until it worked. Here is my current working config:

"@eslint/js": "9.3.0",
"@typescript-eslint/eslint-plugin": "7.10.0",
"@typescript-eslint/parser": "7.10.0",
"eslint-plugin-unused-imports": "^4.0.0",
strongpauly commented 1 month ago

Can this be attributed to the fact that eslint-plugin-unused-imports now requires eslint v9.x ?

mircowidmer commented 1 week ago

I haven't found a solution that fixes this error:

main.ts

import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';

bootstrapApplication(AppComponent, appConfig).catch(() => {});

I'm using the following dependencies:

"@typescript-eslint/eslint-plugin": "7.10.0",
"@typescript-eslint/parser": "7.10.0",
"eslint-plugin-unused-imports": "4.0.0",
"eslint": "9.5.0",

And get the following error when running npm run lint:

frontend/src/main.ts
1:1  error  Definition for rule 'unused-imports/no-unused-imports-ts' was not found  unused-imports/no-unused-imports-ts

What can I do to prevent that?

sweepline commented 1 week ago

I haven't found a solution that fixes this error:

main.ts

import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';

bootstrapApplication(AppComponent, appConfig).catch(() => {});

I'm using the following dependencies:

"@typescript-eslint/eslint-plugin": "7.10.0",
"@typescript-eslint/parser": "7.10.0",
"eslint-plugin-unused-imports": "4.0.0",
"eslint": "9.5.0",

And get the following error when running npm run lint:

frontend/src/main.ts
1:1  error  Definition for rule 'unused-imports/no-unused-imports-ts' was not found  unused-imports/no-unused-imports-ts

What can I do to prevent that?

in newer versions unused-imports/no-unused-imports-ts does not exist, you need to use unused-imports/no-unused-imports

mircowidmer commented 1 week ago

After following the suggestion of @sweepline, the following worked for me: