Typescript: "source.organizeImports" shouldn't remove my unused imports

Created on 8 Jan 2020  路  7Comments  路  Source: microsoft/TypeScript

Search Terms


source.organizeImports, remove unused imports, don't remove unused imports

Suggestion


When i put "source.organize Imports" in "editor.codeActionsOnSave" it removes my unused imports. It would be interesting to have a parameter for it to just organize but not remove.

Use Cases


Many developers save their files all the time, often without even realizing it. This feature is very interesting but ends up "disturbing" who has this habit.

If you can pass a few more configuration parameters (like not removing unused imports or not removing space between imports) this feature would be better taken advantage of.

Awaiting More Feedback Suggestion

Most helpful comment

We definitively need an option to grey out unused imports vs removing them. Here鈥檚 my use case. When I temporarily disable code blocks, the related imports are removed if not used elsewhere in the code. When I re-enable them, the imports are gone and the code is broken.

It happens to me all the time... And worse, I have a habit of saving every line of writing, so I often import, save, write the code I'm going to use that import... This is terrible for me ... I have to "fight the IDE" to program the way I'm used to...

All 7 comments

I would love this feature.

It really helps if we have a parameter to decide if we want vscode to remove or not the unused imports.

I have many problems when I am coding and then suddenly the import disappear.

The solution is: all the time I have to add the import again or I have to add another third party plugin to control my imports instead of using the vscode functionality.

馃殌

We definitively need an option to grey out unused imports vs removing them. Here鈥檚 my use case. When I temporarily disable code blocks, the related imports are removed if not used elsewhere in the code. When I re-enable them, the imports are gone and the code is broken.

@RyanCavanaugh, is there a fix for this aside from disabling source.organizeImports?

We definitively need an option to grey out unused imports vs removing them. Here鈥檚 my use case. When I temporarily disable code blocks, the related imports are removed if not used elsewhere in the code. When I re-enable them, the imports are gone and the code is broken.

It happens to me all the time... And worse, I have a habit of saving every line of writing, so I often import, save, write the code I'm going to use that import... This is terrible for me ... I have to "fight the IDE" to program the way I'm used to...

Just found this via https://github.com/microsoft/vscode/issues/89263 - also was just bitten by this:

  • I was doing some research and left my code in an invalid state: a template string was open, causing the rest of the file (including usages of imports) to be an invalid template string with no end
  • The imports were automatically removed by the Organize Imports in editor.codeActionsOnSave
  • I came back and fixed my code, but now other things were broken.

Two suggestions here:

  1. Maybe don't run Organize Imports if the code is not valid?
  2. Maybe the removal could be a setting, for those who do want the auto-removal behavior? I would count myself in this group too, as long as my code was valid.

@karlhorky I am no longer using organize because of this. In fact I am using it manually sometimes, when I have finished all changes to the file.

It would be nice if there were two separate features, one to organize and one to remove.
I believe that it would answer many other cases, without implementing any "new behavior".

People who just want to remove unused imports and / or people who want to organize imports.

I will close this issue because the problem is with vscode and not with typescript.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Antony-Jones picture Antony-Jones  路  3Comments

remojansen picture remojansen  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments

bgrieder picture bgrieder  路  3Comments

siddjain picture siddjain  路  3Comments