Skip to content

Commit

Permalink
[Fix] order: do not compare first path segment for relative paths (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mihkeleidast authored and ljharb committed Sep 26, 2023
1 parent ee1ea02 commit 32a2b89
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
- [`no-cycle`]: use scc algorithm to optimize ([#2998], thanks [@soryy708])
- [`no-duplicates`]: Removing duplicates breaks in TypeScript ([#3033], thanks [@yesl-kim])
- [`newline-after-import`]: fix considerComments option when require ([#2952], thanks [@developer-bandi])
- [`order`]: do not compare first path segment for relative paths ([#2682]) ([#2885], thanks [@mihkeleidast])

### Changed
- [Docs] `no-extraneous-dependencies`: Make glob pattern description more explicit ([#2944], thanks [@mulztob])
Expand Down Expand Up @@ -1141,6 +1142,7 @@ for info on changes for earlier releases.
[#2944]: https://github.com/import-js/eslint-plugin-import/pull/2944
[#2942]: https://github.com/import-js/eslint-plugin-import/pull/2942
[#2919]: https://github.com/import-js/eslint-plugin-import/pull/2919
[#2885]: https://github.com/import-js/eslint-plugin-import/pull/2885
[#2884]: https://github.com/import-js/eslint-plugin-import/pull/2884
[#2866]: https://github.com/import-js/eslint-plugin-import/pull/2866
[#2854]: https://github.com/import-js/eslint-plugin-import/pull/2854
Expand Down Expand Up @@ -1486,6 +1488,7 @@ for info on changes for earlier releases.
[#2930]: https://github.com/import-js/eslint-plugin-import/issues/2930
[#2687]: https://github.com/import-js/eslint-plugin-import/issues/2687
[#2684]: https://github.com/import-js/eslint-plugin-import/issues/2684
[#2682]: https://github.com/import-js/eslint-plugin-import/issues/2682
[#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
Expand Down Expand Up @@ -1880,6 +1883,7 @@ for info on changes for earlier releases.
[@mgwalker]: https://github.com/mgwalker
[@mhmadhamster]: https://github.com/MhMadHamster
[@michaelfaith]: https://github.com/michaelfaith
[@mihkeleidast]: https://github.com/mihkeleidast
[@MikeyBeLike]: https://github.com/MikeyBeLike
[@minervabot]: https://github.com/minervabot
[@mpint]: https://github.com/mpint
Expand Down
6 changes: 6 additions & 0 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ function getSorter(alphabetizeOptions) {
const b = B.length;

for (let i = 0; i < Math.min(a, b); i++) {
// Skip comparing the first path segment, if they are relative segments for both imports
if (i === 0 && ((A[i] === '.' || A[i] === '..') && (B[i] === '.' || B[i] === '..'))) {
// If one is sibling and the other parent import, no need to compare at all, since the paths belong in different groups
if (A[i] !== B[i]) { break; }
continue;
}
result = compareString(A[i], B[i]);
if (result) { break; }
}
Expand Down
28 changes: 28 additions & 0 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,34 @@ ruleTester.run('order', rule, {
['sibling', 'parent', 'external'],
] }],
}),
// Grouping import types and alphabetize
test({
code: `
import async from 'async';
import fs from 'fs';
import path from 'path';
import index from '.';
import relParent3 from '../';
import relParent1 from '../foo';
import sibling from './foo';
`,
options: [{ groups: [
['builtin', 'external'],
], alphabetize: { order: 'asc', caseInsensitive: true } }],
}),
test({
code: `
import { fooz } from '../baz.js'
import { foo } from './bar.js'
`,
options: [{
alphabetize: { order: 'asc', caseInsensitive: true },
groups: ['builtin', 'external', 'internal', ['parent', 'sibling', 'index'], 'object'],
'newlines-between': 'always',
warnOnUnassignedImports: true,
}],
}),
// Omitted types should implicitly be considered as the last type
test({
code: `
Expand Down

0 comments on commit 32a2b89

Please sign in to comment.