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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! # Rules
//!
//! All metadata structs should **derive** [Default].
//! It means, all field should be `false` by default.

use rnode::{FoldWith, VisitMutWith, VisitWith};
use serde::{Deserialize, Serialize};
use stc_visit::Visitable;
use swc_common::{EqIgnoreSpan, TypeEq};

use crate::type_id::DestructureId;

pub trait TypeMetadata {
    fn common(&self) -> CommonTypeMetadata;
}

macro_rules! impl_basic_traits {
    ($T:ty) => {
        /// # Note
        ///
        /// This struct is treated as a span while comparison. It means, [EqIgnoreSpan]
        /// will always return true.
        impl EqIgnoreSpan for $T {
            #[inline]
            fn eq_ignore_span(&self, _: &Self) -> bool {
                true
            }
        }

        /// # Note
        ///
        /// This struct is treated as a span while comparison. It means, [TypeEq]
        /// will always return true.
        impl TypeEq for $T {
            #[inline]
            fn type_eq(&self, _: &Self) -> bool {
                true
            }
        }

        impl Visitable for $T {}

        /// Noop.
        impl<F: ?Sized> FoldWith<F> for $T {
            #[inline]
            fn fold_children_with(self, _: &mut F) -> Self {
                self
            }
        }

        /// Noop.
        impl<F: ?Sized> VisitWith<F> for $T {
            #[inline]
            fn visit_children_with(&self, _: &mut F) {}
        }

        /// Noop.
        impl<F: ?Sized> VisitMutWith<F> for $T {
            #[inline]
            fn visit_mut_children_with(&mut self, _: &mut F) {}
        }
    };
}

macro_rules! impl_traits {
    ($T:ty) => {
        impl_basic_traits!($T);

        impl TypeMetadata for $T {
            fn common(&self) -> CommonTypeMetadata {
                self.common
            }
        }
    };
}

/// Common metadata shared among [crate::Type]s.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommonTypeMetadata {
    pub implicit: bool,

    pub infected_by_this_in_object_literal: bool,

    pub prevent_converting_to_children: bool,

    pub contains_infer_type: bool,

    /// If this mark is applied, type will not be inferred (based on constraint)
    /// while simplifying.
    pub prevent_complex_simplification: bool,

    /// This mark is applied to types resolved from variables.
    ///
    /// Used to distinguish object literal with a reference to object literal.
    pub resolved_from_var: bool,

    /// TODO(kdy1): Move this to [LitTypeMetadata]
    ///
    /// If the mark is applied, it means that the literal should not be
    /// generalized.
    pub prevent_generalization: bool,

    pub destructure_key: DestructureId,
}

impl_basic_traits!(CommonTypeMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct UnionMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(UnionMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct IntersectionMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(IntersectionMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct KeywordTypeMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(KeywordTypeMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct LitTypeMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(LitTypeMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct TupleMetadata {
    pub prevent_tuple_to_array: bool,

    pub common: CommonTypeMetadata,
}

impl_traits!(TupleMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct SymbolMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(SymbolMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct InstanceMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(InstanceMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ThisTypeMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(ThisTypeMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct StaticThisMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(StaticThisMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct TplTypeMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(TplTypeMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ArrayMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(ArrayMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct QueryTypeMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(QueryTypeMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct InferTypeMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(InferTypeMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImportTypeMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(ImportTypeMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct PredicateMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(PredicateMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct IndexedAccessTypeMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(IndexedAccessTypeMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct RefMetadata {
    /// This can be ignored based on the context.
    pub no_expand: bool,
    /// This can be ignored based on the context.
    pub ignore_no_expand: bool,

    pub common: CommonTypeMetadata,
}

impl_traits!(RefMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ConditionalMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(ConditionalMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct FunctionMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(FunctionMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ConstructorMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(ConstructorMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct OperatorMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(OperatorMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct MappedMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(MappedMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ClassMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(ClassMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ClassDefMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(ClassDefMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct TypeParamMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(TypeParamMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct EnumVariantMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(EnumVariantMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct InterfaceMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(InterfaceMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct AliasMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(AliasMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct EnumMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(EnumMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct StringMappingMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(StringMappingMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct RestTypeMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(RestTypeMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct OptionalTypeMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(OptionalTypeMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct NamespaceTypeMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(NamespaceTypeMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ModuleTypeMetadata {
    pub common: CommonTypeMetadata,
}

impl_traits!(ModuleTypeMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct TypeLitMetadata {
    pub common: CommonTypeMetadata,

    /// `true` if a spread element is used while initializing.
    pub inexact: bool,
    /// `true` if a type literal is modified by object union normalizer.
    pub normalized: bool,

    /// `true` if a type literal is declared by user or created from other
    /// user-declared types like [crate::Interface] or etc..
    ///
    /// # Note
    ///
    /// `specified` for `a` in the code below should be changed to `false`
    ///
    /// ```ts
    /// declare var a: { a: string }
    ///
    /// const b = [a, {b: 'foo'}]
    /// ```
    ///
    /// because `b` is normalized and so that the the code above is validated.
    ///
    /// But it should be `true` for the code below (copied from
    /// `objectLiteralNormalization.ts`).
    ///
    /// ```ts
    /// declare function f<T>(...items: T[]): T;
    /// declare let data: { a: 1, b: "abc", c: true };
    ///
    /// // Object literals are inferred as a single normalized union type
    /// let e1 = f({ a: 1, b: 2 }, { a: "abc" }, {});
    /// let e2 = f({}, { a: "abc" }, { a: 1, b: 2 });
    /// let e3 = f(data, { a: 2 }); // error
    /// let e4 = f({ a: 2 }, data); // error
    /// ```
    ///
    /// because tsc selects type of `data` instead of a normalized type literal
    /// union if one of inferred type literal is `specified`.
    pub specified: bool,
}

impl_traits!(TypeLitMetadata);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct TypeElMetadata {
    /// If `true`, it means the element has a default value.
    ///
    /// While assignment, missing property error will not occur by the element
    /// with this flag set to `true`.
    pub has_default: bool,
}

impl_basic_traits!(TypeElMetadata);