Skip to content

Commit 8226c7f

Browse files
authored
Fix local path sorting (#11)
1 parent 395da9e commit 8226c7f

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

.changeset/cuddly-lobsters-flow.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'eslint-plugin-import-sorting': patch
3+
---
4+
5+
Fix local import sort order when dot segment count is the same
6+
Sorting will now take the entire path into account, instead of just the basename of the path.

.changeset/six-suits-refuse.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'eslint-plugin-import-sorting': minor
3+
---
4+
5+
Sort numerals in path strings naturally
6+
Now ensures that `10` will sort after `2`, for example.

src/utils/alphabetize-ranks.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import path from 'node:path'
2-
31
import groupBy from 'object.groupby'
42

53
import type { ImportNodeObject } from '../rules/order.js'
@@ -15,7 +13,7 @@ const DEFAULT_IMPORT_KIND = 'value'
1513
* @todo Maybe add option to choose between 'natural' and 'literal' sorting.
1614
*/
1715
function compareString(first: string, second: string) {
18-
return first.localeCompare(second, 'en')
16+
return first.localeCompare(second, 'en', { numeric: true })
1917
}
2018

2119
function compareDotSegments(first: string, second: string) {
@@ -27,8 +25,8 @@ function compareDotSegments(first: string, second: string) {
2725
if (secondCount < firstCount) return -1
2826
if (firstCount < secondCount) return 1
2927

30-
// If segment length is the same, compare the basename alphabetically.
31-
return compareString(path.basename(first), path.basename(second))
28+
// If segment length is the same, compare the path alphabetically.
29+
return compareString(first, second)
3230
}
3331

3432
function getSorter(order: OrderDirection) {

tests/rules/order.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ ruleTester.run('order', orderRule, {
3434
3535
import moduleB from '../../../module-b.js'
3636
import moduleD from '../../module-d.js'
37-
import moduleA from '../module-a.js'
38-
import moduleC from '../module-c.js'
37+
import twoThings from '../get-things/get2Things.js'
38+
import tenThings from '../get-things/get10Things.js'
39+
import moduleA from '../HoverCard/module-c.js'
40+
import moduleC from '../Select/module-a.js'
3941
import Module from './index.js'
4042
4143
import styles from './component.module.css'

0 commit comments

Comments
 (0)