IanVS / prettier-plugin-sort-imports

An opinionated but flexible prettier plugin to sort import statements
Apache License 2.0
951 stars 21 forks source link

Feature: Remove Unused Imports #102

Closed regalstreak closed 1 year ago

regalstreak commented 1 year ago

https://www.npmjs.com/package/prettier-plugin-organize-imports already does this (through the typescript language service) but can't sort imports like we can as with the trivago plugin.

I get that it's a 'prettier' plugin and this action might probably change the AST, but it could be opt-in and be a major differentiator from the trivago one

IanVS commented 1 year ago

This plugin currently does not look at any of the code other than imports, so determining whether imports are used would be a big change. I won't immediately say "no" right now, but it would require a lot of upvotes on this request for me to seriously consider it, I'm afraid.

In the meantime, maybe there is an ESLint plugin or custom that can remove unused imports?

fbartho commented 1 year ago

We use eslint to detect unused code, and I found this plugin: https://www.npmjs.com/package/eslint-plugin-unused-imports — it has an auto-fix too! Maybe that will work for your needs?

I concur with @IanVS that I don’t think it’s appropriate behavior for this prettier-plugin. IDEs often call prettier every time they auto-save, and it would be irritating if you add an import, and then scroll down to the function where you want to use the import, and the IDE chooses to save then, and the import has already vanished before you can start using it!