swc_ecma_compat_es2015/
new_target.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
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
use std::{borrow::Cow, mem};

use swc_common::{pass::CompilerPass, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::perf::{should_work, Check};
use swc_ecma_utils::{private_ident, quote_ident, ExprFactory};
use swc_ecma_visit::{
    noop_visit_mut_type, noop_visit_type, visit_mut_pass, Visit, VisitMut, VisitMutWith,
};
use swc_trace_macro::swc_trace;

pub fn new_target() -> impl Pass {
    visit_mut_pass(NewTarget {
        ctx: Ctx::Constructor,
    })
}

struct NewTarget {
    ctx: Ctx,
}

enum Ctx {
    Constructor,
    Method,
    Function(Ident),
}

impl NewTarget {
    fn visit_mut_method<T: VisitMutWith<Self>>(&mut self, c: &mut T) {
        let old = mem::replace(&mut self.ctx, Ctx::Method);

        c.visit_mut_with(self);

        self.ctx = old;
    }
}

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

    fn visit_mut_class_method(&mut self, c: &mut ClassMethod) {
        c.key.visit_mut_with(self);

        self.visit_mut_method(&mut c.function)
    }

    fn visit_mut_constructor(&mut self, c: &mut Constructor) {
        let old = mem::replace(&mut self.ctx, Ctx::Constructor);

        c.visit_mut_children_with(self);

        self.ctx = old;
    }

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

        if let Expr::MetaProp(MetaPropExpr {
            kind: MetaPropKind::NewTarget,
            span,
        }) = e
        {
            let this_ctor = |span| {
                ThisExpr { span }
                    .make_member(quote_ident!("constructor"))
                    .into()
            };
            match &self.ctx {
                Ctx::Constructor => *e = this_ctor(*span),
                Ctx::Method => *e = *Expr::undefined(DUMMY_SP),
                Ctx::Function(i) => {
                    *e = CondExpr {
                        span: *span,
                        // this instanceof Foo
                        test: BinExpr {
                            span: DUMMY_SP,
                            op: op!("instanceof"),
                            left: Box::new(Expr::This(ThisExpr { span: DUMMY_SP })),
                            right: Box::new(Expr::Ident(i.clone())),
                        }
                        .into(),
                        cons: Box::new(this_ctor(DUMMY_SP)),
                        // void 0
                        alt: Expr::undefined(DUMMY_SP),
                    }
                    .into()
                }
            }
        }
    }

    fn visit_mut_fn_decl(&mut self, f: &mut FnDecl) {
        // Ensure that `f` contains `new.target`.
        if !should_work::<ShouldWork, _>(&*f) {
            return;
        }

        let old = mem::replace(&mut self.ctx, Ctx::Function(f.ident.clone()));

        f.visit_mut_children_with(self);

        self.ctx = old;
    }

    fn visit_mut_fn_expr(&mut self, f: &mut FnExpr) {
        // Ensure that `f` contains `new.target`.
        if !should_work::<ShouldWork, _>(&*f) {
            return;
        }

        let i = f
            .ident
            .get_or_insert_with(|| private_ident!("_target"))
            .clone();

        let old = mem::replace(&mut self.ctx, Ctx::Function(i));

        f.visit_mut_children_with(self);

        self.ctx = old;
    }

    fn visit_mut_method_prop(&mut self, m: &mut MethodProp) {
        m.key.visit_mut_with(self);
        self.visit_mut_method(&mut m.function)
    }

    fn visit_mut_getter_prop(&mut self, m: &mut GetterProp) {
        m.key.visit_mut_with(self);
        self.visit_mut_method(&mut m.body)
    }

    fn visit_mut_setter_prop(&mut self, m: &mut SetterProp) {
        m.key.visit_mut_with(self);
        self.visit_mut_method(&mut m.body)
    }
}

impl CompilerPass for NewTarget {
    fn name(&self) -> Cow<'static, str> {
        Cow::Borrowed("new-target")
    }
}

#[derive(Default)]
struct ShouldWork {
    found: bool,
}

impl Visit for ShouldWork {
    noop_visit_type!(fail);

    fn visit_meta_prop_expr(&mut self, n: &MetaPropExpr) {
        if let MetaPropExpr {
            kind: MetaPropKind::NewTarget,
            ..
        } = n
        {
            self.found = true;
        }
    }
}

impl Check for ShouldWork {
    fn should_handle(&self) -> bool {
        self.found
    }
}