Skip to content
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

import/order: alphabetize import type above (or below) import #2339

Closed
aaronadamsCA opened this issue Dec 28, 2021 · 4 comments · Fixed by #2398 or #2544
Closed

import/order: alphabetize import type above (or below) import #2339

aaronadamsCA opened this issue Dec 28, 2021 · 4 comments · Fixed by #2398 or #2544

Comments

@aaronadamsCA
Copy link
Contributor

For the version where types are mixed within groups, it would be nice if ^ was actually enforced. We have a bunch of

import type A from 'a';
import A from 'a';
import B from 'b';
import type B from 'b';

and it would be great to have a more strict ordering if the library is going to start being type-import aware. For reference, @typescript-eslint/consistent-type-imports extracts type imports and places them directly above their corresponding regular imports (though it has no on-going opinion after the initial extraction).

Originally posted by @merrywhether in #2070 (comment)

This would be nice. The main reason we sort imports is to minimize merge conflicts, and I've noticed when sorting with this plugin that it has no opinion on the order between these two lines:

import { foo } from "./foo";
import type { Foo } from "./foo";

Both simple-import-sort and, as mentioned, @typescript-eslint/consistent-type-imports place import type (or export type) above import (or export) when the paths are identical; that seems like it could be a good precedent to follow.

This might be as easy as concatenating importKind within the alphabetize comparator; I tried this with a local monkey patch and it seemed to do the trick.

@aaronadamsCA
Copy link
Contributor Author

As an amusing side effect, this terrible monkey patch also appears to perfectly resolve #1881, since the alphabetization algorithm is slightly influenced by the appended text.

  const comparator = alphabetizeOptions.caseInsensitive
    ? (a, b) => sorterFn(`${a.value.toLowerCase()}|${a.node.importKind}`, `${b.value.toLowerCase()}|${b.node.importKind}`)
    : (a, b) => sorterFn(`${a.value}|${a.node.importKind}`, `${b.value}|${b.node.importKind}`);

@ljharb
Copy link
Member

ljharb commented Dec 29, 2021

@aaronadamsCA that type of comparison is what it used to do, and that caused bugs. That patch would break tests.

@ljharb
Copy link
Member

ljharb commented Dec 29, 2021

I would expect type imports to always sort after value ones, since the types are far far less important and valuable.

I agree that this plugin should have an opinion about which order it sorts those in.

@aaronadamsCA
Copy link
Contributor Author

Note that I filed #2398 as a first attempt at solving this.

ljharb pushed a commit to stropho/eslint-plugin-import that referenced this issue Sep 6, 2022
…ts with same path based on their kind (`type`, `typeof`)

Fixes import-js#2339

Co-authored-by: Aaron Adams <aaron@aaronadams.ca>
Co-authored-by: stropho <3704482+stropho@users.noreply.github.com>
ljharb pushed a commit to aaronadamsCA/eslint-plugin-import that referenced this issue Sep 6, 2022
…ts with same path based on their kind (`type`, `typeof`)

Fixes import-js#2339

Co-authored-by: Aaron Adams <aaron@aaronadams.ca>
Co-authored-by: stropho <3704482+stropho@users.noreply.github.com>
@ljharb ljharb closed this as completed in c4f3cc4 Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants