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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use std::borrow::Cow;

use itertools::Itertools;
use stc_ts_ast_rnode::RBool;
use stc_ts_errors::{DebugExt, ErrorKind};
use stc_ts_type_ops::Fix;
use stc_ts_types::{
    KeywordType, LitType, LitTypeMetadata, PropertySignature, Tuple, TupleElement, Type, TypeElement, TypeLit, Union, UnionMetadata,
};
use stc_utils::cache::{Freeze, ALLOW_DEEP_CLONE};
use swc_common::{Span, DUMMY_SP};
use swc_ecma_ast::TsKeywordTypeKind;

use crate::{
    analyzer::{
        assign::{AssignData, AssignOpts},
        Analyzer,
    },
    VResult,
};

impl Analyzer<'_, '_> {
    /// # Cases
    ///
    /// Cases handled by this methods are
    ///
    ///  - lhs = `(["a", number] | ["b", number] | ["c", string]);`
    ///  - rhs = `[("b" | "a"), 1];`
    pub(super) fn assign_to_union(&mut self, data: &mut AssignData, l: &Type, r: &Type, opts: AssignOpts) -> Option<VResult<()>> {
        let r_res = self.flatten_unions_for_assignment(opts.span, Cow::Borrowed(r));

        match r_res {
            Ok(mut r) => {
                r.freeze();

                if r.is_union_type() {
                    Some(
                        self.assign_with_opts(data, l, &r, opts)
                            .context("tried to assign to a flattened union to another union"),
                    )
                } else {
                    None
                }
            }
            Err(_) => None,
        }
    }

    fn flatten_unions_for_assignment(&mut self, span: Span, ty: Cow<Type>) -> VResult<Type> {
        let ty = self.normalize(Some(span), ty, Default::default())?;

        match ty.normalize() {
            Type::Tuple(ty) => {
                let mut tuple = Type::Tuple(Tuple {
                    elems: Default::default(),
                    ..*ty
                });

                for el in &ty.elems {
                    self.append_tuple_element_to_type(span, &mut tuple, el)
                        .context("tried to append an element to a type")?;
                }

                Ok(tuple)
            }
            Type::TypeLit(ty) => {
                let mut type_lit = Type::TypeLit(TypeLit {
                    members: Default::default(),
                    ..*ty
                });

                for el in &ty.members {
                    self.append_type_element_to_type(span, &mut type_lit, el)
                        .context("tried to append an element to a type")?;
                }

                Ok(type_lit)
            }
            _ => Ok(ty.into_owned()),
        }
    }

    /// TODO(kdy1): Use Cow<TupleElement>
    fn append_type_element_to_type(&mut self, span: Span, to: &mut Type, el: &TypeElement) -> VResult<()> {
        if let TypeElement::Property(el) = el {
            if let Some(el_ty) = &el.type_ann {
                if let Some(ty) = self.expand_union_for_assignment(span, el_ty) {
                    let mut to_types = (0..ty.types.len()).map(|_| ALLOW_DEEP_CLONE.set(&(), || to.clone())).collect_vec();

                    for (idx, el_ty) in ty.types.iter().enumerate() {
                        self.append_type_element_to_type(
                            span,
                            &mut to_types[idx],
                            &TypeElement::Property(PropertySignature {
                                type_ann: Some(box el_ty.clone()),
                                ..el.clone()
                            }),
                        )?;
                    }

                    *to = Type::Union(Union {
                        span: ty.span,
                        types: to_types,
                        metadata: ty.metadata,
                        tracker: Default::default(),
                    })
                    .fixed();

                    return Ok(());
                }
            }
        }

        match to.normalize_mut() {
            Type::Union(to) => {
                for to in &mut to.types {
                    self.append_type_element_to_type(span, to, el)?;
                }

                Ok(())
            }
            Type::TypeLit(to) => {
                to.members.push(el.clone());

                Ok(())
            }
            _ => Err(ErrorKind::SimpleAssignFailed { span, cause: None }.into()),
        }
    }

    /// TODO(kdy1): Use Cow<TupleElement>
    fn append_tuple_element_to_type(&mut self, span: Span, to: &mut Type, el: &TupleElement) -> VResult<()> {
        if let Some(el_ty) = self.expand_union_for_assignment(span, &el.ty) {
            let mut to_types = (0..el_ty.types.len()).map(|_| to.clone()).collect_vec();

            for (idx, el_ty) in el_ty.types.iter().enumerate() {
                self.append_tuple_element_to_type(
                    span,
                    &mut to_types[idx],
                    &TupleElement {
                        span: el.span,
                        label: el.label.clone(),
                        ty: box el_ty.clone(),
                        tracker: Default::default(),
                    },
                )?;
            }

            *to = Type::Union(Union {
                span: el_ty.span,
                types: to_types,
                metadata: el_ty.metadata,
                tracker: Default::default(),
            });

            return Ok(());
        }

        match to.normalize_mut() {
            Type::Union(to) => {
                for to in &mut to.types {
                    self.append_tuple_element_to_type(span, to, el)?;
                }

                Ok(())
            }
            Type::Tuple(to) => {
                to.elems.push(el.clone());

                Ok(())
            }
            _ => Err(ErrorKind::SimpleAssignFailed { span, cause: None }.into()),
        }
    }

    /// Expands `boolean` to `true | false`.
    fn expand_union_for_assignment(&mut self, span: Span, t: &Type) -> Option<Union> {
        let t = self.normalize(Some(span), Cow::Borrowed(t), Default::default()).ok()?;

        match t.normalize() {
            Type::Keyword(KeywordType {
                span,
                metadata,
                kind: TsKeywordTypeKind::TsBooleanKeyword,
                ..
            }) => Some(Union {
                span: *span,
                types: vec![
                    Type::Lit(LitType {
                        span: DUMMY_SP,
                        lit: stc_ts_ast_rnode::RTsLit::Bool(RBool {
                            span: DUMMY_SP,
                            value: true,
                        }),
                        metadata: LitTypeMetadata::default(),
                        tracker: Default::default(),
                    }),
                    Type::Lit(LitType {
                        span: DUMMY_SP,
                        lit: stc_ts_ast_rnode::RTsLit::Bool(RBool {
                            span: DUMMY_SP,
                            value: false,
                        }),
                        metadata: LitTypeMetadata::default(),
                        tracker: Default::default(),
                    }),
                ],
                metadata: UnionMetadata {
                    common: metadata.common,
                    ..Default::default()
                },
                tracker: Default::default(),
            }),
            Type::Union(ty) => Some(ty.clone()),
            _ => None,
        }
    }
}