swc_estree_compat/swcify/
class.rs1use swc_ecma_ast::{
2 ClassMember, Expr, Function, MemberExpr, MemberProp, MethodKind, ParamOrTsParamProp,
3 TsExprWithTypeArgs,
4};
5use swc_estree_ast::{
6 ClassBody, ClassBodyEl, ClassImpl, ClassMethodKind, TSEntityName,
7 TSExpressionWithTypeArguments, TSQualifiedName,
8};
9
10use super::Context;
11use crate::swcify::Swcify;
12
13impl Swcify for ClassBody {
14 type Output = Vec<ClassMember>;
15
16 fn swcify(self, ctx: &Context) -> Self::Output {
17 self.body.swcify(ctx)
18 }
19}
20
21impl Swcify for ClassBodyEl {
22 type Output = ClassMember;
23
24 fn swcify(self, ctx: &Context) -> Self::Output {
25 match self {
26 ClassBodyEl::Method(v) => v.swcify(ctx),
27 ClassBodyEl::PrivateMethod(v) => v.swcify(ctx).into(),
28 ClassBodyEl::Prop(v) => v.swcify(ctx).into(),
29 ClassBodyEl::PrivateProp(v) => v.swcify(ctx).into(),
30 _ => {
31 unimplemented!("swcify: {:?}", self)
32 }
33 }
34 }
35}
36
37impl Swcify for swc_estree_ast::ClassMethod {
38 type Output = swc_ecma_ast::ClassMember;
39
40 fn swcify(self, ctx: &Context) -> Self::Output {
41 match self.kind.unwrap_or(ClassMethodKind::Method) {
42 ClassMethodKind::Get | ClassMethodKind::Set | ClassMethodKind::Method => {
43 swc_ecma_ast::ClassMethod {
44 span: ctx.span(&self.base),
45 key: self.key.swcify(ctx),
46 function: Function {
47 params: self.params.swcify(ctx),
48 decorators: self.decorators.swcify(ctx).unwrap_or_default(),
49 span: ctx.span(&self.base),
50 body: Some(self.body.swcify(ctx)),
51 is_generator: self.generator.unwrap_or_default(),
52 is_async: self.is_async.unwrap_or_default(),
53 type_params: self.type_parameters.swcify(ctx).flatten().map(Box::new),
54 return_type: self.return_type.swcify(ctx).flatten().map(Box::new),
55 ..Default::default()
56 }
57 .into(),
58 kind: self
59 .kind
60 .map(|kind| match kind {
61 ClassMethodKind::Get => MethodKind::Getter,
62 ClassMethodKind::Set => MethodKind::Setter,
63 ClassMethodKind::Method => MethodKind::Getter,
64 ClassMethodKind::Constructor => {
65 unreachable!()
66 }
67 })
68 .unwrap_or(MethodKind::Method),
69 is_static: self.is_static.unwrap_or_default(),
70 accessibility: self.accessibility.swcify(ctx),
71 is_abstract: self.is_abstract.unwrap_or_default(),
72 is_optional: self.optional.unwrap_or_default(),
73 is_override: false,
74 }
75 .into()
76 }
77 ClassMethodKind::Constructor => swc_ecma_ast::Constructor {
78 span: ctx.span(&self.base),
79 key: self.key.swcify(ctx),
80 params: self
81 .params
82 .into_iter()
83 .map(|v| v.swcify(ctx))
84 .map(ParamOrTsParamProp::Param)
85 .collect(),
86 body: Some(self.body.swcify(ctx)),
87 accessibility: self.accessibility.swcify(ctx),
88 is_optional: self.optional.unwrap_or_default(),
89 ..Default::default()
90 }
91 .into(),
92 }
93 }
94}
95
96impl Swcify for swc_estree_ast::ClassPrivateMethod {
97 type Output = swc_ecma_ast::PrivateMethod;
98
99 fn swcify(self, ctx: &Context) -> Self::Output {
100 swc_ecma_ast::PrivateMethod {
101 span: ctx.span(&self.base),
102 key: self.key.swcify(ctx),
103 function: Function {
104 params: self.params.swcify(ctx),
105 decorators: self.decorators.swcify(ctx).unwrap_or_default(),
106 span: ctx.span(&self.base),
107 body: Some(self.body.swcify(ctx)),
108 is_generator: self.generator.unwrap_or_default(),
109 is_async: self.is_async.unwrap_or_default(),
110 type_params: self.type_parameters.swcify(ctx).flatten().map(Box::new),
111 return_type: self.return_type.swcify(ctx).flatten().map(Box::new),
112 ..Default::default()
113 }
114 .into(),
115 kind: match self.kind.unwrap_or(ClassMethodKind::Method) {
116 ClassMethodKind::Get => MethodKind::Getter,
117 ClassMethodKind::Set => MethodKind::Setter,
118 ClassMethodKind::Method => MethodKind::Getter,
119 ClassMethodKind::Constructor => {
120 unreachable!()
121 }
122 },
123 is_static: self.is_static.unwrap_or_default(),
124 accessibility: self.accessibility.swcify(ctx),
125 is_abstract: self.is_abstract.unwrap_or_default(),
126 is_optional: self.optional.unwrap_or_default(),
127 is_override: false,
128 }
129 }
130}
131
132impl Swcify for swc_estree_ast::ClassProperty {
133 type Output = swc_ecma_ast::ClassProp;
134
135 fn swcify(self, ctx: &Context) -> Self::Output {
136 let key = self.key.swcify(ctx);
137
138 swc_ecma_ast::ClassProp {
139 span: ctx.span(&self.base),
140 key,
141 value: self.value.swcify(ctx),
142 type_ann: self.type_annotation.swcify(ctx).flatten().map(Box::new),
143 is_static: self.is_static.unwrap_or(false),
144 decorators: self.decorators.swcify(ctx).unwrap_or_default(),
145 accessibility: self.accessibility.swcify(ctx),
146 is_abstract: self.is_abstract.unwrap_or_default(),
147 is_optional: self.optional.unwrap_or_default(),
148 is_override: false,
149 readonly: self.readonly.unwrap_or_default(),
150 declare: self.declare.unwrap_or_default(),
151 definite: self.definite.unwrap_or_default(),
152 }
153 }
154}
155
156impl Swcify for swc_estree_ast::ClassPrivateProperty {
157 type Output = swc_ecma_ast::PrivateProp;
158
159 fn swcify(self, ctx: &Context) -> Self::Output {
160 swc_ecma_ast::PrivateProp {
161 span: ctx.span(&self.base),
162 key: self.key.swcify(ctx),
163 value: self.value.swcify(ctx),
164 type_ann: self.type_annotation.swcify(ctx).flatten().map(Box::new),
165 is_static: false,
166 decorators: Default::default(),
167 accessibility: Default::default(),
168 is_optional: false,
169 is_override: false,
170 readonly: false,
171 definite: false,
172 ctxt: Default::default(),
173 }
174 }
175}
176
177impl Swcify for ClassImpl {
178 type Output = TsExprWithTypeArgs;
179
180 fn swcify(self, ctx: &Context) -> Self::Output {
181 match self {
182 ClassImpl::TSExpr(v) => v.swcify(ctx),
183 ClassImpl::Implements(_) => {
184 unreachable!()
185 }
186 }
187 }
188}
189
190impl Swcify for TSExpressionWithTypeArguments {
191 type Output = TsExprWithTypeArgs;
192
193 fn swcify(self, ctx: &Context) -> Self::Output {
194 fn swcify_expr(expr: TSEntityName, ctx: &Context) -> Box<Expr> {
199 match expr {
200 TSEntityName::Id(v) => v.swcify(ctx).into(),
201 TSEntityName::Qualified(v) => swcify_qualified_name(v, ctx),
202 }
203 }
204 fn swcify_qualified_name(qualified_name: TSQualifiedName, ctx: &Context) -> Box<Expr> {
205 MemberExpr {
206 obj: swcify_expr(*qualified_name.left, ctx),
207 prop: MemberProp::Ident(qualified_name.right.swcify(ctx).into()),
208 span: ctx.span(&qualified_name.base),
209 }
210 .into()
211 }
212
213 TsExprWithTypeArgs {
214 span: ctx.span(&self.base),
215 expr: swcify_expr(self.expression, ctx),
216 type_args: self.type_parameters.swcify(ctx).map(Box::new),
217 }
218 }
219}