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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use serde::{Deserialize, Serialize};
use serde_json::Value;
use swc_common::ast_serde;

use crate::{
    common::{
        Access, BaseNode, Decorator, Identifier, Param, PrivateName, SuperTypeParams,
        TypeAnnotOrNoop, TypeParamDeclOrNoop,
    },
    expr::{ClassExpression, Expression},
    flow::{ClassImplements, InterfaceExtends},
    object::ObjectKey,
    stmt::{BlockStatement, Statement},
    typescript::{TSDeclareMethod, TSExpressionWithTypeArguments, TSIndexSignature},
};

#[derive(Debug, Clone, PartialEq)]
#[ast_serde]
pub enum Class {
    #[tag("ClassExpression")]
    Expr(ClassExpression),
    #[tag("ClassDeclaration")]
    Decl(ClassDeclaration),
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ClassMethodKind {
    Get,
    Set,
    Method,
    Constructor,
}

#[derive(Debug, Clone, PartialEq)]
#[ast_serde("ClassMethod")]
pub struct ClassMethod {
    #[serde(flatten)]
    pub base: BaseNode,
    #[serde(default)]
    pub kind: Option<ClassMethodKind>,
    pub key: ObjectKey,
    #[serde(default)]
    pub params: Vec<Param>,
    pub body: BlockStatement,
    #[serde(default)]
    pub computed: Option<bool>,
    #[serde(default, rename = "static")]
    pub is_static: Option<bool>,
    #[serde(default)]
    pub generator: Option<bool>,
    #[serde(default, rename = "async")]
    pub is_async: Option<bool>,
    #[serde(default, rename = "abstract")]
    pub is_abstract: Option<bool>,
    #[serde(default)]
    pub access: Option<Access>,
    #[serde(default)]
    pub accessibility: Option<Access>,
    #[serde(default)]
    pub decorators: Option<Vec<Decorator>>,
    #[serde(default)]
    pub optional: Option<bool>,
    #[serde(default)]
    pub return_type: Option<Box<TypeAnnotOrNoop>>,
    #[serde(default)]
    pub type_parameters: Option<TypeParamDeclOrNoop>,
}

#[derive(Debug, Clone, PartialEq)]
#[ast_serde("ClassPrivateProperty")]
pub struct ClassPrivateProperty {
    #[serde(flatten)]
    pub base: BaseNode,
    pub key: PrivateName,
    #[serde(default)]
    pub value: Option<Box<Expression>>,
    #[serde(default)]
    pub decorators: Option<Vec<Decorator>>,
    #[serde(default, rename = "static")]
    pub static_any: Value,
    #[serde(default)]
    pub type_annotation: Option<Box<TypeAnnotOrNoop>>,
}

#[derive(Debug, Clone, PartialEq)]
#[ast_serde("ClassPrivateMethod")]
pub struct ClassPrivateMethod {
    #[serde(flatten)]
    pub base: BaseNode,
    #[serde(default)]
    pub kind: Option<ClassMethodKind>,
    pub key: PrivateName,
    #[serde(default)]
    pub params: Vec<Param>,
    pub body: BlockStatement,
    #[serde(default, rename = "static")]
    pub is_static: Option<bool>,
    #[serde(default, rename = "abstract")]
    pub is_abstract: Option<bool>,
    #[serde(default)]
    pub access: Option<Access>,
    #[serde(default)]
    pub accessibility: Option<Access>,
    #[serde(default, rename = "async")]
    pub is_async: Option<bool>,
    #[serde(default)]
    pub computed: Option<bool>,
    #[serde(default)]
    pub decorators: Option<Vec<Decorator>>,
    #[serde(default)]
    pub generator: Option<bool>,
    #[serde(default)]
    pub optional: Option<bool>,
    #[serde(default)]
    pub return_type: Option<Box<TypeAnnotOrNoop>>,
    #[serde(default)]
    pub type_parameters: Option<TypeParamDeclOrNoop>,
}

#[derive(Debug, Clone, PartialEq)]
#[ast_serde("ClassProperty")]
pub struct ClassProperty {
    #[serde(flatten)]
    pub base: BaseNode,
    pub key: ObjectKey,
    #[serde(default)]
    pub value: Option<Box<Expression>>,
    #[serde(default)]
    pub type_annotation: Option<Box<TypeAnnotOrNoop>>,
    #[serde(default)]
    pub decorators: Option<Vec<Decorator>>,
    #[serde(default)]
    pub computed: Option<bool>,
    #[serde(default, rename = "static")]
    pub is_static: Option<bool>,
    #[serde(default, rename = "abstract")]
    pub is_abstract: Option<bool>,
    #[serde(default)]
    pub accessibility: Option<Access>,
    #[serde(default)]
    pub declare: Option<bool>,
    #[serde(default)]
    pub definite: Option<bool>,
    #[serde(default)]
    pub optional: Option<bool>,
    #[serde(default)]
    pub readonly: Option<bool>,
}

#[derive(Debug, Clone, PartialEq)]
#[ast_serde("StaticBlock")]
pub struct StaticBlock {
    #[serde(flatten)]
    pub base: BaseNode,
    #[serde(default)]
    pub body: Vec<Statement>,
}

#[derive(Debug, Clone, PartialEq)]
#[ast_serde]
pub enum ClassBodyEl {
    #[tag("ClassMethod")]
    Method(ClassMethod),
    #[tag("ClassPrivateMethod")]
    PrivateMethod(ClassPrivateMethod),
    #[tag("ClassProperty")]
    Prop(ClassProperty),
    #[tag("ClassPrivateProperty")]
    PrivateProp(ClassPrivateProperty),
    #[tag("TSDeclareMethod")]
    TSMethod(TSDeclareMethod),
    #[tag("TSIndexSignature")]
    TSIndex(TSIndexSignature),
    #[tag("StaticBlock")]
    StaticBlock(StaticBlock),
}

#[derive(Debug, Clone, PartialEq)]
#[ast_serde("ClassBody")]
pub struct ClassBody {
    #[serde(flatten)]
    pub base: BaseNode,
    #[serde(default)]
    pub body: Vec<ClassBodyEl>,
}

#[derive(Debug, Clone, PartialEq)]
#[ast_serde]
pub enum ClassImpl {
    #[tag("TSExpressionWithTypeArguments")]
    TSExpr(TSExpressionWithTypeArguments),
    #[tag("ClassImplements")]
    Implements(ClassImplements),
}

impl From<TSExpressionWithTypeArguments> for ClassImpl {
    fn from(expr: TSExpressionWithTypeArguments) -> Self {
        ClassImpl::TSExpr(expr)
    }
}

#[derive(Debug, Clone, PartialEq)]
#[ast_serde("ClassDeclaration")]
pub struct ClassDeclaration {
    #[serde(flatten)]
    pub base: BaseNode,
    pub id: Identifier,
    #[serde(default)]
    pub super_class: Option<Box<Expression>>,
    pub body: ClassBody,
    #[serde(default)]
    pub decorators: Option<Vec<Decorator>>,
    #[serde(default, rename = "abstract")]
    pub is_abstract: Option<bool>,
    #[serde(default)]
    pub declare: Option<bool>,
    #[serde(default)]
    pub implements: Option<Vec<ClassImpl>>,
    #[serde(default)]
    pub mixins: Option<InterfaceExtends>,
    #[serde(default)]
    pub super_type_parameters: Option<SuperTypeParams>,
    #[serde(default)]
    pub type_parameters: Option<TypeParamDeclOrNoop>,
}

impl From<ClassExpression> for ClassDeclaration {
    fn from(expr: ClassExpression) -> Self {
        ClassDeclaration {
            base: expr.base,
            id: expr.id.unwrap(),
            super_class: expr.super_class.map(|s| Box::new(*s)),
            body: expr.body,
            decorators: expr.decorators,
            is_abstract: Default::default(),
            declare: Default::default(),
            implements: expr.implements,
            mixins: expr.mixins,
            super_type_parameters: expr.super_type_parameters,
            type_parameters: expr.type_parameters,
        }
    }
}