Skip to content

Commit

Permalink
[Fix] order: fix alphabetical sorting
Browse files Browse the repository at this point in the history
Fixes #2064. See #2065.
  • Loading branch information
geraintwhite authored and ljharb committed May 14, 2021
1 parent 8213543 commit 83f3c3e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Fixed
- [`newline-after-import`]: fix crash with `export {}` syntax ([#2063], [#2056], thanks [@ljharb])
- `ExportMap`: do not crash when tsconfig lacks `.compilerOptions` ([#2067], thanks [@ljharb])
- [`order`]: fix alphabetical sorting ([#2071], thanks [@grit96])

## [2.23.0] - 2021-05-13

Expand Down Expand Up @@ -775,6 +776,7 @@ for info on changes for earlier releases.

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

[#2071]: https://github.com/benmosher/eslint-plugin-import/pull/2071
[#2034]: https://github.com/benmosher/eslint-plugin-import/pull/2034
[#2026]: https://github.com/benmosher/eslint-plugin-import/pull/2026
[#2022]: https://github.com/benmosher/eslint-plugin-import/pull/2022
Expand Down
13 changes: 8 additions & 5 deletions src/rules/order.js
Expand Up @@ -265,14 +265,17 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
if (!Array.isArray(acc[importedItem.rank])) {
acc[importedItem.rank] = [];
}
acc[importedItem.rank].push(`${importedItem.value}-${importedItem.node.importKind}`);
acc[importedItem.rank].push(importedItem);
return acc;
}, {});

const groupRanks = Object.keys(groupedByRanks);

const sorterFn = getSorter(alphabetizeOptions.order === 'asc');
const comparator = alphabetizeOptions.caseInsensitive ? (a, b) => sorterFn(String(a).toLowerCase(), String(b).toLowerCase()) : (a, b) => sorterFn(a, b);
const comparator = alphabetizeOptions.caseInsensitive
? (a, b) => sorterFn(String(a.value).toLowerCase(), String(b.value).toLowerCase())
: (a, b) => sorterFn(a.value, b.value);

// sort imports locally within their group
groupRanks.forEach(function(groupRank) {
groupedByRanks[groupRank].sort(comparator);
Expand All @@ -281,16 +284,16 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
// assign globally unique rank to each import
let newRank = 0;
const alphabetizedRanks = groupRanks.sort().reduce(function(acc, groupRank) {
groupedByRanks[groupRank].forEach(function(importedItemName) {
acc[importedItemName] = parseInt(groupRank, 10) + newRank;
groupedByRanks[groupRank].forEach(function(importedItem) {
acc[`${importedItem.value}|${importedItem.node.importKind}`] = parseInt(groupRank, 10) + newRank;
newRank += 1;
});
return acc;
}, {});

// mutate the original group-rank with alphabetized-rank
imported.forEach(function(importedItem) {
importedItem.rank = alphabetizedRanks[`${importedItem.value}-${importedItem.node.importKind}`];
importedItem.rank = alphabetizedRanks[`${importedItem.value}|${importedItem.node.importKind}`];
});
}

Expand Down
29 changes: 29 additions & 0 deletions tests/src/rules/order.js
Expand Up @@ -706,6 +706,20 @@ ruleTester.run('order', rule, {
},
],
}),
// Order of imports with similar names
test({
code: `
import React from 'react';
import { BrowserRouter } from 'react-router-dom';
`,
options: [
{
alphabetize: {
order: 'asc',
},
},
],
}),
...flatMap(getTSParsers, parser => [
// Order of the `import ... = require(...)` syntax
test({
Expand Down Expand Up @@ -2274,6 +2288,21 @@ context('TypeScript', function () {
},
parserConfig,
),
test(
{
code: `
import { Partner } from '@models/partner/partner';
import { PartnerId } from '@models/partner/partner-id';
`,
parser,
options: [
{
alphabetize: { order: 'asc' },
},
],
},
parserConfig,
),
],
invalid: [
// Option alphabetize: {order: 'asc'}
Expand Down

0 comments on commit 83f3c3e

Please sign in to comment.