mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
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:
parent
ec4be1f839
commit
70f31a8953
1 changed files with 3 additions and 14 deletions
|
|
@ -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())
|
||||
|
|
|
|||
Loading…
Reference in a new issue