From 91be2913dd84db870a3265f1f5d7c996fcdc84a3 Mon Sep 17 00:00:00 2001 From: Sasha Pourcelot Date: Wed, 23 Jun 2021 10:00:18 +0200 Subject: [PATCH] Resolve paths that start with crate --- src/public_api/imports.rs | 30 ++++++++++++++++++++++++++++-- tests/method.rs | 15 ++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/public_api/imports.rs b/src/public_api/imports.rs index be9f6c9..d298107 100644 --- a/src/public_api/imports.rs +++ b/src/public_api/imports.rs @@ -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() @@ -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); + } } diff --git a/tests/method.rs b/tests/method.rs index 7606144..9321b11 100644 --- a/tests/method.rs +++ b/tests/method.rs @@ -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; }", @@ -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"); +}