The Wayback Machine - https://web.archive.org/web/20230529165702/https://github.com/microsoft/TypeScript/issues/36085
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

"source.organizeImports" shouldn't remove my unused imports #36085

Closed
yardz opened this issue Jan 8, 2020 · 20 comments
Closed

"source.organizeImports" shouldn't remove my unused imports #36085

yardz opened this issue Jan 8, 2020 · 20 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@yardz
Copy link

yardz commented Jan 8, 2020

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.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Jan 8, 2020
@ghost
Copy link

ghost commented Feb 13, 2020

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.

🚀

@sunknudsen
Copy link

We definitively need an option to grey out unused imports vs removing them. Here’s 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.

@sunknudsen
Copy link

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

@yardz
Copy link
Author

yardz commented Mar 16, 2020

We definitively need an option to grey out unused imports vs removing them. Here’s 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...

@karlhorky
Copy link

karlhorky commented Dec 7, 2020

Just found this via microsoft/vscode#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.

@yardz
Copy link
Author

yardz commented Dec 7, 2020

@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.

@yardz
Copy link
Author

yardz commented Dec 7, 2020

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

@yardz yardz closed this as completed Dec 7, 2020
@yardz yardz reopened this Dec 17, 2020
@karlhorky
Copy link

@RyanCavanaugh would it be possible to get an option / separate function for this?

Or if not, what would you like to see with "more feedback"? I guess this would be a pretty important issue for a lot of developers...

@karlhorky
Copy link

For an example of a user story why a setting would be critical to some users, here's what we've been running into:

Bootcamp students at @upleveled are beginners - they are learning how to code for the first time. It is very helpful for them to have tools that help them with best practices, such as auto saving, code formatting, auto sorting and import combining tools. If they use source.organizeImports, it gets them some of these features! 🎉 But it has a critical weakness: after they have achieved the task of adding an import, if they switch to another file, the combination of auto save and source.organizeImports will cause their hard-earned imports to be erased. 😫

cc @RyanCavanaugh @amcasey @andrewbranch

@karlhorky
Copy link

I think the following may be a suitable plan, which will still keep those happy who appreciate this removal behavior:

  1. Change the default behavior of source.organizeImports to not remove unused lines
  2. Add a new code action for the current behavior, eg. source.organizeAndRemoveImports

It would also make the behavior of the option more explicit.

karlhorky added a commit to upleveled/system-setup that referenced this issue Feb 15, 2021
@1337cookie
Copy link

  1. Change the default behavior of source.organizeImports to not remove unused lines
  2. Add a new code action for the current behavior, eg. source.organizeAndRemoveImports

I think something even more explicit than this would be source.sortImports and source.removeUnusedImports. This would give more granularity in the behaviour.
I can imagine a scenario where someone may want to remove unused imports but maintain a particular order and a scenario where someone only wants to sort the imports but keep the unused one.
The current behaivour with source.organizeImports could be left as-is to avoid disturbing anyone already happy with how it works.

@OliverJAsh
Copy link
Contributor

OliverJAsh commented May 25, 2021

source.removeUnusedImports

FWIW I discovered this already exists under a slightly different name: source.removeUnused. For example here is how you would setup a keyboard shortcut to remove unused imports:

  {
    "key": "shift+ctrl+r",
    "command": "editor.action.sourceAction",
    "args": {
      "kind": "source.removeUnused",
      "apply": "first"
    }
  },

@andrewbranch
Copy link
Member

Note that removeUnused removes all unused declarations, not just imports.

@karlhorky
Copy link

karlhorky commented Jun 15, 2021

Ohh was this just released in VS Code 1.57? 👀 🎉

The Sort imports Code Action for JavaScript and TypeScript sorts your imports. Unlike Organize imports however, it does not remove any unused imports.

"editor.codeActionsOnSave": {
    "source.sortImports": true
}

https://code.visualstudio.com/updates/v1_57#_sort-imports-source-action

@karlhorky
Copy link

@yardz if you test this out and it addresses your wish, maybe you can close this issue?

@jankkosinski
Copy link

"editor.codeActionsOnSave": { "source.sortImports": true } - it works properly. That's perfect!

@yardz
Copy link
Author

yardz commented Jun 1, 2022

@karlhorky Sorry for the delay, I wasn't following and missed the notification. I just tested it and it really works.
It is perfect!

@sjc5
Copy link

sjc5 commented Jul 1, 2022

I have noticed that switching to sortImports over organizeImports now breaks unused imports highlighting in vs-code, seemingly as though sortImports causes TS to stop checking whether imports are used/unused at all (as opposed to solely not deleting them on save when unused, which IMO would be desired behavior).

Anyone else seeing this?

@NickDiVona
Copy link

@sjc5 I'm not seeing that on my end. Unused imports are still showing as unused and vice versa.

@rosedo
Copy link

rosedo commented May 3, 2023

I guess I should raise a new issue about an import type being removed when working with Flow, but I don't even like Flow, so... yeah just using "source.sortImports" instead of organizeImports will be good enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests