From 1ef41b6f320d929a818f4b7767db4121611058cf Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 31 Mar 2015 03:31:27 +1100 Subject: [PATCH] optimisation.react.inlineElements: move `children` into `props` and leave `children` out if the element has none - @spicyj --- .../optimisation/react.inline-elements.js | 12 ++++++++---- .../inline-elements/expected.js | 5 +++-- .../component-with-props/expected.js | 3 +-- .../component/expected.js | 3 +-- .../html-element-with-props/expected.js | 3 +-- .../html-element/expected.js | 3 +-- .../nested-components/expected.js | 12 ++++++------ .../nested-html-elements/expected.js | 4 ++-- .../nested/expected.js | 14 +++++++------- 9 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/babel/transformation/transformers/optimisation/react.inline-elements.js b/src/babel/transformation/transformers/optimisation/react.inline-elements.js index aa80f6589d..1e06ac833f 100644 --- a/src/babel/transformation/transformers/optimisation/react.inline-elements.js +++ b/src/babel/transformation/transformers/optimisation/react.inline-elements.js @@ -36,15 +36,19 @@ export function JSXElement(node, parent, scope, file) { } function pushElemProp(key, value) { - obj.properties.push(t.property("init", t.identifier(key), value)); + pushProp(obj.properties, t.identifier(key), value); + } + + function pushProp(objProps, key, value) { + objProps.push(t.property("init", key, value)); } // metadata pushElemProp("type", type); pushElemProp("ref", t.literal(null)); - if (!open.selfClosing) { - pushElemProp("children", t.arrayExpression(react.buildChildren(node))); + if (node.children.length) { + pushProp(props.properties, t.identifier("children"), t.arrayExpression(react.buildChildren(node))); } // props @@ -53,7 +57,7 @@ export function JSXElement(node, parent, scope, file) { if (isJSXAttributeOfName(attr, "key")) { key = attr.value; } else { - props.properties.push(t.property("init", attr.name, attr.value)); + pushProp(props.properties, attr.name, attr.value); } } 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 1e3bf1d92d..ac644db0c1 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 @@ -15,8 +15,9 @@ function render() { var _ref = { type: "foo", ref: null, - children: [text], - props: {}, + props: { + children: [text] + }, key: null }; return function () { diff --git a/test/core/fixtures/transformation/optimisation.react.inline-elements/component-with-props/expected.js b/test/core/fixtures/transformation/optimisation.react.inline-elements/component-with-props/expected.js index fae9f383ff..f12d308ad4 100644 --- a/test/core/fixtures/transformation/optimisation.react.inline-elements/component-with-props/expected.js +++ b/test/core/fixtures/transformation/optimisation.react.inline-elements/component-with-props/expected.js @@ -3,9 +3,8 @@ ({ type: Baz, ref: null, - children: [], props: babelHelpers.defaultProps(Baz.defaultProps, { foo: "bar" }), key: null -}); \ No newline at end of file +}); diff --git a/test/core/fixtures/transformation/optimisation.react.inline-elements/component/expected.js b/test/core/fixtures/transformation/optimisation.react.inline-elements/component/expected.js index 9ee63d21ad..ce0544da35 100644 --- a/test/core/fixtures/transformation/optimisation.react.inline-elements/component/expected.js +++ b/test/core/fixtures/transformation/optimisation.react.inline-elements/component/expected.js @@ -3,7 +3,6 @@ ({ type: Baz, ref: null, - children: [], props: babelHelpers.defaultProps(Baz.defaultProps, {}), key: null -}); \ No newline at end of file +}); diff --git a/test/core/fixtures/transformation/optimisation.react.inline-elements/html-element-with-props/expected.js b/test/core/fixtures/transformation/optimisation.react.inline-elements/html-element-with-props/expected.js index a6dc3e0a4b..dfddc6e5be 100644 --- a/test/core/fixtures/transformation/optimisation.react.inline-elements/html-element-with-props/expected.js +++ b/test/core/fixtures/transformation/optimisation.react.inline-elements/html-element-with-props/expected.js @@ -3,9 +3,8 @@ ({ type: "foo", ref: null, - children: [], props: { bar: "foo" }, key: null -}); \ No newline at end of file +}); diff --git a/test/core/fixtures/transformation/optimisation.react.inline-elements/html-element/expected.js b/test/core/fixtures/transformation/optimisation.react.inline-elements/html-element/expected.js index f0b9752862..bd6cead480 100644 --- a/test/core/fixtures/transformation/optimisation.react.inline-elements/html-element/expected.js +++ b/test/core/fixtures/transformation/optimisation.react.inline-elements/html-element/expected.js @@ -3,7 +3,6 @@ ({ type: "foo", ref: null, - children: [], props: {}, key: null -}); \ No newline at end of file +}); diff --git a/test/core/fixtures/transformation/optimisation.react.inline-elements/nested-components/expected.js b/test/core/fixtures/transformation/optimisation.react.inline-elements/nested-components/expected.js index 405c538c03..cdb8f0053a 100644 --- a/test/core/fixtures/transformation/optimisation.react.inline-elements/nested-components/expected.js +++ b/test/core/fixtures/transformation/optimisation.react.inline-elements/nested-components/expected.js @@ -3,13 +3,13 @@ ({ type: Foo, ref: null, - children: [bar, { - type: Baz, - ref: null, - props: babelHelpers.defaultProps(Baz.defaultProps, {}), - key: "baz" - }], props: babelHelpers.defaultProps(Foo.defaultProps, { + children: [bar, { + type: Baz, + ref: null, + props: babelHelpers.defaultProps(Baz.defaultProps, {}), + key: "baz" + }], className: "foo" }), key: null 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 fa75e4186a..566e7915dd 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 @@ -3,9 +3,9 @@ ({ type: "div", ref: null, - children: [bar], props: { + children: [bar], className: "foo" }, key: null -}); \ No newline at end of file +}); diff --git a/test/core/fixtures/transformation/optimisation.react.inline-elements/nested/expected.js b/test/core/fixtures/transformation/optimisation.react.inline-elements/nested/expected.js index b741025c7f..6d9b9a1f1c 100644 --- a/test/core/fixtures/transformation/optimisation.react.inline-elements/nested/expected.js +++ b/test/core/fixtures/transformation/optimisation.react.inline-elements/nested/expected.js @@ -3,14 +3,14 @@ ({ type: "div", ref: null, - children: [bar, { - type: Baz, - ref: null, - props: babelHelpers.defaultProps(Baz.defaultProps, {}), - key: "baz" - }], props: { + children: [bar, { + type: Baz, + ref: null, + props: babelHelpers.defaultProps(Baz.defaultProps, {}), + key: "baz" + }], className: "foo" }, key: null -}); \ No newline at end of file +});