Closed
Description
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
blutorange commentedon Mar 12, 2022
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 commentedon 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 commentedon Apr 7, 2022
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 commentedon Jun 13, 2022
For anyone waiting for this, you can use this extension: https://marketplace.visualstudio.com/items?itemName=kuscamara.remove-unused-imports
andrewbranch commentedon Jun 14, 2022
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 commentedon 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 commentedon Jun 15, 2022
I want the remove without organizing feature too... Ty for extension tip Milosz!
jasonwilliams commentedon Aug 30, 2022
Hey @andrewbranch did you ever get around to exposing this code fix?
andrewbranch commentedon Aug 30, 2022
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 commentedon Aug 31, 2022
@jasonwilliams your original post showed an example code snippet with
"source.fixAll": true
. Do you actually use that, or was it just for context?11 remaining items