Skip to content
This repository has been archived by the owner on Dec 25, 2023. It is now read-only.

Allow Ability to Set Preference on Unused Import Removal #273

Closed
rehatkathuria opened this issue Jun 27, 2021 · 4 comments · Fixed by #454
Closed

Allow Ability to Set Preference on Unused Import Removal #273

rehatkathuria opened this issue Jun 27, 2021 · 4 comments · Fixed by #454
Labels
dependency Something is a dependencies fault enhancement New feature or request

Comments

@rehatkathuria
Copy link

First of all: Thank you for your work on this! I have a habit of hitting CMD + S often as I'm writing code. Having the formatter organise and rearrange imports during those keystrokes is great –– however, having it remove an import that I haven't yet used is a little frustrating. I'd really appreciate the ability to disable that functionality.

@rehatkathuria rehatkathuria added the enhancement New feature or request label Jun 27, 2021
@apexskier
Copy link
Owner

apexskier commented Jun 30, 2021

Looks like this might be possible. tsserver has a skipDestructiveCodeActions argument that skips the remove unused arguments logic.

https://github.com/microsoft/TypeScript/blob/663b19fe4a7c4d4ddaa61aedadd28da06acd27b6/src/services/organizeImports.ts#L91-L95

In order to implement this I'll need to pass that through from typescript-language-server as an argument here, which will require work in that codebase. It looks like for proper typing it'll also require upgrading typescript (from v3 to v4) in the codebase, which might be tricky.

Then I can pass an option from the extension here:

arguments: [editor.document.path],

@apexskier
Copy link
Owner

apexskier commented Jul 3, 2021

It looks like this feature will be new in TypeScript 4.4, which is still in beta currently:

microsoft/TypeScript#43184 / microsoft/TypeScript#43051

@apexskier
Copy link
Owner

Another option I'd recommend if you're looking to improve your experience is to disable organize imports on save through this extension and replace it with an ESLint rule that organizes imports, using https://github.com/apexskier/nova-eslint, and enabling eslint's fix on save.

@apexskier apexskier changed the title [Feature Request] Allow Ability to Set Preference on Unused Import Removal Allow Ability to Set Preference on Unused Import Removal Jul 4, 2021
rchl added a commit to typescript-language-server/typescript-language-server that referenced this issue Nov 8, 2021
This adds support for the new skipDestructiveCodeActions argument to
TypeScript's organize imports feature - [1] to support [2].

Support is added in two places:

 - Automatically inferring the proper value based on diagnostics for the
   file when returning code actions.
 - Supporting sending it when manually executing the organize imports action.

Also added documentation to the readme about the supported commands
that can be manually executed.

[1] microsoft/TypeScript#43051
[2] apexskier/nova-typescript#273

Co-authored-by: Rafal Chlodnicki <rchl2k@gmail.com>
apexskier added a commit that referenced this issue Jan 16, 2023
Using feature added to the language server in v0.7.0

Resolves #273
apexskier added a commit that referenced this issue Jan 16, 2023
* Setting to prevent unused import removal

Using feature added to the language server in v0.7.0

Resolves #273

* Changelog
@github-actions
Copy link

Included in release v2.6.0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
dependency Something is a dependencies fault enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants