From d6ba8910ba31a3a9459a940fbaada425f1e6ddfb Mon Sep 17 00:00:00 2001 From: Boshen Date: Sat, 28 Oct 2023 15:59:46 +0800 Subject: [PATCH] feat(transformer): add props `null` to React.createElement (#1074) --- crates/oxc_transformer/src/react_jsx/mod.rs | 15 +++++++-------- tasks/transform_conformance/babel.snap.md | 6 ++---- tasks/transform_conformance/src/test_case.rs | 2 +- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/crates/oxc_transformer/src/react_jsx/mod.rs b/crates/oxc_transformer/src/react_jsx/mod.rs index e0a6f72d4..54dd13173 100644 --- a/crates/oxc_transformer/src/react_jsx/mod.rs +++ b/crates/oxc_transformer/src/react_jsx/mod.rs @@ -30,8 +30,10 @@ enum JSXElementOrFragment<'a, 'b> { impl<'a, 'b> JSXElementOrFragment<'a, 'b> { fn attributes(&self) -> Option<&'b Vec<'a, JSXAttributeItem<'a>>> { match self { - Self::Element(e) => Some(&e.opening_element.attributes), - Self::Fragment(_) => None, + Self::Element(e) if !e.opening_element.attributes.is_empty() => { + Some(&e.opening_element.attributes) + } + _ => None, } } @@ -125,12 +127,6 @@ impl<'a> ReactJsx<'a> { JSXElementOrFragment::Fragment(_) => self.get_fragment(), })); - if self.options.runtime.is_classic() && e.attributes().is_some_and(|attrs| attrs.is_empty()) - { - let null_expr = self.ast.literal_null_expression(NullLiteral::new(SPAN)); - arguments.push(Argument::Expression(null_expr)); - } - // TODO: compute the correct capacity for both runtimes let mut properties = self.ast.new_vec_with_capacity(0); if let Some(attributes) = e.attributes() { @@ -196,6 +192,9 @@ impl<'a> ReactJsx<'a> { }, } } + } else if self.options.runtime.is_classic() { + let null_expr = self.ast.literal_null_expression(NullLiteral::new(SPAN)); + arguments.push(Argument::Expression(null_expr)); } if self.options.runtime.is_automatic() && !children.is_empty() { diff --git a/tasks/transform_conformance/babel.snap.md b/tasks/transform_conformance/babel.snap.md index 843de56fa..a29b36ff7 100644 --- a/tasks/transform_conformance/babel.snap.md +++ b/tasks/transform_conformance/babel.snap.md @@ -1,4 +1,4 @@ -Passed: 204/1083 +Passed: 206/1083 # All Passed: * babel-plugin-transform-numeric-separator @@ -804,7 +804,7 @@ Passed: 204/1083 * regression/11061/input.mjs * variable-declaration/non-null-in-optional-chain/input.ts -# babel-plugin-transform-react-jsx (55/172) +# babel-plugin-transform-react-jsx (57/172) * autoImport/after-polyfills/input.mjs * autoImport/after-polyfills-2/input.mjs * autoImport/after-polyfills-compiled-to-cjs/input.mjs @@ -836,7 +836,6 @@ Passed: 204/1083 * react/arrow-functions/input.js * react/assignment-babel-7/input.js * react/avoids-spread-babel-7/input.js -* react/does-not-add-source-self/input.mjs * react/does-not-add-source-self-babel-7/input.mjs * react/flattens-spread/input.js * react/handle-spread-with-proto/input.js @@ -849,7 +848,6 @@ Passed: 204/1083 * react/should-add-quotes-es3/input.js * react/should-allow-elements-as-attributes/input.js * react/should-allow-jsx-docs-comment-with-pragma/input.js -* react/should-allow-nested-fragments/input.js * react/should-allow-no-pragmafrag-if-frag-unused/input.js * react/should-allow-pragmafrag-and-frag/input.js * react/should-disallow-spread-children/input.js diff --git a/tasks/transform_conformance/src/test_case.rs b/tasks/transform_conformance/src/test_case.rs index bf3723fcd..5adfe965b 100644 --- a/tasks/transform_conformance/src/test_case.rs +++ b/tasks/transform_conformance/src/test_case.rs @@ -212,7 +212,7 @@ impl TestCase for ConformanceTestCase { println!("{input}\n"); println!("Options:"); println!("{:?}\n", self.transform_options()); - println!("Output:\n"); + println!("Expected:\n"); println!("{output}\n"); println!("Transformed:\n"); println!("{transformed_code}\n");