Skip to content

Commit

Permalink
feat(ast): inherit_variants! macro add into_* methods (#5005)
Browse files Browse the repository at this point in the history
`inherit_variants!` macro generate `into_*` methods for owned objects. e.g.:

```rs
let expr: Expression = get_expr();
match expr {
  match_member_expression!(Expression) => {
    let member_expr: MemberExpression = expr.into_member_expression();
  },
  _ => {},
}
```
  • Loading branch information
overlookmotel committed Aug 20, 2024
1 parent 7f3129e commit 714373d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
29 changes: 29 additions & 0 deletions crates/oxc_ast/src/ast/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
/// shared_enum_variants!(
/// Statement, Declaration,
/// is_declaration,
/// into_declaration,
/// as_declaration, as_declaration_mut,
/// to_declaration, to_declaration_mut,
/// [VariableDeclaration, FunctionDeclaration, ...more]
Expand All @@ -64,6 +65,7 @@
/// shared_enum_variants!(
/// Statement, ModuleDeclaration,
/// is_module_declaration,
/// into_module_declaration,
/// as_module_declaration, as_module_declaration_mut,
/// to_module_declaration, to_module_declaration_mut,
/// [ImportDeclaration, ExportAllDeclaration, ...more]
Expand Down Expand Up @@ -181,6 +183,7 @@ macro_rules! inherit_variants {
$ty,
Expression,
is_expression,
into_expression,
as_expression,
as_expression_mut,
to_expression,
Expand Down Expand Up @@ -267,6 +270,7 @@ macro_rules! inherit_variants {
$ty,
MemberExpression,
is_member_expression,
into_member_expression,
as_member_expression,
as_member_expression_mut,
to_member_expression,
Expand Down Expand Up @@ -300,6 +304,7 @@ macro_rules! inherit_variants {
$ty,
AssignmentTarget,
is_assignment_target,
into_assignment_target,
as_assignment_target,
as_assignment_target_mut,
to_assignment_target,
Expand Down Expand Up @@ -359,6 +364,7 @@ macro_rules! inherit_variants {
$ty,
SimpleAssignmentTarget,
is_simple_assignment_target,
into_simple_assignment_target,
as_simple_assignment_target,
as_simple_assignment_target_mut,
to_simple_assignment_target,
Expand Down Expand Up @@ -404,6 +410,7 @@ macro_rules! inherit_variants {
$ty,
AssignmentTargetPattern,
is_assignment_target_pattern,
into_assignment_target_pattern,
as_assignment_target_pattern,
as_assignment_target_pattern_mut,
to_assignment_target_pattern,
Expand Down Expand Up @@ -454,6 +461,7 @@ macro_rules! inherit_variants {
$ty,
Declaration,
is_declaration,
into_declaration,
as_declaration,
as_declaration_mut,
to_declaration,
Expand Down Expand Up @@ -516,6 +524,7 @@ macro_rules! inherit_variants {
$ty,
ModuleDeclaration,
is_module_declaration,
into_module_declaration,
as_module_declaration,
as_module_declaration_mut,
to_module_declaration,
Expand Down Expand Up @@ -635,6 +644,7 @@ macro_rules! inherit_variants {
$ty,
TSType,
is_ts_type,
into_ts_type,
as_ts_type,
as_ts_type_mut,
to_ts_type,
Expand Down Expand Up @@ -709,6 +719,7 @@ macro_rules! inherit_variants {
$ty,
TSTypeName,
is_ts_type_name,
into_ts_type_name,
as_ts_type_name,
as_ts_type_name_mut,
to_ts_type_name,
Expand Down Expand Up @@ -743,6 +754,7 @@ pub(crate) use inherit_variants;
/// shared_enum_variants!(
/// Statement, Declaration,
/// is_declaration,
/// into_declaration,
/// as_declaration, as_declaration_mut,
/// to_declaration, to_declaration_mut,
/// [VariableDeclaration, FunctionDeclaration]
Expand All @@ -767,6 +779,14 @@ pub(crate) use inherit_variants;
/// }
/// }
///
/// /// Convert `Statement` to `Declaration`.
/// /// # Panic
/// /// Panics if not convertible.
/// #[inline]
/// pub fn into_declaration(self) -> Declaration<'a> {
/// Declaration::try_from(self).unwrap()
/// }
///
/// /// Convert `&Statement` to `&Declaration`.
/// #[inline]
/// pub fn as_declaration(&self) -> Option<&Declaration<'a>> {
Expand Down Expand Up @@ -833,6 +853,7 @@ macro_rules! shared_enum_variants {
(
$parent:ident, $child:ident,
$is_child:ident,
$into_child:ident,
$as_child:ident, $as_child_mut:ident,
$to_child:ident, $to_child_mut:ident,
[$($variant:ident),+ $(,)?]
Expand Down Expand Up @@ -861,6 +882,14 @@ macro_rules! shared_enum_variants {
)
}

#[doc = concat!("Convert `", stringify!($parent), "` to `", stringify!($child), "`.")]
#[doc = "# Panic"]
#[doc = "Panics if not convertible."]
#[inline]
pub fn $into_child(self) -> $child<'a> {
$child::try_from(self).unwrap()
}

#[doc = concat!("Convert `&", stringify!($parent), "` to `&", stringify!($child), "`.")]
#[inline]
pub fn $as_child(&self) -> Option<&$child<'a>> {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
//! shared_enum_variants!(
//! Expression, MemberExpression,
//! is_member_expression,
//! into_member_expression,
//! as_member_expression, as_member_expression_mut,
//! to_member_expression, to_member_expression_mut,
//! [ComputedMemberExpression, StaticMemberExpression, PrivateFieldExpression]
Expand Down

0 comments on commit 714373d

Please sign in to comment.