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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
use std::borrow::Cow;

use rnode::{Visit, VisitWith};
use stc_ts_ast_rnode::{RComputedPropName, RExpr, RGetterProp, RIdent, RMemberExpr, RPrivateName, RProp, RPropName};
use stc_ts_errors::{ErrorKind, Errors};
use stc_ts_file_analyzer_macros::extra_validator;
use stc_ts_types::{Accessor, ComputedKey, Index, Key, KeywordType, PrivateName, TypeParam, Unique};
use stc_utils::{cache::Freeze, dev_span};
use swc_atoms::js_word;
use swc_common::{Span, Spanned, SyntaxContext};
use swc_ecma_ast::*;

use crate::{
    analyzer::{
        expr::{IdCtx, TypeOfMode},
        pat::PatMode,
        scope::ScopeKind,
        util::ResultExt,
        Analyzer, Ctx,
    },
    ty::{MethodSignature, PropertySignature, Type, TypeElement, TypeExt},
    type_facts::TypeFacts,
    validator,
    validator::ValidateWith,
    VResult,
};

#[derive(Debug, Clone, Copy)]
pub(super) enum ComputedPropMode {
    Class {
        has_body: bool,
    },
    /// Object literal
    Object,

    Interface,
}

#[validator]
impl Analyzer<'_, '_> {
    fn validate(&mut self, node: &RPropName) -> VResult<Key> {
        match node {
            RPropName::Computed(c) => c.validate_with(self),
            RPropName::Ident(i) => Ok(Key::Normal {
                span: i.span,
                sym: i.sym.clone(),
            }),
            RPropName::Str(s) => Ok(Key::Normal {
                span: s.span,
                sym: s.value.clone(),
            }),
            RPropName::Num(v) => Ok(Key::Num(v.clone())),
            RPropName::BigInt(v) => Ok(Key::BigInt(v.clone())),
        }
    }
}

#[validator]
impl Analyzer<'_, '_> {
    fn validate(&mut self, n: &RPrivateName) -> VResult<PrivateName> {
        Ok(PrivateName {
            span: n.span,
            id: n.id.clone().into(),
        })
    }
}

#[validator]
impl Analyzer<'_, '_> {
    fn validate(&mut self, node: &RComputedPropName) -> VResult<Key> {
        let ctx = Ctx {
            in_computed_prop_name: true,
            ..self.ctx
        };

        let span = node.span;
        let mode = self.ctx.computed_prop_mode;

        let is_symbol_access = matches!(
            *node.expr,
            RExpr::Member(RMemberExpr {
                obj: box RExpr::Ident(RIdent {
                    sym: js_word!("Symbol"),
                    ..
                }),
                ..
            })
        );

        self.with_ctx(ctx).with(|analyzer: &mut Analyzer| {
            let mut check_for_validity = true;

            let mut errors = Errors::default();

            let mut ty = match node.expr.validate_with_default(analyzer) {
                Ok(ty) => ty,
                Err(err) => {
                    if let ErrorKind::TS2585 { span } = *err {
                        Err(ErrorKind::TS2585 { span })?
                    }

                    errors.push(err);
                    // TODO(kdy1): Change this to something else (maybe any)
                    Type::unknown(span, Default::default())
                }
            };
            ty.freeze();

            if match mode {
                ComputedPropMode::Class { has_body } => errors.is_empty(),
                ComputedPropMode::Object => errors.is_empty(),
                // TODO(kdy1):
                ComputedPropMode::Interface => errors.is_empty(),
            } {
                if !analyzer.is_type_valid_for_computed_key(span, &ty) {
                    check_for_validity = false;

                    analyzer
                        .storage
                        .report(ErrorKind::InvalidTypeForComputedProperty { span, ty: box ty.clone() }.into());
                }
            }

            if check_for_validity {
                match mode {
                    ComputedPropMode::Class { .. } | ComputedPropMode::Interface => {
                        let is_valid_key = is_valid_computed_key(&node.expr);

                        let ty = analyzer
                            .expand(node.span, ty.clone(), Default::default())
                            .report(&mut analyzer.storage);

                        if let Some(ref ty) = ty {
                            // TODO(kdy1): Add support for expressions like '' + ''.
                            match ty.normalize() {
                                _ if is_valid_key => {}
                                Type::Lit(..) => {}
                                Type::EnumVariant(..) => {}
                                _ if ty.is_kwd(TsKeywordTypeKind::TsSymbolKeyword) || ty.is_unique_symbol() || ty.is_symbol() => {}
                                _ => {
                                    if let ComputedPropMode::Interface = mode {
                                        errors.push(ErrorKind::TS1169 { span: node.span }.into());
                                    }
                                }
                            }
                        }
                    }

                    _ => {}
                }
            }

            if !errors.is_empty() {
                analyzer.storage.report_all(errors);
            }

            // match *ty {
            //     Type::Lit(LitType {
            //         lit: RTsLit::Number(n), ..
            //     }) => return Ok(Key::Num(n)),
            //     Type::Lit(LitType {
            //         lit: RTsLit::Str(s), ..
            //     }) => {
            //         return Ok(Key::Normal {
            //             span: s.span,
            //             sym: s.value,
            //         })
            //     }
            //     _ => {}
            // }

            Ok(Key::Computed(ComputedKey {
                span,
                expr: node.expr.clone(),
                ty: box ty,
            }))
        })
    }
}

