willhoney7 / eslint-plugin-import-helpers

ESLint plugin to help enforce a configurable order for import statements
288 stars 17 forks source link

It doesn't respect the order under group #17

Open nowycondro opened 5 years ago

nowycondro commented 5 years ago

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';
willhoney7 commented 5 years ago

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?

nowycondro commented 5 years ago

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];
...
mateushnsoares commented 4 years ago

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
     }
  ]
ssharifdev commented 3 years ago

any update on this @Tibfib ?