Skip to content

Commit

Permalink
Resolve paths that start with crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Sasha Pourcelot committed Jun 23, 2021
1 parent 297a556 commit 91be291
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
30 changes: 28 additions & 2 deletions src/public_api/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,19 @@ impl PathResolver {
}

pub(crate) fn resolve(&self, current_path: &[Ident], item_path: &Path) -> Option<&[Ident]> {
let item_idents = item_path.segments.iter().map(|segment| &segment.ident);
let mut item_idents = item_path
.segments
.iter()
.map(|segment| &segment.ident)
.peekable();

let from_current_path = if item_idents.next_if_eq(&"crate").is_some() {
&[]
} else {
current_path
};

let full_path = current_path
let full_path = from_current_path
.iter()
.chain(item_idents)
.cloned()
Expand Down Expand Up @@ -231,4 +241,20 @@ mod tests {

assert_eq!(left, right);
}

#[test]
fn resolves_when_starts_with_crate() {
let resolver: PathResolver = parse_quote! {
pub mod foo {
pub fn bar() {}
}
};

let tmp = [parse_quote! { foo }, parse_quote! { bar }];

let left = resolver.resolve(&[], &parse_quote! { crate::foo::bar });
let right = Some(&tmp as _);

assert_eq!(left, right);
}
}
15 changes: 14 additions & 1 deletion tests/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn not_reported_when_type_is_not_public() {
}

#[test]
fn is_reported_in_type_definition_path() {
fn is_reported_in_type_definition_path_1() {
let comparator = cargo_breaking::compare(
"pub mod foo { pub struct Bar; } impl foo::Bar { pub fn f() {} }",
"pub mod foo { pub struct Bar; }",
Expand All @@ -84,3 +84,16 @@ fn is_reported_in_type_definition_path() {

assert_eq!(diff.to_string(), "- foo::Bar::f\n");
}

#[test]
fn is_reported_in_type_definition_path_2() {
let comparator = cargo_breaking::compare(
"pub mod foo { pub struct Bar; } pub mod baz { impl crate::foo::Bar { pub fn f() {} } }",
"pub mod foo { pub struct Bar; }",
)
.unwrap();

let diff = comparator.run();

assert_eq!(diff.to_string(), "- foo::Bar::f\n");
}

0 comments on commit 91be291

Please sign in to comment.