#[validator]
impl Analyzer<'_, '_> {
    fn validate(&mut self, prop: &RProp, object_type: Option<&Type>) -> VResult<TypeElement> {
        let ctx = Ctx {
            computed_prop_mode: ComputedPropMode::Object,
            in_shorthand: matches!(prop, RProp::Shorthand(..)),
            ..self.ctx
        };

        let old_this = self.scope.this.take();
        let res = self.with_ctx(ctx).validate_prop_inner(prop, object_type);
        self.scope.this = old_this;

        res
    }
}

impl Analyzer<'_, '_> {
    /// Computed properties should not use type parameters defined by the
    /// declaring class.
    ///
    /// See: `computedPropertyNames32_ES5.ts`
    #[extra_validator]
    pub(crate) fn report_error_for_usage_of_type_param_of_declaring_class(&mut self, used_type_params: &[TypeParam], span: Span) {
        let _tracing = dev_span!("report_error_for_usage_of_type_param_of_declaring_class");

        debug_assert!(self.ctx.in_computed_prop_name);

        for used in used_type_params {
            let scope = self.scope.first_kind(|kind| match kind {
                ScopeKind::Fn
                | ScopeKind::Method { .. }
                | ScopeKind::Module
                | ScopeKind::Constructor
                | ScopeKind::ArrowFn
                | ScopeKind::Class
                | ScopeKind::ClassStaticBlock
                | ScopeKind::ObjectLit => true,
                ScopeKind::LoopBody { .. } | ScopeKind::Block | ScopeKind::Flow | ScopeKind::TypeParams | ScopeKind::Call => false,
            });
            if let Some(scope) = scope {
                match scope.kind() {
                    ScopeKind::Class => {
                        if scope.declaring_type_params.contains(&used.name) {
                            self.storage
                                .report(ErrorKind::DeclaringTypeParamReferencedByComputedPropName { span }.into());
                        }
                    }
                    _ => {
                        dbg!(scope.kind());
                    }
                }
            }
        }
    }

    fn is_type_valid_for_computed_key(&mut self, span: Span, ty: &Type) -> bool {
        let _tracing = dev_span!("is_type_valid_for_computed_key");

        if (ty.metadata().resolved_from_var || ty.metadata().prevent_generalization) && (ty.is_str_lit() || ty.is_num_lit()) {
            return true;
        }

        let ty = ty.clone().generalize_lit();

        if let Type::Function(..) = ty.normalize() {
            return false;
        }
        let ty = self.normalize(None, Cow::Owned(ty), Default::default());
        let ty = match ty {
            Ok(v) => v,
            Err(err) => {
                self.storage.report(err);
                // Don't report more errors.
                return true;
            }
        };

        match ty.normalize() {
            Type::Keyword(KeywordType {
                kind: TsKeywordTypeKind::TsAnyKeyword,
                ..
            })
            | Type::Keyword(KeywordType {
                kind: TsKeywordTypeKind::TsStringKeyword,
                ..
            })
            | Type::Keyword(KeywordType {
                kind: TsKeywordTypeKind::TsNumberKeyword,
                ..
            })
            | Type::Keyword(KeywordType {
                kind: TsKeywordTypeKind::TsSymbolKeyword,
                ..
            })
            | Type::Unique(Unique {
                ty:
                    box Type::Keyword(KeywordType {
                        kind: TsKeywordTypeKind::TsSymbolKeyword,
                        ..
                    }),
                ..
            })
            | Type::EnumVariant(..)
            | Type::Symbol(..)
            | Type::Tpl(..) => true,

            Type::Param(TypeParam { constraint: Some(ty), .. }) => {
                if self.is_type_valid_for_computed_key(span, ty) {
                    return true;
                }

                if let Type::Index(Index { .. }) = ty.normalize() {
                    return true;
                }

                false
            }

            Type::Union(u) => u.types.iter().all(|ty| {
                ty.is_kwd(TsKeywordTypeKind::TsNullKeyword)
                    || ty.is_kwd(TsKeywordTypeKind::TsUndefinedKeyword)
                    || self.is_type_valid_for_computed_key(span, ty)
            }),

            _ => false,
        }
    }

