Skip to content

Commit

Permalink
refactor(ast): replace recursion with loop (#3626)
Browse files Browse the repository at this point in the history
Replace recursion with loop in
`JSXMemberExpression::get_object_identifier`.

Also add a mutable version
`JSXMemberExpression::get_object_identifier_mut`.
  • Loading branch information
overlookmotel authored Jun 11, 2024
1 parent 85c3b83 commit 0f92521
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions crates/oxc_ast/src/ast/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,30 @@ pub struct JSXMemberExpression<'a> {

impl<'a> JSXMemberExpression<'a> {
pub fn get_object_identifier(&self) -> &JSXIdentifier {
match &self.object {
JSXMemberExpressionObject::Identifier(ident) => ident,
JSXMemberExpressionObject::MemberExpression(expr) => expr.get_object_identifier(),
let mut member_expr = self;
loop {
match &member_expr.object {
JSXMemberExpressionObject::Identifier(ident) => {
break ident;
}
JSXMemberExpressionObject::MemberExpression(expr) => {
member_expr = expr;
}
}
}
}

pub fn get_object_identifier_mut(&mut self) -> &mut JSXIdentifier<'a> {
let mut member_expr = self;
loop {
match &mut member_expr.object {
JSXMemberExpressionObject::Identifier(ident) => {
break &mut *ident;
}
JSXMemberExpressionObject::MemberExpression(expr) => {
member_expr = expr;
}
}
}
}
}
Expand Down

0 comments on commit 0f92521

Please sign in to comment.