Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eslint custom import order

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';
like image 559
Radi Mortada Avatar asked Dec 25 '18 12:12

Radi Mortada


People also ask

How do I order an import statement?

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.

Does prettier sort imports?

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.

Why do imports sort?

On imports, it groups similar imports together, and makes it easier to see what is being imported from certain packages.


1 Answers

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!

like image 89
Rom's Avatar answered Sep 17 '22 23:09

Rom's