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
use stc_ts_ast_rnode::RTsConstAssertion;
use stc_ts_errors::{DebugExt, ErrorKind};
use stc_ts_file_analyzer_macros::validator;
use stc_ts_type_ops::{generalization::prevent_generalize, tuple_to_array::prevent_tuple_to_array};
use stc_ts_types::{Type, TypeParamInstantiation};

use crate::{
    analyzer::{expr::TypeOfMode, Analyzer, Ctx},
    validator::ValidateWith,
    VResult,
};

#[validator]
impl Analyzer<'_, '_> {
    fn validate(
        &mut self,
        expr: &RTsConstAssertion,
        mode: TypeOfMode,
        type_args: Option<&TypeParamInstantiation>,
        type_ann: Option<&Type>,
    ) -> VResult<Type> {
        let span = expr.span;

        if mode == TypeOfMode::RValue {
            let ctx = Ctx {
                in_const_assertion: true,
                array_lit_cannot_be_tuple: false,
                prefer_tuple_for_array_lit: true,
                ..self.ctx
            };
            let mut a = self.with_ctx(ctx);

            let mut ty = expr
                .expr
                .validate_with_args(&mut *a, (mode, None, type_ann))
                .context("tried to valid expression of a const assertion")?;

            prevent_generalize(&mut ty);
            prevent_tuple_to_array(&mut ty);

            Ok(ty)
        } else {
            Err(ErrorKind::Unimplemented {
                span,
                msg: "Proper error reporting for using const assertion expression in left hand side of an assignment expression"
                    .to_string(),
            }
            .into())
        }
    }
}