Skip to content

Commit

Permalink
refactor(transformer): reduce branching in JSX transform (#3596)
Browse files Browse the repository at this point in the history
Small optimization to JSX transform. Replace 2 branches on
element/fragment with 1.
  • Loading branch information
overlookmotel authored Jun 10, 2024
1 parent ec4be1f commit 70f31a8
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions crates/oxc_transformer/src/react/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,15 +460,6 @@ impl<'a, 'b> JSXElementOrFragment<'a, 'b> {
}
}

fn attributes(&self) -> Option<&'b Vec<'a, JSXAttributeItem<'a>>> {
match self {
Self::Element(e) if !e.opening_element.attributes.is_empty() => {
Some(&e.opening_element.attributes)
}
_ => None,
}
}

fn children(&self) -> &'b Vec<'a, JSXChild<'a>> {
match self {
Self::Element(e) => &e.children,
Expand Down Expand Up @@ -534,21 +525,19 @@ impl<'a> ReactJsx<'a> {
// The key prop in `<div key={true} />`
let mut key_prop = None;

let attributes = e.attributes();
let attributes_len = attributes.map_or(0, |attrs| attrs.len());

// The object properties for the second argument of `React.createElement`
let mut properties = self.ast().new_vec();

let mut self_attr_span = None;
let mut source_attr_span = None;

if let Some(attributes) = attributes {
if let JSXElementOrFragment::Element(e) = e {
let attributes = &e.opening_element.attributes;
for attribute in attributes {
match attribute {
// optimize `{...prop}` to `prop` in static mode
JSXAttributeItem::SpreadAttribute(spread)
if is_classic && attributes_len == 1 =>
if is_classic && attributes.len() == 1 =>
{
// deopt if spreading an object with `__proto__` key
if !matches!(&spread.argument, Expression::ObjectExpression(o) if o.has_proto())
Expand Down

0 comments on commit 70f31a8

Please sign in to comment.