Skip to content

Commit 03d5f5a

Browse files
committed
fix(formatter/sort-imports)!: Change default order to natural with natord crate (#15828)
Part of #14253 Change the default order type from `alphabetical` to `natural`. In the original JS implementation, `alphabetical` sort uses the built-in `localeCompare()` function, but this does not exist in Rust. I have therefore changed the default to `natural` because: - Adding a crate to handle locale would be excessive - `prettier-plugin-sort-imports` also uses natural sorting
1 parent 56a99d5 commit 03d5f5a

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

Cargo.lock

Lines changed: 13 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/oxc_formatter/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ oxc_syntax = { workspace = true }
3030

3131
cow-utils = { workspace = true }
3232
json-strip-comments = { workspace = true }
33+
natord = "1.0.9"
3334
phf = { workspace = true, features = ["macros"] }
3435
rustc-hash = { workspace = true }
3536
schemars = { workspace = true }

crates/oxc_formatter/src/ir_transform/sort_imports/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ fn sort_imports(imports: &mut [SortableImport], options: &options::SortImports)
266266
}
267267

268268
// Within the same group, sort by source respecting the order option
269-
let source_ord = imports[a].normalized_source.cmp(&imports[b].normalized_source);
269+
let source_ord =
270+
natord::compare(&imports[a].normalized_source, &imports[b].normalized_source);
270271
if options.order.is_desc() { source_ord.reverse() } else { source_ord }
271272
});
272273

crates/oxc_formatter/tests/ir_transform/sort_imports.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import * as c from "c";
3838
import d from "d";
3939
"#,
4040
);
41-
// Alphabetical ASC order by default
41+
// Natural ASC order by default
4242
assert_format(
4343
r#"
4444
import { log } from "./log";
@@ -49,9 +49,9 @@ import { log2 } from "./log2";
4949
r#"{ "experimentalSortImports": {} }"#,
5050
r#"
5151
import { log } from "./log";
52-
import { log10 } from "./log10";
5352
import { log1p } from "./log1p";
5453
import { log2 } from "./log2";
54+
import { log10 } from "./log10";
5555
"#,
5656
);
5757
// Dynamic imports should not affect sorting
@@ -612,7 +612,7 @@ import B from "b";
612612

613613
#[test]
614614
fn should_sort_by_order() {
615-
// Z-A
615+
// Z-A (natural order reversed)
616616
assert_format(
617617
r#"
618618
import { log } from "./log";
@@ -622,13 +622,13 @@ import { log2 } from "./log2";
622622
"#,
623623
r#"{ "experimentalSortImports": { "order": "desc" } }"#,
624624
r#"
625+
import { log10 } from "./log10";
625626
import { log2 } from "./log2";
626627
import { log1p } from "./log1p";
627-
import { log10 } from "./log10";
628628
import { log } from "./log";
629629
"#,
630630
);
631-
// A-Z - default
631+
// A-Z - default (natural order)
632632
assert_format(
633633
r#"
634634
import { log } from "./log";
@@ -639,9 +639,9 @@ import { log2 } from "./log2";
639639
r#"{ "experimentalSortImports": { "order": "asc" } }"#,
640640
r#"
641641
import { log } from "./log";
642-
import { log10 } from "./log10";
643642
import { log1p } from "./log1p";
644643
import { log2 } from "./log2";
644+
import { log10 } from "./log10";
645645
"#,
646646
);
647647
assert_format(
@@ -654,9 +654,9 @@ import { log2 } from "./log2";
654654
r#"{ "experimentalSortImports": {} }"#,
655655
r#"
656656
import { log } from "./log";
657-
import { log10 } from "./log10";
658657
import { log1p } from "./log1p";
659658
import { log2 } from "./log2";
659+
import { log10 } from "./log10";
660660
"#,
661661
);
662662
}

0 commit comments

Comments
 (0)