Skip to content
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
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const newLineCharacters = '\n\n';
*/
export const THIRD_PARTY_MODULES_SPECIAL_WORD = '<THIRD_PARTY_MODULES>';

export const THIRD_PARTY_TYPES_SPECIAL_WORD = '<THIRD_PARTY_TS_TYPES>';
export const TYPES_SPECIAL_WORD = '<TS_TYPES>';

const PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE =
'PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE';

Expand Down
70 changes: 70 additions & 0 deletions src/utils/__tests__/get-sorted-nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ import XY from 'XY';
import Xa from 'Xa';
`;

const typeCode = `// first comment
// second comment
import type sm from 'sm';
import type xl from './xl';
import l from './l';
import z from 'z';
import c, { cD } from 'c';
import g from 'g';
import { tC, tA, tB } from 't';
import k, { kE, kB } from 'k';
import * as a from 'a';
import * as x from 'x';
import BY from 'BY';
import Ba from 'Ba';
import XY from 'XY';
import Xa from 'Xa';
`;

test('it returns all sorted nodes', () => {
const result = getImportNodes(code);
const sorted = getSortedNodes(result, {
Expand Down Expand Up @@ -329,3 +347,55 @@ test('it returns all sorted nodes with namespace specifiers at the top', () => {
'z',
]);
});

test('it returns all sorted nodes with types', () => {
const result = getImportNodes(typeCode, {
plugins: ['typescript'],
});
const sorted = getSortedNodes(result, {
importOrder: ["<THIRD_PARTY_TS_TYPES>", "^[./]", "<TS_TYPES>^[./]"],
importOrderCaseInsensitive: false,
importOrderSeparation: false,
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
}) as ImportDeclaration[];

expect(getSortedNodesNames(sorted)).toEqual([
'BY',
'Ba',
'XY',
'Xa',
'a',
'c',
'g',
'k',
't',
'x',
'z',
'sm',
'./l',
'./xl',
]);
expect(
sorted
.filter((node) => node.type === 'ImportDeclaration')
.map((importDeclaration) =>
getSortedNodesModulesNames(importDeclaration.specifiers),
),
).toEqual([
['BY'],
['Ba'],
['XY'],
['Xa'],
['a'],
['c', 'cD'],
['g'],
['k', 'kE', 'kB'],
['tC', 'tA', 'tB'],
['x'],
['z'],
['sm'],
['l'],
['xl'],
]);
});
23 changes: 20 additions & 3 deletions src/utils/get-import-nodes-matched-group.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ImportDeclaration } from '@babel/types';

import { THIRD_PARTY_MODULES_SPECIAL_WORD } from '../constants';
import {
THIRD_PARTY_MODULES_SPECIAL_WORD,
THIRD_PARTY_TYPES_SPECIAL_WORD,
TYPES_SPECIAL_WORD,
} from '../constants';

/**
* Get the regexp group to keep the import nodes.
Expand All @@ -13,13 +17,26 @@ export const getImportNodesMatchedGroup = (
) => {
const groupWithRegExp = importOrder.map((group) => ({
group,
regExp: new RegExp(group),
regExp: group.startsWith(TYPES_SPECIAL_WORD)
? new RegExp(group.replace(TYPES_SPECIAL_WORD, ''))
: new RegExp(group),
}));

for (const { group, regExp } of groupWithRegExp) {
if (
(group.startsWith(TYPES_SPECIAL_WORD) &&
node.importKind !== 'type') ||
(!group.startsWith(TYPES_SPECIAL_WORD) &&
node.importKind === 'type')
)
continue;

const matched = node.source.value.match(regExp) !== null;
if (matched) return group;
}

return THIRD_PARTY_MODULES_SPECIAL_WORD;
return node.importKind === 'type' &&
importOrder.find((group) => group === THIRD_PARTY_TYPES_SPECIAL_WORD)
? THIRD_PARTY_TYPES_SPECIAL_WORD
: THIRD_PARTY_MODULES_SPECIAL_WORD;
};