Skip to content

fix: imports_granularity module with path containing self #5253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
50 changes: 38 additions & 12 deletions src/imports.rs
Original file line number Diff line number Diff line change
@@ -190,13 +190,17 @@ pub(crate) fn merge_use_trees(use_trees: Vec<UseTree>, merge_by: SharedPrefix) -
continue;
}

for flattened in use_tree.flatten() {
for mut flattened in use_tree.flatten() {
if let Some(tree) = result
.iter_mut()
.find(|tree| tree.share_prefix(&flattened, merge_by))
{
tree.merge(&flattened, merge_by);
} else {
// If this is the first tree with this prefix, handle potential trailing ::self
if merge_by == SharedPrefix::Module {
flattened = flattened.nest_trailing_self();
}
result.push(flattened);
}
}
@@ -208,17 +212,7 @@ pub(crate) fn flatten_use_trees(use_trees: Vec<UseTree>) -> Vec<UseTree> {
use_trees
.into_iter()
.flat_map(UseTree::flatten)
.map(|mut tree| {
// If a path ends in `::self`, rewrite it to `::{self}`.
if let Some(UseSegment::Slf(..)) = tree.path.last() {
let self_segment = tree.path.pop().unwrap();
tree.path.push(UseSegment::List(vec![UseTree::from_path(
vec![self_segment],
DUMMY_SP,
)]));
}
tree
})
.map(UseTree::nest_trailing_self)
.collect()
}

@@ -635,6 +629,18 @@ impl UseTree {
self.span = self.span.to(other.span);
}
}

/// If this tree ends in `::self`, rewrite it to `::{self}`.
fn nest_trailing_self(mut self) -> UseTree {
if let Some(UseSegment::Slf(..)) = self.path.last() {
let self_segment = self.path.pop().unwrap();
self.path.push(UseSegment::List(vec![UseTree::from_path(
vec![self_segment],
DUMMY_SP,
)]));
}
self
}
}

fn merge_rest(
@@ -1311,4 +1317,24 @@ mod test {
< parse_use_tree("std::cmp::{b, e, g, f}").normalize()
);
}

#[test]
fn test_use_tree_nest_trailing_self() {
assert_eq!(
parse_use_tree("a::b::self").nest_trailing_self(),
parse_use_tree("a::b::{self}")
);
assert_eq!(
parse_use_tree("a::b::c").nest_trailing_self(),
parse_use_tree("a::b::c")
);
assert_eq!(
parse_use_tree("a::b::{c, d}").nest_trailing_self(),
parse_use_tree("a::b::{c, d}")
);
assert_eq!(
parse_use_tree("a::b::{self, c}").nest_trailing_self(),
parse_use_tree("a::b::{self, c}")
);
}
}
1 change: 1 addition & 0 deletions tests/source/imports_granularity_module.rs
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ use a::{b::c, d::e};
use a::{f, g::{h, i}};
use a::{j::{self, k::{self, l}, m}, n::{o::p, q}};
pub use a::{r::s, t};
use b::{c::d, self};

#[cfg(test)]
use foo::{a::b, c::d};
2 changes: 2 additions & 0 deletions tests/target/imports_granularity_module.rs
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@ use a::n::o::p;
use a::n::q;
pub use a::r::s;
pub use a::t;
use b::c::d;
use b::{self};

use foo::e;
#[cfg(test)]