swc_ecma_transforms_typescript/
utils.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_common::DUMMY_SP;
use swc_ecma_ast::*;
use swc_ecma_utils::ExprFactory;

///
/// this.prop = value
pub(crate) fn assign_value_to_this_prop(prop_name: PropName, value: Expr) -> Box<Expr> {
    let target = MemberExpr {
        obj: ThisExpr { span: DUMMY_SP }.into(),
        span: DUMMY_SP,
        prop: prop_name.into(),
    };

    let expr = value.make_assign_to(op!("="), target.into());

    Box::new(expr)
}

/// this.#prop = value
pub(crate) fn assign_value_to_this_private_prop(
    private_name: PrivateName,
    value: Expr,
) -> Box<Expr> {
    let target = MemberExpr {
        obj: ThisExpr { span: DUMMY_SP }.into(),
        span: DUMMY_SP,
        prop: MemberProp::PrivateName(private_name),
    };

    let expr = value.make_assign_to(op!("="), target.into());

    Box::new(expr)
}

pub(crate) struct Factory;

impl Factory {
    pub(crate) fn function(params: Vec<Param>, body: BlockStmt) -> Function {
        Function {
            params,
            decorators: Default::default(),
            span: DUMMY_SP,
            body: Some(body),
            is_generator: false,
            is_async: false,
            ..Default::default()
        }
    }
}