Skip to content

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

Closed
@jasonwilliams

Description

@jasonwilliams

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,
    },

Activity

added
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this feature
SuggestionAn idea for TypeScript
on Mar 8, 2021
blutorange

blutorange commented on Mar 12, 2022

@blutorange

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

jasonwilliams commented on Mar 12, 2022

@jasonwilliams
Author

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

jasonwilliams commented on Apr 7, 2022

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

miloszpp-sumo commented on Jun 13, 2022

@miloszpp-sumo

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

andrewbranch

andrewbranch commented on Jun 14, 2022

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

jasonwilliams commented on Jun 15, 2022

@jasonwilliams
Author

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

ionut-mocan commented on Jun 15, 2022

@ionut-mocan

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

jasonwilliams

jasonwilliams commented on Aug 30, 2022

@jasonwilliams
Author

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

andrewbranch

andrewbranch commented on Aug 30, 2022

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

andrewbranch commented on Aug 31, 2022

@andrewbranch
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?

added and removed
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this feature
on Aug 31, 2022

11 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

    Development

    Participants

    @jasonwilliams@blutorange@andrewbranch@RyanCavanaugh@typescript-bot

    Issue actions

      organizeImports without the reordering ("remove unused imports" being its own option) · Issue #43141 · microsoft/TypeScript