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
use std::borrow::Cow;

use fxhash::FxHashMap;
use stc_ts_errors::{debug::dump_type_as_string, DebugExt};
use stc_ts_types::{ClassDef, ClassMember, ClassProperty, Id, Interface, Method, Type, TypeElement, TypeParam};
use stc_utils::cache::Freeze;
use swc_common::{Span, Spanned};
use tracing::info;

use crate::{analyzer::Analyzer, VResult};

impl Analyzer<'_, '_> {
    fn type_element_to_class_member(&mut self, el: &TypeElement) -> VResult<Option<ClassMember>> {
        match el {
            TypeElement::Call(_) => Ok(None),
            TypeElement::Constructor(c) => Ok(Some(ClassMember::Constructor(c.clone()))),
            TypeElement::Property(p) => Ok(Some(ClassMember::Property(ClassProperty {
                span: p.span,
                key: p.key.clone(),
                value: p.type_ann.clone(),
                is_static: false,
                accessibility: p.accessibility,
                is_abstract: false,
                is_optional: p.optional,
                readonly: p.readonly,
                definite: false,
                accessor: p.accessor,
            }))),
            TypeElement::Method(m) => Ok(Some(ClassMember::Method(Method {
                span: m.span,
                accessibility: m.accessibility,
                key: m.key.clone(),
                is_static: false,
                is_abstract: false,
                is_optional: m.optional,
                type_params: m.type_params.clone(),
                params: m.params.clone(),
                ret_ty: m.ret_ty.clone().unwrap_or_else(|| box Type::any(m.span, Default::default())),
            }))),
            TypeElement::Index(i) => Ok(Some(ClassMember::IndexSignature(i.clone()))),
        }
    }

    /// Handle declaration merging. This method is used to avoid implementing
    /// same logic twice.
    fn merge_from_to(&mut self, span: Span, a: Type, b: Type) -> VResult<Option<Type>> {
        if self.config.is_builtin {
            return Ok(None);
        }

        debug_assert!(a.is_clone_cheap());
        debug_assert!(b.is_clone_cheap());

        match (a.normalize(), b.normalize()) {
            (Type::ClassDef(a), Type::Interface(bi)) => {
                // TODO: Handle the number of type parameters.
                let mut type_params = FxHashMap::default();
                if let Some(b_tps) = &bi.type_params {
                    if let Some(a_tp) = &a.type_params {
                        for (idx, b_tp) in b_tps.params.iter().enumerate() {
                            if let Some(a_param) = a_tp.params.get(idx) {
                                type_params.insert(
                                    b_tp.name.clone(),
                                    Type::Param(TypeParam {
                                        span: a_tp.span,
                                        name: a_param.name.clone(),
                                        constraint: None,
                                        default: None,
                                        metadata: Default::default(),
                                        tracker: Default::default(),
                                    }),
                                );
                            }
                        }
                    }
                }
                let b = self.expand_type_params(&type_params, b, Default::default())?;

                let mut new_members = a.body.clone();

                let b = self
                    .convert_type_to_type_lit(span, Cow::Owned(b))
                    .context("tried to convert an interface to a type literal to merge with a class definition")?;
                if let Some(b) = b {
                    for el in &b.members {
                        new_members.extend(self.type_element_to_class_member(el)?);
                    }

                    return Ok(Some(Type::ClassDef(
                        ClassDef {
                            body: new_members,
                            ..(**a).clone()
                        }
                        .into(),
                    )));
                }
            }

            (Type::Interface(a), Type::Interface(bi)) => {
                // TODO: Handle the number of type parameters.
                let mut type_params = FxHashMap::default();
                if let Some(b_tps) = &bi.type_params {
                    if let Some(a_tp) = &a.type_params {
                        for (idx, b_tp) in b_tps.params.iter().enumerate() {
                            type_params.insert(
                                b_tp.name.clone(),
                                Type::Param(TypeParam {
                                    span: a_tp.span,
                                    name: a_tp.params[idx].name.clone(),
                                    constraint: None,
                                    default: None,
                                    metadata: Default::default(),
                                    tracker: Default::default(),
                                }),
                            );
                        }
                    }
                }
                let b = self.expand_type_params(&type_params, b, Default::default())?.freezed();

                let mut new_members = a.body.clone();

                // Convert to a type literal first.
                if let Some(b) = self
                    .convert_type_to_type_lit(span, Cow::Owned(b))
                    .context("tried to convert an interface to a type literal")?
                {
                    new_members.extend(b.into_owned().members);

                    return Ok(Some(Type::Interface(Interface {
                        body: new_members,
                        ..a.clone()
                    })));
                }
            }

            _ => {}
        }

        Ok(None)
    }

    /// Handle declaration merging.
    fn merge_declaration_types(&mut self, span: Span, orig: Type, new: Type) -> VResult<Type> {
        debug_assert!(orig.is_clone_cheap());
        debug_assert!(new.is_clone_cheap());

        if let Some(new_ty) = self.merge_from_to(span, orig.clone(), new.clone())? {
            return Ok(new_ty);
        }
        if let Some(new_ty) = self.merge_from_to(span, new.clone(), orig)? {
            return Ok(new_ty);
        }

        Ok(new)
    }

    pub(crate) fn merge_decl_with_name(&mut self, name: Id, new: Type) -> VResult<(Type, bool)> {
        let orig = self.find_type(&name)?;
        let mut orig = match orig {
            Some(v) => v,
            None => return Ok((new, false)),
        };

        let orig = orig.next().unwrap().into_owned();

        let new = self.merge_declaration_types(new.span(), orig, new)?;
        info!("Merging declaration {} with type {}", name, dump_type_as_string(&new));

        Ok((new, true))
    }
}