Is it possible to make Eslint rule that supports custom import order
I want to trigger Eslint warning or error upon having following invalid order.
i.e
Invalid:
import utilsMicky from 'utils/micky';
import containersMicky from'containers/micky';
import componentsMicky from 'components/micky';
Valid:
import containersMicky from 'containers/micky';
import utilsMicky from 'utils/micky';
import componentsMicky from 'components/micky';
The import statements must follow the package statement. import statements should be sorted with the most fundamental packages first, and grouped with associated packages together and one blank line between groups. The import statement location is enforced by the Java language.
With the help of prettier and a plugin, we can easily sort imports! As a side note if you're using ESLint I have another article to sort imports using that.
On imports, it groups similar imports together, and makes it easier to see what is being imported from certain packages.
Apparently reading from the discussion there you probably would have to create your own ESLint rule for that kind of specific requirement. Their own requirement is to prevent people from importing from a nested path, as you can see in the unit-test. Based on this, you could potentially implement a rule enforcing this specific import order of yours.
eslint-plugin-import also has an existing order related rule, but I haven't tested it so I'm not quite sure it fits for your requirement. Might be worth giving a try first :)
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With