Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ast): inherit_variants! macro add into_* methods #5005

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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