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
use fxhash::FxHashSet;
use rnode::{Visit, VisitWith};
use stc_ts_types::{Id, InferType, TypeParam, TypeParamDecl};
use stc_utils::cache::ALLOW_DEEP_CLONE;

/// This looks for type parameter declarations and infer type parameters.
#[derive(Debug, Default)]
pub struct TypeParamDeclFinder {
    pub params: FxHashSet<Id>,
}

impl Visit<TypeParamDecl> for TypeParamDeclFinder {
    #[inline]
    fn visit(&mut self, decl: &TypeParamDecl) {
        decl.visit_children_with(self);

        self.params.extend(decl.params.iter().map(|v| v.name.clone()));
    }
}

impl Visit<InferType> for TypeParamDeclFinder {
    #[inline]
    fn visit(&mut self, ty: &InferType) {
        ty.visit_children_with(self);

        self.params.insert(ty.type_param.name.clone());
    }
}

#[derive(Debug, Default)]
pub struct TypeParamNameUsageFinder {
    pub params: Vec<Id>,
}

/// Noop as declaration is not usage.
impl Visit<TypeParamDecl> for TypeParamNameUsageFinder {
    #[inline]
    fn visit(&mut self, _: &TypeParamDecl) {}
}

impl Visit<TypeParam> for TypeParamNameUsageFinder {
    fn visit(&mut self, node: &TypeParam) {
        for p in &self.params {
            if node.name == *p {
                return;
            }
        }

        // info!( "Found type parameter({})", node.name);

        self.params.push(node.name.clone());
    }
}

#[derive(Debug, Default)]
pub struct TypeParamUsageFinder {
    pub params: Vec<TypeParam>,
}

/// Noop as declaration is not usage.
impl Visit<TypeParamDecl> for TypeParamUsageFinder {
    #[inline]
    fn visit(&mut self, _: &TypeParamDecl) {}
}

impl Visit<TypeParam> for TypeParamUsageFinder {
    fn visit(&mut self, node: &TypeParam) {
        for p in &self.params {
            if node.name == p.name {
                return;
            }
        }

        // info!( "Found type parameter({})", node.name);

        self.params.push(ALLOW_DEEP_CLONE.set(&(), || node.clone()));
    }
}