Skip to main content

swc_ecma_ast/
class.rs

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, FunctionBody, 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    /// Typescript extension.
57    #[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    /// `es2015`
77    #[tag("ClassMethod")]
78    Method(ClassMethod),
79    #[tag("PrivateMethod")]
80    PrivateMethod(PrivateMethod),
81    /// stage 0 / Typescript
82    #[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    /// Stage 3
92    #[tag("StaticBlock")]
93    StaticBlock(StaticBlock),
94
95    /// Stage 3
96    #[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    /// Typescript extension.
137    #[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    /// Typescript extension.
145    #[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    /// Typescript extension.
198    #[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<FunctionBody>,
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(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))]
319#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
320#[cfg_attr(
321    feature = "encoding-impl",
322    derive(::swc_common::Encode, ::swc_common::Decode)
323)]
324#[cfg_attr(swc_ast_unknown, non_exhaustive)]
325pub enum MethodKind {
326    #[default]
327    #[cfg_attr(feature = "serde-impl", serde(rename = "method"))]
328    Method,
329    #[cfg_attr(feature = "serde-impl", serde(rename = "getter"))]
330    Getter,
331    #[cfg_attr(feature = "serde-impl", serde(rename = "setter"))]
332    Setter,
333}
334
335#[ast_node("StaticBlock")]
336#[derive(Eq, Hash, EqIgnoreSpan, Default)]
337#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
338#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
339pub struct StaticBlock {
340    pub span: Span,
341    pub body: BlockStmt,
342}
343
344impl Take for StaticBlock {
345    fn dummy() -> Self {
346        StaticBlock {
347            span: DUMMY_SP,
348            body: Take::dummy(),
349        }
350    }
351}
352
353/// Either a private name or a public name.
354#[ast_node]
355#[derive(Is, Eq, Hash, EqIgnoreSpan)]
356#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
357#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
358pub enum Key {
359    #[tag("PrivateName")]
360    Private(PrivateName),
361    #[tag("Identifier")]
362    #[tag("StringLiteral")]
363    #[tag("NumericLiteral")]
364    #[tag("Computed")]
365    #[tag("BigIntLiteral")]
366    Public(PropName),
367}
368
369bridge_from!(Key, IdentName, Ident);
370bridge_from!(Key, PropName, IdentName);
371bridge_from!(Key, PropName, Id);
372bridge_from!(Key, PropName, Number);
373bridge_from!(Key, PropName, ComputedPropName);
374bridge_from!(Key, PropName, BigInt);
375
376impl Take for Key {
377    fn dummy() -> Self {
378        Default::default()
379    }
380}
381
382impl Default for Key {
383    fn default() -> Self {
384        Key::Public(Default::default())
385    }
386}
387
388#[ast_node("AutoAccessor")]
389#[derive(Eq, Hash, EqIgnoreSpan, Default)]
390#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
391#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
392pub struct AutoAccessor {
393    #[cfg_attr(feature = "serde-impl", serde(default))]
394    pub span: Span,
395
396    pub key: Key,
397
398    #[cfg_attr(feature = "serde-impl", serde(default))]
399    #[cfg_attr(
400        feature = "encoding-impl",
401        encoding(with = "cbor4ii::core::types::Maybe")
402    )]
403    pub value: Option<Box<Expr>>,
404
405    #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))]
406    #[cfg_attr(
407        feature = "encoding-impl",
408        encoding(with = "cbor4ii::core::types::Maybe")
409    )]
410    pub type_ann: Option<Box<TsTypeAnn>>,
411
412    #[cfg_attr(feature = "serde-impl", serde(default))]
413    pub is_static: bool,
414
415    #[cfg_attr(feature = "serde-impl", serde(default))]
416    pub decorators: Vec<Decorator>,
417
418    /// Typescript extension.
419    #[cfg_attr(feature = "serde-impl", serde(default))]
420    #[cfg_attr(
421        feature = "encoding-impl",
422        encoding(with = "cbor4ii::core::types::Maybe")
423    )]
424    pub accessibility: Option<Accessibility>,
425
426    #[cfg_attr(feature = "serde-impl", serde(default))]
427    pub is_abstract: bool,
428
429    #[cfg_attr(feature = "serde-impl", serde(default))]
430    pub is_override: bool,
431
432    #[cfg_attr(feature = "serde-impl", serde(default))]
433    pub definite: bool,
434}
435
436impl Take for AutoAccessor {
437    fn dummy() -> AutoAccessor {
438        AutoAccessor {
439            span: Take::dummy(),
440            key: Take::dummy(),
441            value: Take::dummy(),
442            type_ann: None,
443            is_static: false,
444            decorators: Take::dummy(),
445            accessibility: None,
446            is_abstract: false,
447            is_override: false,
448            definite: false,
449        }
450    }
451}