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

use rnode::{Fold, FoldWith};
use stc_ts_ast_rnode::{RBindingIdent, RFnDecl, RFnExpr, RFunction, RIdent, RParamOrTsParamProp, RPat, RTsEntityName};
use stc_ts_errors::{ErrorKind, Errors};
use stc_ts_type_ops::Fix;
use stc_ts_types::{CallSignature, Class, ClassMetadata, Function, Id, KeywordType, KeywordTypeMetadata, Ref, TypeElement};
use stc_ts_utils::find_ids_in_pat;
use stc_utils::cache::Freeze;
use swc_common::{Span, Spanned, SyntaxContext};
use swc_ecma_ast::TsKeywordTypeKind;
use ty::TypeExt;

use crate::{
    analyzer::{pat::PatMode, scope::VarKind, util::ResultExt, Analyzer, Ctx, ScopeKind},
    ty,
    ty::{FnParam, Tuple, Type, TypeParam},
    validator,
    validator::ValidateWith,
    VResult,
};

mod return_type;

#[validator]
impl Analyzer<'_, '_> {
    fn validate(&mut self, f: &RFunction, name: Option<&RIdent>) -> VResult<ty::Function> {
        let marks = self.marks();

        if !self.ctx.reevaluating() && !self.ctx.ignore_errors && f.body.is_some() {
            if let Some(id) = name {
                let v = self.data.fn_impl_spans.entry(id.into()).or_default();

                v.push(f.span);
                // TODO(kdy1): Make this efficient by report same error only once.
                if v.len() >= 2 {
                    for &span in &*v {
                        self.storage.report(ErrorKind::DuplicateFnImpl { span }.into())
                    }
                }
            }
        }

        self.with_child(ScopeKind::Fn, Default::default(), |child: &mut Analyzer| {
            child.ctx.allow_new_target = true;
            child.ctx.in_fn_with_return_type = f.return_type.is_some();
            child.ctx.in_async = f.is_async;
            child.ctx.in_generator = f.is_generator;
            child.ctx.in_static_method = false;
            child.ctx.in_static_property_initializer = false;
            child.ctx.in_static_block = false;
            child.ctx.super_references_super_class = false;

            let mut errors = Errors::default();

            {
                // Validate params
                // TODO(kdy1): Move this to parser
                let mut has_optional = false;
                for p in &f.params {
                    if has_optional {
                        match p.pat {
                            RPat::Ident(RBindingIdent {
                                id: RIdent { optional: true, .. },
                                ..
                            })
                            | RPat::Rest(..) => {}
                            _ => {
                                child.storage.report(ErrorKind::TS1016 { span: p.span() }.into());
                            }
                        }
                    }

                    if let RPat::Ident(RBindingIdent {
                        id: RIdent { optional, .. },
                        ..
                    }) = p.pat
                    {
                        // Allow optional after optional parameter
                        if optional {
                            has_optional = true;
                        }
                    }
                }
            }

            let mut type_params = try_opt!(f.type_params.validate_with(child));

            if type_params.is_none() {
                if let Some(mutations) = &child.mutations {
                    if let Some(ann) = mutations.for_callable.get(&f.node_id) {
                        type_params = ann.type_params.clone();
                    }
                }
            }

            let params = {
                let prev_len = child.scope.declaring_parameters.len();
                let ids: Vec<Id> = find_ids_in_pat(&f.params);
                child.scope.declaring_parameters.extend(ids);

                let ctx = Ctx {
                    pat_mode: PatMode::Decl,
                    in_fn_without_body: f.body.is_none(),
                    allow_ref_declaring: false,
                    is_fn_param: true,
                    ..child.ctx
                };
                let res = f.params.validate_with(&mut *child.with_ctx(ctx));

                child.scope.declaring_parameters.truncate(prev_len);

                res?
            };

            let mut declared_ret_ty = {
                let ctx = Ctx {
                    in_actual_type: true,
                    ..child.ctx
                };
                try_opt!(f.return_type.validate_with(&mut *child.with_ctx(ctx)))
            }
            .freezed();

            child.scope.declared_return_type = declared_ret_ty.clone();

            if let Some(ty) = &mut declared_ret_ty {
                ty.freeze();

                child.expand_return_type_of_fn(ty).report(&mut child.storage);
            }

            if let Some(ret_ty) = declared_ret_ty {
                let span = ret_ty.span();
                let metadata = ret_ty.metadata();
                declared_ret_ty = Some(match ret_ty {
                    Type::ClassDef(def) => Type::Class(Class {
                        span,
                        def,
                        metadata: ClassMetadata {
                            common: metadata,
                            ..Default::default()
                        },
                        tracker: Default::default(),
                    }),

                    _ => ret_ty,
                });
            }

            if let Some(ty) = &mut declared_ret_ty {
                if let Type::Ref(..) = ty.normalize() {
                    child.prevent_expansion(ty);
                }
            }

            let span = f.span;
            let is_async = f.is_async;
            let is_generator = f.is_generator;

            let inferred_return_type =
                try_opt!(f
                    .body
                    .as_ref()
                    .map(|body| child.visit_stmts_for_return(span, is_async, is_generator, &body.stmts)));

            let mut inferred_return_type = match inferred_return_type {
                Some(Some(inferred_return_type)) => {
                    let mut inferred_return_type = match inferred_return_type {
                        Type::Ref(ty) => Type::Ref(child.qualify_ref_type_args(ty.span, ty)?),
                        _ => inferred_return_type,
                    };

                    if let Some(declared) = &declared_ret_ty {
                        // Expand before assigning
                        let span = inferred_return_type.span();

                        if f.is_generator && declared.is_kwd(TsKeywordTypeKind::TsVoidKeyword) {
                            child
                                .storage
                                .report(ErrorKind::GeneratorCannotHaveVoidAsReturnType { span: declared.span() }.into())
                        }
                    } else {
                        if child.rule().no_implicit_any {
                            if child.is_implicitly_typed(&inferred_return_type) {
                                child.storage.report(ErrorKind::ImplicitReturnType { span }.into())
                            }
                        }

                        if child.may_generalize(&inferred_return_type) {
                            inferred_return_type = inferred_return_type.generalize_lit();
                        }
                    }

                    inferred_return_type
                }
                Some(None) => {
                    let mut span = f.span;

                    if let Some(ref declared) = declared_ret_ty {
                        span = declared.span();
                        let declared = child.normalize(Some(span), Cow::Borrowed(declared), Default::default())?;

                        match declared.normalize() {
                            Type::Keyword(KeywordType {
                                kind: TsKeywordTypeKind::TsAnyKeyword,
                                ..
                            })
                            | Type::Keyword(KeywordType {
                                kind: TsKeywordTypeKind::TsVoidKeyword,
                                ..
                            })
                            | Type::Keyword(KeywordType {
                                kind: TsKeywordTypeKind::TsNeverKeyword,
                                ..
                            }) => {}
                            _ => errors.push(ErrorKind::ReturnRequired { span }.into()),
                        }
                    }

                    // No return statement -> void
                    if f.return_type.is_none() {
                        if let Some(m) = &mut child.mutations {
                            if m.for_fns.entry(f.node_id).or_default().ret_ty.is_none() {
                                m.for_fns.entry(f.node_id).or_default().ret_ty = Some(Type::Keyword(KeywordType {
                                    span,
                                    kind: TsKeywordTypeKind::TsVoidKeyword,
                                    metadata: Default::default(),
                                    tracker: Default::default(),
                                }));
                            }
                        }
                    }
                    Type::Keyword(KeywordType {
                        span,
                        kind: TsKeywordTypeKind::TsVoidKeyword,
                        metadata: Default::default(),
                        tracker: Default::default(),
                    })
                }
                None => Type::any(f.span, Default::default()),
            };

            inferred_return_type.freeze();

            if f.return_type.is_none() {
                if let Some(m) = &mut child.mutations {
                    if m.for_fns.entry(f.node_id).or_default().ret_ty.is_none() {
                        m.for_fns.entry(f.node_id).or_default().ret_ty = Some(inferred_return_type.clone())
                    }
                }
            }

            child.storage.report_all(errors);

            Ok(ty::Function {
                span: f.span,
                type_params,
                params,
                ret_ty: box declared_ret_ty.unwrap_or(inferred_return_type),
                metadata: Default::default(),
                tracker: Default::default(),
            })
        })
    }
}

