Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Fix] order: Fix group ranks order when alphabetizing
Fixes #2671
  • Loading branch information
Pearce-Ropion authored and ljharb committed Jan 13, 2023
1 parent af8fd26 commit 0778b03
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## [Unreleased]

### Fixed
- [`order]`: Fix group ranks order when alphabetizing ([#2674], thanks [@Pearce-Ropion])

## [2.27.4] - 2023-01-11

### Fixed
Expand Down Expand Up @@ -1376,6 +1379,7 @@ for info on changes for earlier releases.
[#211]: https://github.com/import-js/eslint-plugin-import/pull/211
[#164]: https://github.com/import-js/eslint-plugin-import/pull/164
[#157]: https://github.com/import-js/eslint-plugin-import/pull/157
[#2674]: https://github.com/import-js/eslint-plugin-import/issues/2674
[#2668]: https://github.com/import-js/eslint-plugin-import/issues/2668
[#2666]: https://github.com/import-js/eslint-plugin-import/issues/2666
[#2665]: https://github.com/import-js/eslint-plugin-import/issues/2665
Expand Down
9 changes: 6 additions & 3 deletions src/rules/order.js
Expand Up @@ -334,18 +334,21 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
return acc;
}, {});

const groupRanks = Object.keys(groupedByRanks);

const sorterFn = getSorter(alphabetizeOptions);

// sort group keys so that they can be iterated on in order
const groupRanks = Object.keys(groupedByRanks).sort(function (a, b) {
return a - b;
});

// sort imports locally within their group
groupRanks.forEach(function (groupRank) {
groupedByRanks[groupRank].sort(sorterFn);
});

// assign globally unique rank to each import
let newRank = 0;
const alphabetizedRanks = groupRanks.sort().reduce(function (acc, groupRank) {
const alphabetizedRanks = groupRanks.reduce(function (acc, groupRank) {
groupedByRanks[groupRank].forEach(function (importedItem) {
acc[`${importedItem.value}|${importedItem.node.importKind}`] = parseInt(groupRank, 10) + newRank;
newRank += 1;
Expand Down
43 changes: 43 additions & 0 deletions tests/src/rules/order.js
Expand Up @@ -2280,6 +2280,8 @@ ruleTester.run('order', rule, {
},
],
}),

// pathGroups overflowing to previous/next groups
test({
code: `
import path from 'path';
Expand Down Expand Up @@ -2349,6 +2351,47 @@ ruleTester.run('order', rule, {
errors: Array.from({ length: 11 }, () => 'There should be at least one empty line between import groups'),
}),

// rankings that overflow to double-digit ranks
test({
code: `
import external from 'external';
import a from '@namespace/a';
import b from '@namespace/b';
import { parent } from '../../parent';
import local from './local';
import './side-effect';`,
output: `
import external from 'external';
import a from '@namespace/a';
import b from '@namespace/b';
import { parent } from '../../parent';
import local from './local';
import './side-effect';`,
options: [
{
alphabetize: {
order: 'asc',
caseInsensitive: true,
},
groups: ['type', 'builtin', 'external', 'internal', 'parent', 'sibling', 'index', 'object'],
'newlines-between': 'always',
pathGroups: [
{ pattern: '@namespace', group: 'external', position: 'after' },
{ pattern: '@namespace/**', group: 'external', position: 'after' },
],
pathGroupsExcludedImportTypes: ['@namespace'],
},
],
errors: [
'There should be at least one empty line between import groups',
'There should be at least one empty line between import groups',
'There should be at least one empty line between import groups',
],
}),

// reorder fix cannot cross non import or require
test(withoutAutofixOutput({
code: `
Expand Down

0 comments on commit 0778b03

Please sign in to comment.