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

Control the separation of import groups as mentioned in #36 #42

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

atombrenner
Copy link

Add the possibility to control the separation of import groups as mentioned in #36

Motivation: You want to move "React imports" always to the top and only add a separator between node_modules imports and local imports.

import thirdParty from 'third-party';
import React, { FC } from 'react';
import {localstuff} from '../localstuff';
import otherthing from '@core/otherthing';
export const bla = "..."

should be formatted as

import React, { FC } from 'react';
import thirdParty from 'third-party';

import otherthing from '@core/otherthing';
import { localstuff } from '../localstuff';

export const bla = "..."

You can do this by specifying empty strings in importOrder in .prettierrc:

{
 "importOrder": [
    "^react",
    "^(?!(react|@core/))\\w+",
    "",  // use empty strings to separate groups with empty lines
    "^@(core|ui)/",
    "^."
  ]
}

@atombrenner
Copy link
Author

Hi @ayusharma, any feedback highly appreciated. Is there any chance that this PR gets accepted or should I use a fork?

@ayusharma
Copy link
Collaborator

Hi @atombrenner, I really appreciate your efforts here. Sorry, We do not agree with this approach, This plugin's idea is to keep the separation between only third and local imports. Please proceed with your fork. I do not think this implementation fulfil the general idea.

@ayusharma ayusharma closed this Apr 27, 2021
@byara
Copy link
Collaborator

byara commented Nov 2, 2021

I'll re-open this PR due to the fact that it can help a lot of people and solve lot's of issues.
Considering v3, the idea is to either add new special words or options to the current one to create groupings.

@byara byara reopened this Nov 2, 2021
@pjc0247
Copy link

pjc0247 commented Apr 12, 2022

This PR gives more flexibility and does not break previous behaviours. (unless you put empty lines)

@elliotwestlake
Copy link

Any chance we can get this merged soon 🙏

IanVS added a commit to IanVS/prettier-plugin-sort-imports that referenced this pull request May 17, 2022
Closes #11

This is an adaptation of trivago/prettier-plugin-sort-imports#42 from @atombrenner.  I wasn't able to use most of the commits from the original branch, because our approach is different in this project, but I did pull in his commit with tests.  

The other change I needed to make here was to modify the cloned nodes in `adjustCommentsOnSortedNodes`, rather than mutating the original, which was leading to some strange bugs in the tests.  

* Add test for "local" groups, include newlines in tests

`getSortedNodesByImportOrder` includes a check for `importOrderSeparation`,
and adds newlines when it's true, so we should include that in our tests.

* Add support for custom import group separators

* add tests

* Update snapshots

But the last snapshot is wrong, will fix in next commit

* Copy nodes when adjusting comments

Mutating the original nodes causes strange effects, as seen in the snapshot

* Avoid duplicate newlines

* minor: Remove unused variables

* Update README

Co-authored-by: Christian Rodemeyer <christian.rodemeyer@sz.de>
@IanVS
Copy link

IanVS commented May 17, 2022

For those who need this, it has been released in a fork, https://github.com/IanVS/prettier-plugin-sort-imports/releases/tag/v3.4.0.

@Kovic77
Copy link

Kovic77 commented Oct 25, 2022

You can use regex for grouping specific libraries like =>
importOrder: ['((^|, )(react|react-native))+$', '<THIRD_PARTY_MODULES>', ... ],

@Velua
Copy link

Velua commented Dec 28, 2022

It's clear there's a fair amount of support for this one, any ideas on ETA?

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

Successfully merging this pull request may close these issues.

None yet

9 participants