eslint-plugin-import-helpers icon indicating copy to clipboard operation
eslint-plugin-import-helpers copied to clipboard

It doesn't respect the order under group

Open nowycondro opened this issue 6 years ago • 4 comments

Lets say I have this configuration

'rules': {
      'import-helpers/order-imports': ['error',
        {
          newlinesBetween: 'always',
          groups: ['module', ['/aaa/', '/bbb/', '/ccc/']],
        },
      ],
    },
import {...} from '@external-module';

import {...} from '../ccc/';  // <= It doesn't complain about this line. It suppose to be placed after '/bbb/'.
import {...} from '../aaa/';
import {...} from '../bbb/';

import {...} from '../../somewhere/local';

nowycondro avatar Aug 09 '19 14:08 nowycondro

Hi!

So, currently, ordering within a subgroup is not enforced. In your example, /aaa/, /bbb/, and /ccc/ are merged into a single group.

Since you do not have the alphabetize setting, the rule will allow you to do any order of imports within that subgroup. If you set alphabetize: { order: 'asc', ignoreCase: true }, you will get your desired order... However that only works because your desired order in this example is alphabetical order.

It sounds like you think the order of the items in a subgroup should be enforced? Hmm, I can see the value in that... I personally prefer to sort alphabetically for all groups. I'm on the fence. Any additional arguments for/against it?

willhoney7 avatar Aug 16 '19 15:08 willhoney7

In the beginning I though the behavior of Regex should be the same other group (e.g, parent, siblings, etc). It took me quite sometime to realise it doesn't respect the order for Regex.

In my case, alphabetically sort doesn't solve the problem

groups: [
      'module', 
      [
             '/name/',
             '/relatively_short_folder_name/',
             '/a_extremely_long_folder_name_that_is_hard_to_read/'
      ]
],

In other case, it might be sort by string length 🤷‍♂ and lead me to think is there any possibility to allow custom sort function as an item under groups.

function sort(a, b) {
    // sort the import whatever you want 
}
...
groups: ['module', sort];
...

nowycondro avatar Aug 19 '19 07:08 nowycondro

In the beginning I though the behavior of Regex should be the same other group (e.g, parent, siblings, etc). It took me quite sometime to realise it doesn't respect the order for Regex.

In my case, alphabetically sort doesn't solve the problem

groups: [
      'module', 
      [
             '/name/',
             '/relatively_short_folder_name/',
             '/a_extremely_long_folder_name_that_is_hard_to_read/'
      ]
],

In other case, it might be sort by string length 🤷‍♂ and lead me to think is there any possibility to allow custom sort function as an item under groups.

function sort(a, b) {
    // sort the import whatever you want 
}
...
groups: ['module', sort];
...

Good idea but sometimes is not necessary. I suppose that beyond it this rule should support an Object like this:

  "groups": [
    {
      "group": [
          "some_same_values_here"
       ],
       "order": "asc|desc|asc-by-type|desc-by-type" // default is alphabetize.order
     }
  ]

mateushnsoares avatar Aug 19 '20 18:08 mateushnsoares

any update on this @Tibfib ?

ssharifdev avatar Sep 10 '21 05:09 ssharifdev