Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] order: fix alphabetical sorting #2071

Merged
merged 1 commit into from May 14, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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