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

fix(transformer): arrow function transform alter </this> #3627

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
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