mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(ast): inherit_variants! macro add into_* methods (#5005)
`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();
},
_ => {},
}
```
This commit is contained in:
parent
7f3129efd1
commit
714373dc5e
2 changed files with 30 additions and 0 deletions
|
|
@ -56,6 +56,7 @@
|
||||||
/// shared_enum_variants!(
|
/// shared_enum_variants!(
|
||||||
/// Statement, Declaration,
|
/// Statement, Declaration,
|
||||||
/// is_declaration,
|
/// is_declaration,
|
||||||
|
/// into_declaration,
|
||||||
/// as_declaration, as_declaration_mut,
|
/// as_declaration, as_declaration_mut,
|
||||||
/// to_declaration, to_declaration_mut,
|
/// to_declaration, to_declaration_mut,
|
||||||
/// [VariableDeclaration, FunctionDeclaration, ...more]
|
/// [VariableDeclaration, FunctionDeclaration, ...more]
|
||||||
|
|
@ -64,6 +65,7 @@
|
||||||
/// shared_enum_variants!(
|
/// shared_enum_variants!(
|
||||||
/// Statement, ModuleDeclaration,
|
/// Statement, ModuleDeclaration,
|
||||||
/// is_module_declaration,
|
/// is_module_declaration,
|
||||||
|
/// into_module_declaration,
|
||||||
/// as_module_declaration, as_module_declaration_mut,
|
/// as_module_declaration, as_module_declaration_mut,
|
||||||
/// to_module_declaration, to_module_declaration_mut,
|
/// to_module_declaration, to_module_declaration_mut,
|
||||||
/// [ImportDeclaration, ExportAllDeclaration, ...more]
|
/// [ImportDeclaration, ExportAllDeclaration, ...more]
|
||||||
|
|
@ -181,6 +183,7 @@ macro_rules! inherit_variants {
|
||||||
$ty,
|
$ty,
|
||||||
Expression,
|
Expression,
|
||||||
is_expression,
|
is_expression,
|
||||||
|
into_expression,
|
||||||
as_expression,
|
as_expression,
|
||||||
as_expression_mut,
|
as_expression_mut,
|
||||||
to_expression,
|
to_expression,
|
||||||
|
|
@ -267,6 +270,7 @@ macro_rules! inherit_variants {
|
||||||
$ty,
|
$ty,
|
||||||
MemberExpression,
|
MemberExpression,
|
||||||
is_member_expression,
|
is_member_expression,
|
||||||
|
into_member_expression,
|
||||||
as_member_expression,
|
as_member_expression,
|
||||||
as_member_expression_mut,
|
as_member_expression_mut,
|
||||||
to_member_expression,
|
to_member_expression,
|
||||||
|
|
@ -300,6 +304,7 @@ macro_rules! inherit_variants {
|
||||||
$ty,
|
$ty,
|
||||||
AssignmentTarget,
|
AssignmentTarget,
|
||||||
is_assignment_target,
|
is_assignment_target,
|
||||||
|
into_assignment_target,
|
||||||
as_assignment_target,
|
as_assignment_target,
|
||||||
as_assignment_target_mut,
|
as_assignment_target_mut,
|
||||||
to_assignment_target,
|
to_assignment_target,
|
||||||
|
|
@ -359,6 +364,7 @@ macro_rules! inherit_variants {
|
||||||
$ty,
|
$ty,
|
||||||
SimpleAssignmentTarget,
|
SimpleAssignmentTarget,
|
||||||
is_simple_assignment_target,
|
is_simple_assignment_target,
|
||||||
|
into_simple_assignment_target,
|
||||||
as_simple_assignment_target,
|
as_simple_assignment_target,
|
||||||
as_simple_assignment_target_mut,
|
as_simple_assignment_target_mut,
|
||||||
to_simple_assignment_target,
|
to_simple_assignment_target,
|
||||||
|
|
@ -404,6 +410,7 @@ macro_rules! inherit_variants {
|
||||||
$ty,
|
$ty,
|
||||||
AssignmentTargetPattern,
|
AssignmentTargetPattern,
|
||||||
is_assignment_target_pattern,
|
is_assignment_target_pattern,
|
||||||
|
into_assignment_target_pattern,
|
||||||
as_assignment_target_pattern,
|
as_assignment_target_pattern,
|
||||||
as_assignment_target_pattern_mut,
|
as_assignment_target_pattern_mut,
|
||||||
to_assignment_target_pattern,
|
to_assignment_target_pattern,
|
||||||
|
|
@ -454,6 +461,7 @@ macro_rules! inherit_variants {
|
||||||
$ty,
|
$ty,
|
||||||
Declaration,
|
Declaration,
|
||||||
is_declaration,
|
is_declaration,
|
||||||
|
into_declaration,
|
||||||
as_declaration,
|
as_declaration,
|
||||||
as_declaration_mut,
|
as_declaration_mut,
|
||||||
to_declaration,
|
to_declaration,
|
||||||
|
|
@ -516,6 +524,7 @@ macro_rules! inherit_variants {
|
||||||
$ty,
|
$ty,
|
||||||
ModuleDeclaration,
|
ModuleDeclaration,
|
||||||
is_module_declaration,
|
is_module_declaration,
|
||||||
|
into_module_declaration,
|
||||||
as_module_declaration,
|
as_module_declaration,
|
||||||
as_module_declaration_mut,
|
as_module_declaration_mut,
|
||||||
to_module_declaration,
|
to_module_declaration,
|
||||||
|
|
@ -635,6 +644,7 @@ macro_rules! inherit_variants {
|
||||||
$ty,
|
$ty,
|
||||||
TSType,
|
TSType,
|
||||||
is_ts_type,
|
is_ts_type,
|
||||||
|
into_ts_type,
|
||||||
as_ts_type,
|
as_ts_type,
|
||||||
as_ts_type_mut,
|
as_ts_type_mut,
|
||||||
to_ts_type,
|
to_ts_type,
|
||||||
|
|
@ -709,6 +719,7 @@ macro_rules! inherit_variants {
|
||||||
$ty,
|
$ty,
|
||||||
TSTypeName,
|
TSTypeName,
|
||||||
is_ts_type_name,
|
is_ts_type_name,
|
||||||
|
into_ts_type_name,
|
||||||
as_ts_type_name,
|
as_ts_type_name,
|
||||||
as_ts_type_name_mut,
|
as_ts_type_name_mut,
|
||||||
to_ts_type_name,
|
to_ts_type_name,
|
||||||
|
|
@ -743,6 +754,7 @@ pub(crate) use inherit_variants;
|
||||||
/// shared_enum_variants!(
|
/// shared_enum_variants!(
|
||||||
/// Statement, Declaration,
|
/// Statement, Declaration,
|
||||||
/// is_declaration,
|
/// is_declaration,
|
||||||
|
/// into_declaration,
|
||||||
/// as_declaration, as_declaration_mut,
|
/// as_declaration, as_declaration_mut,
|
||||||
/// to_declaration, to_declaration_mut,
|
/// to_declaration, to_declaration_mut,
|
||||||
/// [VariableDeclaration, FunctionDeclaration]
|
/// [VariableDeclaration, FunctionDeclaration]
|
||||||
|
|
@ -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`.
|
/// /// Convert `&Statement` to `&Declaration`.
|
||||||
/// #[inline]
|
/// #[inline]
|
||||||
/// pub fn as_declaration(&self) -> Option<&Declaration<'a>> {
|
/// pub fn as_declaration(&self) -> Option<&Declaration<'a>> {
|
||||||
|
|
@ -833,6 +853,7 @@ macro_rules! shared_enum_variants {
|
||||||
(
|
(
|
||||||
$parent:ident, $child:ident,
|
$parent:ident, $child:ident,
|
||||||
$is_child:ident,
|
$is_child:ident,
|
||||||
|
$into_child:ident,
|
||||||
$as_child:ident, $as_child_mut:ident,
|
$as_child:ident, $as_child_mut:ident,
|
||||||
$to_child:ident, $to_child_mut:ident,
|
$to_child:ident, $to_child_mut:ident,
|
||||||
[$($variant:ident),+ $(,)?]
|
[$($variant:ident),+ $(,)?]
|
||||||
|
|
@ -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), "`.")]
|
#[doc = concat!("Convert `&", stringify!($parent), "` to `&", stringify!($child), "`.")]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn $as_child(&self) -> Option<&$child<'a>> {
|
pub fn $as_child(&self) -> Option<&$child<'a>> {
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,7 @@
|
||||||
//! shared_enum_variants!(
|
//! shared_enum_variants!(
|
||||||
//! Expression, MemberExpression,
|
//! Expression, MemberExpression,
|
||||||
//! is_member_expression,
|
//! is_member_expression,
|
||||||
|
//! into_member_expression,
|
||||||
//! as_member_expression, as_member_expression_mut,
|
//! as_member_expression, as_member_expression_mut,
|
||||||
//! to_member_expression, to_member_expression_mut,
|
//! to_member_expression, to_member_expression_mut,
|
||||||
//! [ComputedMemberExpression, StaticMemberExpression, PrivateFieldExpression]
|
//! [ComputedMemberExpression, StaticMemberExpression, PrivateFieldExpression]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue