From 1eef2417881935c85f1d5e6eeb8f075deda61c5b Mon Sep 17 00:00:00 2001 From: Dunqing Date: Sun, 12 Nov 2023 18:37:19 +0800 Subject: [PATCH] feat(transformer/react-jsx): support for throwing SpreadChildrenAreNotSupported error (#1234) --- crates/oxc_transformer/src/react_jsx/mod.rs | 11 +++++++++++ tasks/transform_conformance/babel.snap.md | 6 ++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/crates/oxc_transformer/src/react_jsx/mod.rs b/crates/oxc_transformer/src/react_jsx/mod.rs index 3dc2455b4..382a446ae 100644 --- a/crates/oxc_transformer/src/react_jsx/mod.rs +++ b/crates/oxc_transformer/src/react_jsx/mod.rs @@ -37,6 +37,11 @@ struct NamespaceDoesNotSupport(#[label] Span); #[diagnostic(severity(warning))] struct ValuelessKey(#[label] Span); +#[derive(Debug, Error, Diagnostic)] +#[error("Spread children are not supported in React.")] +#[diagnostic(severity(warning))] +struct SpreadChildrenAreNotSupported(#[label] Span); + /// Transform React JSX /// /// References: @@ -370,6 +375,12 @@ impl<'a> ReactJsx<'a> { let children = e.children(); + if children.len() == 1 { + if let Some(JSXChild::Spread(s)) = children.get(0) { + self.ctx.error(SpreadChildrenAreNotSupported(s.span)); + } + } + // Append children to object properties in automatic mode if is_automatic { let allocator = self.ast.allocator; diff --git a/tasks/transform_conformance/babel.snap.md b/tasks/transform_conformance/babel.snap.md index 038826d3f..431e0a999 100644 --- a/tasks/transform_conformance/babel.snap.md +++ b/tasks/transform_conformance/babel.snap.md @@ -1,4 +1,4 @@ -Passed: 275/1113 +Passed: 277/1113 # All Passed: * babel-plugin-transform-numeric-separator @@ -852,7 +852,7 @@ Passed: 275/1113 * regression/11061/input.mjs * variable-declaration/non-null-in-optional-chain/input.ts -# babel-plugin-transform-react-jsx (140/170) +# babel-plugin-transform-react-jsx (142/170) * autoImport/after-polyfills-compiled-to-cjs/input.mjs * autoImport/complicated-scope-module/input.js * react/adds-appropriate-newlines-when-using-spread-attribute-babel-7/input.js @@ -863,14 +863,12 @@ Passed: 275/1113 * react/handle-spread-with-proto-babel-7/input.js * react/optimisation.react.constant-elements/input.js * react/should-add-quotes-es3/input.js -* react/should-disallow-spread-children/input.js * react/wraps-props-in-react-spread-for-first-spread-attributes-babel-7/input.js * react/wraps-props-in-react-spread-for-last-spread-attributes-babel-7/input.js * react/wraps-props-in-react-spread-for-middle-spread-attributes-babel-7/input.js * react-automatic/arrow-functions/input.js * react-automatic/optimisation.react.constant-elements/input.js * react-automatic/should-add-quotes-es3/input.js -* react-automatic/should-disallow-spread-children/input.js * react-automatic/should-handle-attributed-elements/input.js * react-automatic/should-throw-when-filter-is-specified/input.js * regression/issue-12478-automatic/input.js