Skip to content

Commit 83f3c3e

Browse files
geraintwhiteljharb
authored andcommitted
[Fix] order: fix alphabetical sorting
Fixes #2064. See #2065.
1 parent 8213543 commit 83f3c3e

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
99
### Fixed
1010
- [`newline-after-import`]: fix crash with `export {}` syntax ([#2063], [#2056], thanks [@ljharb])
1111
- `ExportMap`: do not crash when tsconfig lacks `.compilerOptions` ([#2067], thanks [@ljharb])
12+
- [`order`]: fix alphabetical sorting ([#2071], thanks [@grit96])
1213

1314
## [2.23.0] - 2021-05-13
1415

@@ -775,6 +776,7 @@ for info on changes for earlier releases.
775776

776777
[`memo-parser`]: ./memo-parser/README.md
777778

779+
[#2071]: https://github.com/benmosher/eslint-plugin-import/pull/2071
778780
[#2034]: https://github.com/benmosher/eslint-plugin-import/pull/2034
779781
[#2026]: https://github.com/benmosher/eslint-plugin-import/pull/2026
780782
[#2022]: https://github.com/benmosher/eslint-plugin-import/pull/2022

src/rules/order.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,17 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
265265
if (!Array.isArray(acc[importedItem.rank])) {
266266
acc[importedItem.rank] = [];
267267
}
268-
acc[importedItem.rank].push(`${importedItem.value}-${importedItem.node.importKind}`);
268+
acc[importedItem.rank].push(importedItem);
269269
return acc;
270270
}, {});
271271

272272
const groupRanks = Object.keys(groupedByRanks);
273273

274274
const sorterFn = getSorter(alphabetizeOptions.order === 'asc');
275-
const comparator = alphabetizeOptions.caseInsensitive ? (a, b) => sorterFn(String(a).toLowerCase(), String(b).toLowerCase()) : (a, b) => sorterFn(a, b);
275+
const comparator = alphabetizeOptions.caseInsensitive
276+
? (a, b) => sorterFn(String(a.value).toLowerCase(), String(b.value).toLowerCase())
277+
: (a, b) => sorterFn(a.value, b.value);
278+
276279
// sort imports locally within their group
277280
groupRanks.forEach(function(groupRank) {
278281
groupedByRanks[groupRank].sort(comparator);
@@ -281,16 +284,16 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
281284
// assign globally unique rank to each import
282285
let newRank = 0;
283286
const alphabetizedRanks = groupRanks.sort().reduce(function(acc, groupRank) {
284-
groupedByRanks[groupRank].forEach(function(importedItemName) {
285-
acc[importedItemName] = parseInt(groupRank, 10) + newRank;
287+
groupedByRanks[groupRank].forEach(function(importedItem) {
288+
acc[`${importedItem.value}|${importedItem.node.importKind}`] = parseInt(groupRank, 10) + newRank;
286289
newRank += 1;
287290
});
288291
return acc;
289292
}, {});
290293

291294
// mutate the original group-rank with alphabetized-rank
292295
imported.forEach(function(importedItem) {
293-
importedItem.rank = alphabetizedRanks[`${importedItem.value}-${importedItem.node.importKind}`];
296+
importedItem.rank = alphabetizedRanks[`${importedItem.value}|${importedItem.node.importKind}`];
294297
});
295298
}
296299

tests/src/rules/order.js

+29
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,20 @@ ruleTester.run('order', rule, {
706706
},
707707
],
708708
}),
709+
// Order of imports with similar names
710+
test({
711+
code: `
712+
import React from 'react';
713+
import { BrowserRouter } from 'react-router-dom';
714+
`,
715+
options: [
716+
{
717+
alphabetize: {
718+
order: 'asc',
719+
},
720+
},
721+
],
722+
}),
709723
...flatMap(getTSParsers, parser => [
710724
// Order of the `import ... = require(...)` syntax
711725
test({
@@ -2274,6 +2288,21 @@ context('TypeScript', function () {
22742288
},
22752289
parserConfig,
22762290
),
2291+
test(
2292+
{
2293+
code: `
2294+
import { Partner } from '@models/partner/partner';
2295+
import { PartnerId } from '@models/partner/partner-id';
2296+
`,
2297+
parser,
2298+
options: [
2299+
{
2300+
alphabetize: { order: 'asc' },
2301+
},
2302+
],
2303+
},
2304+
parserConfig,
2305+
),
22772306
],
22782307
invalid: [
22792308
// Option alphabetize: {order: 'asc'}

0 commit comments

Comments
 (0)