Skip to content

Commit

Permalink
fix(es/react): Make jsx with single spread child static (#8339)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyzx committed Nov 26, 2023
1 parent 0b2fd10 commit 58568fa
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
15 changes: 10 additions & 5 deletions crates/swc_ecma_transforms_react/src/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,9 @@ where
fn jsx_frag_to_expr(&mut self, el: JSXFragment) -> Expr {
let mut span = el.span();

let use_jsxs = count_children(&el.children) > 1;
let count = count_children(&el.children);
let use_jsxs = count > 1
|| (count == 1 && matches!(&el.children[0], JSXElementChild::JSXSpreadChild(..)));

if let Some(comments) = &self.comments {
if span.lo.is_dummy() {
Expand Down Expand Up @@ -478,9 +480,9 @@ where
.map(Some)
.collect::<Vec<_>>();

match children.len() {
0 => {}
1 if children[0].as_ref().unwrap().spread.is_none() => {
match (children.len(), use_jsxs) {
(0, _) => {}
(1, false) => {
props_obj
.props
.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
Expand Down Expand Up @@ -565,7 +567,10 @@ where
Runtime::Automatic => {
// function jsx(tagName: string, props: { children: Node[], ... }, key: string)

let use_jsxs = count_children(&el.children) > 1;
let count = count_children(&el.children);
let use_jsxs = count > 1
|| (count == 1
&& matches!(&el.children[0], JSXElementChild::JSXSpreadChild(..)));

let jsx = if use_create_element {
self.import_create_element
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const h1 = <h1></h1>
const h2 = <h2>{}</h2>
const h3 = <h3>{a}</h3>
const h4 = <h4>{a}b</h4>
const h5 = <h5>{...a}</h5>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const h1 = /*#__PURE__*/ _jsx("h1", {});
const h2 = /*#__PURE__*/ _jsx("h2", {});
const h3 = /*#__PURE__*/ _jsx("h3", {
children: a
});
const h4 = /*#__PURE__*/ _jsxs("h4", {
children: [
a,
"b"
]
});
const h5 = /*#__PURE__*/ _jsxs("h5", {
children: [
...a
]
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*#__PURE__*/ import { jsx as _jsx } from "react/jsx-runtime";
_jsx("div", {
/*#__PURE__*/ import { jsxs as _jsxs } from "react/jsx-runtime";
_jsxs("div", {
children: [
...children
]
Expand Down

0 comments on commit 58568fa

Please sign in to comment.