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

organizeImports without the reordering ("remove unused imports" being its own option) #43141

Closed
jasonwilliams opened this issue Mar 8, 2021 · 15 comments · Fixed by #50931
Closed
Assignees
Labels
Experience Enhancement Noncontroversial enhancements Fix Available A PR has been opened for this issue Suggestion An idea for TypeScript

Comments

@jasonwilliams
Copy link

jasonwilliams commented Mar 8, 2021

Suggestion

I was originally using the organizeImports in my "editor.codeActionsOnSave" block on VSCode. The issue with this feature is it also does the sorting which we don't want.

It's currently not possible to switch the sorting off without losing the "remove unused imports".

According to VSCode documentation "addMissingImports" is offered as an "on save" option, but not "removeUnusedImports".

⭐ Suggestion

As some projects already use tools which handle ordering it would be nice to just add/remove imports without changing the order.

Then have a separate command which handles ordering.

💻 Use Cases

My team are happy with the ordering of imports and don't want changes. However by keeping the ordering I lose the ability to remove unused imports.

Example

    "editor.codeActionsOnSave": {
      "source.organizeImports": false,
      "source.fixAll": true,
    },
@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 Mar 8, 2021
@blutorange
Copy link

Another use is when you are using a code formatter such as prettier that sorts the imports (with an appropriate prettiert plugin) but doesn't have enough knowledge to find and remove unused imports. It would be great if the IDE would remove the unused imports, and prettier could format them.

@jasonwilliams
Copy link
Author

jasonwilliams commented Mar 12, 2022

Hey @RyanCavanaugh do you still need more feedback for this?

It seems like this would be an easy win, as you already implement both features, this is just asking you to separate them out.

@jasonwilliams
Copy link
Author

Another reason why these command should be separated is due to the fact that some projects like to use ESLint for organising imports but still want TypeScript to bring in imports which are being used.

Sadly at the moment you can't do that without conflicting with ESLint.

@miloszpp-sumo
Copy link

For anyone waiting for this, you can use this extension: https://marketplace.visualstudio.com/items?itemName=kuscamara.remove-unused-imports

@andrewbranch
Copy link
Member

We have a code fix for removing all unused declarations that I think would be trivial to expose to codeActionsOnSave. I actually thought it already was exposed, but it looks like surprisingly little is exposed. Would that satisfy this request, or does it need to be specific to imports?

@jasonwilliams
Copy link
Author

jasonwilliams commented Jun 15, 2022

Yes exposing that would be great @andrewbranch, it would be nice if VSCode offered this as an "on save" action via the extension, but just exposing it would be a win here

@ionut-mocan
Copy link

ionut-mocan commented Jun 15, 2022

I want the remove without organizing feature too... Ty for extension tip Milosz!

@jasonwilliams
Copy link
Author

Hey @andrewbranch did you ever get around to exposing this code fix?

@andrewbranch
Copy link
Member

I think this slipped through the cracks, @jasonwilliams. Bringing it up with the VS Code team tomorrow. Looking back at my last comment, I’m not sure offering the remove-all-unused-stuff on save is a workable alternative after all, since that could delete entire local function and class declarations, which is probably not what anyone wants 😅

@andrewbranch
Copy link
Member

@jasonwilliams your original post showed an example code snippet with "source.fixAll": true. Do you actually use that, or was it just for context?

@andrewbranch andrewbranch added Experience Enhancement Noncontroversial enhancements and removed Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Aug 31, 2022
@andrewbranch andrewbranch self-assigned this Aug 31, 2022
@andrewbranch andrewbranch added this to the TypeScript 4.9.0 milestone Aug 31, 2022
@jasonwilliams
Copy link
Author

@jasonwilliams your original post showed an example code snippet with "source.fixAll": true. Do you actually use that, or was it just for context?

Yes I used to use that also but I can’t remember specifically what for. It may have been to remove imports I wasn’t using.

yenbekbay referenced this issue in useVenice/venice Sep 9, 2022
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Sep 23, 2022
@jasonwilliams
Copy link
Author

Thanks so much for this @andrewbranch can't wait to try it

@andrewbranch
Copy link
Member

andrewbranch commented Sep 30, 2022

Once microsoft/vscode#161654 lands you’ll be able to try it in nightly/Insiders. I’m working on exposing Sort Imports and Remove Unused Imports in the command palette as well.

@jasonwilliams
Copy link
Author

Wil remove unused imports map to VSCode’s “source.fixAll” option on save? And sort imports maps to “source.organizeImports” then I guess these will “just work” for people who have that set

@andrewbranch
Copy link
Member

I haven’t made any changes to source.fixAll in microsoft/vscode#161654. I’ll take a look at what it currently does and see if it makes sense to include it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Fix Available A PR has been opened for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants