swc_estree_compat/babelify/
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
218
219
220
221
222
223
224
225
226
227
228
use copyless::BoxHelper;
use serde_json::value::Value;
use swc_ecma_ast::{
    Class, ClassMember, ClassMethod, ClassProp, Constructor, Decorator, MethodKind, PrivateMethod,
    PrivateProp, StaticBlock,
};
use swc_estree_ast::{
    ClassBody, ClassBodyEl, ClassExpression, ClassMethod as BabelClassMethod, ClassMethodKind,
    ClassPrivateMethod, ClassPrivateProperty, ClassProperty, Decorator as BabelDecorator,
    StaticBlock as BabelStaticBlock,
};

use crate::babelify::{extract_class_body_span, Babelify, Context};

impl Babelify for Class {
    type Output = ClassExpression;

    fn babelify(self, ctx: &Context) -> Self::Output {
        let body = ClassBody {
            base: ctx.base(extract_class_body_span(&self, ctx)),
            body: self.body.babelify(ctx),
        };

        ClassExpression {
            base: ctx.base(self.span),
            decorators: Some(self.decorators.babelify(ctx)),
            body,
            super_class: self
                .super_class
                .map(|expr| Box::alloc().init(expr.babelify(ctx).into())),
            type_parameters: self.type_params.map(|param| param.babelify(ctx).into()),
            super_type_parameters: self
                .super_type_params
                .map(|param| param.babelify(ctx).into()),
            implements: Some(
                self.implements
                    .into_iter()
                    .map(|imp| imp.babelify(ctx).into())
                    .collect(),
            ),
            id: Default::default(),
            mixins: Default::default(),
        }
    }
}

impl Babelify for ClassMember {
    type Output = ClassBodyEl;

    fn babelify(self, ctx: &Context) -> Self::Output {
        match self {
            ClassMember::Constructor(c) => ClassBodyEl::Method(c.babelify(ctx)),
            ClassMember::Method(m) => ClassBodyEl::Method(m.babelify(ctx)),
            ClassMember::PrivateMethod(m) => ClassBodyEl::PrivateMethod(m.babelify(ctx)),
            ClassMember::ClassProp(p) => ClassBodyEl::Prop(p.babelify(ctx)),
            ClassMember::PrivateProp(p) => ClassBodyEl::PrivateProp(p.babelify(ctx)),
            ClassMember::TsIndexSignature(s) => ClassBodyEl::TSIndex(s.babelify(ctx)),
            ClassMember::Empty(_) => panic!(
                "illegal conversion: Cannot convert {:?} to ClassBodyEl",
                &self
            ),
            ClassMember::StaticBlock(s) => ClassBodyEl::StaticBlock(s.babelify(ctx)),
            ClassMember::AutoAccessor(..) => todo!("auto accessor"),
        }
    }
}

impl Babelify for ClassProp {
    type Output = ClassProperty;

    fn babelify(self, ctx: &Context) -> Self::Output {
        let computed = Some(self.key.is_computed());

        ClassProperty {
            base: ctx.base(self.span),
            key: self.key.babelify(ctx),
            value: self
                .value
                .map(|val| Box::alloc().init(val.babelify(ctx).into())),
            type_annotation: self
                .type_ann
                .map(|ann| Box::alloc().init(ann.babelify(ctx).into())),
            is_static: Some(self.is_static),
            decorators: Some(self.decorators.babelify(ctx)),
            computed,
            accessibility: self.accessibility.map(|access| access.babelify(ctx)),
            is_abstract: Some(self.is_abstract),
            optional: Some(self.is_optional),
            readonly: Some(self.readonly),
            declare: Some(self.declare),
            definite: Some(self.definite),
        }
    }
}

impl Babelify for PrivateProp {
    type Output = ClassPrivateProperty;

    fn babelify(self, ctx: &Context) -> Self::Output {
        ClassPrivateProperty {
            base: ctx.base(self.span),
            key: self.key.babelify(ctx),
            value: self
                .value
                .map(|expr| Box::alloc().init(expr.babelify(ctx).into())),
            type_annotation: self
                .type_ann
                .map(|ann| Box::alloc().init(ann.babelify(ctx).into())),
            static_any: Value::Bool(self.is_static),
            decorators: Some(self.decorators.babelify(ctx)),
        }
    }
}

impl Babelify for ClassMethod {
    type Output = BabelClassMethod;

    fn babelify(self, ctx: &Context) -> Self::Output {
        BabelClassMethod {
            base: ctx.base(self.span),
            key: self.key.babelify(ctx),
            kind: Some(self.kind.babelify(ctx)),
            is_static: Some(self.is_static),
            access: self.accessibility.map(|access| access.babelify(ctx)),
            accessibility: self.accessibility.map(|access| access.babelify(ctx)),
            is_abstract: Some(self.is_abstract),
            optional: Some(self.is_optional),
            params: self.function.params.babelify(ctx),
            body: self.function.body.unwrap().babelify(ctx),
            generator: Some(self.function.is_generator),
            is_async: Some(self.function.is_async),
            decorators: Some(self.function.decorators.babelify(ctx)),
            type_parameters: self.function.type_params.map(|t| t.babelify(ctx).into()),
            return_type: self
                .function
                .return_type
                .map(|t| Box::alloc().init(t.babelify(ctx).into())),
            computed: Default::default(),
        }
    }
}

impl Babelify for PrivateMethod {
    type Output = ClassPrivateMethod;

    fn babelify(self, ctx: &Context) -> Self::Output {
        ClassPrivateMethod {
            base: ctx.base(self.span),
            key: self.key.babelify(ctx),
            kind: Some(self.kind.babelify(ctx)),
            is_static: Some(self.is_static),
            access: self.accessibility.map(|access| access.babelify(ctx)),
            accessibility: self.accessibility.map(|access| access.babelify(ctx)),
            is_abstract: Some(self.is_abstract),
            optional: Some(self.is_optional),
            params: self.function.params.babelify(ctx),
            body: self.function.body.unwrap().babelify(ctx),
            generator: Some(self.function.is_generator),
            is_async: Some(self.function.is_async),
            decorators: Some(self.function.decorators.babelify(ctx)),
            type_parameters: self.function.type_params.map(|t| t.babelify(ctx).into()),
            return_type: self
                .function
                .return_type
                .map(|t| Box::alloc().init(t.babelify(ctx).into())),
            computed: Default::default(),
        }
    }
}

impl Babelify for Constructor {
    type Output = BabelClassMethod;

    fn babelify(self, ctx: &Context) -> Self::Output {
        BabelClassMethod {
            base: ctx.base(self.span),
            kind: Some(ClassMethodKind::Constructor),
            key: self.key.babelify(ctx),
            params: self.params.babelify(ctx),
            body: self.body.unwrap().babelify(ctx),
            access: self.accessibility.map(|access| access.babelify(ctx)),
            accessibility: self.accessibility.map(|access| access.babelify(ctx)),
            optional: Some(self.is_optional),
            computed: Default::default(),
            is_static: Default::default(),
            generator: Default::default(),
            is_async: Default::default(),
            is_abstract: Default::default(),
            decorators: Default::default(),
            return_type: Default::default(),
            type_parameters: Default::default(),
        }
    }
}

impl Babelify for Decorator {
    type Output = BabelDecorator;

    fn babelify(self, ctx: &Context) -> Self::Output {
        BabelDecorator {
            base: ctx.base(self.span),
            expression: Box::alloc().init(self.expr.babelify(ctx).into()),
        }
    }
}

impl Babelify for MethodKind {
    type Output = ClassMethodKind;

    fn babelify(self, _ctx: &Context) -> Self::Output {
        match self {
            MethodKind::Method => ClassMethodKind::Method,
            MethodKind::Getter => ClassMethodKind::Get,
            MethodKind::Setter => ClassMethodKind::Set,
        }
    }
}

impl Babelify for StaticBlock {
    type Output = BabelStaticBlock;

    fn babelify(self, ctx: &Context) -> Self::Output {
        BabelStaticBlock {
            base: ctx.base(self.span),
            body: self.body.stmts.babelify(ctx),
        }
    }
}