Skip to content

Commit

Permalink
fix(transformer): arrow function transform alter </this> (#3627)
Browse files Browse the repository at this point in the history
Arrow function transform transforms `this` in `<this>` (JSX opening element), but was missing out transforming `</this>` (JSX closing element).
  • Loading branch information
overlookmotel committed Jun 12, 2024
1 parent e32ce00 commit 0c4ccb4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
31 changes: 9 additions & 22 deletions crates/oxc_transformer/src/es2015/arrow_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,34 +119,21 @@ impl<'a> ArrowFunctions<'a> {
}

/// Change <this></this> to <_this></_this>, and mark it as found
pub fn transform_jsx_opening_element(&mut self, elem: &mut JSXOpeningElement<'a>) {
pub fn transform_jsx_element_name(&mut self, name: &mut JSXElementName<'a>) {
if !self.is_inside_arrow_function() {
return;
}

let mut change_ident = |ident: &mut JSXIdentifier<'a>| {
if ident.name == "this" {
self.mark_this_as_found();
ident.name = self.get_this_name();
let ident = match name {
JSXElementName::Identifier(ident) => ident,
JSXElementName::MemberExpression(member_expr) => {
member_expr.get_object_identifier_mut()
}
JSXElementName::NamespacedName(_) => return,
};
match &mut elem.name {
JSXElementName::Identifier(ref mut ident) => change_ident(ident),
JSXElementName::MemberExpression(ref mut member) => {
let mut member_expr = member;
loop {
match &mut member_expr.object {
JSXMemberExpressionObject::Identifier(ident) => {
change_ident(ident);
break;
}
JSXMemberExpressionObject::MemberExpression(expr) => {
member_expr = expr;
}
}
}
}
JSXElementName::NamespacedName(_) => {}
if ident.name == "this" {
self.mark_this_as_found();
ident.name = self.get_this_name();
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/es2015/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ impl<'a> ES2015<'a> {
}
}

pub fn transform_jsx_opening_element(&mut self, elem: &mut JSXOpeningElement<'a>) {
pub fn transform_jsx_element_name(&mut self, elem: &mut JSXElementName<'a>) {
if self.options.arrow_function.is_some() {
self.arrow_functions.transform_jsx_opening_element(elem);
self.arrow_functions.transform_jsx_element_name(elem);
}
}

Expand Down
9 changes: 8 additions & 1 deletion crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,14 @@ impl<'a> Traverse<'a> for Transformer<'a> {
) {
self.x0_typescript.transform_jsx_opening_element(elem);
self.x1_react.transform_jsx_opening_element(elem, ctx);
self.x3_es2015.transform_jsx_opening_element(elem);
}

fn enter_jsx_element_name(
&mut self,
elem: &mut JSXElementName<'a>,
_ctx: &mut TraverseCtx<'a>,
) {
self.x3_es2015.transform_jsx_element_name(elem);
}

fn enter_method_definition(
Expand Down

0 comments on commit 0c4ccb4

Please sign in to comment.