Skip to content

Commit

Permalink
feat: Scope names in uses-declarations (Rust)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Dec 18, 2023
1 parent b3345c4 commit cda850d
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ language such as `python`. See [below](#custom-queries) for more on this advance

This section shows examples for some of the **premade queries**.

###### Mass import (module) renaming (Python)
###### Mass import (module) renaming (Python, Rust)

As part of a large refactor (say, after an acquisition), imagine all imports of a
specific package needed renaming:
Expand Down Expand Up @@ -562,6 +562,39 @@ good_company = "good_company" # good_company
Note how the last line remains untouched by this particular operation. To run across
many files, see [the `files` option](#run-against-multiple-files).

Similar import-related edits are supported for other languages as well, for example
Rust:

```rust imports.rs
use std::collections::HashMap;
use good_company::infra;
use good_company::aws::auth as aws_auth;
use good_company::util::iter::dedupe;
use good_company::shopping::cart::*;
good_company = "good_company"; // good_company
```

which, using

```bash
cat imports.rs | srgn --rust 'uses' '^good_company' 'better_company'
```

becomes

```rust output-imports.rs
use std::collections::HashMap;
use better_company::infra;
use better_company::aws::auth as aws_auth;
use better_company::util::iter::dedupe;
use better_company::shopping::cart::*;
good_company = "good_company"; // good_company
```

###### Assigning `TODO`s (TypeScript)

Perhaps you're using a system of `TODO` notes in comments:
Expand Down
17 changes: 17 additions & 0 deletions src/scoping/langs/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub enum PremadeRustQuery {
Comments,
/// Doc comments (comment chars included).
DocComments,
/// Use statements (paths only; excl. `use`/`as`/`*`).
Uses,
/// Strings (regular, raw, byte; includes interpolation parts in format strings!).
///
/// There is currently no support for an 'interpolation' type node in
Expand Down Expand Up @@ -46,6 +48,21 @@ impl From<PremadeRustQuery> for TSQuery {
)
"#
}
PremadeRustQuery::Uses => {
r#"
(scoped_identifier
path: [
(scoped_identifier)
(identifier)
] @use)
(scoped_use_list
path: [
(scoped_identifier)
(identifier)
] @use)
(use_wildcard (scoped_identifier) @use)
"#
}
PremadeRustQuery::Strings => {
r#"
[
Expand Down
22 changes: 22 additions & 0 deletions tests/langs/rust/in/uses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[rustfmt::skip] // `ignore` in `rustfmt.toml` is nightly-only
mod uses {
use super::mo__T__re::ParentModu__T__le;
use super::ParentModule;
use crate::mo__T__re::SiblingModule;
use crate::SiblingModule;
use std__T__::more::{cmp::Ordering, io};
use std::collec__T__tions::mo__T__re::stuff::HashMap;
use std::collections__T__::HashMap;
use std::f__T__s::read_to_stri__T__ng;
use std::i__T__o::Re__T__sult as IoRes__T__ult;
pub use std::i__T__o::Result;
#[cfg(target_os = "linux")]
use std::os::__T__linux::fs::MetadataExt;
use std::other::collectio__T__ns::*;
use std::{cmp__T__::Ordering, io};
use std::{io, more::__T__cmp::Ordering};
use std::{
io__T__::{Write, __T__self},
net__T__,
};
}
1 change: 1 addition & 0 deletions tests/langs/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::{get_input_output, nuke_target};
#[rstest]
#[case("comments.rs", RustQuery::Premade(PremadeRustQuery::Comments))]
#[case("doc-comments.rs", RustQuery::Premade(PremadeRustQuery::DocComments))]
#[case("uses.rs", RustQuery::Premade(PremadeRustQuery::Uses))]
#[case("strings.rs", RustQuery::Premade(PremadeRustQuery::Strings))]
fn test_rust_nuke(#[case] file: &str, #[case] query: RustQuery) {
let lang = Rust::new(query);
Expand Down
22 changes: 22 additions & 0 deletions tests/langs/rust/out/uses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[rustfmt::skip] // `ignore` in `rustfmt.toml` is nightly-only
mod uses {
use super::more::ParentModu__T__le;
use super::ParentModule;
use crate::more::SiblingModule;
use crate::SiblingModule;
use std::more::{cmp::Ordering, io};
use std::collections::more::stuff::HashMap;
use std::collections::HashMap;
use std::fs::read_to_stri__T__ng;
use std::io::Re__T__sult as IoRes__T__ult;
pub use std::io::Result;
#[cfg(target_os = "linux")]
use std::os::linux::fs::MetadataExt;
use std::other::collections::*;
use std::{cmp::Ordering, io};
use std::{io, more::cmp::Ordering};
use std::{
io::{Write, __T__self},
net__T__,
};
}

0 comments on commit cda850d

Please sign in to comment.