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 group by prefix #795

Open
merlinstardust opened this issue Apr 6, 2017 · 9 comments
Open

import/order group by prefix #795

merlinstardust opened this issue Apr 6, 2017 · 9 comments

Comments

@merlinstardust
Copy link

I import from all over my app, with a mix of relative and root imports. The current order rule covers most of my cases, and with #629 fixing #389 (sorting alphabetically), that will cover more. But one case that won't be covered is that I prefer to have all my global functions imported first, which come from a /lib directory, followed by my components which come from a /client/modules directory.

Sorting alphabetically, I would have to do

import someComponent from '/client/modules/someComponent';
import someFunction from '/lib/someFunction';

But I'd rather do

import someFunction from '/lib/someFunction';
import someComponent from '/client/modules/someComponent';

Within the rule itself, there's one way I could see it being defined

Use the existing groups option, and anything that isn't currently an option (builtins, sibling, etc) would be considered a prefix
"import/order": ["error", {"groups": ["builtins", "external", "/lib", "/client"]}]

This would also handle the concern in this comment that people will start to request a bunch of order types and exceptions to them

@ljharb
Copy link
Member

ljharb commented Apr 6, 2017

If we allow any string there, then adding any new options goes from being a semver-minor to a semver-major, and there'd be no easy way to tell people when they have to update their configs.

@merlinstardust
Copy link
Author

What if the prefixes were done in a second option prefixGroups, like so?

"import/order": ["error", {
  "groups": ["builtins", "external"]
  "prefixGroups": [
    {
      "prefix": "/lib",
      "before": "external"
    },
    {
      "prefix": "/client",
      "after": "external"
    },
}]

The before and after arguments allow for ordering alongside the base groups that you have.

@ljharb
Copy link
Member

ljharb commented Apr 6, 2017

That would avoid the conflict I specified.

Also, instead of a prefix, we'd probably want to use globs - ie, /lib/** would be for a prefix, *_test.js for a suffix, etc (call it "pattern")

@merlinstardust
Copy link
Author

Globs would work as well. And I like the addition of the suffix.

What's the difficulty of doing something like this? I'd love to contribute this myself if possible. Do you have any suggested resources (aside from the code itself) on how to get started with writing part of a linter plugin?

@bensampaio
Copy link

I am also interested in this. Are there any plans to implement this?

@gapipro
Copy link

gapipro commented Jun 21, 2017

Yea I would also like to see support for this.

Is it posible to also declare imports based on imported file extension?

For example I want to have .css and .md imports at the bottom of the list.

@loopmode
Copy link

See my comment in #807 (comment)

I think in times of yarn/lerna workspaces, users need some degree of control about e.g. what is "internal". Having namespaced packages should actually help the situation.

@AdrienLemaire
Copy link

Would love to see some action on this feature request.
Using babel-plugin-root-import, I wish import/order could interpret all my ~/ imports as one group.

@kg-currenxie
Copy link

Any news on this? :(

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

No branches or pull requests

7 participants