impl Analyzer<'_, '_> {
    pub(crate) fn fn_to_type_element(&mut self, f: &Function) -> VResult<TypeElement> {
        Ok(TypeElement::Call(CallSignature {
            span: f.span.with_ctxt(SyntaxContext::empty()),
            params: f.params.clone(),
            type_params: f.type_params.clone(),
            ret_ty: Some(f.ret_ty.clone()),
        }))
    }

    /// Fill type arguments using default value.
    ///
    /// If the referred type has default type parameter, we have to include it
    /// in function type of output (.d.ts)
    fn qualify_ref_type_args(&mut self, span: Span, mut ty: Ref) -> VResult<Ref> {
        let actual_ty = self.type_of_ts_entity_name(span, &ty.type_name.clone().into(), ty.type_args.as_deref())?;

        // TODO(kdy1): PERF
        let type_params = match actual_ty.get_type_param_decl() {
            Some(type_params) => type_params.clone(),

            _ => return Ok(ty),
        };

        let arg_cnt = ty.type_args.as_ref().map(|v| v.params.len()).unwrap_or(0);
        if type_params.params.len() <= arg_cnt {
            return Ok(ty);
        }

        self.prevent_expansion(&mut ty);

        if let Some(args) = ty.type_args.as_mut() {
            for (span, default) in type_params
                .params
                .into_iter()
                .skip(arg_cnt)
                .map(|param| (param.span, param.default.map(|v| *v)))
            {
                if let Some(default) = default {
                    args.params.push(default);
                } else {
                    self.storage
                        .report(ErrorKind::ImplicitAny { span }.context("qualify_ref_type_args"));
                    args.params
                        .push(Type::any(span.with_ctxt(SyntaxContext::empty()), Default::default()));
                }
            }
        }

        Ok(ty)
    }

