deno icon indicating copy to clipboard operation
deno copied to clipboard

LSP should support organising imports

Open KnorpelSenf opened this issue 2 years ago • 4 comments

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.

KnorpelSenf avatar Apr 01 '22 13:04 KnorpelSenf

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

Soremwar avatar Apr 01 '22 14:04 Soremwar

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.

KnorpelSenf avatar Apr 01 '22 22:04 KnorpelSenf

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.

thegaryroberts avatar May 22 '22 14:05 thegaryroberts

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.

stale[bot] avatar Jul 25 '22 05:07 stale[bot]

Related: https://github.com/denoland/deno/issues/12672

KnorpelSenf avatar Aug 29 '22 08:08 KnorpelSenf

It looks like this is working now, are there any updates on this?

KnorpelSenf avatar Aug 29 '22 08:08 KnorpelSenf

Has any work been done on this? Let me know, because I might give this a shot.

denizdogan avatar May 13 '23 13:05 denizdogan

This seems to be working

nayeemrmn avatar Sep 17 '23 04:09 nayeemrmn