trivago / prettier-plugin-sort-imports

A prettier plugin to sort imports in typescript and javascript files by the provided RegEx order.
Apache License 2.0
3.21k stars 128 forks source link

non deterministic order for "import type" #116

Open artola opened 2 years ago

artola commented 2 years ago

Your Environment

Describe the bug

Depending the order of import type and import is derived the output. Having 2 files with multiple mixed imports (different between them) it might produce a different output.

To Reproduce

Input:

import type {Foo} from 'foo';
import type {Bar} from 'bar';
import foo from 'foo';
import bar from 'bar';

Output:

import type {Bar} from 'bar';
import bar from 'bar';
import type {Foo} from 'foo';
import foo from 'foo';

But different from:

import foo from 'foo';
import bar from 'bar';
import type {Foo} from 'foo';
import type {Bar} from 'bar';

Output:

import bar from 'bar';
import type {Bar} from 'bar';
import foo from 'foo';
import type {Foo} from 'foo';

Expected behavior

Independently of the original order of the statements, the output should be the same (no human error prone).

import type {Bar} from 'bar';
import bar from 'bar';
import type {Foo} from 'foo';
import foo from 'foo';
danielwii commented 2 years ago

I have wrote a version for support import type on v2.0.2 but not the newest version. Try import as "@trivago/prettier-plugin-sort-imports": "git+https://github.com/danielwii/prettier-plugin-sort-imports.git#main",

artola commented 2 years ago

@danielwii It seems to work but not as expected:

Input:

import type {Foo} from 'foo';

import foo from 'foo';

import bar from 'bar';

import type {Bar} from 'bar';

Output:

import bar from 'bar';
import foo from 'foo';

import type {Foo} from 'foo';
import type {Bar} from 'bar';
mattleong commented 2 years ago

Seeing the same.. Is there progress on this?

Along those lines, it would also be great to specify whether or not to place type imports directly to be mixed with regular imports OR to organize type imports above or below regular imports.

artola commented 2 years ago

@byara @ayusharma I did some local development to sort the imports, it seems trivial to fix, please take a look to this idea and give me some hint.

Just modify:

https://github.com/trivago/prettier-plugin-sort-imports/blob/e83f3f14705643ded7ec3b26c945c045b47a43ab/src/utils/get-sorted-nodes.ts#L55

by adding importKind (i.e., type or value):

naturalSort(a.source.value + ' ' + a.importKind, b.source.value + ' ' + b.importKind)

Files:

tconroy commented 2 years ago

curious if any progress on this?

polaroidkidd commented 2 years ago

to add to this, ideally an option to group type imports in their category would be great.

tusharf5 commented 1 year ago

Until this is implemented, I will shamelessly plug this fork https://github.com/serverless-guru/prettier-import-order that has this feature.

SrBrahma commented 1 year ago

I was about to use this plugin until I saw this, it's not an option for me to lose this behavior that I have with the simple-import-sort/imports ESLint rule

iMoses commented 10 months ago

Is this not a priority? A non-deterministic import sorter feels like a double edge sword...