1use serde::{Deserialize, Serialize};
2use swc_atoms::Atom;
3use swc_common::ast_serde;
4
5use crate::{
6 class::*, comment::Comment, decl::*, expr::*, flow::*, jsx::*, lit::*, module::*, object::*,
7 pat::*, stmt::*, typescript::*,
8};
9
10#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
11#[serde(rename_all = "camelCase")]
12pub struct LineCol {
13 pub line: usize,
14 pub column: usize,
15}
16
17impl LineCol {
18 pub fn dummy() -> Self {
19 LineCol { line: 0, column: 0 }
20 }
21}
22
23#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
24#[serde(rename_all = "camelCase")]
25pub struct Loc {
26 pub start: LineCol,
27 pub end: LineCol,
28}
29
30impl Loc {
31 pub fn dummy() -> Self {
32 Loc {
33 start: LineCol::dummy(),
34 end: LineCol::dummy(),
35 }
36 }
37}
38
39#[derive(Debug, Clone, PartialEq, Eq)]
40#[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))]
41#[cfg_attr(feature = "serde-impl", serde(rename_all = "camelCase"))]
42pub struct BaseNode {
43 #[serde(default, skip_serializing_if = "Vec::is_empty")]
44 pub leading_comments: Vec<Comment>,
45 #[serde(default, skip_serializing_if = "Vec::is_empty")]
46 pub inner_comments: Vec<Comment>,
47 #[serde(default, skip_serializing_if = "Vec::is_empty")]
48 pub trailing_comments: Vec<Comment>,
49
50 #[serde(default)]
51 pub start: Option<u32>,
52 #[serde(default)]
53 pub end: Option<u32>,
54
55 #[serde(default, skip_serializing_if = "crate::flavor::Flavor::skip_range")]
56 pub range: Option<[u32; 2]>,
57
58 #[serde(default)]
59 pub loc: Option<Loc>,
60}
61
62#[derive(Debug, Clone, PartialEq)]
63#[ast_serde]
64pub enum Binary {
65 #[tag("BinaryExpression")]
66 BinaryExpr(BinaryExpression),
67 #[tag("LogicalExpression")]
68 LogicalExpr(LogicalExpression),
69}
70
71#[derive(Debug, Clone, PartialEq)]
72#[ast_serde]
73pub enum Conditional {
74 #[tag("ConditionalExpression")]
75 Expr(ConditionalExpression),
76 #[tag("IfStatement")]
77 If(IfStatement),
78}
79
80#[derive(Debug, Clone, PartialEq)]
81#[ast_serde]
82pub enum Function {
83 #[tag("FunctionDeclaration")]
84 Decl(FunctionDeclaration),
85 #[tag("FunctionExpression")]
86 Expr(FunctionExpression),
87 #[tag("ObjectMethod")]
88 ObjectMethod(ObjectMethod),
89 #[tag("ArrowFunctionExpression")]
90 Arrow(ArrowFunctionExpression),
91 #[tag("ClassMethod")]
92 ClassMethod(ClassMethod),
93 #[tag("ClassPrivateMethod")]
94 ClassPrivateMethod(ClassPrivateMethod),
95}
96
97#[derive(Debug, Clone, PartialEq)]
98#[ast_serde]
99pub enum FunctionParent {
100 #[tag("FunctionDeclaration")]
101 Decl(FunctionDeclaration),
102 #[tag("FunctionExpression")]
103 Expr(FunctionExpression),
104 #[tag("ObjectMethod")]
105 ObjectMethod(ObjectMethod),
106 #[tag("ArrowFunctionExpression")]
107 Arrow(ArrowFunctionExpression),
108 #[tag("ClassMethod")]
109 ClassMethod(ClassMethod),
110 #[tag("ClassPrivateMethod")]
111 ClassPrivateMethod(ClassPrivateMethod),
112}
113
114#[derive(Debug, Clone, PartialEq)]
115#[ast_serde]
116pub enum Immutable {
117 #[tag("StringLiteral")]
118 #[tag("DecimalLiteral")]
119 #[tag("NumericLiteral")]
120 #[tag("NullLiteral")]
121 #[tag("BooleanLiteral")]
122 #[tag("BigIntLiteral")]
123 Literal(Literal),
124 #[tag("JSXAttribute")]
125 JSXAttribute(JSXAttribute),
126 #[tag("JSXClosingElement")]
127 JSXClosingElement(JSXClosingElement),
128 #[tag("JSXElement")]
129 JSXElement(JSXElement),
130 #[tag("JSXExpressionContainer")]
131 JSXExpressionContainer(JSXExpressionContainer),
132 #[tag("JSXSpreadChild")]
133 JSXSpreadChild(JSXSpreadChild),
134 #[tag("JSXOpeningElement")]
135 JSXOpeningElement(JSXOpeningElement),
136 #[tag("JSXText")]
137 JSXText(JSXText),
138 #[tag("JSXFragment")]
139 JSXFragment(JSXFragment),
140 #[tag("JSXOpeningFragment")]
141 JSXOpeningFragment(JSXOpeningFragment),
142 #[tag("JSXClosingFragment")]
143 JSXClosingFragment(JSXClosingFragment),
144}
145
146#[derive(Debug, Clone, PartialEq)]
147#[ast_serde]
148pub enum Method {
149 #[tag("ObjectMethod")]
150 Object(ObjectMethod),
151 #[tag("ClassMethod")]
152 Class(ClassMethod),
153 #[tag("ClassPrivateMethod")]
154 ClassPrivate(ClassPrivateMethod),
155}
156
157#[derive(Debug, Clone, PartialEq)]
158#[ast_serde]
159pub enum Private {
160 #[tag("ClassPrivateProperty")]
161 ClassProp(ClassPrivateProperty),
162 #[tag("ClassPrivateMethod")]
163 ClassMethod(ClassPrivateMethod),
164 #[tag("PrivateName")]
165 Name(PrivateName),
166}
167
168#[derive(Debug, Clone, PartialEq)]
169#[ast_serde]
170pub enum Property {
171 #[tag("ObjectProperty")]
172 ObjectProp(ObjectProperty),
173 #[tag("ClassProperty")]
174 ClassProp(ClassProperty),
175 #[tag("ClassPrivateProperty")]
176 ClassPrivateProp(ClassPrivateProperty),
177}
178
179#[derive(Debug, Clone, PartialEq)]
180#[ast_serde]
181pub enum Pureish {
182 #[tag("FunctionDeclaration")]
183 FunctionDecl(FunctionDeclaration),
184 #[tag("FunctionExpression")]
185 FunctionExpr(FunctionExpression),
186 #[tag("StringLiteral")]
187 #[tag("NumericLiteral")]
188 #[tag("NullLiteral")]
189 #[tag("BooleanLiteral")]
190 #[tag("RegExpLiteral")]
191 #[tag("BigIntLiteral")]
192 #[tag("DecimalLiteral")]
193 Literal(Literal),
194 #[tag("ArrowFunctionExpression")]
195 ArrowFuncExpr(ArrowFunctionExpression),
196}
197
198#[derive(Debug, Clone, PartialEq)]
199#[ast_serde]
200pub enum Scopable {
201 #[tag("BlockStatement")]
202 BlockStmt(BlockStatement),
203 #[tag("CatchClause")]
204 CatchClause(CatchClause),
205 #[tag("DoWhileStatement")]
206 DoWhileStmt(DoWhileStatement),
207 #[tag("ForInStatement")]
208 ForInStmt(ForInStatement),
209 #[tag("ForStatement")]
210 ForStmt(ForStatement),
211 #[tag("FunctionDeclaration")]
212 FuncDecl(FunctionDeclaration),
213 #[tag("FunctionExpression")]
214 FuncExpr(FunctionExpression),
215 #[tag("Program")]
216 Program(Program),
217 #[tag("ObjectMethod")]
218 ObjectMethod(ObjectMethod),
219 #[tag("SwitchStatement")]
220 SwitchStmt(SwitchStatement),
221 #[tag("WhileStatement")]
222 WhileStmt(WhileStatement),
223 #[tag("ArrowFunctionExpression")]
224 ArrowFuncExpr(ArrowFunctionExpression),
225 #[tag("ClassExpression")]
226 ClassExpr(ClassExpression),
227 #[tag("ClassDeclaration")]
228 ClassDecl(ClassDeclaration),
229 #[tag("ForOfStatement")]
230 ForOfStmt(ForOfStatement),
231 #[tag("ClassMethod")]
232 ClassMethod(ClassMethod),
233 #[tag("ClassPrivateMethod")]
234 ClassPrivateMethod(ClassPrivateMethod),
235 #[tag("StaticBlock")]
236 StaticBlock(StaticBlock),
237 #[tag("TSModuleBlock")]
238 TSModuleBlock(TSModuleBlock),
239}
240
241#[derive(Debug, Clone, PartialEq)]
242#[ast_serde]
243pub enum BlockParent {
244 #[tag("BlockStatement")]
245 BlockStmt(BlockStatement),
246 #[tag("CatchClause")]
247 CatchClause(CatchClause),
248 #[tag("DoWhileStatement")]
249 DoWhileStmt(DoWhileStatement),
250 #[tag("ForInStatement")]
251 ForInStmt(ForInStatement),
252 #[tag("ForStatement")]
253 ForStmt(ForStatement),
254 #[tag("FunctionDeclaration")]
255 FuncDecl(FunctionDeclaration),
256 #[tag("FunctionExpression")]
257 FuncExpr(FunctionExpression),
258 #[tag("Program")]
259 Program(Program),
260 #[tag("ObjectMethod")]
261 ObjectMethod(ObjectMethod),
262 #[tag("SwitchStatement")]
263 SwitchStmt(SwitchStatement),
264 #[tag("WhileStatement")]
265 WhileStmt(WhileStatement),
266 #[tag("ArrowFunctionExpression")]
267 ArrowFuncExpr(ArrowFunctionExpression),
268 #[tag("ForOfStatement")]
269 ForOfStmt(ForOfStatement),
270 #[tag("ClassMethod")]
271 ClassMethod(ClassMethod),
272 #[tag("ClassPrivateMethod")]
273 ClassPrivateMethod(ClassPrivateMethod),
274 #[tag("StaticBlock")]
275 StaticBlock(StaticBlock),
276 #[tag("TSModuleBlock")]
277 TSModuleBlock(TSModuleBlock),
278}
279
280#[derive(Debug, Clone, PartialEq)]
281#[ast_serde]
282pub enum Block {
283 #[tag("BlockStatement")]
284 BlockStmt(BlockStatement),
285 #[tag("Program")]
286 Program(Program),
287 #[tag("TSModuleBlock")]
288 TSModuleBlock(TSModuleBlock),
289}
290
291#[derive(Debug, Clone, PartialEq)]
292#[ast_serde]
293pub enum Terminatorless {
294 #[tag("BreakStatement")]
295 Break(BreakStatement),
296 #[tag("ContinueStatement")]
297 Continue(ContinueStatement),
298 #[tag("ReturnStatement")]
299 Return(ReturnStatement),
300 #[tag("ThrowStatement")]
301 Throw(ThrowStatement),
302 #[tag("YieldExpression")]
303 Yield(YieldExpression),
304 #[tag("AwaitExpression")]
305 Await(AwaitExpression),
306}
307
308#[derive(Debug, Clone, PartialEq)]
309#[ast_serde]
310pub enum UnaryLike {
311 #[tag("UnaryExpression")]
312 Expr(UnaryExpression),
313 #[tag("SpreadElement")]
314 Spread(SpreadElement),
315}
316
317#[derive(Debug, Clone, PartialEq)]
318#[ast_serde("SpreadElement")]
319pub struct SpreadElement {
320 #[serde(flatten)]
321 pub base: BaseNode,
322 pub argument: Box<Expression>,
323}
324
325#[derive(Debug, Clone, PartialEq)]
327#[ast_serde("SpreadProperty")]
328pub struct SpreadProperty {
329 #[serde(flatten)]
330 pub base: BaseNode,
331 pub argument: Box<Expression>,
332}
333
334#[derive(Debug, Clone, PartialEq)]
335#[ast_serde("RestElement")]
336pub struct RestElement {
337 #[serde(flatten)]
338 pub base: BaseNode,
339 pub argument: Box<LVal>,
340 #[serde(default, skip_serializing_if = "crate::flavor::Flavor::skip_empty")]
341 pub decorators: Option<Vec<Decorator>>,
342 #[serde(default, skip_serializing_if = "crate::flavor::Flavor::skip_none")]
343 pub type_annotation: Option<Box<TypeAnnotOrNoop>>,
344}
345
346#[derive(Debug, Clone, PartialEq)]
348#[ast_serde("RestProperty")]
349pub struct RestProperty {
350 #[serde(flatten)]
351 pub base: BaseNode,
352 pub argument: LVal,
353 #[serde(default)]
354 pub decorators: Option<Vec<Decorator>>,
355 #[serde(default)]
356 pub type_annotation: Option<Box<TypeAnnotOrNoop>>,
357}
358
359#[derive(Debug, Clone, PartialEq)]
360#[ast_serde("Identifier")]
361pub struct Identifier {
362 #[serde(flatten)]
363 pub base: BaseNode,
364 #[serde(default)]
365 pub name: Atom,
366 #[serde(default, skip_serializing_if = "crate::flavor::Flavor::skip_empty")]
367 pub decorators: Option<Vec<Decorator>>,
368 #[serde(
369 default,
370 skip_serializing_if = "crate::flavor::Flavor::skip_none_and_false"
371 )]
372 pub optional: Option<bool>,
373 #[serde(default, skip_serializing_if = "crate::flavor::Flavor::skip_none")]
374 pub type_annotation: Option<Box<TypeAnnotOrNoop>>,
375}
376
377#[derive(Debug, Clone, PartialEq)]
378#[ast_serde]
379pub enum IdOrQualifiedId {
380 #[tag("Identifier")]
381 Id(Identifier),
382 #[tag("QualifiedTypeIdentifier")]
383 QualifiedId(QualifiedTypeIdentifier),
384}
385
386#[derive(Debug, Clone, PartialEq)]
387#[ast_serde]
388pub enum IdOrString {
389 #[tag("Identifier")]
390 Id(Identifier),
391 #[tag("StringLiteral")]
392 String(StringLiteral),
393}
394
395#[derive(Debug, Clone, PartialEq)]
396#[ast_serde]
397pub enum IdOrRest {
398 #[tag("Identifier")]
399 Id(Identifier),
400 #[tag("RestElement")]
401 Rest(RestElement),
402}
403
404#[derive(Debug, Clone, PartialEq)]
405#[ast_serde("Decorator")]
406pub struct Decorator {
407 #[serde(flatten)]
408 pub base: BaseNode,
409 pub expression: Box<Expression>,
410}
411
412#[derive(Debug, Clone, PartialEq, Eq)]
413#[ast_serde("Noop")]
414pub struct Noop {
415 #[serde(flatten)]
416 pub base: BaseNode,
417}
418
419#[derive(Debug, Clone, PartialEq)]
420#[ast_serde]
421pub enum Param {
422 #[tag("Identifier")]
423 Id(Identifier),
424 #[tag("AssignmentPattern")]
425 #[tag("ArrayPattern")]
426 #[tag("ObjectPattern")]
427 Pat(Pattern),
428 #[tag("RestElement")]
429 Rest(RestElement),
430 #[tag("TSParameterProperty")]
431 TSProp(TSParameterProperty),
432}
433
434#[derive(Debug, Clone, PartialEq)]
435#[ast_serde]
436pub enum LVal {
437 #[tag("Identifier")]
438 Id(Identifier),
439 #[tag("MemberExpression")]
440 MemberExpr(MemberExpression),
441 #[tag("RestElement")]
442 RestEl(RestElement),
443 #[tag("AssignmentPattern")]
444 AssignmentPat(AssignmentPattern),
445 #[tag("ArrayPattern")]
446 ArrayPat(ArrayPattern),
447 #[tag("ObjectPattern")]
448 ObjectPat(ObjectPattern),
449 #[tag("TSParameterProperty")]
450 TSParamProp(TSParameterProperty),
451}
452
453#[derive(Debug, Clone, PartialEq)]
454#[ast_serde]
455pub enum PatternLike {
456 #[tag("Identifier")]
457 Id(Identifier),
458 #[tag("RestElement")]
459 RestEl(RestElement),
460 #[tag("AssignmentPattern")]
461 AssignmentPat(AssignmentPattern),
462 #[tag("ArrayPattern")]
463 ArrayPat(ArrayPattern),
464 #[tag("ObjectPattern")]
465 ObjectPat(ObjectPattern),
466}
467
468#[derive(Debug, Clone, PartialEq)]
469#[ast_serde]
470pub enum TypeAnnotOrNoop {
471 #[tag("TypeAnnotation")]
472 Flow(TypeAnnotation),
473 #[tag("TSTypeAnnotation")]
474 TS(Box<TSTypeAnnotation>),
475 #[tag("Noop")]
476 Noop(Noop),
477}
478
479impl From<TSTypeAnnotation> for TypeAnnotOrNoop {
480 fn from(annot: TSTypeAnnotation) -> Self {
481 TypeAnnotOrNoop::TS(Box::new(annot))
482 }
483}
484
485#[derive(Debug, Clone, PartialEq)]
486#[ast_serde]
487pub enum TypeParamDeclOrNoop {
488 #[tag("TypeParameterDeclaration")]
489 Flow(TypeParameterDeclaration),
490 #[tag("TSTypeParameterDeclaration")]
491 TS(TSTypeParameterDeclaration),
492 #[tag("Noop")]
493 Noop(Noop),
494}
495
496impl From<TSTypeParameterDeclaration> for TypeParamDeclOrNoop {
497 fn from(decl: TSTypeParameterDeclaration) -> Self {
498 TypeParamDeclOrNoop::TS(decl)
499 }
500}
501
502#[derive(Debug, Clone, PartialEq)]
503#[ast_serde]
504pub enum SuperTypeParams {
505 #[tag("TypeParameterInstantiation")]
506 Flow(TypeParameterInstantiation),
507 #[tag("TSTypeParameterInstantiation")]
508 TS(TSTypeParameterInstantiation),
509}
510
511impl From<TSTypeParameterInstantiation> for SuperTypeParams {
512 fn from(param: TSTypeParameterInstantiation) -> Self {
513 SuperTypeParams::TS(param)
514 }
515}
516
517#[derive(Debug, Clone, PartialEq)]
518#[ast_serde("PrivateName")]
519pub struct PrivateName {
520 #[serde(flatten)]
521 pub base: BaseNode,
522 pub id: Identifier,
523}
524
525#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
526#[serde(rename_all = "lowercase")]
527pub enum Access {
528 Public,
529 Private,
530 Protected,
531}
532
533#[derive(Debug, Clone, PartialEq)]
534#[ast_serde("MetaProperty")]
535pub struct MetaProperty {
536 #[serde(flatten)]
537 pub base: BaseNode,
538 pub meta: Identifier,
539 pub property: Identifier,
540}
541
542#[derive(Debug, Clone, PartialEq, Eq)]
543#[ast_serde("Directive")]
544pub struct Directive {
545 #[serde(flatten)]
546 pub base: BaseNode,
547 pub value: DirectiveLiteral,
548}
549
550#[derive(Debug, Clone, PartialEq, Eq)]
551#[ast_serde("DirectiveLiteral")]
552pub struct DirectiveLiteral {
553 #[serde(flatten)]
554 pub base: BaseNode,
555 #[serde(default)]
556 pub value: Atom,
557}
558
559#[derive(Debug, Clone, PartialEq)]
560#[ast_serde("PipelineBareFunction")]
561pub struct PipelineBareFunction {
562 #[serde(flatten)]
563 pub base: BaseNode,
564 pub callee: Box<Expression>,
565}
566
567#[derive(Debug, Clone, PartialEq)]
568#[ast_serde("PipelineTopicExpression")]
569pub struct PipelineTopicExpression {
570 #[serde(flatten)]
571 pub base: BaseNode,
572 pub expression: Box<Expression>,
573}
574
575#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
576pub enum PlaceholderExpectedNode {
577 Identifier,
578 StringLiteral,
579 Expression,
580 Statement,
581 Declaration,
582 BlockStatement,
583 ClassBody,
584 Pattern,
585}
586
587#[derive(Debug, Clone, PartialEq)]
588#[ast_serde("Placeholder")]
589pub struct Placeholder {
590 #[serde(flatten)]
591 pub base: BaseNode,
592 pub expected_node: PlaceholderExpectedNode,
593 pub name: Identifier,
594}
595
596