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

[Breaking] Remove importOrderSeparation option #62

Merged
merged 9 commits into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 23 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,31 @@ Since then more critical features & fixes have been added. As a result, this rep
- Combine imports from the same source ([`importOrderMergeDuplicateImports`](#importordermergeduplicateimports))
- Combine type and value imports ([`importOrderCombineTypeAndValueImports`](#importordercombinetypeandvalueimports))
- Sorts node.js builtin modules to top
- Custom import order separation ([`importOrderSeparation`](#importorderseparation))
- Custom import order separation

[We welcome contributions!](./CONTRIBUTING.md)

**Table of Contents**

- [Sample](#sample)
- [Input](#input)
- [Output](#output)
- [Install](#install)
- [Usage](#usage)
- [How does import sort work?](#how-does-import-sort-work)
- [Options](#options)
- [`importOrder`](#importorder)
- [`importOrderSeparation`](#importorderseparation)
- [`importOrderSortSpecifiers`](#importordersortspecifiers)
- [`importOrderGroupNamespaceSpecifiers`](#importordergroupnamespacespecifiers)
- [`importOrderCaseInsensitive`](#importordercaseinsensitive)
- [`importOrderMergeDuplicateImports`](#importordermergeduplicateimports)
- [`importOrderCombineTypeAndValueImports`](#importordercombinetypeandvalueimports)
- [`importOrderParserPlugins`](#importorderparserplugins)
- [Prevent imports from being sorted](#prevent-imports-from-being-sorted)
- [FAQ / Troubleshooting](#faq--troubleshooting)
- [Compatibility](#compatibility)
- [Contribution](#contribution)
- [Disclaimer](#disclaimer)
- [Sample](#sample)
- [Input](#input)
- [Output](#output)
- [Install](#install)
- [Usage](#usage)
- [How does import sort work?](#how-does-import-sort-work)
- [Options](#options)
- [`importOrder`](#importorder)
- [`importOrderSortSpecifiers`](#importordersortspecifiers)
- [`importOrderGroupNamespaceSpecifiers`](#importordergroupnamespacespecifiers)
- [`importOrderCaseInsensitive`](#importordercaseinsensitive)
- [`importOrderMergeDuplicateImports`](#importordermergeduplicateimports)
- [`importOrderCombineTypeAndValueImports`](#importordercombinetypeandvalueimports)
- [`importOrderParserPlugins`](#importorderparserplugins)
- [Prevent imports from being sorted](#prevent-imports-from-being-sorted)
- [FAQ / Troubleshooting](#faq--troubleshooting)
- [Compatibility](#compatibility)
- [Contribution](#contribution)
- [Disclaimer](#disclaimer)

## Sample

Expand Down Expand Up @@ -113,7 +112,7 @@ or, using yarn
yarn add --dev @ianvs/prettier-plugin-sort-imports
```

**Note: If you are migrating from v2.x.x to v3.x.x, [Please Read Migration Guidelines](./docs/MIGRATION.md)**
**Note: If you are migrating from v3.x.x to v4.x.x, [Please Read Migration Guidelines](./docs/MIGRATION.md)**

## Usage

Expand All @@ -129,12 +128,11 @@ module.exports = {
trailingComma: 'all',
singleQuote: true,
semi: true,
importOrder: ['^@core/(.*)$', '^@server/(.*)$', '^@ui/(.*)$', '^[./]'],
importOrder: ['^@core/(.*)$', '', '^@server/(.*)$', '', '^@ui/(.*)$', '', '^[./]'],
importOrderCaseInsensitive: true,
importOrderParserPlugins: ['typescript', 'jsx', 'decorators-legacy'],
importOrderMergeDuplicateImports: true,
importOrderCombineTypeAndValueImports: true,
importOrderSeparation: true,
importOrderSortSpecifiers: true,
};
```
Expand Down Expand Up @@ -212,23 +210,9 @@ If you would like to order type imports differently from value imports, you can
"importOrder": ["<TYPES>", "<TYPES>^[./]", "<THIRD_PARTY_MODULES>", "^[./]"],
```

#### `importOrderSeparation`

**type**: `boolean`

**default value**: `false`

A boolean value to enable or disable the new line separation
between sorted import declarations group. The separation takes place according to the `importOrder`.

```json
"importOrderSeparation": true,
```

_Note:_ If you want greater control over which groups are separated from others, you can add an empty string to your `importOrder` array to signify newlines. For example:
_Note:_ If you want to separate some groups from others, you can add an empty string to your `importOrder` array to signify newlines. For example:

```js
"importOrderSeparation": false,
"importOrder": [
"^react", // React will be placed at the top of third-party modules
"<THIRD_PARTY_MODULES>",
Expand Down
33 changes: 33 additions & 0 deletions docs/MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,39 @@

---

### Migrating from v3.x.x to v4.x.x

- The `importOrderBuiltinModulesToTop` option has been removed, and node.js built in modules are always sorted to the top.
- The `importOrderSeparation` option has been removed. Use empty quotes in your `importOrder` to control the placement of blank lines.

#### `importOrderSeparation` removed

This option was removed to simplify the configuration of the plugin. But if you like to separate your import groups with newlines, you can do so by adding `""` groups to your `importOrder` array.

For example:

```js
"importOrder": [
"", // This emptry group at the start will add separators for side-effect imports and node.js built-in modules
"<THIRD_PARTY_MODULES>",
"",
"^@app/(.*)$",
"",
"^[./]"
]
```

Or, if you would like to keep all imports together, but add a newline before side-effect imports:

```js
"importOrder": [
"<THIRD_PARTY_MODULES>",
"^@app/(.*)$",
"^[./]"
"", // This will add a newline between side-effect groups (i.e. the chunks that are sorted)
]
```

### Migrating from v2.x.x to v3.x.x

#### TL;DR
Expand Down
3 changes: 1 addition & 2 deletions prettier.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module.exports = {
bracketSameLine: true,
semi: true,
plugins: [require('./lib/src/index.js')],
importOrder: ['^[./]'],
importOrderSeparation: true,
importOrder: ['', '<THIRD_PARTY_MODULES>', '', '^[./]'],
importOrderSortSpecifiers: true,
};
6 changes: 0 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ export const options: Record<
default: [{ value: ['typescript', 'jsx'] }],
description: 'Provide a list of plugins for special syntax',
},
importOrderSeparation: {
type: 'boolean',
category: 'Global',
default: false,
description: 'Should imports be separated by new line?',
},
importOrderGroupNamespaceSpecifiers: {
type: 'boolean',
category: 'Global',
Expand Down
2 changes: 0 additions & 2 deletions src/preprocessors/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export function preprocessor(code: string, options: PrettierOptions): string {
importOrderCaseInsensitive,
importOrderGroupNamespaceSpecifiers,
importOrderMergeDuplicateImports,
importOrderSeparation,
importOrderSortSpecifiers,
} = options;

Expand Down Expand Up @@ -73,7 +72,6 @@ export function preprocessor(code: string, options: PrettierOptions): string {
importOrderGroupNamespaceSpecifiers,
importOrderMergeDuplicateImports,
importOrderCombineTypeAndValueImports,
importOrderSeparation,
importOrderSortSpecifiers,
});

Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export type GetSortedNodes = (
| 'importOrderGroupNamespaceSpecifiers'
| 'importOrderMergeDuplicateImports'
| 'importOrderCombineTypeAndValueImports'
| 'importOrderSeparation'
| 'importOrderSortSpecifiers'
>,
) => ImportOrLine[];
Expand Down
1 change: 0 additions & 1 deletion src/utils/__tests__/get-all-comments-from-nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const getSortedImportNodes = (code: string, options?: ParserOptions) => {
importOrderGroupNamespaceSpecifiers: false,
importOrderMergeDuplicateImports: false,
importOrderCombineTypeAndValueImports: false,
importOrderSeparation: false,
importOrderSortSpecifiers: false,
});
};
Expand Down
2 changes: 0 additions & 2 deletions src/utils/__tests__/get-code-from-ast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import a from 'a';
importOrderGroupNamespaceSpecifiers: false,
importOrderMergeDuplicateImports: false,
importOrderCombineTypeAndValueImports: false,
importOrderSeparation: false,
importOrderSortSpecifiers: false,
});
const formatted = getCodeFromAst({
Expand Down Expand Up @@ -63,7 +62,6 @@ import type {See} from 'c';
importOrderGroupNamespaceSpecifiers: false,
importOrderMergeDuplicateImports: true,
importOrderCombineTypeAndValueImports: false,
importOrderSeparation: false,
importOrderSortSpecifiers: false,
});
const formatted = getCodeFromAst({
Expand Down
87 changes: 0 additions & 87 deletions src/utils/__tests__/get-sorted-nodes-by-import-order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ test('it returns all sorted nodes', () => {
importOrderGroupNamespaceSpecifiers: false,
importOrderMergeDuplicateImports: false,
importOrderCombineTypeAndValueImports: false,
importOrderSeparation: false,
importOrderSortSpecifiers: false,
}) as ImportDeclaration[];

Expand Down Expand Up @@ -87,7 +86,6 @@ test('it returns all sorted nodes case-insensitive', () => {
importOrderGroupNamespaceSpecifiers: false,
importOrderMergeDuplicateImports: false,
importOrderCombineTypeAndValueImports: false,
importOrderSeparation: false,
importOrderSortSpecifiers: false,
}) as ImportDeclaration[];

Expand Down Expand Up @@ -141,7 +139,6 @@ test('it returns all sorted nodes with sort order', () => {
importOrderGroupNamespaceSpecifiers: false,
importOrderMergeDuplicateImports: false,
importOrderCombineTypeAndValueImports: false,
importOrderSeparation: false,
importOrderSortSpecifiers: false,
}) as ImportDeclaration[];

Expand Down Expand Up @@ -195,7 +192,6 @@ test('it returns all sorted nodes with sort order case-insensitive', () => {
importOrderGroupNamespaceSpecifiers: false,
importOrderMergeDuplicateImports: false,
importOrderCombineTypeAndValueImports: false,
importOrderSeparation: false,
importOrderSortSpecifiers: false,
}) as ImportDeclaration[];
expect(getSortedNodesNamesAndNewlines(sorted)).toEqual([
Expand Down Expand Up @@ -248,7 +244,6 @@ test('it returns all sorted import nodes with sorted import specifiers', () => {
importOrderGroupNamespaceSpecifiers: false,
importOrderMergeDuplicateImports: false,
importOrderCombineTypeAndValueImports: false,
importOrderSeparation: false,
importOrderSortSpecifiers: true,
}) as ImportDeclaration[];
expect(getSortedNodesNamesAndNewlines(sorted)).toEqual([
Expand Down Expand Up @@ -301,7 +296,6 @@ test('it returns all sorted import nodes with sorted import specifiers with case
importOrderGroupNamespaceSpecifiers: false,
importOrderMergeDuplicateImports: false,
importOrderCombineTypeAndValueImports: false,
importOrderSeparation: false,
importOrderSortSpecifiers: true,
}) as ImportDeclaration[];
expect(getSortedNodesNamesAndNewlines(sorted)).toEqual([
Expand Down Expand Up @@ -354,7 +348,6 @@ test('it returns all sorted nodes with namespace specifiers at the top (under bu
importOrderGroupNamespaceSpecifiers: true,
importOrderMergeDuplicateImports: false,
importOrderCombineTypeAndValueImports: false,
importOrderSeparation: false,
importOrderSortSpecifiers: false,
}) as ImportDeclaration[];

Expand Down Expand Up @@ -385,7 +378,6 @@ test('it returns all sorted nodes with builtin specifiers at the top, ', () => {
importOrderGroupNamespaceSpecifiers: false,
importOrderMergeDuplicateImports: false,
importOrderCombineTypeAndValueImports: false,
importOrderSeparation: false,
importOrderSortSpecifiers: false,
}) as ImportDeclaration[];

Expand Down Expand Up @@ -416,7 +408,6 @@ test('it returns all sorted nodes with custom third party modules and builtins a
importOrderGroupNamespaceSpecifiers: false,
importOrderMergeDuplicateImports: false,
importOrderCombineTypeAndValueImports: false,
importOrderSeparation: false,
importOrderSortSpecifiers: false,
}) as ImportDeclaration[];
expect(getSortedNodesNamesAndNewlines(sorted)).toEqual([
Expand All @@ -438,39 +429,6 @@ test('it returns all sorted nodes with custom third party modules and builtins a
]);
});

test('it adds newlines when importOrderSeparation is true', () => {
const result = getImportNodes(code);
const sorted = getSortedNodesByImportOrder(result, {
importOrder: ['^[./]'],
importOrderCaseInsensitive: true,
importOrderGroupNamespaceSpecifiers: false,
importOrderMergeDuplicateImports: false,
importOrderCombineTypeAndValueImports: false,
importOrderSeparation: true,
importOrderSortSpecifiers: false,
}) as ImportDeclaration[];
expect(getSortedNodesNamesAndNewlines(sorted)).toEqual([
'node:fs/promises',
'node:url',
'path',
'',
'a',
'Ba',
'BY',
'c',
'g',
'k',
't',
'x',
'Xa',
'XY',
'z',
'',
'./local',
'',
]);
});

test('it returns all sorted nodes with custom separation', () => {
const result = getImportNodes(code);
const sorted = getSortedNodesByImportOrder(result, {
Expand All @@ -486,54 +444,13 @@ test('it returns all sorted nodes with custom separation', () => {
importOrderGroupNamespaceSpecifiers: false,
importOrderMergeDuplicateImports: false,
importOrderCombineTypeAndValueImports: false,
importOrderSeparation: false,
importOrderSortSpecifiers: false,
}) as ImportDeclaration[];
expect(getSortedNodesNamesAndNewlines(sorted)).toEqual([
'node:fs/promises',
'node:url',
'path',
'a',
'Ba',
'BY',
'c',
'g',
'x',
'Xa',
'XY',
'z',
't',
'',
'k',
'./local',
]);
});

test('it allows both importOrderSeparation and custom separation (but why?)', () => {
const result = getImportNodes(code);
const sorted = getSortedNodesByImportOrder(result, {
importOrder: [
'^a$',
'<THIRD_PARTY_MODULES>',
'^t$',
'',
'^k$',
'^[./]',
],
importOrderCaseInsensitive: true,
importOrderGroupNamespaceSpecifiers: false,
importOrderMergeDuplicateImports: false,
importOrderCombineTypeAndValueImports: false,
importOrderSeparation: true,
importOrderSortSpecifiers: false,
}) as ImportDeclaration[];
expect(getSortedNodesNamesAndNewlines(sorted)).toEqual([
'node:fs/promises',
'node:url',
'path',
'',
'a',
'',
'Ba',
'BY',
'c',
Expand All @@ -542,13 +459,10 @@ test('it allows both importOrderSeparation and custom separation (but why?)', ()
'Xa',
'XY',
'z',
'',
't',
'',
'k',
'',
'./local',
'',
]);
});

Expand All @@ -569,7 +483,6 @@ test('it does not add multiple custom import separators', () => {
importOrderGroupNamespaceSpecifiers: false,
importOrderMergeDuplicateImports: false,
importOrderCombineTypeAndValueImports: false,
importOrderSeparation: false,
importOrderSortSpecifiers: false,
}) as ImportDeclaration[];
expect(getSortedNodesNamesAndNewlines(sorted)).toEqual([
Expand Down
Loading