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
use stc_ts_types::{replace::replace_type, Array, ArrayMetadata, Type};
use stc_ts_utils::MapWithMut;
use stc_utils::ext::TypeVecExt;
use swc_common::Spanned;

pub fn normalize_tuples(ty: &mut Type) {
    replace_type(
        ty,
        |ty| {
            if let Type::Tuple(tuple) = ty.normalize() {
                if tuple.metadata.prevent_tuple_to_array {
                    return false;
                }

                if tuple.elems.is_empty() {
                    return false;
                }

                return true;
            }

            false
        },
        |ty| {
            if let Type::Tuple(tuple) = ty.normalize() {
                let common_metadata = tuple.metadata.common;

                let span = ty.span();
                let mut types = ty.take().expect_tuple().elems.into_iter().map(|elem| *elem.ty).collect::<Vec<_>>();
                types.dedup_type();

                let has_other = types.iter().any(|ty| !ty.is_null_or_undefined());
                if has_other {
                    types.retain(|ty| !ty.is_null_or_undefined())
                }

                return Some(Type::Array(Array {
                    span,
                    elem_type: box Type::new_union(span, types),
                    metadata: ArrayMetadata {
                        common: common_metadata,
                        ..Default::default()
                    },
                    tracker: Default::default(),
                }));
            }

            None
        },
    )
}