| 1 | use serde::{Deserialize, Serialize}; |
| 2 | |
| 3 | use crate::common::BaseNode; |
| 4 | use crate::common::RawNode; |
| 5 | use crate::expressions::{Expression, Identifier}; |
| 6 | |
| 7 | /// Covers assignment targets and patterns. |
| 8 | /// In Babel, LVal includes Identifier, MemberExpression, ObjectPattern, ArrayPattern, |
| 9 | /// RestElement, AssignmentPattern. |
| 10 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 11 | #[serde(tag = "type")] |
| 12 | pub enum PatternLike { |
| 13 | Identifier(Identifier), |
| 14 | ObjectPattern(ObjectPattern), |
| 15 | ArrayPattern(ArrayPattern), |
| 16 | AssignmentPattern(AssignmentPattern), |
| 17 | RestElement(RestElement), |
| 18 | // Expressions can appear in pattern positions (e.g., MemberExpression as LVal) |
| 19 | MemberExpression(crate::expressions::MemberExpression), |
| 20 | TSAsExpression(crate::expressions::TSAsExpression), |
| 21 | TSSatisfiesExpression(crate::expressions::TSSatisfiesExpression), |
| 22 | TSNonNullExpression(crate::expressions::TSNonNullExpression), |
| 23 | TSTypeAssertion(crate::expressions::TSTypeAssertion), |
| 24 | // Flow's analogue of the TS cast wrappers: `(expr: SomeType)`. |
| 25 | TypeCastExpression(crate::expressions::TypeCastExpression), |
| 26 | } |
| 27 | |
| 28 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 29 | pub struct ObjectPattern { |
| 30 | #[serde(flatten)] |
| 31 | pub base: BaseNode, |
| 32 | pub properties: Vec<ObjectPatternProperty>, |
| 33 | #[serde( |
| 34 | default, |
| 35 | skip_serializing_if = "Option::is_none", |
| 36 | rename = "typeAnnotation" |
| 37 | )] |
| 38 | pub type_annotation: Option<RawNode>, |
| 39 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 40 | pub decorators: Option<Vec<RawNode>>, |
| 41 | } |
| 42 | |
| 43 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 44 | #[serde(tag = "type")] |
| 45 | pub enum ObjectPatternProperty { |
| 46 | ObjectProperty(ObjectPatternProp), |
| 47 | RestElement(RestElement), |
| 48 | } |
| 49 | |
| 50 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 51 | pub struct ObjectPatternProp { |
| 52 | #[serde(flatten)] |
| 53 | pub base: BaseNode, |
| 54 | pub key: Box<Expression>, |
| 55 | pub value: Box<PatternLike>, |
| 56 | pub computed: bool, |
| 57 | pub shorthand: bool, |
| 58 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 59 | pub decorators: Option<Vec<RawNode>>, |
| 60 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 61 | pub method: Option<bool>, |
| 62 | } |
| 63 | |
| 64 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 65 | pub struct ArrayPattern { |
| 66 | #[serde(flatten)] |
| 67 | pub base: BaseNode, |
| 68 | pub elements: Vec<Option<PatternLike>>, |
| 69 | #[serde( |
| 70 | default, |
| 71 | skip_serializing_if = "Option::is_none", |
| 72 | rename = "typeAnnotation" |
| 73 | )] |
| 74 | pub type_annotation: Option<RawNode>, |
| 75 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 76 | pub decorators: Option<Vec<RawNode>>, |
| 77 | } |
| 78 | |
| 79 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 80 | pub struct AssignmentPattern { |
| 81 | #[serde(flatten)] |
| 82 | pub base: BaseNode, |
| 83 | pub left: Box<PatternLike>, |
| 84 | pub right: Box<Expression>, |
| 85 | #[serde( |
| 86 | default, |
| 87 | skip_serializing_if = "Option::is_none", |
| 88 | rename = "typeAnnotation" |
| 89 | )] |
| 90 | pub type_annotation: Option<RawNode>, |
| 91 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 92 | pub decorators: Option<Vec<RawNode>>, |
| 93 | } |
| 94 | |
| 95 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 96 | pub struct RestElement { |
| 97 | #[serde(flatten)] |
| 98 | pub base: BaseNode, |
| 99 | pub argument: Box<PatternLike>, |
| 100 | #[serde( |
| 101 | default, |
| 102 | skip_serializing_if = "Option::is_none", |
| 103 | rename = "typeAnnotation" |
| 104 | )] |
| 105 | pub type_annotation: Option<RawNode>, |
| 106 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 107 | pub decorators: Option<Vec<RawNode>>, |
| 108 | } |