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 27, 2023
1 parent 28ac9ef commit ea2d6b1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
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: 5 additions & 1 deletion src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,11 @@ function getSorter(alphabetizeOptions) {

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] === '..'))) { continue; }
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
12 changes: 12 additions & 0 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ ruleTester.run('order', rule, {
['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 ea2d6b1

Please sign in to comment.