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
use rnode::{VisitMut, VisitMutWith};
use stc_ts_types::{Array, Conditional, FnParam, Intersection, KeywordTypeMetadata, Type, TypeOrSpread, TypeParam, Union, Valid};
use swc_common::TypeEq;

pub trait Fix: Sized {
    fn fix(&mut self);

    fn fixed(mut self) -> Self {
        self.fix();
        self
    }
}

impl<T> Fix for Vec<T>
where
    T: Fix,
{
    fn fix(&mut self) {
        self.iter_mut().for_each(|item| item.fix())
    }
}

impl<T> Fix for Option<T>
where
    T: Fix,
{
    fn fix(&mut self) {
        match self {
            Some(v) => v.fix(),
            None => {}
        }
    }
}

impl<T> Fix for Box<T>
where
    T: Fix,
{
    fn fix(&mut self) {
        (**self).fix()
    }
}

macro_rules! impl_fix {
    ($T:ty) => {
        impl Fix for $T {
            fn fix(&mut self) {
                let _tracing = stc_utils::dev_span!("fix");

                self.visit_mut_with(&mut Fixer);
            }
        }
    };
}

impl_fix!(Type);
impl_fix!(Array);
impl_fix!(Union);
impl_fix!(Intersection);
impl_fix!(TypeOrSpread);
impl_fix!(Conditional);
impl_fix!(FnParam);
impl_fix!(TypeParam);

struct Fixer;

impl VisitMut<Union> for Fixer {
    fn visit_mut(&mut self, u: &mut Union) {
        for ty in &u.types {
            ty.assert_valid();
        }

        let mut new: Vec<Type> = Vec::with_capacity(u.types.capacity());
        for ty in u.types.drain(..) {
            if ty.is_never() {
                continue;
            }

            if new.iter().any(|stored| stored.type_eq(&ty)) {
                continue;
            }

            if ty.is_union_type() {
                let u = ty.expect_union_type();
                for ty in u.types {
                    if new.iter().any(|stored| stored.type_eq(&ty)) {
                        continue;
                    }
                    if ty.is_never() {
                        continue;
                    }
                    new.push(ty);
                }
                continue;
            }

            new.push(ty);
        }
        u.types = new;
    }
}

impl VisitMut<Intersection> for Fixer {
    fn visit_mut(&mut self, ty: &mut Intersection) {
        for ty in &ty.types {
            ty.assert_valid();
        }

        let mut new: Vec<Type> = Vec::with_capacity(ty.types.capacity());
        for ty in ty.types.drain(..) {
            if new.iter().any(|stored| stored.type_eq(&ty)) {
                continue;
            }

            if ty.is_intersection() {
                let i = ty.expect_intersection();
                for ty in i.types {
                    if new.iter().any(|stored| stored.type_eq(&ty)) {
                        continue;
                    }
                    new.push(ty);
                }
                continue;
            }

            new.push(ty);
        }
        ty.types = new;
    }
}

impl Fixer {
    fn fix_type(&mut self, ty: &mut Type) {
        if matches!(ty, Type::Arc(..)) {
            return;
        }

        if matches!(ty, Type::Keyword(..) | Type::Lit(..)) {
            return;
        }

        if ty.is_valid() {
            return;
        }

        ty.normalize_mut();
        ty.visit_mut_children_with(self);

        match ty {
            Type::Union(u) => match u.types.len() {
                0 => {
                    *ty = Type::never(
                        u.span,
                        KeywordTypeMetadata {
                            common: u.metadata.common,
                            ..Default::default()
                        },
                    );
                }
                1 => {
                    let mut elem = u.types.drain(..).next().unwrap();
                    elem.respan(u.span);
                    *ty = elem;
                }
                _ => {}
            },

            Type::Intersection(i) => match i.types.len() {
                0 => {
                    *ty = Type::any(
                        i.span,
                        KeywordTypeMetadata {
                            common: i.metadata.common,
                            ..Default::default()
                        },
                    );
                }
                1 => {
                    let mut elem = i.types.drain(..).next().unwrap();
                    elem.respan(i.span);
                    *ty = elem;
                }
                _ => {}
            },
            _ => {}
        }
    }
}

impl VisitMut<Type> for Fixer {
    fn visit_mut(&mut self, ty: &mut Type) {
        self.fix_type(ty);
    }
}