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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use std::borrow::Cow;

use stc_ts_ast_rnode::{RExpr, RIdent, RTsEntityName};
use stc_ts_errors::{DebugExt, ErrorKind};
use stc_ts_types::{Array, ArrayMetadata, Ref, Type, TypeElement};
use swc_atoms::js_word;
use swc_common::{Spanned, TypeEq};

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

impl Analyzer<'_, '_> {
    /// This handles the assignment to builtin types.
    ///
    /// - Handles assignment of `Function` types.
    /// - Handles assignment of various array types.
    /// - Handles assignment of promise types.
    pub(super) fn assign_to_builtin(&mut self, data: &mut AssignData, l: &Type, r: &Type, opts: AssignOpts) -> Option<VResult<()>> {
        let span = opts.span;
        let l = l.normalize();
        let r = r.normalize();

        match l {
            Type::Ref(Ref {
                type_name: RTsEntityName::Ident(RIdent { sym, .. }),
                ..
            }) if *sym == *"ThisType" => return Some(Ok(())),

            Type::Ref(Ref {
                type_name: RTsEntityName::Ident(RIdent {
                    sym: js_word!("Array"), ..
                }),
                type_args: Some(type_args),
                ..
            }) => match r {
                Type::Array(r) => {
                    if type_args.params.len() == 1 {
                        return Some(self.assign_inner(data, &type_args.params[0], &r.elem_type, opts));
                    }
                    return Some(Ok(()));
                }
                Type::Tuple(r) => {
                    if type_args.params.len() == 1 {
                        let mut errors = vec![];
                        for el in &r.elems {
                            errors.extend(self.assign_inner(data, &type_args.params[0], &el.ty, opts).err());
                        }
                        if !errors.is_empty() {
                            return Some(Err(ErrorKind::TupleAssignError { span, errors }.into()));
                        }
                    }
                    return Some(Ok(()));
                }
                _ => {}
            },

            Type::Ref(Ref {
                type_name: RTsEntityName::Ident(RIdent {
                    sym: js_word!("Function"), ..
                }),
                ..
            }) => match r {
                Type::Ref(Ref {
                    type_name: RTsEntityName::Ident(RIdent {
                        sym: js_word!("Function"), ..
                    }),
                    ..
                }) => return Some(Ok(())),

                Type::TypeLit(rt) => {
                    if rt
                        .members
                        .iter()
                        .any(|r| matches!(r, TypeElement::Call(..) | TypeElement::Constructor(..)))
                    {
                        return Some(Ok(()));
                    }

                    return Some(Err(ErrorKind::NoCallSignature {
                        span: opts.span,
                        callee: box r.clone(),
                    }
                    .into()));
                }
                Type::Interface(ri) => {
                    if *ri.name.sym() == js_word!("Function") {
                        return Some(Ok(()));
                    }

                    if ri
                        .body
                        .iter()
                        .any(|r| matches!(r, TypeElement::Call(..) | TypeElement::Constructor(..)))
                    {
                        return Some(Ok(()));
                    }

                    for parent in &ri.extends {
                        if let RExpr::Ident(RIdent {
                            sym: js_word!("Function"), ..
                        }) = &*parent.expr
                        {
                            return Some(Ok(()));
                        }

                        let parent = self.type_of_ts_entity_name(opts.span, &parent.expr, parent.type_args.as_deref());
                        let parent = match parent {
                            Ok(ty) => ty,
                            Err(err) => return Some(Err(err)),
                        };

                        if let Some(Ok(())) = self.assign_to_builtin(data, l, &parent, opts) {
                            return Some(Ok(()));
                        }
                    }

                    return Some(Err(ErrorKind::NoCallSignature {
                        span: opts.span,
                        callee: box r.clone(),
                    }
                    .into()));
                }
                _ => {}
            },

            Type::Ref(Ref {
                type_name: RTsEntityName::Ident(type_name),
                type_args: Some(type_args),
                ..
            }) if type_name.sym == *"ReadonlyArray" => {
                if type_args.params.len() == 1 {
                    if let Type::Array(Array { elem_type, .. }) = r.normalize() {
                        return Some(
                            self.assign_inner(data, &type_args.params[0], elem_type, opts)
                                .context("tried to assign an array to a readonly array (builtin)"),
                        );
                    }
                }
            }

            _ => {}
        }

        if cfg!(feature = "fastpath") {
            if let Type::Array(l) = l {
                if let Some(r_elem) = unwrap_builtin_with_single_arg(r, "TemplateStringArray")
                    .or_else(|| unwrap_builtin_with_single_arg(r, "Array"))
                    .or_else(|| unwrap_builtin_with_single_arg(r, "ReadonlyArray"))
                {
                    return Some(
                        self.assign_with_opts(data, &l.elem_type, r_elem, opts)
                            .context("tried fast-path assignment to an array"),
                    );
                }
            }
        }

        if cfg!(feature = "fastpath") {
            if let Some(r_elem) = unwrap_builtin_with_single_arg(r, "ReadonlyArray") {
                return Some(self.assign_with_opts(
                    data,
                    l,
                    &Type::Array(Array {
                        span: r.span(),
                        elem_type: box r_elem.clone(),
                        metadata: ArrayMetadata { common: r.metadata() },
                        tracker: Default::default(),
                    }),
                    opts,
                ));
            }
        }

        if cfg!(feature = "fastpath") && opts.allow_assignment_to_param {
            // Fast path for
            //
            // lhs: (TResult1#0#0 | PromiseLike<TResult1>);
            // rhs: Promise<boolean>
            if let Type::Union(l) = l.normalize() {
                if l.types.len() == 2 && unwrap_builtin_with_single_arg(&l.types[1], "PromiseLike").type_eq(&Some(&l.types[0])) {
                    return Some(Ok(()));
                }
            }
        }

        if cfg!(feature = "fastpath") {
            if let Type::Union(l) = l.normalize() {
                if let Some(r) = unwrap_builtin_with_single_arg(r, "Promise") {
                    // Fast path for
                    //
                    // (Promise<number> | Promise<string> | Promise<boolean> |
                    // PromiseLike<(Promise<number> | Promise<string> |
                    // Promise<boolean>)>); = Promise<boolean>;
                    let mut done = true;
                    for l in &l.types {
                        if let Some(l) = unwrap_builtin_with_single_arg(l, "Promise") {
                            if let Ok(()) = self.assign_with_opts(data, l, r, opts) {
                                return Some(Ok(()));
                            }
                        } else {
                            done = false;
                        }
                    }

                    if done {
                        return Some(Err(ErrorKind::SimpleAssignFailed { span, cause: None }
                            .context("tried optimized assignment of `Promise<T>` to union")));
                    }
                }
            }
        }

        if opts.may_unwrap_promise {
            if let Ok(l) = self.get_awaited_type(span, Cow::Borrowed(l), true) {
                // We are in return type of an async function.

                if let Ok(r) = self.get_awaited_type(span, Cow::Borrowed(r), true) {
                    return Some(
                        self.assign_with_opts(data, &l, &r, AssignOpts { ..opts })
                            .context("tried to assign an awaited type to an awaited type"),
                    );
                }

                return Some(
                    self.assign_with_opts(
                        data,
                        &l,
                        r,
                        AssignOpts {
                            may_unwrap_promise: false,
                            ..opts
                        },
                    )
                    .context("tried to assign an awaited type to a non-awaited type"),
                );
            } else {
                if let Ok(r) = self.get_awaited_type(span, Cow::Borrowed(r), true) {
                    return Some(
                        self.assign_with_opts(data, l, &r, AssignOpts { ..opts })
                            .context("tried to assign an non-waited type to an awaited type"),
                    );
                }
            }
        }

        if let Some(l) = unwrap_builtin_with_single_arg(l, "Promise") {
            if let Some(r) = unwrap_builtin_with_single_arg(r, "Promise") {
                return Some(
                    self.assign_with_opts(data, l, r, opts)
                        .context("tried to assign a promise to another using optimized algorithm"),
                );
            }
        }

        None
    }
}