1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//!
//! Small library helper that uses syn::visit::Visit trait to find all macro calls.
//!
//! By the way of traversing, looking for imports, so end user can
//! rename macros and mix macros with same name from different crates.
//!

use std::{cell::RefCell, collections::BTreeMap, rc::Rc};

use proc_macro2::TokenStream;

/// Macro visitor.
///
/// Handle all macro calls, and call appropriate function.
/// on the way, it will find all `use` items, and add new imports to the list.
///
/// Creates new visitor for each function, to avoid mixed `use` items.
///
/// It uses lifetime to allow variable to be captured into closure.

pub type RcMacro<'a> = Rc<RefCell<dyn FnMut(TokenStream) + 'a>>;
pub type MacroMap<'a> = BTreeMap<String, RcMacro<'a>>;
#[derive(Clone)]
pub struct Visitor<'a> {
    searched_imports: MacroMap<'a>,
}
impl std::fmt::Debug for Visitor<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Visitor").finish()
    }
}

impl<'a> Default for Visitor<'a> {
    fn default() -> Self {
        Self::new()
    }
}

impl<'a> Visitor<'a> {
    /// Creates empty visitor.
    pub fn new() -> Self {
        Self {
            searched_imports: BTreeMap::new(),
        }
    }
    /// Add macro implementation to the macro
    pub fn add_macro(&mut self, imports: Vec<String>, macro_call: impl FnMut(TokenStream) + 'a) {
        let macro_call = Rc::new(RefCell::new(macro_call));
        for import in imports {
            self.searched_imports.insert(import, macro_call.clone());
        }
    }
    pub fn add_rc_macro(&mut self, imports: Vec<String>, macro_call: RcMacro<'a>) {
        for import in imports {
            self.searched_imports.insert(import, macro_call.clone());
        }
    }
    /// Handle content of file.
    pub fn visit_file_content(&mut self, content: &str) {
        let file = syn::parse_file(content).unwrap();
        syn::visit::visit_file(self, &file)
    }
    /// Handle all *.rs files in src of project directory.
    ///
    /// `project_path` - is path to Cargo.toml of the project
    pub fn visit_project(self, project_path: &str) {
        let pattern = format!("{}/src/**/*.rs", project_path);
        for file in glob::glob(&pattern).unwrap() {
            let file = file.unwrap();
            let content = std::fs::read_to_string(&file).unwrap();
            self.new_subcall().visit_file_content(&content)
        }
    }
    fn new_subcall(&self) -> Self {
        Self {
            searched_imports: self.searched_imports.clone(),
        }
    }
    fn get_macro(&self, path: syn::Path) -> Option<RcMacro<'a>> {
        let path_str = path
            .segments
            .iter()
            .map(|s| s.ident.to_string())
            .collect::<Vec<_>>()
            .join("::");
        self.searched_imports.get(&path_str).cloned()
    }
}

impl syn::visit::Visit<'_> for Visitor<'_> {
    fn visit_use_tree(&mut self, node: &syn::UseTree) {
        let mut new_imports = vec![];
        for (import, macro_call) in &self.searched_imports {
            let use_tree_form = use_tree_from_str(import);
            let new = compare_use_tree(use_tree_form, node.clone());
            new_imports.extend(new.into_iter().map(|i| (i, macro_call.clone())))
        }
        self.searched_imports.extend(new_imports);
    }
    fn visit_item_fn(&mut self, node: &syn::ItemFn) {
        let mut new_visitor = self.new_subcall();
        syn::visit::visit_item_fn(&mut new_visitor, node);
    }

    fn visit_impl_item_fn(&mut self, i: &syn::ImplItemFn) {
        let mut new_visitor = self.new_subcall();
        syn::visit::visit_impl_item_fn(&mut new_visitor, i);
    }
    fn visit_macro(&mut self, i: &syn::Macro) {
        if let Some(macro_impl) = self.get_macro(i.path.clone()) {
            macro_impl.borrow_mut()(i.tokens.clone());
        }
    }
}

