@@ -625,7 +625,7 @@ impl<'a> JsxImpl<'a, '_> {
625
625
// Append children to object properties in automatic mode
626
626
if is_automatic {
627
627
let mut children = ctx. ast . vec_from_iter (
628
- children. iter ( ) . filter_map ( |child| self . transform_jsx_child ( child, ctx) ) ,
628
+ children. iter ( ) . filter_map ( |child| self . transform_jsx_child_automatic ( child, ctx) ) ,
629
629
) ;
630
630
children_len = children. len ( ) ;
631
631
if children_len != 0 {
@@ -750,10 +750,7 @@ impl<'a> JsxImpl<'a, '_> {
750
750
// React.createElement(type, arguments, ...children)
751
751
// ^^^^^^^^^^^
752
752
arguments. extend (
753
- children
754
- . iter ( )
755
- . filter_map ( |child| self . transform_jsx_child ( child, ctx) )
756
- . map ( Argument :: from) ,
753
+ children. iter ( ) . filter_map ( |child| self . transform_jsx_child_classic ( child, ctx) ) ,
757
754
) ;
758
755
}
759
756
@@ -883,6 +880,40 @@ impl<'a> JsxImpl<'a, '_> {
883
880
}
884
881
}
885
882
883
+ fn transform_jsx_child_automatic (
884
+ & mut self ,
885
+ child : & JSXChild < ' a > ,
886
+ ctx : & mut TraverseCtx < ' a > ,
887
+ ) -> Option < Expression < ' a > > {
888
+ // Align spread child behavior with esbuild.
889
+ // Instead of Babel throwing `Spread children are not supported in React.`
890
+ // `<>{...foo}</>` -> `jsxs(Fragment, { children: [ ...foo ] })`
891
+ if let JSXChild :: Spread ( e) = child {
892
+ // SAFETY: `ast.copy` is unsound! We need to fix.
893
+ let argument = unsafe { ctx. ast . copy ( & e. expression ) } ;
894
+ let spread_element = ctx. ast . array_expression_element_spread_element ( e. span , argument) ;
895
+ let elements = ctx. ast . vec1 ( spread_element) ;
896
+ return Some ( ctx. ast . expression_array ( e. span , elements, None ) ) ;
897
+ }
898
+ self . transform_jsx_child ( child, ctx)
899
+ }
900
+
901
+ fn transform_jsx_child_classic (
902
+ & mut self ,
903
+ child : & JSXChild < ' a > ,
904
+ ctx : & mut TraverseCtx < ' a > ,
905
+ ) -> Option < Argument < ' a > > {
906
+ // Align spread child behavior with esbuild.
907
+ // Instead of Babel throwing `Spread children are not supported in React.`
908
+ // `<>{...foo}</>` -> `React.createElement(React.Fragment, null, ...foo)`
909
+ if let JSXChild :: Spread ( e) = child {
910
+ // SAFETY: `ast.copy` is unsound! We need to fix.
911
+ let argument = unsafe { ctx. ast . copy ( & e. expression ) } ;
912
+ return Some ( ctx. ast . argument_spread_element ( e. span , argument) ) ;
913
+ }
914
+ self . transform_jsx_child ( child, ctx) . map ( Argument :: from)
915
+ }
916
+
886
917
fn transform_jsx_child (
887
918
& mut self ,
888
919
child : & JSXChild < ' a > ,
@@ -903,10 +934,7 @@ impl<'a> JsxImpl<'a, '_> {
903
934
JSXChild :: Fragment ( e) => {
904
935
Some ( self . transform_jsx ( & JSXElementOrFragment :: Fragment ( e) , ctx) )
905
936
}
906
- JSXChild :: Spread ( e) => {
907
- self . ctx . error ( diagnostics:: spread_children_are_not_supported ( e. span ) ) ;
908
- None
909
- }
937
+ JSXChild :: Spread ( _) => unreachable ! ( ) ,
910
938
}
911
939
}
912
940
0 commit comments