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
use fxhash::FxHashSet;
use rnode::{Visit, VisitMut, VisitMutWith, VisitWith};
use stc_ts_generics::type_param::finder::TypeParamDeclFinder;
use stc_ts_types::{Id, Mapped, Type, TypeLit};

use crate::analyzer::{scope::Scope, Analyzer};

impl Analyzer<'_, '_> {
    /// Convert type parameters declared in dead scopes to `{}`.
    ///
    /// This does not touch type parameters declared in parent scopes and this
    /// method should be called when a type had escaped a scope.
    ///
    /// In this way, we can handle both of cases below.
    ///
    /// ## Invalid assignment.
    ///
    /// ```ts
    /// function foo<T, U>() {
    ///     var a: T;
    ///     var b: U;
    ///     a = b; // This is wrong.
    /// }
    /// ```
    ///
    /// This case is handled because type parameters are not touched (while we
    /// are analyzing function) by the method.
    ///
    /// ## Escaping
    ///
    /// ```ts
    /// function foo<T>() {}
    /// function bar<T>() {}
    ///
    /// var a = foo(); // type is {}
    /// a = bar();
    /// ```
    ///
    /// This method is called at the end of each call and each `T` is converted
    /// to `{}` even though span hygiene differs.
    pub(crate) fn replace_invalid_type_params(&mut self, ty: &mut Type) {
        if self.config.is_builtin {
            return;
        }

        let declared = {
            let mut v = TypeParamDeclFinder::default();
            ty.visit_with(&mut v);
            v.params
        };

        let mut v = TypeParamEscapeHandler { analyzer: self, declared };

        ty.visit_mut_with(&mut v);
    }

    fn is_type_param_dead(&mut self, name: &Id) -> bool {
        fn is_dead(s: &Scope, name: &Id) -> bool {
            if s.is_root() {
                return true;
            }

            if let Some(..) = s.facts.types.get(name) {
                return false;
            }
            if let Some(..) = s.types.get(name) {
                return false;
            }

            match s.parent {
                Some(p) => is_dead(p, name),
                None => true,
            }
        }

        is_dead(&self.scope, name)
    }
}

struct TypeParamEscapeVisitor<'a, 'b, 'c> {
    analyzer: &'a mut Analyzer<'b, 'c>,
    declared: &'a FxHashSet<Id>,
    should_work: bool,
}

impl Visit<Type> for TypeParamEscapeVisitor<'_, '_, '_> {
    fn visit(&mut self, ty: &Type) {
        if let Type::Param(ty) = ty.normalize() {
            if self.declared.contains(&ty.name) {
                return;
            }

            if self.analyzer.is_type_param_dead(&ty.name) {
                self.should_work = true;
                return;
            }
        }

        ty.visit_children_with(self);
    }
}

struct TypeParamEscapeHandler<'a, 'b, 'c> {
    analyzer: &'a mut Analyzer<'b, 'c>,

    /// Type parameters declared by the type we are visiting.
    declared: FxHashSet<Id>,
}

impl VisitMut<Mapped> for TypeParamEscapeHandler<'_, '_, '_> {
    fn visit_mut(&mut self, _: &mut Mapped) {}
}

impl VisitMut<Type> for TypeParamEscapeHandler<'_, '_, '_> {
    fn visit_mut(&mut self, ty: &mut Type) {
        if !ty.is_type_param() {
            // Fast path
            let mut v = TypeParamEscapeVisitor {
                analyzer: self.analyzer,
                declared: &self.declared,
                should_work: false,
            };
            ty.visit_with(&mut v);
            if !v.should_work {
                return;
            }
        }

        // TODO(kdy1): PERF
        ty.normalize_mut();
        ty.visit_mut_children_with(self);

        if let Type::Param(param) = ty {
            if self.declared.contains(&param.name) {
                return;
            }

            if self.analyzer.is_type_param_dead(&param.name) {
                *ty = Type::TypeLit(TypeLit {
                    span: param.span,
                    members: vec![],
                    metadata: Default::default(),
                    tracker: Default::default(),
                });
            }
        }
    }
}