Eslint-plugin-import: Import order rule

Created on 5 Jan 2021  路  7Comments  路  Source: benmosher/eslint-plugin-import

Is there a rule that won't allow local imports to be mixed with library imports? It would require an empty line between them, for example:

wrong:

import foo from 'foo'
import bar from 'bar'
import biz from './biz'
import boz from './taz/boz'

wrong:

import biz from './biz'
import foo from 'foo'
import bar from 'bar'
import boz from './taz/boz'

correct:

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

import biz from './biz'
import boz from './taz/boz'

I want to group libraries import on top and local imports bellow with a space between them.

I tried setting the rule to always for newlines-between but it's adding a line between biz and boz as well.

imporexport ordering question

All 7 comments

That's because those paths are in different groups. You can configure the rule like this and play with how the groups are combined or separated.

If I might be honest with you I couldn't understand much the usage and how to accomplish what I want with that config.

Specifying the groups, and mixing ['sibling', 'parent'] should achieve what you want.

It worked like this:

    "import/order": [
      "error",
      {
        "newlines-between": "always",
        "groups": ["external", "parent"]
      }
    ]

Thank you very much @ljharb

I'm sorry, I just noticed it didn't work, it's still putting a new line on local imports if it's in a different folder (group).

Try "groups": ["external", ["parent", "sibling"]]?

It worked, thank you very much @ljharb

Was this page helpful?
0 / 5 - 0 ratings

Related issues

msuntharesan picture msuntharesan  路  29Comments

ThomasdenH picture ThomasdenH  路  31Comments

kirill-konshin picture kirill-konshin  路  22Comments

steve-taylor picture steve-taylor  路  24Comments

mieszko4 picture mieszko4  路  25Comments