swc_ecma_compat_es2022/class_properties/
this_in_static.rs

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
use swc_ecma_ast::*;
use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
use swc_trace_macro::swc_trace;

pub(super) struct ThisInStaticFolder {
    pub ident: Ident,
}

#[swc_trace]
impl VisitMut for ThisInStaticFolder {
    noop_visit_mut_type!(fail);

    // once again, for computed props
    fn visit_mut_constructor(&mut self, _: &mut Constructor) {}

    fn visit_mut_expr(&mut self, e: &mut Expr) {
        e.visit_mut_children_with(self);

        if let Expr::This(..) = e {
            *e = self.ident.clone().into()
        }
    }

    fn visit_mut_function(&mut self, _: &mut Function) {}
}

pub(super) struct NewTargetInProp;

#[swc_trace]
impl VisitMut for NewTargetInProp {
    noop_visit_mut_type!(fail);

    // once again, for computed props
    fn visit_mut_constructor(&mut self, _: &mut Constructor) {}

    fn visit_mut_expr(&mut self, e: &mut Expr) {
        e.visit_mut_children_with(self);

        if let Expr::MetaProp(MetaPropExpr {
            span,
            kind: MetaPropKind::NewTarget,
        }) = e
        {
            *e = *Expr::undefined(*span);
        }
    }

    fn visit_mut_function(&mut self, _: &mut Function) {}
}