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

import/order Fix group ranks order when alphabetizing #2674

Merged
merged 1 commit into from Jan 14, 2023
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
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