From 74364adce40afa5333b5fe01ee526e1c547214ad Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Thu, 19 Sep 2024 06:19:12 +0000 Subject: [PATCH] refactor(transformer): JSX: merge `transform_jsx_attribute_item` into `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. --- crates/oxc_transformer/src/react/jsx.rs | 60 +++++++++++-------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/crates/oxc_transformer/src/react/jsx.rs b/crates/oxc_transformer/src/react/jsx.rs index f578e0c6999da..fb4ac481ccd7d 100644 --- a/crates/oxc_transformer/src/react/jsx.rs +++ b/crates/oxc_transformer/src/react/jsx.rs @@ -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) => { @@ -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); } } @@ -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>>,