use crate::lexer::Span;
#[derive(Debug, Clone)]
pub struct SwcPatternMetadata {
pub swc_pattern: String,
pub unwrap_strategy: UnwrapStrategy,
pub inner: Option<Box<SwcPatternMetadata>>,
pub span: Option<Span>,
pub source_pattern: Option<String>,
pub desugar_strategy: Option<DesugarStrategy>,
}
#[derive(Debug, Clone)]
pub struct SwcFieldMetadata {
pub swc_field_name: String,
pub accessor: FieldAccessor,
pub field_type: String,
pub source_field: Option<String>,
pub span: Option<Span>,
pub read_conversion: String,
}
#[derive(Debug, Clone)]
pub struct SwcIdentifierMetadata {
pub use_sym: bool,
pub deref_pattern: Option<String>,
pub span: Option<Span>,
}
#[derive(Debug, Clone)]
pub struct SwcExprMetadata {
pub swc_type: String,
pub is_boxed: bool,
pub is_optional: bool,
pub type_kind: crate::type_system::SwcTypeKind,
pub needs_enum_unwrap: Option<(String, String)>,
pub needs_to_string: bool,
pub span: Option<Span>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum UnwrapStrategy {
AsRef,
RefDeref,
Ref,
None,
}
#[derive(Debug, Clone)]
pub enum DesugarStrategy {
NestedIfLet {
outer_pattern: String,
outer_binding: String,
inner_pattern: String,
inner_binding: String,
unwrap_expr: String,
},
}
#[derive(Debug, Clone)]
pub enum FieldAccessor {
Direct,
BoxedAsRef,
BoxedRefDeref,
DerefDisplay,
Utf8Lossy,
EnumField {
enum_name: String,
is_boxed: bool,
},
Optional {
inner: Box<FieldAccessor>,
},
Replace {
with: String,
},
}
#[derive(Debug, Clone)]
pub struct SwcIfLetMetadata {
pub condition_swc_type: String,
pub pattern_translation: String,
pub bindings: Vec<(String, String)>,
}
#[derive(Debug, Clone)]
pub struct SwcBinaryMetadata {
pub left_needs_deref: bool,
pub right_needs_deref: bool,
pub right_is_option: bool,
pub span: Option<Span>,
}
#[derive(Debug, Clone)]
pub struct SwcUnaryMetadata {
pub override_op: Option<String>,
pub span: Option<Span>,
}
#[derive(Debug, Clone)]
pub struct SwcRegexMetadata {
pub cache_pattern: bool,
pub pattern_id: Option<usize>,
pub needs_helper: bool,
pub helper_name: Option<String>,
}
impl SwcPatternMetadata {
pub fn direct(swc_pattern: String) -> Self {
Self {
swc_pattern,
unwrap_strategy: UnwrapStrategy::None,
inner: None,
span: None,
source_pattern: None,
desugar_strategy: None,
}
}
pub fn with_as_ref(swc_pattern: String) -> Self {
Self {
swc_pattern,
unwrap_strategy: UnwrapStrategy::AsRef,
inner: None,
span: None,
source_pattern: None,
desugar_strategy: None,
}
}
pub fn with_ref_deref(swc_pattern: String) -> Self {
Self {
swc_pattern,
unwrap_strategy: UnwrapStrategy::RefDeref,
inner: None,
span: None,
source_pattern: None,
desugar_strategy: None,
}
}
pub fn needs_desugaring(&self) -> bool {
self.desugar_strategy.is_some()
}
}
impl SwcFieldMetadata {
pub fn direct(swc_field_name: String, field_type: String) -> Self {
Self {
swc_field_name,
accessor: FieldAccessor::Direct,
field_type,
source_field: None,
span: None,
read_conversion: String::new(),
}
}
pub fn boxed(swc_field_name: String, field_type: String, in_pattern: bool) -> Self {
Self {
swc_field_name,
accessor: if in_pattern {
FieldAccessor::BoxedRefDeref
} else {
FieldAccessor::BoxedAsRef
},
field_type,
source_field: None,
span: None,
read_conversion: String::new(),
}
}
pub fn enum_field(swc_field_name: String, enum_name: String, field_type: String) -> Self {
Self {
swc_field_name,
accessor: FieldAccessor::EnumField {
enum_name,
is_boxed: false
},
field_type,
source_field: None,
span: None,
read_conversion: String::new(),
}
}
}
impl SwcIdentifierMetadata {
pub fn sym_with_deref() -> Self {
Self {
use_sym: true,
deref_pattern: Some("&*".to_string()),
span: None,
}
}
pub fn name() -> Self {
Self {
use_sym: false,
deref_pattern: None,
span: None,
}
}
}
#[derive(Debug, Clone)]
pub struct SwcCustomPropAssignmentMetadata {
pub value_type: crate::parser::Type,
pub variant: String,
pub is_deletion: bool,
pub span: Option<crate::lexer::Span>,
}
#[derive(Debug, Clone)]
pub struct SwcCustomPropAccessMetadata {
pub property_type: Option<crate::parser::Type>,
pub unwrapper_pattern: Option<String>,
pub span: Option<crate::lexer::Span>,
}