    fn validate_prop_inner(&mut self, prop: &RProp, object_type: Option<&Type>) -> VResult<TypeElement> {
        let computed = match prop {
            RProp::KeyValue(ref kv) => match &kv.key {
                RPropName::Computed(c) => {
                    c.visit_with(self);

                    true
                }
                _ => false,
            },
            _ => false,
        };

        let span = prop.span();
        // TODO(kdy1): Validate prop key

        let shorthand_type_ann = match prop {
            RProp::Shorthand(i) => {
                // TODO(kdy1): Check if RValue is correct
                self.type_of_var(i, TypeOfMode::RValue, None)
                    .report(&mut self.storage)
                    .map(Box::new)
            }
            _ => None,
        };

        let span = prop.span();

        Ok(match prop {
            RProp::Shorthand(i) => {
                let key = Key::Normal { span, sym: i.sym.clone() };
                PropertySignature {
                    span: prop.span().with_ctxt(SyntaxContext::empty()),
                    accessibility: None,
                    readonly: false,
                    key,
                    optional: false,
                    params: Default::default(),
                    type_ann: shorthand_type_ann,
                    type_params: Default::default(),
                    metadata: Default::default(),
                    accessor: Default::default(),
                }
                .into()
            }

            RProp::KeyValue(ref kv) => {
                let key = kv.key.validate_with(self)?;
                let computed = matches!(kv.key, RPropName::Computed(_));

                let type_ann = object_type.and_then(|obj| {
                    self.access_property(span, obj, &key, TypeOfMode::RValue, IdCtx::Var, Default::default())
                        .ok()
                });

                let ty = kv.value.validate_with_args(self, (TypeOfMode::RValue, None, type_ann.as_ref()))?;

                PropertySignature {
                    span,
                    accessibility: None,
                    readonly: false,
                    key,
                    optional: false,
                    params: Default::default(),
                    type_ann: Some(box ty),
                    type_params: Default::default(),
                    metadata: Default::default(),
                    accessor: Default::default(),
                }
                .into()
            }

            RProp::Assign(ref p) => unimplemented!("validate_key(AssignProperty): {:?}", p),
            RProp::Getter(ref p) => p.validate_with(self)?,
            RProp::Setter(ref p) => {
                let key = p.key.validate_with(self)?;
                let computed = matches!(p.key, RPropName::Computed(_));
                let param_span = p.param.span();
                let param = &p.param;

                self.with_child(ScopeKind::Method { is_static: false }, Default::default(), {
                    |child: &mut Analyzer| -> VResult<_> {
                        child.ctx.pat_mode = PatMode::Decl;
                        let param = param.validate_with(child)?;

                        p.body.visit_with(child);

                        Ok(PropertySignature {
                            span,
                            accessibility: None,
                            readonly: false,
                            key,
                            optional: false,
                            params: vec![param],
                            type_ann: Some(box Type::any(param_span, Default::default())),
                            type_params: Default::default(),
                            metadata: Default::default(),
                            accessor: Accessor {
                                getter: false,
                                setter: true,
                            },
                        }
                        .into())
                    }
                })?
            }

            RProp::Method(ref p) => {
                let key = p.key.validate_with(self)?;
                let computed = matches!(p.key, RPropName::Computed(..));
                let method_type_ann = object_type
                    .cloned()
                    .map(|ty| self.apply_type_facts_to_type(TypeFacts::NEUndefinedOrNull, ty))
                    .and_then(|obj| {
                        self.access_property(span, &obj, &key, TypeOfMode::RValue, IdCtx::Var, Default::default())
                            .ok()
                    });

                self.with_child(ScopeKind::Method { is_static: false }, Default::default(), {
                    |child: &mut Analyzer| -> VResult<_> {
                        child.ctx.in_async = p.function.is_async;
                        child.ctx.in_generator = p.function.is_generator;

                        child.apply_fn_type_ann(
                            p.function.span,
                            p.function.node_id,
                            p.function.params.iter().map(|v| &v.pat),
                            method_type_ann.as_ref(),
                        );

                        // We mark as wip
                        if !computed {
                            if let RPropName::Ident(i) = &p.key {
                                child.scope.declaring_prop = Some(i.into());
                            };
                        }

                        let type_params = try_opt!(p.function.type_params.validate_with(child));
                        let params = p.function.params.validate_with(child)?;

                        let ret_ty = try_opt!(p.function.return_type.validate_with(child));
                        let ret_ty = ret_ty.map(|ty| ty.freezed());
                        child.scope.declared_return_type = ret_ty.clone();

                        let mut inferred = None;

                        if let Some(body) = &p.function.body {
                            let mut inferred_ret_ty = child
                                .visit_stmts_for_return(p.function.span, p.function.is_async, p.function.is_generator, &body.stmts)?
                                .unwrap_or_else(|| {
                                    Type::Keyword(KeywordType {
                                        span: body.span,
                                        kind: TsKeywordTypeKind::TsVoidKeyword,
                                        metadata: Default::default(),
                                        tracker: Default::default(),
                                    })
                                });
                            inferred_ret_ty.freeze();

                            // Preserve return type if `this` is not involved in return type.
                            if p.function.return_type.is_none() {
                                inferred_ret_ty = if inferred_ret_ty.metadata().infected_by_this_in_object_literal {
                                    Type::any(span, Default::default())
                                } else {
                                    inferred_ret_ty
                                };

                                if let Some(m) = &mut child.mutations {
                                    m.for_fns.entry(p.function.node_id).or_default().ret_ty = Some(inferred_ret_ty.clone());
                                }
                            }

                            inferred = Some(inferred_ret_ty)

                            // TODO(kdy1): Assign
                        }
                        let ret_ty = ret_ty.or(inferred).map(Box::new);

                        Ok(MethodSignature {
                            span,
                            accessibility: None,
                            readonly: false,
                            key,
                            optional: false,
                            params,
                            ret_ty,
                            type_params,
                            metadata: Default::default(),
                        }
                        .into())
                    }
                })?
            }
        })
    }
}

