Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import order rule #1971

Closed
theprobugmaker opened this issue Jan 5, 2021 · 7 comments
Closed

Import order rule #1971

theprobugmaker opened this issue Jan 5, 2021 · 7 comments

Comments

@theprobugmaker
Copy link

theprobugmaker commented Jan 5, 2021

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.

@ljharb
Copy link
Member

ljharb commented Jan 5, 2021

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.

@theprobugmaker
Copy link
Author

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

@ljharb
Copy link
Member

ljharb commented Jan 5, 2021

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

@theprobugmaker
Copy link
Author

It worked like this:

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

Thank you very much @ljharb

@theprobugmaker
Copy link
Author

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).

@ljharb
Copy link
Member

ljharb commented Jan 6, 2021

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

@theprobugmaker
Copy link
Author

It worked, thank you very much @ljharb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants