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

Problems when there are too many unused imports #73

Open
obeattie opened this issue Mar 24, 2024 · 1 comment
Open

Problems when there are too many unused imports #73

obeattie opened this issue Mar 24, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@obeattie
Copy link

I'm having some difficulties where this plugin leaves some unused imports "behind" when there are too many.

My use-case is for code generation, where I'm not certain what imports a piece of code will use, so I am putting all possible imports at the top of the file and then using --fix to remove those that are not used. One of the packages I'm using in this way is lucide-react, which exports a lot of different icons as separate components.

However when I do this, it seems to always leave four imports behind. I've come up with a minimal reproduction.

  1. Create an empty directory
  2. npm init
  3. npm i --save react lucide-react
  4. npm i --save-dev eslint typescript typescript-eslint eslint-plugin-unused-imports
  5. Create the following these two files: https://gist.github.com/obeattie/e3dff83624fc0f01449b8288a45f93fb (unfortunately, they are too long to include in the issue directly)
  6. npx eslint --fix ./index.ts

You'll see 4 imports were left "behind". In the output:

/Users/obeattie/src/github.com/lispwoso/unused-import-repro/index.ts
  1:10  error  'ExpandIcon' is defined but never used. Allowed unused vars must match /^_/u           @typescript-eslint/no-unused-vars
  1:10  error  'ExpandIcon' is defined but never used                                                 unused-imports/no-unused-imports
  1:22  error  'LucideClipboardType' is defined but never used. Allowed unused vars must match /^_/u  @typescript-eslint/no-unused-vars
  1:22  error  'LucideClipboardType' is defined but never used                                        unused-imports/no-unused-imports
  1:43  error  'LucideTv2' is defined but never used. Allowed unused vars must match /^_/u            @typescript-eslint/no-unused-vars
  1:43  error  'LucideTv2' is defined but never used                                                  unused-imports/no-unused-imports
  1:54  error  'Strikethrough' is defined but never used. Allowed unused vars must match /^_/u        @typescript-eslint/no-unused-vars
  1:54  error  'Strikethrough' is defined but never used                                              unused-imports/no-unused-imports

✖ 8 problems (8 errors, 0 warnings)
  4 errors and 0 warnings potentially fixable with the `--fix` option.

…and they will remain in the index.ts file, as well:

import { ExpandIcon, LucideClipboardType, LucideTv2, Strikethrough } from "lucide-react";

export default function foo() {
}

If you run npx eslint --fix ./index.ts again, they will be removed.

Not sure why this is happening but would really appreciate some help! 🙏

@sweepline
Copy link
Owner

Thanks for an excellent bug report. I can reproduce the issue. It seems to be at fixed intervals that it does not remove an import.

@sweepline sweepline added the bug Something isn't working label Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants