swc_estree_compat/babelify/
prop.rs

1use copyless::BoxHelper;
2use swc_common::Spanned;
3use swc_ecma_ast::{
4    AssignProp, ComputedPropName, GetterProp, KeyValueProp, MethodProp, Prop, PropName, SetterProp,
5};
6use swc_estree_ast::{
7    AssignmentPattern, AssignmentPatternLeft, Expression, FunctionExpression, ObjectKey,
8    ObjectMember, ObjectMethod, ObjectMethodKind, ObjectPropVal, ObjectProperty,
9};
10
11use crate::babelify::{Babelify, Context};
12
13impl Babelify for Prop {
14    type Output = ObjectMember;
15
16    fn babelify(self, ctx: &Context) -> Self::Output {
17        match self {
18            Prop::Shorthand(i) => {
19                let id = i.babelify(ctx);
20                ObjectMember::Prop(ObjectProperty {
21                    base: id.base.clone(),
22                    key: ObjectKey::Id(id.clone()),
23                    value: ObjectPropVal::Expr(Box::alloc().init(Expression::Id(id))),
24                    computed: Default::default(),
25                    shorthand: true,
26                    decorators: Default::default(),
27                })
28            }
29            Prop::KeyValue(k) => ObjectMember::Prop(k.babelify(ctx)),
30            Prop::Getter(g) => ObjectMember::Method(g.babelify(ctx)),
31            Prop::Setter(s) => ObjectMember::Method(s.babelify(ctx)),
32            Prop::Method(m) => ObjectMember::Method(m.babelify(ctx)),
33            _ => panic!(
34                "illegal conversion: Cannot convert {:?} to ObjectMember",
35                &self
36            ),
37        }
38    }
39}
40
41impl Babelify for KeyValueProp {
42    type Output = ObjectProperty;
43
44    fn babelify(self, ctx: &Context) -> Self::Output {
45        ObjectProperty {
46            base: ctx.base(self.span()),
47            key: self.key.babelify(ctx),
48            value: ObjectPropVal::Expr(Box::alloc().init(self.value.babelify(ctx).into())),
49            computed: Default::default(),
50            shorthand: Default::default(),
51            decorators: Default::default(),
52        }
53    }
54}
55
56// TODO(dwoznicki): What is AssignProp used for? Should it babelify into
57// AssignmentPattern or AssignmentExpression?
58impl Babelify for AssignProp {
59    type Output = AssignmentPattern;
60
61    fn babelify(self, ctx: &Context) -> Self::Output {
62        AssignmentPattern {
63            base: ctx.base(self.span()),
64            left: AssignmentPatternLeft::Id(self.key.babelify(ctx)),
65            right: Box::alloc().init(self.value.babelify(ctx).into()),
66            decorators: Default::default(),
67            type_annotation: Default::default(),
68        }
69    }
70}
71
72impl Babelify for GetterProp {
73    type Output = ObjectMethod;
74
75    fn babelify(self, ctx: &Context) -> Self::Output {
76        ObjectMethod {
77            base: ctx.base(self.span),
78            kind: ObjectMethodKind::Get,
79            key: self.key.babelify(ctx),
80            return_type: self
81                .type_ann
82                .map(|ann| Box::alloc().init(ann.babelify(ctx).into())),
83            body: self.body.unwrap().babelify(ctx),
84            params: Default::default(),
85            computed: Default::default(),
86            generator: Default::default(),
87            is_async: Default::default(),
88            decorator: Default::default(),
89            type_parameters: Default::default(),
90        }
91    }
92}
93
94impl Babelify for SetterProp {
95    type Output = ObjectMethod;
96
97    fn babelify(self, ctx: &Context) -> Self::Output {
98        ObjectMethod {
99            base: ctx.base(self.span),
100            kind: ObjectMethodKind::Set,
101            key: self.key.babelify(ctx),
102            params: vec![self.param.babelify(ctx).into()],
103            body: self.body.unwrap().babelify(ctx),
104            return_type: Default::default(),
105            computed: Default::default(),
106            generator: Default::default(),
107            is_async: Default::default(),
108            decorator: Default::default(),
109            type_parameters: Default::default(),
110        }
111    }
112}
113
114impl Babelify for MethodProp {
115    type Output = ObjectMethod;
116
117    fn babelify(self, ctx: &Context) -> Self::Output {
118        let func: FunctionExpression = self.function.babelify(ctx);
119        ObjectMethod {
120            base: func.base,
121            kind: ObjectMethodKind::Method,
122            key: self.key.babelify(ctx),
123            params: func.params,
124            body: func.body,
125            computed: Default::default(),
126            generator: func.generator,
127            is_async: func.is_async,
128            decorator: Default::default(),
129            return_type: func.return_type,
130            type_parameters: func.type_parameters,
131        }
132    }
133}
134
135impl Babelify for PropName {
136    type Output = ObjectKey;
137
138    fn babelify(self, ctx: &Context) -> Self::Output {
139        match self {
140            PropName::Ident(i) => ObjectKey::Id(i.babelify(ctx)),
141            PropName::Str(s) => ObjectKey::String(s.babelify(ctx)),
142            PropName::Num(n) => ObjectKey::Numeric(n.babelify(ctx)),
143            PropName::Computed(e) => ObjectKey::Expr(Box::alloc().init(e.babelify(ctx))),
144            _ => panic!(
145                "illegal conversion: Cannot convert {:?} to ObjectKey",
146                &self
147            ),
148        }
149    }
150}
151
152impl Babelify for ComputedPropName {
153    type Output = Expression;
154
155    fn babelify(self, ctx: &Context) -> Self::Output {
156        self.expr.babelify(ctx).into()
157    }
158}