Skip to content
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ import b from 'b'
import c from 'c'
```

### `importOrderSortGroups`
**type**: `boolean`
**default value**: `true`

By default, the plugin sorts imports within groups. If you need to keep ordering within groups consistent, set this option to false.


### Ignoring import ordering

Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ const options: Options = {
default: 'with',
description: 'Provide a keyword for import attributes',
},
importOrderSortGroups: {
type: 'boolean',
category: 'Global',
default: true,
description: 'Should imports be sorted within groups?',
},
};

export default {
Expand Down
2 changes: 2 additions & 0 deletions src/preprocessors/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function preprocessor(code: string, options: PrettierOptions) {
importOrderSideEffects,
importOrderImportAttributesKeyword,
importOrderExclude,
importOrderSortGroups,
filepath,
} = options;

Expand Down Expand Up @@ -62,6 +63,7 @@ export function preprocessor(code: string, options: PrettierOptions) {
importOrderSortSpecifiers,
importOrderSortByLength,
importOrderSideEffects,
importOrderSortGroups,
});

return getCodeFromAst(allImports, code, injectIdx, {
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { RequiredOptions } from 'prettier';
import { PluginConfig } from '../types';

export interface PrettierOptions
extends Required<PluginConfig>,
RequiredOptions {}
extends Required<PluginConfig>, RequiredOptions {}

export type ImportGroups = Record<string, ImportDeclaration[]>;
export type ImportOrLine = ImportDeclaration | ExpressionStatement;
Expand All @@ -21,6 +20,7 @@ export type GetSortedNodes = (
| 'importOrderSortSpecifiers'
| 'importOrderSortByLength'
| 'importOrderSideEffects'
| 'importOrderSortGroups'
>,
) => ImportOrLine[];

Expand Down
2 changes: 2 additions & 0 deletions src/utils/__tests__/assemble-updated-code.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ test('it should remove nodes from the original code', async () => {
importOrderSortSpecifiers: false,
importOrderSideEffects: true,
importOrderSortByLength: null,
importOrderSortGroups: true,
});
const allCommentsFromImports = getAllCommentsFromNodes(sortedNodes);

Expand Down Expand Up @@ -57,6 +58,7 @@ test('it should inject the generated code at the correct location', async () =>
importOrderSortSpecifiers: false,
importOrderSideEffects: true,
importOrderSortByLength: null,
importOrderSortGroups: true,
});
const allCommentsFromImports = getAllCommentsFromNodes(sortedNodes);

Expand Down
1 change: 1 addition & 0 deletions src/utils/__tests__/get-all-comments-from-nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const getSortedImportNodes = (code: string, options?: ParserOptions) => {
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderSortByLength: null,
importOrderSortGroups: true,
importOrderSideEffects: true,
});
};
Expand Down
1 change: 1 addition & 0 deletions src/utils/__tests__/get-code-from-ast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import a from 'a';
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderSortByLength: null,
importOrderSortGroups: true,
importOrderSideEffects: true,
});
const formatted = getCodeFromAst(sortedNodes, code);
Expand Down
13 changes: 13 additions & 0 deletions src/utils/__tests__/get-sorted-nodes-by-import-order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ test('it returns all sorted nodes', () => {
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderSortByLength: null,
importOrderSortGroups: true,
importOrderSideEffects: true,
}) as ImportDeclaration[];

Expand Down Expand Up @@ -78,6 +79,7 @@ test('it returns all sorted nodes case-insensitive', () => {
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderSortByLength: null,
importOrderSortGroups: true,
importOrderSideEffects: true,
}) as ImportDeclaration[];

Expand Down Expand Up @@ -124,6 +126,7 @@ test('it returns all sorted nodes with sort order', () => {
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderSortByLength: null,
importOrderSortGroups: true,
importOrderSideEffects: true,
}) as ImportDeclaration[];

Expand Down Expand Up @@ -170,6 +173,7 @@ test('it returns all sorted nodes with sort order case-insensitive', () => {
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderSortByLength: null,
importOrderSortGroups: true,
importOrderSideEffects: true,
}) as ImportDeclaration[];
expect(getSortedNodesNames(sorted)).toEqual([
Expand Down Expand Up @@ -215,6 +219,7 @@ test('it returns all sorted import nodes with sorted import specifiers', () => {
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: true,
importOrderSortByLength: null,
importOrderSortGroups: true,
importOrderSideEffects: true,
}) as ImportDeclaration[];
expect(getSortedNodesNames(sorted)).toEqual([
Expand Down Expand Up @@ -260,6 +265,7 @@ test('it returns all sorted import nodes with sorted import specifiers with case
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: true,
importOrderSortByLength: null,
importOrderSortGroups: true,
importOrderSideEffects: true,
}) as ImportDeclaration[];
expect(getSortedNodesNames(sorted)).toEqual([
Expand Down Expand Up @@ -305,6 +311,7 @@ test('it returns all sorted nodes with custom third party modules', () => {
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderSortByLength: null,
importOrderSortGroups: true,
importOrderSideEffects: true,
}) as ImportDeclaration[];
expect(getSortedNodesNames(sorted)).toEqual([
Expand All @@ -331,6 +338,7 @@ test('it returns all sorted nodes with namespace specifiers at the top', () => {
importOrderGroupNamespaceSpecifiers: true,
importOrderSortSpecifiers: false,
importOrderSortByLength: null,
importOrderSortGroups: true,
importOrderSideEffects: true,
}) as ImportDeclaration[];

Expand Down Expand Up @@ -367,6 +375,7 @@ test('it returns the default separations if `importOrderSeparation` is false', (
importOrderSortSpecifiers: false,
importOrderSideEffects: true,
importOrderSortByLength: null,
importOrderSortGroups: true,
});
expect(getSeparationData(sorted)).toEqual([
{ type: 'ImportDeclaration', value: 'XY' },
Expand Down Expand Up @@ -394,6 +403,7 @@ test('it returns default import module separations', () => {
importOrderSortSpecifiers: false,
importOrderSideEffects: true,
importOrderSortByLength: null,
importOrderSortGroups: true,
});
expect(getSeparationData(sorted)).toEqual([
{ type: 'ImportDeclaration', value: 'XY' },
Expand Down Expand Up @@ -426,6 +436,7 @@ test('it returns targeted import module separations', () => {
importOrderSortSpecifiers: false,
importOrderSideEffects: true,
importOrderSortByLength: null,
importOrderSortGroups: true,
});
expect(getSeparationData(sorted)).toEqual([
{ type: 'ImportDeclaration', value: 'XY' },
Expand Down Expand Up @@ -459,6 +470,7 @@ test('it never returns a separation at the top of the list (leading separator)',
importOrderSortSpecifiers: false,
importOrderSideEffects: true,
importOrderSortByLength: null,
importOrderSortGroups: true,
});
expect(getSeparationData(sorted)).toEqual([
{ type: 'ImportDeclaration', value: './test' },
Expand All @@ -480,6 +492,7 @@ test('it never returns a separation at the top of the list (zero preceding impor
importOrderSortSpecifiers: false,
importOrderSideEffects: true,
importOrderSortByLength: null,
importOrderSortGroups: true,
});
expect(getSeparationData(sorted)).toEqual([
{ type: 'ImportDeclaration', value: './test' },
Expand Down
Loading