    /// TODO(kdy1): Handle recursive function
    fn visit_fn(&mut self, name: Option<&RIdent>, f: &RFunction, type_ann: Option<&Type>) -> Type {
        let fn_ty: Result<_, _> = try {
            let no_implicit_any_span = name.as_ref().map(|name| name.span);

            self.apply_fn_type_ann(f.span, f.node_id, f.params.iter().map(|p| &p.pat), type_ann);

            // if let Some(name) = name {
            //     // We use `typeof function` to infer recursive function's return type.
            //     match self.declare_var(
            //         f.span,
            //         VarDeclKind::Var,
            //         name.into(),
            //         Some(Type::Query(QueryType {
            //             span: f.span,
            //             expr: RTsEntityName::Ident(name.clone()).into(),
            //         })),
            //         // value is initialized
            //         true,
            //         // Allow overriding
            //         true,
            //     ) {
            //         Ok(()) => {}
            //         Err(err) => {
            //             self.storage.report(err);
            //         }
            //     }
            // }

            if let Some(name) = name {
                assert_eq!(self.scope.declaring_fn, None);
                self.scope.declaring_fn = Some(name.into());
            }

            let mut fn_ty: ty::Function = f.validate_with_args(self, name)?;
            // Handle type parameters in return type.
            fn_ty.ret_ty = fn_ty.ret_ty.fold_with(&mut TypeParamHandler {
                params: fn_ty.type_params.as_ref().map(|v| &*v.params),
            });
            let ty::Function { ref mut ret_ty, .. } = fn_ty;
            if let Type::Tuple(Tuple { ref mut elems, .. }) = **ret_ty {
                for element in elems.iter_mut() {
                    let span = element.span();

                    match element.ty.normalize() {
                        Type::Keyword(KeywordType {
                            kind: TsKeywordTypeKind::TsUndefinedKeyword,
                            ..
                        })
                        | Type::Keyword(KeywordType {
                            kind: TsKeywordTypeKind::TsNullKeyword,
                            ..
                        }) => {}
                        _ => continue,
                    }

                    //if child.rule.no_implicit_any
                    //    && child.span_allowed_implicit_any != f.span
                    //{
                    //    child.storage.report(Error::ImplicitAny {
                    //        span: no_implicit_any_span.unwrap_or(span),
                    //    });
                    //}

                    element.ty = box Type::any(
                        span,
                        KeywordTypeMetadata {
                            common: element.ty.metadata(),
                            ..Default::default()
                        },
                    );
                }
            };

            if let Some(name) = name {
                self.scope.declaring_fn = None;
            }

            fn_ty
        };

        match fn_ty {
            Ok(ty) => Type::Function(ty).fixed().freezed(),
            Err(err) => {
                self.storage.report(err);
                Type::any(f.span, Default::default())
            }
        }
    }
}

