-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sorting of "type FooX" and "Foo" imports disagrees with typescript, dprint #124
Comments
Hi! The default in this plugin is to ignore Regarding Now I have some questions:
Just trying to understand all the ins and outs of this so the right decision can be made. |
Thanks for the great questions! The context here is microsoft/TypeScript#52090, if you didn't see the cross link. The reason that I personally care about Organize Imports being compatible with other tools is that there are many on my team who do not want to run ESLint live in their editor or enable auto-fixes. One reason is preference, but mainly that in the TypeScript compiler itself we have a few heavily-edited but massive files like But, moreso, the organization logic used by Organize Imports is also the same logic used by auto-imports; when we offer up an auto-import completion, we have to stick that import somewhere. If we can't detect the intended ordering (or, have settings to configure it), then we have to resort to sticking imports on the end. For those who don't run ESLint, this means they will have to fix their PRs up once they see that they fail in CI; for those who run ESLint in their editors, it's another quickfix that could have been avoided. So, getting this sort order to match the user's intent is just generally good. What recently changed was the merge of this PR: microsoft/TypeScript#52115 Effectively, this means that tsserver can use
So, as a combined workflow, this means that users can run plugins like this one to enforce certain orderings, but still be able to use Organize Imports and auto-import completions without extra steps. (We are even considering offering up some presets for common sorting choices; the above being one that would match this plugin... besides the sorting inconsistency I found for this issue.) I'll also note that neither of the other tools (TypeScript or dprint) will move imports between import groups, opting to preserve what the user wrote; this means that Unfortunately, in trying to answer your first question, I found that it's more inconsistent between these three toolings than I thought. Namely:
So just to answer your questions specifically:
|
That’s a very good point. Minimizing unnecessary diff is an important idea of this plugin. I thought that things would only ever be types or values, but I now realize that when you import a class it can be either to use it in a type annotation or to do I now feel like making that change. What do you say? |
To give more context, I actually found this bug while dogfooding the new TS 5.0 option I think it's likely that people are going to be in the same situation as me, so I would agree that it's a good change to make. It would reduce the number of behaviors between TS, dprint, and this plugin from three to two (as dprint and simple-import-format would agree), and I think it's something to look at from TypeScript's side as well (which I'm hoping brings it down to just one behavior, the golden number). (There's an aside here about how to handle the case where you have both |
I just released 10.0.0-beta.1 with this change. Could you try it out in your TypeScript PR before I release for real? |
I tested it out on my personal project and TypeScript, and it seems to work as expected. As a goofy edge case, this: import {
type Foo as Foo2,
type Foo,
Foo,
Foo as Foo3
} from "foo"; Now becomes: import {
type Foo,
Foo,
type Foo as Foo2,
Foo as Foo3
} from "foo"; Which exactly matches dprint too. TypeScript itself still puts all of the Thank you so much for looking into this! |
FWIW testing on microsoft/TypeScript#52090 didn't net anything as our repo doesn't have any type imports (yet!), hence coming up with contrived examples and using my own projects for testing. (TypeScript 5.0 is the first release that has imports internally, which is why we're discovering all of these things now. Yay dogfooding!) |
Awesome! Thanks for reporting, answering all my questions, testing and suggestions! Released 10.0.0. |
I have something like this:
TypeScript's sort/organize imports as well as dprint's (so,
deno fmt
too) case insensitive sortings both order these withFoo
first, thentype FooX
, as above.However,
simple-import-sort
appears to flip them, puttingtype FooX
beforeFoo
, like:Even though if the
type
keyword were removed, it'd sort the other way.This leads to a frustrating scenario in my editor where the formatter and eslint fix both run and flip it back and forth, leaving nobody happy.
The only clue I have about this is the mention of
\u0000
in the readme, but I'm not sure how"Foo"
would sort after"FooX\u0000"
.The text was updated successfully, but these errors were encountered: