Skip to content

Commit 0778b03

Browse files
Pearce-Ropionljharb
authored andcommitted
[Fix] order: Fix group ranks order when alphabetizing
Fixes #2671
1 parent af8fd26 commit 0778b03

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
66

77
## [Unreleased]
88

9+
### Fixed
10+
- [`order]`: Fix group ranks order when alphabetizing ([#2674], thanks [@Pearce-Ropion])
11+
912
## [2.27.4] - 2023-01-11
1013

1114
### Fixed
@@ -1376,6 +1379,7 @@ for info on changes for earlier releases.
13761379
[#211]: https://github.com/import-js/eslint-plugin-import/pull/211
13771380
[#164]: https://github.com/import-js/eslint-plugin-import/pull/164
13781381
[#157]: https://github.com/import-js/eslint-plugin-import/pull/157
1382+
[#2674]: https://github.com/import-js/eslint-plugin-import/issues/2674
13791383
[#2668]: https://github.com/import-js/eslint-plugin-import/issues/2668
13801384
[#2666]: https://github.com/import-js/eslint-plugin-import/issues/2666
13811385
[#2665]: https://github.com/import-js/eslint-plugin-import/issues/2665

src/rules/order.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -334,18 +334,21 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {
334334
return acc;
335335
}, {});
336336

337-
const groupRanks = Object.keys(groupedByRanks);
338-
339337
const sorterFn = getSorter(alphabetizeOptions);
340338

339+
// sort group keys so that they can be iterated on in order
340+
const groupRanks = Object.keys(groupedByRanks).sort(function (a, b) {
341+
return a - b;
342+
});
343+
341344
// sort imports locally within their group
342345
groupRanks.forEach(function (groupRank) {
343346
groupedByRanks[groupRank].sort(sorterFn);
344347
});
345348

346349
// assign globally unique rank to each import
347350
let newRank = 0;
348-
const alphabetizedRanks = groupRanks.sort().reduce(function (acc, groupRank) {
351+
const alphabetizedRanks = groupRanks.reduce(function (acc, groupRank) {
349352
groupedByRanks[groupRank].forEach(function (importedItem) {
350353
acc[`${importedItem.value}|${importedItem.node.importKind}`] = parseInt(groupRank, 10) + newRank;
351354
newRank += 1;

tests/src/rules/order.js

+43
Original file line numberDiff line numberDiff line change
@@ -2280,6 +2280,8 @@ ruleTester.run('order', rule, {
22802280
},
22812281
],
22822282
}),
2283+
2284+
// pathGroups overflowing to previous/next groups
22832285
test({
22842286
code: `
22852287
import path from 'path';
@@ -2349,6 +2351,47 @@ ruleTester.run('order', rule, {
23492351
errors: Array.from({ length: 11 }, () => 'There should be at least one empty line between import groups'),
23502352
}),
23512353

2354+
// rankings that overflow to double-digit ranks
2355+
test({
2356+
code: `
2357+
import external from 'external';
2358+
import a from '@namespace/a';
2359+
import b from '@namespace/b';
2360+
import { parent } from '../../parent';
2361+
import local from './local';
2362+
import './side-effect';`,
2363+
output: `
2364+
import external from 'external';
2365+
2366+
import a from '@namespace/a';
2367+
import b from '@namespace/b';
2368+
2369+
import { parent } from '../../parent';
2370+
2371+
import local from './local';
2372+
import './side-effect';`,
2373+
options: [
2374+
{
2375+
alphabetize: {
2376+
order: 'asc',
2377+
caseInsensitive: true,
2378+
},
2379+
groups: ['type', 'builtin', 'external', 'internal', 'parent', 'sibling', 'index', 'object'],
2380+
'newlines-between': 'always',
2381+
pathGroups: [
2382+
{ pattern: '@namespace', group: 'external', position: 'after' },
2383+
{ pattern: '@namespace/**', group: 'external', position: 'after' },
2384+
],
2385+
pathGroupsExcludedImportTypes: ['@namespace'],
2386+
},
2387+
],
2388+
errors: [
2389+
'There should be at least one empty line between import groups',
2390+
'There should be at least one empty line between import groups',
2391+
'There should be at least one empty line between import groups',
2392+
],
2393+
}),
2394+
23522395
// reorder fix cannot cross non import or require
23532396
test(withoutAutofixOutput({
23542397
code: `

0 commit comments

Comments
 (0)