fix(transformer/react-jsx): no need to wrap the Array when there is only one correct child element (#1205)

This commit is contained in:
Dunqing 2023-11-10 10:52:37 +08:00 committed by GitHub
parent e4c097bb91
commit b65094b995
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 23 deletions

View file

@ -316,23 +316,28 @@ impl<'a> ReactJsx<'a> {
let children = e.children();
// Append children to object properties in automatic mode
if is_automatic && !children.is_empty() {
let ident = IdentifierName::new(SPAN, "children".into());
let key = self.ast.property_key_identifier(ident);
let value = if children.len() == 1 {
self.transform_jsx_child(&children[0])
} else {
let mut elements = self.ast.new_vec_with_capacity(children.len());
for child in children {
if let Some(e) = self.transform_jsx_child(child) {
elements.push(ArrayExpressionElement::Expression(e));
}
}
need_jsxs = true;
Some(self.ast.array_expression(SPAN, elements, None))
};
if let Some(value) = value {
if is_automatic {
let allocator = self.ast.allocator;
let mut children = Vec::from_iter_in(
children.iter().filter_map(|child| self.transform_jsx_child(child)),
allocator,
);
let children_len = children.len();
if children_len != 0 {
let value = if children_len == 1 {
children.pop().unwrap()
} else {
let elements = Vec::from_iter_in(
children.into_iter().map(ArrayExpressionElement::Expression),
allocator,
);
need_jsxs = true;
self.ast.array_expression(SPAN, elements, None)
};
let kind = PropertyKind::Init;
let ident = IdentifierName::new(SPAN, "children".into());
let key = self.ast.property_key_identifier(ident);
let object_property =
self.ast.object_property(SPAN, kind, key, value, None, false, false, false);
properties.push(ObjectPropertyKind::ObjectProperty(object_property));

View file

@ -1,4 +1,4 @@
Passed: 278/1113
Passed: 283/1113
# All Passed:
* babel-plugin-transform-numeric-separator
@ -825,9 +825,8 @@ Passed: 278/1113
* regression/11061/input.mjs
* variable-declaration/non-null-in-optional-chain/input.ts
# babel-plugin-transform-react-jsx (116/170)
# babel-plugin-transform-react-jsx (121/170)
* autoImport/after-polyfills-compiled-to-cjs/input.mjs
* autoImport/auto-import-react-source-type-module/input.js
* autoImport/auto-import-react-source-type-script/input.js
* autoImport/complicated-scope-module/input.js
* pure/false-pragma-comment-automatic-runtime/input.js
@ -855,17 +854,13 @@ Passed: 278/1113
* 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/does-not-add-source-self-automatic/input.mjs
* react-automatic/handle-nonstatic-children/input.js
* react-automatic/optimisation.react.constant-elements/input.js
* react-automatic/should-add-quotes-es3/input.js
* react-automatic/should-allow-nested-fragments/input.js
* react-automatic/should-avoid-wrapping-in-extra-parens-if-not-needed/input.js
* react-automatic/should-disallow-spread-children/input.js
* react-automatic/should-disallow-valueless-key/input.js
* react-automatic/should-disallow-xml-namespacing/input.js
* react-automatic/should-handle-attributed-elements/input.js
* react-automatic/should-properly-handle-comments-between-props/input.js
* react-automatic/should-throw-error-namespaces-if-not-flag/input.js
* react-automatic/should-throw-when-filter-is-specified/input.js
* react-automatic/should-warn-when-pragma-or-pragmaFrag-is-set/input.js