Skip to content

Commit 868001e

Browse files
Merge branch 'v6' into pr354
2 parents d9cae1b + e4a1b27 commit 868001e

14 files changed

Lines changed: 618 additions & 451 deletions

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,14 @@ with options as a JSON string of the plugin array:
206206
importOrderParserPlugins: []
207207
```
208208

209-
#### `importOrderSideEffects`
209+
### `importOrderSortByLength`
210+
**type**: `'asc' | 'desc' | null`
211+
**default value**: `null`
210212

213+
A choice value to enable sorting imports within their groups based on their string lengths, the two options being ascending and descending.
214+
Leaving the value blank or setting it to null will result in length being ignored
215+
216+
### `importOrderSideEffects`
211217
**type**: `boolean`
212218

213219
**default value**: `true`

src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ const options: Options = {
5656
default: false,
5757
description: 'Should specifiers be sorted?',
5858
},
59+
importOrderSortByLength: {
60+
type: 'choice',
61+
category: 'Global',
62+
default: null,
63+
choices: [
64+
{ value: 'asc', description: 'will sort from shortest to longest' },
65+
{
66+
value: 'desc',
67+
description: 'will sort from longest to shortest',
68+
},
69+
{
70+
value: null,
71+
description: 'will disable sorting based on length',
72+
},
73+
],
74+
description: 'Should imports be sorted by their string length',
75+
},
5976
importOrderSideEffects: {
6077
type: 'boolean',
6178
category: 'Global',

src/preprocessors/preprocessor.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export function preprocessor(code: string, options: PrettierOptions) {
1616
importOrderSeparation,
1717
importOrderGroupNamespaceSpecifiers,
1818
importOrderSortSpecifiers,
19+
importOrderSortByLength,
1920
importOrderSideEffects,
2021
importOrderImportAttributesKeyword,
2122
} = options;
@@ -44,6 +45,7 @@ export function preprocessor(code: string, options: PrettierOptions) {
4445
importOrderSeparation,
4546
importOrderGroupNamespaceSpecifiers,
4647
importOrderSortSpecifiers,
48+
importOrderSortByLength,
4749
importOrderSideEffects,
4850
});
4951

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type GetSortedNodes = (
1919
| 'importOrderSeparation'
2020
| 'importOrderGroupNamespaceSpecifiers'
2121
| 'importOrderSortSpecifiers'
22+
| 'importOrderSortByLength'
2223
| 'importOrderSideEffects'
2324
>,
2425
) => ImportOrLine[];

src/utils/__tests__/get-all-comments-from-nodes.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const getSortedImportNodes = (code: string, options?: ParserOptions) => {
1515
importOrderSeparation: false,
1616
importOrderGroupNamespaceSpecifiers: false,
1717
importOrderSortSpecifiers: false,
18+
importOrderSortByLength: null,
1819
importOrderSideEffects: true,
1920
});
2021
};

src/utils/__tests__/get-code-from-ast.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import a from 'a';
2525
importOrderSeparation: false,
2626
importOrderGroupNamespaceSpecifiers: false,
2727
importOrderSortSpecifiers: false,
28+
importOrderSortByLength: null,
2829
importOrderSideEffects: true,
2930
});
3031
const formatted = getCodeFromAst(sortedNodes, [], code, null);

src/utils/__tests__/get-import-nodes-matched-group.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { expect, test } from 'vitest';
22

3-
import { THIRD_PARTY_MODULES_SPECIAL_WORD } from '../../constants.js';
4-
import { ImportGroups } from '../../types';
53
import { getImportNodesMatchedGroup } from '../get-import-nodes-matched-group.js';
64
import { getImportNodes } from '../get-import-nodes.js';
75

src/utils/__tests__/get-sorted-nodes-by-import-order.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ test('it returns all sorted nodes', () => {
3131
importOrderSeparation: false,
3232
importOrderGroupNamespaceSpecifiers: false,
3333
importOrderSortSpecifiers: false,
34+
importOrderSortByLength: null,
3435
importOrderSideEffects: true,
3536
}) as ImportDeclaration[];
3637

@@ -76,6 +77,7 @@ test('it returns all sorted nodes case-insensitive', () => {
7677
importOrderSeparation: false,
7778
importOrderGroupNamespaceSpecifiers: false,
7879
importOrderSortSpecifiers: false,
80+
importOrderSortByLength: null,
7981
importOrderSideEffects: true,
8082
}) as ImportDeclaration[];
8183

@@ -121,6 +123,7 @@ test('it returns all sorted nodes with sort order', () => {
121123
importOrderSeparation: false,
122124
importOrderGroupNamespaceSpecifiers: false,
123125
importOrderSortSpecifiers: false,
126+
importOrderSortByLength: null,
124127
importOrderSideEffects: true,
125128
}) as ImportDeclaration[];
126129

@@ -166,6 +169,7 @@ test('it returns all sorted nodes with sort order case-insensitive', () => {
166169
importOrderSeparation: false,
167170
importOrderGroupNamespaceSpecifiers: false,
168171
importOrderSortSpecifiers: false,
172+
importOrderSortByLength: null,
169173
importOrderSideEffects: true,
170174
}) as ImportDeclaration[];
171175
expect(getSortedNodesNames(sorted)).toEqual([
@@ -210,6 +214,7 @@ test('it returns all sorted import nodes with sorted import specifiers', () => {
210214
importOrderSeparation: false,
211215
importOrderGroupNamespaceSpecifiers: false,
212216
importOrderSortSpecifiers: true,
217+
importOrderSortByLength: null,
213218
importOrderSideEffects: true,
214219
}) as ImportDeclaration[];
215220
expect(getSortedNodesNames(sorted)).toEqual([
@@ -254,6 +259,7 @@ test('it returns all sorted import nodes with sorted import specifiers with case
254259
importOrderSeparation: false,
255260
importOrderGroupNamespaceSpecifiers: false,
256261
importOrderSortSpecifiers: true,
262+
importOrderSortByLength: null,
257263
importOrderSideEffects: true,
258264
}) as ImportDeclaration[];
259265
expect(getSortedNodesNames(sorted)).toEqual([
@@ -298,6 +304,7 @@ test('it returns all sorted nodes with custom third party modules', () => {
298304
importOrderCaseInsensitive: true,
299305
importOrderGroupNamespaceSpecifiers: false,
300306
importOrderSortSpecifiers: false,
307+
importOrderSortByLength: null,
301308
importOrderSideEffects: true,
302309
}) as ImportDeclaration[];
303310
expect(getSortedNodesNames(sorted)).toEqual([
@@ -323,6 +330,7 @@ test('it returns all sorted nodes with namespace specifiers at the top', () => {
323330
importOrderSeparation: false,
324331
importOrderGroupNamespaceSpecifiers: true,
325332
importOrderSortSpecifiers: false,
333+
importOrderSortByLength: null,
326334
importOrderSideEffects: true,
327335
}) as ImportDeclaration[];
328336

@@ -358,6 +366,7 @@ test('it returns the default separations if `importOrderSeparation` is false', (
358366
importOrderGroupNamespaceSpecifiers: false,
359367
importOrderSortSpecifiers: false,
360368
importOrderSideEffects: true,
369+
importOrderSortByLength: null,
361370
});
362371
expect(getSeparationData(sorted)).toEqual([
363372
{ type: 'ImportDeclaration', value: 'XY' },
@@ -384,6 +393,7 @@ test('it returns default import module separations', () => {
384393
importOrderGroupNamespaceSpecifiers: false,
385394
importOrderSortSpecifiers: false,
386395
importOrderSideEffects: true,
396+
importOrderSortByLength: null,
387397
});
388398
expect(getSeparationData(sorted)).toEqual([
389399
{ type: 'ImportDeclaration', value: 'XY' },
@@ -415,6 +425,7 @@ test('it returns targeted import module separations', () => {
415425
importOrderGroupNamespaceSpecifiers: false,
416426
importOrderSortSpecifiers: false,
417427
importOrderSideEffects: true,
428+
importOrderSortByLength: null,
418429
});
419430
expect(getSeparationData(sorted)).toEqual([
420431
{ type: 'ImportDeclaration', value: 'XY' },
@@ -447,6 +458,7 @@ test('it never returns a separation at the top of the list (leading separator)',
447458
importOrderGroupNamespaceSpecifiers: false,
448459
importOrderSortSpecifiers: false,
449460
importOrderSideEffects: true,
461+
importOrderSortByLength: null,
450462
});
451463
expect(getSeparationData(sorted)).toEqual([
452464
{ type: 'ImportDeclaration', value: './test' },
@@ -467,6 +479,7 @@ test('it never returns a separation at the top of the list (zero preceding impor
467479
importOrderGroupNamespaceSpecifiers: false,
468480
importOrderSortSpecifiers: false,
469481
importOrderSideEffects: true,
482+
importOrderSortByLength: null,
470483
});
471484
expect(getSeparationData(sorted)).toEqual([
472485
{ type: 'ImportDeclaration', value: './test' },

src/utils/__tests__/get-sorted-nodes.spec.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ test('it returns all sorted nodes', () => {
4747
importOrderSeparation: false,
4848
importOrderGroupNamespaceSpecifiers: false,
4949
importOrderSortSpecifiers: false,
50+
importOrderSortByLength: null,
5051
importOrderSideEffects: true,
5152
}) as ImportDeclaration[];
5253

@@ -92,6 +93,7 @@ test('it returns all sorted nodes case-insensitive', () => {
9293
importOrderSeparation: false,
9394
importOrderGroupNamespaceSpecifiers: false,
9495
importOrderSortSpecifiers: false,
96+
importOrderSortByLength: null,
9597
importOrderSideEffects: true,
9698
}) as ImportDeclaration[];
9799

@@ -137,6 +139,7 @@ test('it returns all sorted nodes with sort order', () => {
137139
importOrderSeparation: false,
138140
importOrderGroupNamespaceSpecifiers: false,
139141
importOrderSortSpecifiers: false,
142+
importOrderSortByLength: null,
140143
importOrderSideEffects: true,
141144
}) as ImportDeclaration[];
142145

@@ -182,6 +185,7 @@ test('it returns all sorted nodes with sort order case-insensitive', () => {
182185
importOrderSeparation: false,
183186
importOrderGroupNamespaceSpecifiers: false,
184187
importOrderSortSpecifiers: false,
188+
importOrderSortByLength: null,
185189
importOrderSideEffects: true,
186190
}) as ImportDeclaration[];
187191
expect(getSortedNodesNames(sorted)).toEqual([
@@ -226,6 +230,7 @@ test('it returns all sorted import nodes with sorted import specifiers', () => {
226230
importOrderSeparation: false,
227231
importOrderGroupNamespaceSpecifiers: false,
228232
importOrderSortSpecifiers: true,
233+
importOrderSortByLength: null,
229234
importOrderSideEffects: true,
230235
}) as ImportDeclaration[];
231236
expect(getSortedNodesNames(sorted)).toEqual([
@@ -270,6 +275,7 @@ test('it returns all sorted import nodes with sorted import specifiers with case
270275
importOrderSeparation: false,
271276
importOrderGroupNamespaceSpecifiers: false,
272277
importOrderSortSpecifiers: true,
278+
importOrderSortByLength: null,
273279
importOrderSideEffects: true,
274280
}) as ImportDeclaration[];
275281
expect(getSortedNodesNames(sorted)).toEqual([
@@ -314,6 +320,7 @@ test('it returns all sorted nodes with custom third party modules', () => {
314320
importOrderCaseInsensitive: true,
315321
importOrderGroupNamespaceSpecifiers: false,
316322
importOrderSortSpecifiers: false,
323+
importOrderSortByLength: null,
317324
importOrderSideEffects: true,
318325
}) as ImportDeclaration[];
319326
expect(getSortedNodesNames(sorted)).toEqual([
@@ -339,6 +346,7 @@ test('it returns all sorted nodes with namespace specifiers at the top', () => {
339346
importOrderSeparation: false,
340347
importOrderGroupNamespaceSpecifiers: true,
341348
importOrderSortSpecifiers: false,
349+
importOrderSortByLength: null,
342350
importOrderSideEffects: true,
343351
}) as ImportDeclaration[];
344352

@@ -357,6 +365,58 @@ test('it returns all sorted nodes with namespace specifiers at the top', () => {
357365
]);
358366
});
359367

368+
test('it returns all sorted nodes, sorted shortest to longest', () => {
369+
const result = getImportNodes(code);
370+
const sorted = getSortedNodes(result, {
371+
importOrder: [],
372+
importOrderCaseInsensitive: false,
373+
importOrderSeparation: false,
374+
importOrderGroupNamespaceSpecifiers: false,
375+
importOrderSortSpecifiers: false,
376+
importOrderSideEffects: true,
377+
importOrderSortByLength: 'asc',
378+
}) as ImportDeclaration[];
379+
expect(getSortedNodesNames(sorted)).toEqual([
380+
'g',
381+
'z',
382+
'Ba',
383+
'BY',
384+
'Xa',
385+
'XY',
386+
'a',
387+
'x',
388+
'c',
389+
'k',
390+
't',
391+
]);
392+
});
393+
394+
test('it returns all sorted nodes, sorted longest to shortest', () => {
395+
const result = getImportNodes(code);
396+
const sorted = getSortedNodes(result, {
397+
importOrder: [],
398+
importOrderCaseInsensitive: false,
399+
importOrderSeparation: false,
400+
importOrderGroupNamespaceSpecifiers: false,
401+
importOrderSortSpecifiers: false,
402+
importOrderSideEffects: false,
403+
importOrderSortByLength: 'desc',
404+
}) as ImportDeclaration[];
405+
expect(getSortedNodesNames(sorted)).toEqual([
406+
't',
407+
'k',
408+
'c',
409+
'a',
410+
'x',
411+
'Ba',
412+
'BY',
413+
'Xa',
414+
'XY',
415+
'g',
416+
'z',
417+
]);
418+
});
419+
360420
test('it returns all sorted nodes with types', () => {
361421
const result = getImportNodes(typeCode, {
362422
plugins: ['typescript'],
@@ -368,6 +428,7 @@ test('it returns all sorted nodes with types', () => {
368428
importOrderGroupNamespaceSpecifiers: false,
369429
importOrderSortSpecifiers: false,
370430
importOrderSideEffects: true,
431+
importOrderSortByLength: null,
371432
}) as ImportDeclaration[];
372433

373434
expect(getSortedNodesNames(sorted)).toEqual([

src/utils/__tests__/remove-nodes-from-original-code.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ test('it should remove nodes from the original code', async () => {
2626
importOrderSeparation: false,
2727
importOrderGroupNamespaceSpecifiers: false,
2828
importOrderSortSpecifiers: false,
29+
importOrderSortByLength: null,
2930
importOrderSideEffects: true,
3031
});
3132
const allCommentsFromImports = getAllCommentsFromNodes(sortedNodes);

0 commit comments

Comments
 (0)