Skip to content

Commit c65ba34

Browse files
committed
Alphabetize type, (almost) fixes import-js#2339
1 parent fb90a81 commit c65ba34

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

src/rules/order.js

+23-16
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ function fixOutOfOrder(context, firstNode, secondNode, order) {
191191
newCode = newCode + '\n';
192192
}
193193

194-
const message = `\`${secondNode.displayName}\` import should occur ${order} import of \`${firstNode.displayName}\``;
194+
const firstImport = firstNode.node.importKind === 'type' ? 'type import' : 'import';
195+
const secondImport = secondNode.node.importKind === 'type' ? 'type import' : 'import';
196+
const message = `\`${secondNode.displayName}\` ${secondImport} should occur ${order} ${firstImport} of \`${firstNode.displayName}\``;
195197

196198
if (order === 'before') {
197199
context.report({
@@ -240,21 +242,30 @@ function makeOutOfOrderReport(context, imported) {
240242
reportOutOfOrder(context, imported, outOfOrder, 'before');
241243
}
242244

243-
function getSorter(ascending) {
244-
const multiplier = ascending ? 1 : -1;
245+
function getSorter(alphabetizeOptions) {
246+
const result = alphabetizeOptions.order === 'asc' ? 1 : -1;
247+
248+
function normalizeCase(value) {
249+
return alphabetizeOptions.caseInsensitive ? String(value).toLowerCase() : value;
250+
}
245251

246252
return function importsSorter(importA, importB) {
247-
let result;
253+
const valueA = normalizeCase(importA.value);
254+
const valueB = normalizeCase(importB.value);
248255

249-
if (importA < importB) {
250-
result = -1;
251-
} else if (importA > importB) {
252-
result = 1;
253-
} else {
254-
result = 0;
256+
if (valueA < valueB) {
257+
return -result;
258+
} else if (valueA > valueB) {
259+
return result;
255260
}
256261

257-
return result * multiplier;
262+
if (importA.node.importKind < importB.node.importKind) {
263+
return -result;
264+
} else if (importA.node.importKind > importB.node.importKind) {
265+
return result;
266+
}
267+
268+
return 0;
258269
};
259270
}
260271

@@ -268,11 +279,7 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
268279
}, {});
269280

270281
const groupRanks = Object.keys(groupedByRanks);
271-
272-
const sorterFn = getSorter(alphabetizeOptions.order === 'asc');
273-
const comparator = alphabetizeOptions.caseInsensitive
274-
? (a, b) => sorterFn(String(a.value).toLowerCase(), String(b.value).toLowerCase())
275-
: (a, b) => sorterFn(a.value, b.value);
282+
const comparator = getSorter(alphabetizeOptions);
276283

277284
// sort imports locally within their group
278285
groupRanks.forEach(function (groupRank) {

tests/src/rules/order.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -2338,11 +2338,11 @@ context('TypeScript', function () {
23382338
// Option alphabetize: {order: 'asc'}
23392339
test({
23402340
code: `
2341-
import c from 'Bar';
23422341
import type { C } from 'Bar';
2342+
import c from 'Bar';
23432343
import b from 'bar';
2344-
import a from 'foo';
23452344
import type { A } from 'foo';
2345+
import a from 'foo';
23462346
23472347
import index from './';
23482348
`,
@@ -2486,12 +2486,12 @@ context('TypeScript', function () {
24862486
}),
24872487
test({
24882488
code: `
2489-
import { serialize, parse, mapFieldErrors } from '@vtaits/form-schema';
24902489
import type { GetFieldSchema } from '@vtaits/form-schema';
2491-
import { useMemo, useCallback } from 'react';
2490+
import { serialize, parse, mapFieldErrors } from '@vtaits/form-schema';
24922491
import type { ReactElement, ReactNode } from 'react';
2493-
import { Form } from 'react-final-form';
2492+
import { useMemo, useCallback } from 'react';
24942493
import type { FormProps as FinalFormProps } from 'react-final-form';
2494+
import { Form } from 'react-final-form';
24952495
`,
24962496
...parserConfig,
24972497
options: [
@@ -2532,11 +2532,11 @@ context('TypeScript', function () {
25322532
import index from './';
25332533
`,
25342534
output: `
2535-
import c from 'Bar';
25362535
import type { C } from 'Bar';
2536+
import c from 'Bar';
25372537
import b from 'bar';
2538-
import a from 'foo';
25392538
import type { A } from 'foo';
2539+
import a from 'foo';
25402540
25412541
import index from './';
25422542
`,
@@ -2549,6 +2549,8 @@ context('TypeScript', function () {
25492549
],
25502550
errors: [
25512551
{ message: '`Bar` import should occur before import of `bar`' },
2552+
{ message: '`Bar` type import should occur before import of `bar`' },
2553+
{ message: '`foo` type import should occur before import of `foo`' },
25522554
],
25532555
}),
25542556
// Option alphabetize: {order: 'desc'}
@@ -2613,7 +2615,7 @@ context('TypeScript', function () {
26132615
],
26142616
errors: [
26152617
{ message: '`Bar` import should occur before import of `bar`' },
2616-
{ message: '`Bar` import should occur before import of `foo`' },
2618+
{ message: '`Bar` type import should occur before type import of `foo`' },
26172619
],
26182620
}),
26192621
// Option alphabetize: {order: 'desc'} with type group
@@ -2647,7 +2649,7 @@ context('TypeScript', function () {
26472649
],
26482650
errors: [
26492651
{ message: '`bar` import should occur before import of `Bar`' },
2650-
{ message: '`foo` import should occur before import of `Bar`' },
2652+
{ message: '`foo` type import should occur before type import of `Bar`' },
26512653
],
26522654
}),
26532655
// warns for out of order unassigned imports (warnOnUnassignedImports enabled)
@@ -2730,8 +2732,8 @@ context('TypeScript', function () {
27302732
},
27312733
],
27322734
errors: [
2733-
{ message: '`fs` import should occur before import of `path`' },
2734-
{ message: '`fs` import should occur before import of `path`' },
2735+
{ message: '`fs` type import should occur before type import of `path`' },
2736+
{ message: '`fs` type import should occur before type import of `path`' },
27352737
],
27362738
}),
27372739
],

0 commit comments

Comments
 (0)