1use is_macro::Is;
2use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, SyntaxContext, DUMMY_SP};
3
4use crate::{
5 expr::Expr,
6 function::{Function, ParamOrTsParamProp},
7 ident::PrivateName,
8 prop::PropName,
9 stmt::BlockStmt,
10 typescript::{
11 Accessibility, TsExprWithTypeArgs, TsIndexSignature, TsTypeAnn, TsTypeParamDecl,
12 TsTypeParamInstantiation,
13 },
14 BigInt, ComputedPropName, EmptyStmt, Id, Ident, IdentName, Number,
15};
16
17#[ast_node]
18#[derive(Eq, Hash, EqIgnoreSpan, Default)]
19#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
20#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
21pub struct Class {
22 pub span: Span,
23
24 pub ctxt: SyntaxContext,
25
26 #[cfg_attr(feature = "serde-impl", serde(default))]
27 pub decorators: Vec<Decorator>,
28
29 #[cfg_attr(feature = "serde-impl", serde(default))]
30 pub body: Vec<ClassMember>,
31
32 #[cfg_attr(feature = "serde-impl", serde(default))]
33 #[cfg_attr(
34 feature = "encoding-impl",
35 encoding(with = "cbor4ii::core::types::Maybe")
36 )]
37 pub super_class: Option<Box<Expr>>,
38
39 #[cfg_attr(feature = "serde-impl", serde(default))]
40 pub is_abstract: bool,
41
42 #[cfg_attr(feature = "serde-impl", serde(default))]
43 #[cfg_attr(
44 feature = "encoding-impl",
45 encoding(with = "cbor4ii::core::types::Maybe")
46 )]
47 pub type_params: Option<Box<TsTypeParamDecl>>,
48
49 #[cfg_attr(feature = "serde-impl", serde(default))]
50 #[cfg_attr(
51 feature = "encoding-impl",
52 encoding(with = "cbor4ii::core::types::Maybe")
53 )]
54 pub super_type_params: Option<Box<TsTypeParamInstantiation>>,
55
56 #[cfg_attr(feature = "serde-impl", serde(default))]
58 pub implements: Vec<TsExprWithTypeArgs>,
59}
60
61impl Take for Class {
62 fn dummy() -> Self {
63 Class {
64 ..Default::default()
65 }
66 }
67}
68
69#[ast_node]
70#[derive(Eq, Hash, Is, EqIgnoreSpan)]
71#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
72#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
73pub enum ClassMember {
74 #[tag("Constructor")]
75 Constructor(Constructor),
76 #[tag("ClassMethod")]
78 Method(ClassMethod),
79 #[tag("PrivateMethod")]
80 PrivateMethod(PrivateMethod),
81 #[tag("ClassProperty")]
83 ClassProp(ClassProp),
84 #[tag("PrivateProperty")]
85 PrivateProp(PrivateProp),
86 #[tag("TsIndexSignature")]
87 TsIndexSignature(TsIndexSignature),
88 #[tag("EmptyStatement")]
89 Empty(EmptyStmt),
90
91 #[tag("StaticBlock")]
93 StaticBlock(StaticBlock),
94
95 #[tag("AutoAccessor")]
97 AutoAccessor(AutoAccessor),
98}
99
100impl Take for ClassMember {
101 fn dummy() -> Self {
102 ClassMember::Empty(EmptyStmt { span: DUMMY_SP })
103 }
104}
105
106#[ast_node("ClassProperty")]
107#[derive(Eq, Hash, EqIgnoreSpan, Default)]
108#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
109#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
110pub struct ClassProp {
111 #[cfg_attr(feature = "serde-impl", serde(default))]
112 pub span: Span,
113
114 pub key: PropName,
115
116 #[cfg_attr(feature = "serde-impl", serde(default))]
117 #[cfg_attr(
118 feature = "encoding-impl",
119 encoding(with = "cbor4ii::core::types::Maybe")
120 )]
121 pub value: Option<Box<Expr>>,
122
123 #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))]
124 #[cfg_attr(
125 feature = "encoding-impl",
126 encoding(with = "cbor4ii::core::types::Maybe")
127 )]
128 pub type_ann: Option<Box<TsTypeAnn>>,
129
130 #[cfg_attr(feature = "serde-impl", serde(default))]
131 pub is_static: bool,
132
133 #[cfg_attr(feature = "serde-impl", serde(default))]
134 pub decorators: Vec<Decorator>,
135
136 #[cfg_attr(feature = "serde-impl", serde(default))]
138 #[cfg_attr(
139 feature = "encoding-impl",
140 encoding(with = "cbor4ii::core::types::Maybe")
141 )]
142 pub accessibility: Option<Accessibility>,
143
144 #[cfg_attr(feature = "serde-impl", serde(default))]
146 pub is_abstract: bool,
147
148 #[cfg_attr(feature = "serde-impl", serde(default))]
149 pub is_optional: bool,
150
151 #[cfg_attr(feature = "serde-impl", serde(default))]
152 pub is_override: bool,
153
154 #[cfg_attr(feature = "serde-impl", serde(default))]
155 pub readonly: bool,
156
157 #[cfg_attr(feature = "serde-impl", serde(default))]
158 pub declare: bool,
159
160 #[cfg_attr(feature = "serde-impl", serde(default))]
161 pub definite: bool,
162}
163
164#[ast_node("PrivateProperty")]
165#[derive(Eq, Hash, EqIgnoreSpan, Default)]
166#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
167#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
168pub struct PrivateProp {
169 #[cfg_attr(feature = "serde-impl", serde(default))]
170 pub span: Span,
171
172 #[cfg_attr(feature = "serde-impl", serde(default))]
173 pub ctxt: SyntaxContext,
174
175 pub key: PrivateName,
176
177 #[cfg_attr(feature = "serde-impl", serde(default))]
178 #[cfg_attr(
179 feature = "encoding-impl",
180 encoding(with = "cbor4ii::core::types::Maybe")
181 )]
182 pub value: Option<Box<Expr>>,
183
184 #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))]
185 #[cfg_attr(
186 feature = "encoding-impl",
187 encoding(with = "cbor4ii::core::types::Maybe")
188 )]
189 pub type_ann: Option<Box<TsTypeAnn>>,
190
191 #[cfg_attr(feature = "serde-impl", serde(default))]
192 pub is_static: bool,
193
194 #[cfg_attr(feature = "serde-impl", serde(default))]
195 pub decorators: Vec<Decorator>,
196
197 #[cfg_attr(feature = "serde-impl", serde(default))]
199 #[cfg_attr(
200 feature = "encoding-impl",
201 encoding(with = "cbor4ii::core::types::Maybe")
202 )]
203 pub accessibility: Option<Accessibility>,
204
205 #[cfg_attr(feature = "serde-impl", serde(default))]
206 pub is_optional: bool,
207
208 #[cfg_attr(feature = "serde-impl", serde(default))]
209 pub is_override: bool,
210
211 #[cfg_attr(feature = "serde-impl", serde(default))]
212 pub readonly: bool,
213
214 #[cfg_attr(feature = "serde-impl", serde(default))]
215 pub definite: bool,
216}
217
218#[ast_node("ClassMethod")]
219#[derive(Eq, Hash, EqIgnoreSpan, Default)]
220#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
221#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
222pub struct ClassMethod {
223 #[cfg_attr(feature = "serde-impl", serde(default))]
224 pub span: Span,
225 pub key: PropName,
226 pub function: Box<Function>,
227 pub kind: MethodKind,
228 #[cfg_attr(feature = "serde-impl", serde(default))]
229 pub is_static: bool,
230 #[doc = r" Typescript extension."]
231 #[cfg_attr(feature = "serde-impl", serde(default))]
232 #[cfg_attr(
233 feature = "encoding-impl",
234 encoding(with = "cbor4ii::core::types::Maybe")
235 )]
236 pub accessibility: Option<Accessibility>,
237 #[doc = r" Typescript extension."]
238 #[cfg_attr(feature = "serde-impl", serde(default))]
239 pub is_abstract: bool,
240 #[cfg_attr(feature = "serde-impl", serde(default))]
241 pub is_optional: bool,
242 #[cfg_attr(feature = "serde-impl", serde(default))]
243 pub is_override: bool,
244}
245
246#[ast_node("PrivateMethod")]
247#[derive(Eq, Hash, EqIgnoreSpan, Default)]
248#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
249#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
250pub struct PrivateMethod {
251 #[cfg_attr(feature = "serde-impl", serde(default))]
252 pub span: Span,
253 pub key: PrivateName,
254 pub function: Box<Function>,
255 pub kind: MethodKind,
256 #[cfg_attr(feature = "serde-impl", serde(default))]
257 pub is_static: bool,
258 #[doc = r" Typescript extension."]
259 #[cfg_attr(feature = "serde-impl", serde(default))]
260 #[cfg_attr(
261 feature = "encoding-impl",
262 encoding(with = "cbor4ii::core::types::Maybe")
263 )]
264 pub accessibility: Option<Accessibility>,
265 #[doc = r" Typescript extension."]
266 #[cfg_attr(feature = "serde-impl", serde(default))]
267 pub is_abstract: bool,
268 #[cfg_attr(feature = "serde-impl", serde(default))]
269 pub is_optional: bool,
270 #[cfg_attr(feature = "serde-impl", serde(default))]
271 pub is_override: bool,
272}
273
274#[ast_node("Constructor")]
275#[derive(Eq, Hash, EqIgnoreSpan, Default)]
276#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
277#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
278pub struct Constructor {
279 pub span: Span,
280
281 pub ctxt: SyntaxContext,
282
283 pub key: PropName,
284
285 pub params: Vec<ParamOrTsParamProp>,
286
287 #[cfg_attr(feature = "serde-impl", serde(default))]
288 #[cfg_attr(
289 feature = "encoding-impl",
290 encoding(with = "cbor4ii::core::types::Maybe")
291 )]
292 pub body: Option<BlockStmt>,
293
294 #[cfg_attr(feature = "serde-impl", serde(default))]
295 #[cfg_attr(
296 feature = "encoding-impl",
297 encoding(with = "cbor4ii::core::types::Maybe")
298 )]
299 pub accessibility: Option<Accessibility>,
300
301 #[cfg_attr(feature = "serde-impl", serde(default))]
302 pub is_optional: bool,
303}
304
305#[ast_node("Decorator")]
306#[derive(Eq, Hash, EqIgnoreSpan, Default)]
307#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
308#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
309pub struct Decorator {
310 pub span: Span,
311
312 #[cfg_attr(feature = "serde-impl", serde(rename = "expression"))]
313 pub expr: Box<Expr>,
314}
315
316#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EqIgnoreSpan, Default)]
317#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
318#[cfg_attr(
319 any(feature = "rkyv-impl"),
320 derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
321)]
322#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))]
323#[cfg_attr(feature = "rkyv-impl", repr(u32))]
324#[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))]
325#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
326#[cfg_attr(
327 feature = "encoding-impl",
328 derive(::swc_common::Encode, ::swc_common::Decode)
329)]
330#[cfg_attr(swc_ast_unknown, non_exhaustive)]
331pub enum MethodKind {
332 #[default]
333 #[cfg_attr(feature = "serde-impl", serde(rename = "method"))]
334 Method,
335 #[cfg_attr(feature = "serde-impl", serde(rename = "getter"))]
336 Getter,
337 #[cfg_attr(feature = "serde-impl", serde(rename = "setter"))]
338 Setter,
339}
340
341#[ast_node("StaticBlock")]
342#[derive(Eq, Hash, EqIgnoreSpan, Default)]
343#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
344#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
345pub struct StaticBlock {
346 pub span: Span,
347 pub body: BlockStmt,
348}
349
350impl Take for StaticBlock {
351 fn dummy() -> Self {
352 StaticBlock {
353 span: DUMMY_SP,
354 body: Take::dummy(),
355 }
356 }
357}
358
359#[ast_node]
361#[derive(Is, Eq, Hash, EqIgnoreSpan)]
362#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
363#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
364pub enum Key {
365 #[tag("PrivateName")]
366 Private(PrivateName),
367 #[tag("Identifier")]
368 #[tag("StringLiteral")]
369 #[tag("NumericLiteral")]
370 #[tag("Computed")]
371 #[tag("BigIntLiteral")]
372 Public(PropName),
373}
374
375bridge_from!(Key, IdentName, Ident);
376bridge_from!(Key, PropName, IdentName);
377bridge_from!(Key, PropName, Id);
378bridge_from!(Key, PropName, Number);
379bridge_from!(Key, PropName, ComputedPropName);
380bridge_from!(Key, PropName, BigInt);
381
382impl Take for Key {
383 fn dummy() -> Self {
384 Default::default()
385 }
386}
387
388impl Default for Key {
389 fn default() -> Self {
390 Key::Public(Default::default())
391 }
392}
393
394#[ast_node("AutoAccessor")]
395#[derive(Eq, Hash, EqIgnoreSpan, Default)]
396#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
397#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
398pub struct AutoAccessor {
399 #[cfg_attr(feature = "serde-impl", serde(default))]
400 pub span: Span,
401
402 pub key: Key,
403
404 #[cfg_attr(feature = "serde-impl", serde(default))]
405 #[cfg_attr(
406 feature = "encoding-impl",
407 encoding(with = "cbor4ii::core::types::Maybe")
408 )]
409 pub value: Option<Box<Expr>>,
410
411 #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))]
412 #[cfg_attr(
413 feature = "encoding-impl",
414 encoding(with = "cbor4ii::core::types::Maybe")
415 )]
416 pub type_ann: Option<Box<TsTypeAnn>>,
417
418 #[cfg_attr(feature = "serde-impl", serde(default))]
419 pub is_static: bool,
420
421 #[cfg_attr(feature = "serde-impl", serde(default))]
422 pub decorators: Vec<Decorator>,
423
424 #[cfg_attr(feature = "serde-impl", serde(default))]
426 #[cfg_attr(
427 feature = "encoding-impl",
428 encoding(with = "cbor4ii::core::types::Maybe")
429 )]
430 pub accessibility: Option<Accessibility>,
431
432 #[cfg_attr(feature = "serde-impl", serde(default))]
433 pub is_abstract: bool,
434
435 #[cfg_attr(feature = "serde-impl", serde(default))]
436 pub is_override: bool,
437
438 #[cfg_attr(feature = "serde-impl", serde(default))]
439 pub definite: bool,
440}
441
442impl Take for AutoAccessor {
443 fn dummy() -> AutoAccessor {
444 AutoAccessor {
445 span: Take::dummy(),
446 key: Take::dummy(),
447 value: Take::dummy(),
448 type_ann: None,
449 is_static: false,
450 decorators: Take::dummy(),
451 accessibility: None,
452 is_abstract: false,
453 is_override: false,
454 definite: false,
455 }
456 }
457}