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
//! Module to precompute known bindings.

use rnode::{Visit, VisitWith};
use stc_ts_ast_rnode::*;
use stc_ts_types::Id;
use swc_common::{
    collections::{AHashMap, AHashSet},
    EqIgnoreSpan, TypeEq,
};
use swc_ecma_ast::VarDeclKind;

#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, EqIgnoreSpan, TypeEq)]
pub enum BindingKind {
    Import,
    Class,
    Function,
    Enum,
    TypeAlias,
    /// Modules declared using `module` keyword.
    TsModule,
    Interface,
    Namespace,
    Module,
    Var(#[use_eq_ignore_span] VarDeclKind),
}

#[derive(Debug, Default)]
pub struct Bindings {
    pub collected: bool,
    pub all: AHashMap<Id, Vec<BindingKind>>,
    pub types: AHashSet<Id>,
}

pub fn collect_bindings<N>(n: &N) -> Bindings
where
    N: Send + Sync + for<'aa> VisitWith<BindingCollector<'aa>> + VisitWith<KnownTypeVisitor>,
{
    let (all, types) = rayon::join(
        || {
            let mut all = AHashMap::default();

            n.visit_with(&mut BindingCollector { data: &mut all });
            all
        },
        || {
            let mut v = KnownTypeVisitor::default();
            n.visit_with(&mut v);
            v.types
        },
    );

    Bindings {
        collected: true,
        all,
        types,
    }
}

pub struct BindingCollector<'a> {
    data: &'a mut AHashMap<Id, Vec<BindingKind>>,
}

impl Visit<RTsNamespaceDecl> for BindingCollector<'_> {
    fn visit(&mut self, decl: &RTsNamespaceDecl) {
        self.data.entry(decl.id.clone().into()).or_default().push(BindingKind::Namespace);

        decl.visit_children_with(self);
    }
}

impl Visit<RTsInterfaceDecl> for BindingCollector<'_> {
    fn visit(&mut self, decl: &RTsInterfaceDecl) {
        self.data.entry(decl.id.clone().into()).or_default().push(BindingKind::Interface);

        decl.visit_children_with(self);
    }
}

impl Visit<RClassDecl> for BindingCollector<'_> {
    fn visit(&mut self, decl: &RClassDecl) {
        self.data.entry(decl.ident.clone().into()).or_default().push(BindingKind::Class);

        decl.visit_children_with(self);
    }
}

impl Visit<RFnDecl> for BindingCollector<'_> {
    fn visit(&mut self, decl: &RFnDecl) {
        self.data.entry(decl.ident.clone().into()).or_default().push(BindingKind::Function);

        decl.visit_children_with(self);
    }
}

impl Visit<RTsEnumDecl> for BindingCollector<'_> {
    fn visit(&mut self, decl: &RTsEnumDecl) {
        self.data.entry(decl.id.clone().into()).or_default().push(BindingKind::Enum);

        decl.visit_children_with(self);
    }
}

impl Visit<RTsTypeAliasDecl> for BindingCollector<'_> {
    fn visit(&mut self, decl: &RTsTypeAliasDecl) {
        self.data.entry(decl.id.clone().into()).or_default().push(BindingKind::TypeAlias);

        decl.visit_children_with(self);
    }
}

impl Visit<RTsModuleDecl> for BindingCollector<'_> {
    fn visit(&mut self, d: &RTsModuleDecl) {
        d.visit_children_with(self);

        match &d.id {
            RTsModuleName::Ident(i) => {
                self.data.entry(i.clone().into()).or_default().push(BindingKind::TsModule);
            }
            RTsModuleName::Str(_) => {}
        }
    }
}

#[derive(Default)]
pub struct KnownTypeVisitor {
    types: AHashSet<Id>,
}

impl KnownTypeVisitor {
    fn add(&mut self, id: &RIdent) {
        self.types.insert(id.into());
    }
}

impl Visit<RClassDecl> for KnownTypeVisitor {
    fn visit(&mut self, d: &RClassDecl) {
        d.visit_children_with(self);

        self.add(&d.ident);
    }
}

impl Visit<RTsInterfaceDecl> for KnownTypeVisitor {
    fn visit(&mut self, d: &RTsInterfaceDecl) {
        d.visit_children_with(self);

        self.add(&d.id);
    }
}

impl Visit<RTsTypeAliasDecl> for KnownTypeVisitor {
    fn visit(&mut self, d: &RTsTypeAliasDecl) {
        d.visit_children_with(self);

        self.add(&d.id);
    }
}

impl Visit<RTsEnumDecl> for KnownTypeVisitor {
    fn visit(&mut self, d: &RTsEnumDecl) {
        d.visit_children_with(self);

        self.add(&d.id);
    }
}

impl Visit<RTsModuleDecl> for KnownTypeVisitor {
    fn visit(&mut self, d: &RTsModuleDecl) {
        d.visit_children_with(self);

        match &d.id {
            RTsModuleName::Ident(i) => {
                self.add(i);
            }
            RTsModuleName::Str(_) => {}
        }
    }
}