Skip to content

Commit

Permalink
refactor(transformer): JSX: merge transform_jsx_attribute_item into…
Browse files Browse the repository at this point in the history
… `transform_jsx` (#5861)

`transform_jsx` branches on the type of each attribute, and then passes each attribute to `transform_jsx_attribute_item`, which branches on the type again. Instead, inline the two arms of `transform_jsx_attribute_item` into the match arms of `transform_jsx`.

This does make `transform_jsx` even bigger than it was already. I'll break it up in a later PR.
  • Loading branch information
overlookmotel committed Sep 19, 2024
1 parent d2eaa7d commit 74364ad
Showing 1 changed file with 25 additions and 35 deletions.
60 changes: 25 additions & 35 deletions crates/oxc_transformer/src/react/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,15 @@ impl<'a> ReactJsx<'a> {
continue;
}
}

// Add attribute to prop object
let kind = PropertyKind::Init;
let key = self.get_attribute_name(&attr.name);
let value = self.transform_jsx_attribute_value(attr.value.as_ref(), ctx);
let object_property = self.ast().object_property_kind_object_property(
attr.span, kind, key, value, None, false, false, false,
);
properties.push(object_property);
}
// optimize `{...prop}` to `prop` in static mode
JSXAttributeItem::SpreadAttribute(spread) => {
Expand All @@ -495,11 +504,24 @@ impl<'a> ReactJsx<'a> {
continue;
}
}

// Add attribute to prop object
match &spread.argument {
Expression::ObjectExpression(expr) if !expr.has_proto() => {
// SAFETY: `ast.copy` is unsound! We need to fix.
properties.extend(unsafe { self.ast().copy(&expr.properties) });
}
expr => {
// SAFETY: `ast.copy` is unsound! We need to fix.
let argument = unsafe { self.ast().copy(expr) };
let object_property = self
.ast()
.object_property_kind_spread_element(spread.span, argument);
properties.push(object_property);
}
}
}
}

// Add attribute to prop object
self.transform_jsx_attribute_item(&mut properties, attribute, ctx);
}
}

Expand Down Expand Up @@ -732,38 +754,6 @@ impl<'a> ReactJsx<'a> {
self.ast().member_expression_static(expr.span, object, property, false).into()
}

fn transform_jsx_attribute_item(
&mut self,
properties: &mut Vec<'a, ObjectPropertyKind<'a>>,
attribute: &JSXAttributeItem<'a>,
ctx: &mut TraverseCtx<'a>,
) {
match attribute {
JSXAttributeItem::Attribute(attr) => {
let kind = PropertyKind::Init;
let key = self.get_attribute_name(&attr.name);
let value = self.transform_jsx_attribute_value(attr.value.as_ref(), ctx);
let object_property = self.ast().object_property_kind_object_property(
attr.span, kind, key, value, None, false, false, false,
);
properties.push(object_property);
}
JSXAttributeItem::SpreadAttribute(attr) => match &attr.argument {
Expression::ObjectExpression(expr) if !expr.has_proto() => {
// SAFETY: `ast.copy` is unsound! We need to fix.
properties.extend(unsafe { self.ast().copy(&expr.properties) });
}
expr => {
// SAFETY: `ast.copy` is unsound! We need to fix.
let argument = unsafe { self.ast().copy(expr) };
let object_property =
self.ast().object_property_kind_spread_element(attr.span, argument);
properties.push(object_property);
}
},
}
}

fn transform_jsx_attribute_value(
&mut self,
value: Option<&JSXAttributeValue<'a>>,
Expand Down

0 comments on commit 74364ad

Please sign in to comment.