// Compare two paths, and return new one, if path was renamed.
// Expect left path to be flat, and right might be nested.
pub(crate) fn compare_use_tree(left: syn::UseTree, right: syn::UseTree) -> Vec<String> {
    match (left, right) {
        (syn::UseTree::Glob(_), _)
        | (syn::UseTree::Group(_), _)
        | (syn::UseTree::Rename(_), _) => {
            panic!("Import path is not valid")
        }
        // If right is glob, then we remove prefix, and keep the rest import path as synonim.
        (left_tree, syn::UseTree::Glob(_)) => {
            vec![create_import_path(left_tree)]
        }
        // If right is group - traverse each group item.
        (left_tree, syn::UseTree::Group(right_g)) => {
            right_g.items.into_iter().flat_map(move |item| {
                compare_use_tree(left_tree.clone(), item)
            }).collect::<Vec<_>>()
        }
        // Name is terminal node,
        // if it equal - we can use macro by its name without full path.
        (syn::UseTree::Name(left_i), syn::UseTree::Name(right_i))
        if right_i.ident == left_i.ident  =>
        {
            vec![create_import_path(syn::UseTree::Name(left_i))]
        }
        // Same but ident is renambed
        (syn::UseTree::Name(left_i), syn::UseTree::Rename(right_r))
        if right_r.ident == left_i.ident => {
            vec![create_import_path(syn::UseTree::Name(
                syn::UseName {
                    ident: right_r.rename,
                }))]
        }
        (syn::UseTree::Path(left_p), syn::UseTree::Name(right_i))
        if right_i.ident == left_p.ident => {
            vec![create_import_path(syn::UseTree::Path(left_p))]
        }
        (syn::UseTree::Path(left_p), syn::UseTree::Rename(right_r))
        if right_r.ident == left_p.ident => {
            let mut new_tree = left_p.clone();
            new_tree.ident = right_r.rename;
            vec![create_import_path(syn::UseTree::Path(new_tree))]
        }
        (syn::UseTree::Path(left_p), syn::UseTree::Path(right_p))
        if right_p.ident == left_p.ident => {
            // traverse deeper, while path is same
            compare_use_tree(*left_p.tree, *right_p.tree)
        }
        (syn::UseTree::Path(_), syn::UseTree::Name(_))
        | (syn::UseTree::Path(_), syn::UseTree::Rename(_))
        | (syn::UseTree::Name(_), syn::UseTree::Name(_))
        | (syn::UseTree::Name(_), syn::UseTree::Rename(_))
        | (syn::UseTree::Path(_), syn::UseTree::Path(_))
        // not comparable
        | (syn::UseTree::Name(_), syn::UseTree::Path(_))
         => {
            // if path is different, then we can't add new synonim for this import.
            vec![]
        }
    }
}
pub(crate) fn use_tree_from_str(path: &str) -> syn::UseTree {
    syn::parse_str(path).unwrap()
}

pub(crate) fn create_import_path(remining: syn::UseTree) -> String {
    let mut path = String::new();
    match remining {
        syn::UseTree::Name(ident) => {
            path.push_str(&ident.ident.to_string());
        }
        syn::UseTree::Path(path_tree) => {
            path.push_str(&path_tree.ident.to_string());
            path.push_str("::");
            path.push_str(&create_import_path(*path_tree.tree));
        }
        syn::UseTree::Rename(_) | syn::UseTree::Group(_) | syn::UseTree::Glob(_) => {
            panic!("Import path is not valid")
        }
    }
    path
}

#[cfg(test)]
mod test {
    use super::*;

    // Check that Visitor can find macro call
    #[test]
    fn test_simple_macro_call() {
        let mut found = false;
        let mut visitor = super::Visitor::new();
        let macro_call = |_| {
            found = true;
        };
        visitor.add_macro(vec!["rcss::file::css_module::css".to_owned()], macro_call);
        let input = syn::parse_str::<syn::Item>(
            r#"rcss::file::css_module::css! { .my-class { color: red; } }"#,
        )
        .unwrap();
        syn::visit::visit_item(&mut visitor, &input);
        drop(visitor);
        assert!(found)
    }

