swc_ecma_transforms_compat/bugfixes/
async_arrows_in_class.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
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
use swc_common::{util::take::Take, Mark, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::prepend_stmt;
use swc_ecma_visit::{noop_fold_type, Fold, FoldWith, InjectVars};
use swc_trace_macro::swc_trace;

use crate::es2015::arrow;

/// Safari 10.3 had an issue where async arrow function expressions within any
/// class method would throw. After an initial fix, any references to the
/// instance via `this` within those methods would also throw. This is fixed by
/// converting arrow functions in class methods into equivalent function
/// expressions. See https://bugs.webkit.org/show_bug.cgi?id=166879
#[tracing::instrument(level = "info", skip_all)]
pub fn async_arrows_in_class(unresolved_mark: Mark) -> impl Fold {
    AsyncArrowsInClass {
        unresolved_mark,
        ..Default::default()
    }
}
#[derive(Default, Clone)]
struct AsyncArrowsInClass {
    in_class_method: bool,
    unresolved_mark: Mark,
    vars: Vec<VarDeclarator>,
}

/// TODO: VisitMut
#[swc_trace]
impl Fold for AsyncArrowsInClass {
    noop_fold_type!();

    fn fold_class_method(&mut self, n: ClassMethod) -> ClassMethod {
        self.in_class_method = true;
        let res = n.fold_children_with(self);
        self.in_class_method = false;
        res
    }

    fn fold_constructor(&mut self, n: Constructor) -> Constructor {
        self.in_class_method = true;
        let res = n.fold_children_with(self);
        self.in_class_method = false;
        res
    }

    fn fold_expr(&mut self, n: Expr) -> Expr {
        let n = n.fold_children_with(self);
        if !self.in_class_method {
            return n;
        }

        match n {
            Expr::Arrow(ref a) => {
                if a.is_async {
                    let mut v = arrow(self.unresolved_mark);
                    let n = n.fold_with(&mut v);
                    self.vars.extend(v.take_vars());
                    n
                } else {
                    n
                }
            }
            _ => n,
        }
    }

    fn fold_module_items(&mut self, stmts: Vec<ModuleItem>) -> Vec<ModuleItem> {
        let mut stmts = stmts.fold_children_with(self);
        if !self.vars.is_empty() {
            prepend_stmt(
                &mut stmts,
                VarDecl {
                    span: DUMMY_SP,
                    kind: VarDeclKind::Var,
                    declare: false,
                    decls: self.vars.take(),
                }
                .into(),
            );
        }

        stmts
    }

    fn fold_stmts(&mut self, stmts: Vec<Stmt>) -> Vec<Stmt> {
        let mut stmts = stmts.fold_children_with(self);
        if !self.vars.is_empty() {
            prepend_stmt(
                &mut stmts,
                VarDecl {
                    span: DUMMY_SP,
                    kind: VarDeclKind::Var,
                    declare: false,
                    decls: self.vars.take(),
                }
                .into(),
            );
        }

        stmts
    }
}

#[cfg(test)]
mod tests {
    use swc_common::chain;
    use swc_ecma_transforms_base::resolver;
    use swc_ecma_transforms_testing::test;

    use super::*;

    fn tr() -> impl Fold {
        let unresolved = Mark::new();
        chain!(
            resolver(unresolved, Mark::new(), false),
            async_arrows_in_class(unresolved)
        )
    }

    test!(
        ::swc_ecma_parser::Syntax::default(),
        |_| tr(),
        async_arrows,
        r#"
        class Foo {
            constructor() {
                this.x = async () => await 1;
            }
            bar() {
                (async () => { })();
            }
        }"#,
        r#"
        class Foo {
            constructor() {
                this.x = async function () {
                    return await 1;
                };
            }

            bar() {
                (async function () {})();
            }
        }"#
    );

    test!(
        ::swc_ecma_parser::Syntax::default(),
        |_| tr(),
        callback,
        r#"
        class Foo {
            foo() {
                bar(async () => await 1);
            }
        }"#,
        r#"
        class Foo {
            foo() {
              bar(async function () {
                  return await 1;
              });
            }
        }"#
    );

    test!(
        ::swc_ecma_parser::Syntax::default(),
        |_| tr(),
        this,
        r#"
        class Foo {
            constructor() {
                this.x = () => async () => await this;
            }
        }"#,
        r#"
        class Foo {
            constructor() {
                var _this = this;
                this.x = () => async function () {
                    return await _this;
                };
            }
        }"#
    );

    // TODO: handle arguments and super. This isn't handled in general for arrow
    // functions atm...

    test!(
        ::swc_ecma_parser::Syntax::default(),
        |_| tr(),
        non_async_arrow,
        r#"
        class Foo {
            constructor() {
                this.x = () => {};
            }
        }"#,
        r#"
        class Foo {
            constructor() {
                this.x = () => {};
            }
        }"#
    );

    test!(
        ::swc_ecma_parser::Syntax::default(),
        |_| tr(),
        non_class_async_arrow,
        "let x = async () => await 1;",
        "let x = async () => await 1;"
    );
}