Skip to content

Commit 32a2b89

Browse files
mihkeleidastljharb
authored andcommitted
[Fix] order: do not compare first path segment for relative paths (#2682)
1 parent ee1ea02 commit 32a2b89

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1717
- [`no-cycle`]: use scc algorithm to optimize ([#2998], thanks [@soryy708])
1818
- [`no-duplicates`]: Removing duplicates breaks in TypeScript ([#3033], thanks [@yesl-kim])
1919
- [`newline-after-import`]: fix considerComments option when require ([#2952], thanks [@developer-bandi])
20+
- [`order`]: do not compare first path segment for relative paths ([#2682]) ([#2885], thanks [@mihkeleidast])
2021

2122
### Changed
2223
- [Docs] `no-extraneous-dependencies`: Make glob pattern description more explicit ([#2944], thanks [@mulztob])
@@ -1141,6 +1142,7 @@ for info on changes for earlier releases.
11411142
[#2944]: https://github.com/import-js/eslint-plugin-import/pull/2944
11421143
[#2942]: https://github.com/import-js/eslint-plugin-import/pull/2942
11431144
[#2919]: https://github.com/import-js/eslint-plugin-import/pull/2919
1145+
[#2885]: https://github.com/import-js/eslint-plugin-import/pull/2885
11441146
[#2884]: https://github.com/import-js/eslint-plugin-import/pull/2884
11451147
[#2866]: https://github.com/import-js/eslint-plugin-import/pull/2866
11461148
[#2854]: https://github.com/import-js/eslint-plugin-import/pull/2854
@@ -1486,6 +1488,7 @@ for info on changes for earlier releases.
14861488
[#2930]: https://github.com/import-js/eslint-plugin-import/issues/2930
14871489
[#2687]: https://github.com/import-js/eslint-plugin-import/issues/2687
14881490
[#2684]: https://github.com/import-js/eslint-plugin-import/issues/2684
1491+
[#2682]: https://github.com/import-js/eslint-plugin-import/issues/2682
14891492
[#2674]: https://github.com/import-js/eslint-plugin-import/issues/2674
14901493
[#2668]: https://github.com/import-js/eslint-plugin-import/issues/2668
14911494
[#2666]: https://github.com/import-js/eslint-plugin-import/issues/2666
@@ -1880,6 +1883,7 @@ for info on changes for earlier releases.
18801883
[@mgwalker]: https://github.com/mgwalker
18811884
[@mhmadhamster]: https://github.com/MhMadHamster
18821885
[@michaelfaith]: https://github.com/michaelfaith
1886+
[@mihkeleidast]: https://github.com/mihkeleidast
18831887
[@MikeyBeLike]: https://github.com/MikeyBeLike
18841888
[@minervabot]: https://github.com/minervabot
18851889
[@mpint]: https://github.com/mpint

src/rules/order.js

+6
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,12 @@ function getSorter(alphabetizeOptions) {
302302
const b = B.length;
303303

304304
for (let i = 0; i < Math.min(a, b); i++) {
305+
// Skip comparing the first path segment, if they are relative segments for both imports
306+
if (i === 0 && ((A[i] === '.' || A[i] === '..') && (B[i] === '.' || B[i] === '..'))) {
307+
// If one is sibling and the other parent import, no need to compare at all, since the paths belong in different groups
308+
if (A[i] !== B[i]) { break; }
309+
continue;
310+
}
305311
result = compareString(A[i], B[i]);
306312
if (result) { break; }
307313
}

tests/src/rules/order.js

+28
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,34 @@ ruleTester.run('order', rule, {
169169
['sibling', 'parent', 'external'],
170170
] }],
171171
}),
172+
// Grouping import types and alphabetize
173+
test({
174+
code: `
175+
import async from 'async';
176+
import fs from 'fs';
177+
import path from 'path';
178+
179+
import index from '.';
180+
import relParent3 from '../';
181+
import relParent1 from '../foo';
182+
import sibling from './foo';
183+
`,
184+
options: [{ groups: [
185+
['builtin', 'external'],
186+
], alphabetize: { order: 'asc', caseInsensitive: true } }],
187+
}),
188+
test({
189+
code: `
190+
import { fooz } from '../baz.js'
191+
import { foo } from './bar.js'
192+
`,
193+
options: [{
194+
alphabetize: { order: 'asc', caseInsensitive: true },
195+
groups: ['builtin', 'external', 'internal', ['parent', 'sibling', 'index'], 'object'],
196+
'newlines-between': 'always',
197+
warnOnUnassignedImports: true,
198+
}],
199+
}),
172200
// Omitted types should implicitly be considered as the last type
173201
test({
174202
code: `

0 commit comments

Comments
 (0)