#[validator]
impl Analyzer<'_, '_> {
    fn validate(&mut self, n: &RGetterProp) -> VResult<TypeElement> {
        let key = n.key.validate_with(self)?;
        let computed = key.is_computed();

        let type_ann = self
            .with_child(
                ScopeKind::Method { is_static: false },
                Default::default(),
                |child: &mut Analyzer| {
                    if let Some(body) = &n.body {
                        let ret_ty = child.visit_stmts_for_return(n.span, false, false, &body.stmts)?;
                        if ret_ty.is_none() {
                            // getter property must have return statements.
                            child.storage.report(ErrorKind::TS2378 { span: n.key.span() }.into());
                        }

                        return Ok(ret_ty);
                    }

                    Ok(None)
                },
            )
            .report(&mut self.storage)
            .flatten();

        Ok(PropertySignature {
            span: n.span(),
            accessibility: None,
            readonly: true,
            key,
            optional: false,
            params: Default::default(),
            type_ann: if computed {
                type_ann.map(Box::new)
            } else {
                Some(box Type::any(n.span, Default::default()))
            },
            type_params: Default::default(),
            metadata: Default::default(),
            accessor: Accessor {
                getter: true,
                setter: false,
            },
        }
        .into())
    }
}

fn is_valid_computed_key(key: &RExpr) -> bool {
    let mut v = ValidKeyChecker { valid: true };
    key.visit_with(&mut v);
    v.valid
}

#[derive(Debug)]
struct ValidKeyChecker {
    valid: bool,
}

impl Visit<RIdent> for ValidKeyChecker {
    fn visit(&mut self, _: &RIdent) {
        self.valid = false;
    }
}