prettier-plugin-sort-imports icon indicating copy to clipboard operation
prettier-plugin-sort-imports copied to clipboard

Control the separation of import groups as mentioned in #36

Open atombrenner opened this issue 4 years ago • 9 comments

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 avatar Mar 25 '21 14:03 atombrenner

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

atombrenner avatar Apr 27 '21 07:04 atombrenner

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 avatar Apr 27 '21 07:04 ayusharma

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 avatar Nov 02 '21 12:11 byara

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

pjc0247 avatar Apr 12 '22 02:04 pjc0247

Any chance we can get this merged soon 🙏

elliotwestlake avatar Apr 26 '22 15:04 elliotwestlake

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.

IanVS avatar May 17 '22 00:05 IanVS

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

Kovic77 avatar Oct 25 '22 14:10 Kovic77

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

Velua avatar Dec 28 '22 02:12 Velua