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
use super::State;
use crate::Config;
use std::{cell::RefCell, rc::Rc};
use swc_ecmascript::{
    ast::*,
    visit::{as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitWith},
};

pub fn analyzer(config: Rc<Config>, state: Rc<RefCell<State>>) -> impl VisitMut + Fold {
    as_folder(AsAnalyzer { config, state })
}

struct AsAnalyzer {
    config: Rc<Config>,
    state: Rc<RefCell<State>>,
}

impl VisitMut for AsAnalyzer {
    noop_visit_mut_type!();

    fn visit_mut_module(&mut self, p: &mut Module) {
        let mut v = Analyzer {
            config: &self.config,
            state: &mut *self.state.borrow_mut(),
        };

        p.visit_with(&mut v);
    }

    fn visit_mut_script(&mut self, p: &mut Script) {
        let mut v = Analyzer {
            config: &self.config,
            state: &mut *self.state.borrow_mut(),
        };

        p.visit_with(&mut v);
    }
}

pub fn analyze(config: &Config, program: &Program) -> State {
    let mut state = State::default();

    let mut v = Analyzer {
        config,
        state: &mut state,
    };

    program.visit_with(&mut v);

    state
}

struct Analyzer<'a> {
    config: &'a Config,
    state: &'a mut State,
}

impl Visit for Analyzer<'_> {
    noop_visit_type!();

    fn visit_var_declarator(&mut self, v: &VarDeclarator) {
        v.visit_children_with(self);

        if let Pat::Ident(name) = &v.name {
            if let Some(Expr::Call(CallExpr {
                callee: Callee::Expr(callee),
                args,
                ..
            })) = v.init.as_deref()
            {
                if let Expr::Ident(callee) = &**callee {
                    if &*callee.sym == "require" && args.len() == 1 && args[0].spread.is_none() {
                        if let Expr::Lit(Lit::Str(v)) = &*args[0].expr {
                            let is_styled = if self.config.top_level_import_paths.is_empty() {
                                &*v.value == "styled-components"
                                    || v.value.starts_with("styled-components/")
                            } else {
                                self.config.top_level_import_paths.contains(&v.value)
                            };

                            if is_styled {
                                self.state.styled_required = Some(name.id.to_id());
                                self.state.unresolved_ctxt = Some(callee.span.ctxt);
                            }
                        }
                    }
                }
            }
        }
    }

    fn visit_import_decl(&mut self, i: &ImportDecl) {
        let is_custom = !self.config.top_level_import_paths.is_empty();

        let is_styled = if self.config.top_level_import_paths.is_empty() {
            &*i.src.value == "styled-components" || i.src.value.starts_with("styled-components/")
        } else {
            self.config.top_level_import_paths.contains(&i.src.value)
        };

        if is_styled {
            for s in &i.specifiers {
                match s {
                    ImportSpecifier::Named(s) => {
                        if is_custom
                            && s.imported
                                .as_ref()
                                .map(|v| match v {
                                    ModuleExportName::Ident(v) => &*v.sym,
                                    ModuleExportName::Str(v) => &*v.value,
                                })
                                .unwrap_or(&*s.local.sym)
                                == "styled"
                        {
                            self.state.imported_local_name = Some(s.local.to_id());
                        }
                    }
                    ImportSpecifier::Default(s) => {
                        self.state.imported_local_name = Some(s.local.to_id());
                    }
                    ImportSpecifier::Namespace(s) => {
                        self.state.imported_local_ns = Some(s.local.to_id());
                    }
                }
            }
        }
    }
}