From 224db29c051fce41a030ff404093076f319758c7 Mon Sep 17 00:00:00 2001 From: Conrad Zimmerman Date: Fri, 3 Jul 2015 18:55:38 -0400 Subject: [PATCH] Handle single child when using React inlining Changes optimisation.react.inlineElements to handle a single child as the value of the `children` property instead of wrapping it with an array. This matches the behavior of `React.createElement`. --- .../transformers/optimisation/react.inline-elements.js | 4 +++- .../inline-elements/expected.js | 4 ++-- .../nested-html-elements/expected.js | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/babel/transformation/transformers/optimisation/react.inline-elements.js b/src/babel/transformation/transformers/optimisation/react.inline-elements.js index ed1ecaaba0..eacf2c41d5 100644 --- a/src/babel/transformation/transformers/optimisation/react.inline-elements.js +++ b/src/babel/transformation/transformers/optimisation/react.inline-elements.js @@ -49,7 +49,9 @@ export var visitor = { pushElemProp("ref", t.literal(null)); if (node.children.length) { - pushProp(props.properties, t.identifier("children"), t.arrayExpression(react.buildChildren(node))); + var children = react.buildChildren(node); + children = children.length === 1 ? children[0] : t.arrayExpression(children); + pushProp(props.properties, t.identifier("children"), children); } // props diff --git a/test/core/fixtures/transformation/optimisation.react.constant-elements/inline-elements/expected.js b/test/core/fixtures/transformation/optimisation.react.constant-elements/inline-elements/expected.js index 9c2f6fec0d..338bdb7ee9 100644 --- a/test/core/fixtures/transformation/optimisation.react.constant-elements/inline-elements/expected.js +++ b/test/core/fixtures/transformation/optimisation.react.constant-elements/inline-elements/expected.js @@ -16,11 +16,11 @@ function render() { type: "foo", ref: null, props: { - children: [text] + children: text }, key: null }; return function () { return _ref2; }; -} \ No newline at end of file +} diff --git a/test/core/fixtures/transformation/optimisation.react.inline-elements/nested-html-elements/expected.js b/test/core/fixtures/transformation/optimisation.react.inline-elements/nested-html-elements/expected.js index 566e7915dd..90b1ad9b93 100644 --- a/test/core/fixtures/transformation/optimisation.react.inline-elements/nested-html-elements/expected.js +++ b/test/core/fixtures/transformation/optimisation.react.inline-elements/nested-html-elements/expected.js @@ -4,7 +4,7 @@ type: "div", ref: null, props: { - children: [bar], + children: bar, className: "foo" }, key: null