Skip to content

Commit

Permalink
parse use rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Mar 18, 2024
1 parent 6415b42 commit 2309656
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/import_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,25 @@ impl ImportCollector {
}

impl<'a> syn::visit::Visit<'a> for ImportCollector {
/// A path at which a named item is exported (e.g. `std::collections::HashMap`).
///
/// This also gets crate level or renamed imports (I don't know how to fix yet).
fn visit_path(&mut self, i: &'a syn::Path) {
self.collect_path(i);
syn::visit::visit_path(self, i);
}

/// A path prefix of imports in a use item: `std::....`
fn visit_use_path(&mut self, i: &'a syn::UsePath) {
self.collect_use_path(i);
// collect top level use, no need to descend into the use tree
// syn::visit::visit_use_path(self, i);
}

/// A path at which a named item is exported (e.g. `std::collections::HashMap`).
///
/// This also gets crate level or renamed imports (I don't know how to fix yet).
fn visit_path(&mut self, i: &'a syn::Path) {
self.collect_path(i);
syn::visit::visit_path(self, i);
/// A suffix of an import tree in a use item: Type as Renamed or *.
fn visit_use_rename(&mut self, i: &'a syn::UseRename) {
let ident = i.ident.to_string();
self.add_import(ident);
}

/// A path like `std::slice::Iter`, optionally qualified with a self-type as in <Vec<T> as `SomeTrait>::Associated`.
Expand Down Expand Up @@ -184,4 +190,9 @@ mod tests {
"#,
);
}

#[test]
fn use_rename() {
test("use foo as bar;");
}
}

0 comments on commit 2309656

Please sign in to comment.