    #[test]
    fn test_macro_inside_fn() {
        let mut found = false;
        let mut visitor = super::Visitor::new();
        let macro_call = |_| {
            found = true;
        };
        visitor.add_macro(vec!["rcss::file::css_module::css".to_owned()], macro_call);
        let input = syn::parse_quote!(
            fn test() {
                rcss::file::css_module::css! { .my-class { color: red; } }
            }
        );
        syn::visit::visit_item(&mut visitor, &input);
        drop(visitor);
        assert!(found)
    }

    #[test]
    fn test_macro_inside_impl_fn() {
        let mut found = false;
        let mut visitor = super::Visitor::new();
        let macro_call = |_| {
            found = true;
        };
        visitor.add_macro(vec!["rcss::file::css_module::css".to_owned()], macro_call);
        let input = syn::parse_quote!(
            impl Test {
                fn test() {
                    rcss::file::css_module::css! { .my-class { color: red; } }
                }
            }
        );
        syn::visit::visit_file(&mut visitor, &input);
        drop(visitor);
        assert!(found)
    }

    #[test]
    fn test_macro_inside_fn_with_outer_and_inner_reimport() {
        let mut found = false;
        let mut visitor = super::Visitor::new();
        let macro_call = |_| {
            found = true;
        };
        visitor.add_macro(vec!["rcss::file::css_module::css".to_owned()], macro_call);
        let input = syn::parse_quote!(
            use rcss::file;
            fn test() {
                use file::css_module;
                file::css_module::css! { .my-class { color: red; } }
            }
        );
        syn::visit::visit_file(&mut visitor, &input);
        drop(visitor);
        assert!(found)
    }
    //check that import handle name;
    #[test]
    fn test_compare_use_by_name() {
        let path = "rcss::file::css_module::css_struct";
        let path = super::use_tree_from_str(path);
        let use_item: syn::ItemUse = syn::parse_quote! {
            use rcss::file;
        };

        let new_imports = compare_use_tree(path, use_item.tree);
        assert_eq!(new_imports, vec!["file::css_module::css_struct".to_owned()]);
    }

    #[test]
    fn test_compare_use_in_group() {
        let path = "rcss::file::css_module::css_struct";
        let path = super::use_tree_from_str(path);
        let use_item: syn::ItemUse = syn::parse_quote! {
            use rcss::file::{css_module, scoped};
        };

        let new_imports = compare_use_tree(path, use_item.tree);
        assert_eq!(new_imports, vec!["css_module::css_struct".to_owned()]);
    }

    #[test]
    fn test_compare_use_by_glob() {
        let path = "rcss::file::css_module::css_struct";
        let path = super::use_tree_from_str(path);
        let use_item: syn::ItemUse = syn::parse_quote! {
            use rcss::file::*;
        };

        let new_imports = compare_use_tree(path, use_item.tree);
        assert_eq!(new_imports, vec!["css_module::css_struct".to_owned()]);
    }
    #[test]
    fn test_compare_use_by_glob_in_group() {
        let path = "rcss::file::css_module::css_struct";
        let path = super::use_tree_from_str(path);
        let use_item: syn::ItemUse = syn::parse_quote! {
            use rcss::file::{*, scoped};
        };

        let new_imports = compare_use_tree(path, use_item.tree);
        assert_eq!(new_imports, vec!["css_module::css_struct".to_owned()]);
    }

    #[test]
    fn test_compare_deep_group_with_glob() {
        let path = "rcss::file::css_module::css_struct";
        let path = super::use_tree_from_str(path);
        let use_item: syn::ItemUse = syn::parse_quote! {
            use rcss::file::{*, css_module::{css, *}};
        };

        let new_imports = compare_use_tree(path, use_item.tree);
        assert_eq!(
            new_imports,
            vec!["css_module::css_struct".to_owned(), "css_struct".to_owned()]
        );
    }

    #[test]
    fn test_compare_with_rename() {
        let path = "rcss::file::css_module::css";
        let path = super::use_tree_from_str(path);
        let use_item: syn::ItemUse = syn::parse_quote! {
            use rcss::file::{*, css_module::{css as css2, *}};
        };

        let new_imports = compare_use_tree(path, use_item.tree);
        assert_eq!(
            new_imports,
            vec![
                "css_module::css".to_owned(),
                "css2".to_owned(),
                "css".to_owned()
            ]
        );
    }
}