refactor(transformer): reduce branching in JSX transform (#3596)

Small optimization to JSX transform. Replace 2 branches on
element/fragment with 1.
This commit is contained in:
overlookmotel 2024-06-10 05:41:02 +01:00 committed by GitHub
parent ec4be1f839
commit 70f31a8953
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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,
@ -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())