deno
deno copied to clipboard
LSP should support organising imports
Feature request
It would be nice to be able to sort imports automatically.
It probably needs to be decided how they should be organised.
Alphabetically
Simply sort everything alphabetically.
By import type
First remote URLs, then imports from an import map, then absolute file paths, then relative paths. Imports of the same type are sorted alphabetically.
By import type and domain
Sort by import type, but also parse URLs and sort by domain. That way, imports from sub.domain.com
and domain.com
could be next to each other.
Import order is significant for the execution of code, so this could break a lot of existing code
See https://github.com/denoland/deno/pull/8551
I agree that this should not happen with deno fmt
. I'm explicitly talking about the language server. VSCode has a shortcut exactly for organising imports. It can be used when one doesn't care about the order but rather wants to have a readable import section.
Personally, I think that side-effects during module loading is an anti-pattern and I generally try to avoid it as much as possible. Hence, a different order of imports almost never introduces bugs. It would be good to be able to use this feature when explicitly requested.
Typescript now has a pattern for this with specific ordering being maintained via groups/comments: https://devblogs.microsoft.com/typescript/announcing-typescript-4-7-rc/#group-aware-organize-imports
New developers picking up Deno might be surprised if this feature does not propagate through to Typescript files being processed by Deno.
As well as support for manually running the command, it does feel worthy of an addition to deno fmt
, but with a flag to prevent breaking existing codebases.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.
Related: https://github.com/denoland/deno/issues/12672
It looks like this is working now, are there any updates on this?
Has any work been done on this? Let me know, because I might give this a shot.
This seems to be working