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

Add import groups configuration #36

Closed
bastienrobert opened this issue Mar 10, 2021 · 7 comments
Closed

Add import groups configuration #36

bastienrobert opened this issue Mar 10, 2021 · 7 comments

Comments

@bastienrobert
Copy link

bastienrobert commented Mar 10, 2021

Summary

My actual use-case is that I want React to be imported as first but I also want line breaking between my imports. If in the importOrder configuration I setup the following : "importOrder": ["^react", "^(?!react)\\w+$"] (from #7 (comment)) with importOrderSeparation: true, it will add a line break between my imports from node_modules.

Actual behavior

import React from 'react'

import classnames from 'classnames'

Expected behavior

import React from 'react'
import classnames from 'classnames'

For the moment, if you want to importOrderSeparation, there is no way of configuring import groups. I think it could be awesome if we could specify in the configuration where is the line breaking.

Possible solution

Create a configuration like:

{
  "importOrder": [
    // no line breaking if specified in a sub-array
    ["^react", "^(?!react)\\w+$"],
    "^[./]"
  ]
}

We could event remove the importOrderSeparation option because this way will allow you to put everything in the same block:

{
  "importOrder": [
    // no line breaking if specified in a sub-array
     [
       "^react", 
       "^(?!react)\\w+$", 
       "^[./]"
     ]
  ]
}

I'll be glad to PR this ;)

Related to #1 & #7

@atombrenner
Copy link

atombrenner commented Mar 25, 2021

I like the idea of a nested array for configuring groups. Unfortunately, prettier does not allow this type of option. Only int, boolean, choice and path are allowed. importOrder is of type array of path (=string), an array of arrays would lead to an invalid prettier configuration.
Maybe empty strings could be used to indicate where separtors should be inserted:

{
  "importOrder": [
    "^react", 
    "^(?!react)\\w+$", 
    "", // empty string separates import
    "^[./]"
  ]
}

@atombrenner
Copy link

I added PR #42 as implementation

@atombrenner
Copy link

Hi @ayusharma did you have time to look into my PR? Totally OK if you don't like it, just a little feedback would be nice, if there is any chance that you could look into it. :-)

@ayusharma
Copy link
Collaborator

Closing the issue in favor of #42

@nomoon
Copy link

nomoon commented Oct 15, 2021

I see that #42 was closed. This is a shame. This is currently one of the only widely-available and actively maintained import sorting packages, but its inability to let people configure it in a few more ways (such as being able to decide what patterns are grouped together and which are separated) makes it far less usable in projects which have an established convention.

@bastienrobert
Copy link
Author

bastienrobert commented Oct 15, 2021

Dude, I agree with you but please keep it cool.
I'm going to open a new repo ASAP, I'll keep you in touch.

@byara
Copy link
Collaborator

byara commented Nov 2, 2021

I have re-open #42 due to the fact that it can help a lot of people and solve lot's of issues. Let's work on it and improve the PR.

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

No branches or pull requests

5 participants