+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_js_analyze): sort imports using natural ordering #3900

Merged
merged 1 commit into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/rome_js_analyze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rustc-hash = { workspace = true }
serde = { version = "1.0.136", features = ["derive"] }
serde_json = { version = "1.0.74", features = ["raw_value"] }
lazy_static = "1.4.0"
natord = "1.0.9"

[dev-dependencies]
tests_macros = { path = "../tests_macros" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use rome_rowan::{
use crate::JsRuleAction;

declare_rule! {
/// Provides a whole-source code action to sort the imports in the file alphabetically
/// Provides a whole-source code action to sort the imports in the file
/// using natural ordering
///
/// ## Examples
///
Expand Down Expand Up @@ -273,15 +274,15 @@ struct ImportGroup {
/// The import that was at the start of the group before sorting
first_node: JsImport,
/// Multimap storing all the imports for each import source in the group,
/// sorted in alphabetical order
/// sorted in natural order
nodes: BTreeMap<ImportKey, Vec<JsImport>>,
}

impl ImportGroup {
/// Returns true if the nodes in the group are already sorted in the file
fn is_sorted(&self) -> bool {
// The imports are sorted if the start of each node in the `BTreeMap`
// (sorted alphabetically) is higher or equal to the previous item in
// (sorted in natural order) is higher or equal to the previous item in
// the sequence
let mut iter = self
.nodes
Expand All @@ -307,9 +308,8 @@ struct ImportKey(SyntaxTokenText);

impl Ord for ImportKey {
fn cmp(&self, other: &Self) -> Ordering {
// Sort imports alphabetically by defering to the string ordering logic
// of the standard library
(*self.0).cmp(&*other.0)
// Sort imports using natural ordering
natord::compare(&self.0, &other.0)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import a1 from 'a1';
import a10 from 'a10';
import a100 from 'a100';
import a2 from 'a2';
import a20 from 'a20';
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: natural-sort.js
---
# Input
```js
import a1 from 'a1';
import a10 from 'a10';
import a100 from 'a100';
import a2 from 'a2';
import a20 from 'a20';

```

# Actions
```diff
@@ -1,5 +1,5 @@
import a1 from 'a1';
+import a2 from 'a2';
import a10 from 'a10';
+import a20 from 'a20';
import a100 from 'a100';
-import a2 from 'a2';
-import a20 from 'a20';

```


点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载