#[validator]
impl Analyzer<'_, '_> {
    fn validate(&mut self, p: &RParamOrTsParamProp) -> VResult<FnParam> {
        match p {
            RParamOrTsParamProp::TsParamProp(p) => p.validate_with(self),
            RParamOrTsParamProp::Param(p) => p.validate_with(self),
        }
    }
}

#[validator]
impl Analyzer<'_, '_> {
    /// NOTE: This method **should not call f.fold_children_with(self)**
    fn validate(&mut self, f: &RFnDecl) {
        let ctx = Ctx {
            in_declare: self.ctx.in_declare || f.declare || f.function.body.is_none(),
            in_async: f.function.is_async,
            in_generator: f.function.is_generator,
            ..self.ctx
        };
        let fn_ty = self
            .with_ctx(ctx)
            .with_child(ScopeKind::Fn, Default::default(), |a: &mut Analyzer| {
                Ok(a.visit_fn(Some(&f.ident), &f.function, None).freezed())
            })?;

        let mut a = self.with_ctx(ctx);
        match a.declare_var(
            f.span(),
            VarKind::Fn,
            f.ident.clone().into(),
            Some(fn_ty),
            None,
            true,
            true,
            false,
            f.function.body.is_some(),
        ) {
            Ok(..) => {}
            Err(err) => {
                a.storage.report(err);
            }
        }

        Ok(())
    }
}

#[validator]
impl Analyzer<'_, '_> {
    /// NOTE: This method **should not call f.fold_children_with(self)**
    fn validate(&mut self, f: &RFnExpr, type_ann: Option<&Type>) -> VResult<Type> {
        Ok(self.visit_fn(f.ident.as_ref(), &f.function, type_ann))
    }
}

struct TypeParamHandler<'a> {
    params: Option<&'a [TypeParam]>,
}

impl Fold<Type> for TypeParamHandler<'_> {
    fn fold(&mut self, mut ty: Type) -> Type {
        if let Some(params) = self.params {
            // TODO(kdy1): PERF
            ty.normalize_mut();

            let ty: Type = ty.fold_children_with(self);

            match ty {
                Type::Ref(ref r) if r.type_args.is_none() => {
                    if let RTsEntityName::Ident(ref i) = r.type_name {
                        //
                        for param in params {
                            if param.name == i {
                                return Type::Param(param.clone());
                            }
                        }
                    }
                }

                _ => {}
            }

            ty
        } else {
            ty
        }
    }
}