diff --git a/lib/6to5/generation/generators/expressions.js b/lib/6to5/generation/generators/expressions.js index b246abf27f..42c97f69d6 100644 --- a/lib/6to5/generation/generators/expressions.js +++ b/lib/6to5/generation/generators/expressions.js @@ -55,10 +55,30 @@ exports.ThisExpression = function () { this.push("this"); }; -exports.CallExpression = function (node, print) { +exports.CallExpression = function (node, print, parent) { print(node.callee); + this.push("("); - print.join(node.arguments, { separator: ", " }); + + var separator = ","; + + if (node._prettyPrint) { + separator += "\n"; + this.newline(); + this.indent(); + } else { + separator += " "; + } + + print.join(node.arguments, { + separator: separator + }); + + if (node._prettyPrint) { + this.newline(); + this.dedent(); + } + this.push(")"); }; diff --git a/lib/6to5/transformation/transformers/react.js b/lib/6to5/transformation/transformers/react.js index 4b06b04ea3..09bd579414 100644 --- a/lib/6to5/transformation/transformers/react.js +++ b/lib/6to5/transformation/transformers/react.js @@ -32,7 +32,7 @@ exports.XJSExpressionContainer = function (node) { exports.XJSAttribute = { exit: function (node) { var value = node.value || t.literal(true); - return t.property("init", node.name, value); + return t.inherits(t.property("init", node.name, value), node); } }; @@ -103,30 +103,29 @@ exports.XJSOpeningElement = { args.push(props); + var setPrettyPrint = function (call, args) { + if (args.length >= 3) { + call._prettyPrint = true; + } + return call; + }; + if (reactCompat) { if (tagName && isTag(tagName)) { - return t.callExpression( + return setPrettyPrint(t.callExpression( t.memberExpression( - t.memberExpression( - t.identifier('React'), - t.identifier('DOM'), - false - ), + t.memberExpression(t.identifier("React"), t.identifier("DOM")), tagExpr, t.isLiteral(tagExpr) ), args - ); - } else { - return t.callExpression( - tagExpr, - args - ); + ), args); } } else { tagExpr = t.memberExpression(t.identifier("React"), t.identifier("createElement")); - return t.callExpression(tagExpr, args); } + + return setPrettyPrint(t.callExpression(tagExpr, args), args); } }; @@ -148,16 +147,16 @@ exports.XJSElement = { var isLastLine = +i === lines.length - 1; // replace rendered whitespace tabs with spaces - var trimmedLine = line.replace(/\t/g, ' '); + var trimmedLine = line.replace(/\t/g, " "); // trim whitespace touching a newline if (!isFirstLine) { - trimmedLine = trimmedLine.replace(/^[ ]+/, ''); + trimmedLine = trimmedLine.replace(/^[ ]+/, ""); } // trim whitespace touching an endline if (!isLastLine) { - trimmedLine = trimmedLine.replace(/[ ]+$/, ''); + trimmedLine = trimmedLine.replace(/[ ]+$/, ""); } if (trimmedLine) { diff --git a/test/fixtures/transformation/react/adds-appropriate-newlines-when-using-spread-attribute/expected.js b/test/fixtures/transformation/react/adds-appropriate-newlines-when-using-spread-attribute/expected.js index 31b010b451..9ef7b00ddf 100644 --- a/test/fixtures/transformation/react/adds-appropriate-newlines-when-using-spread-attribute/expected.js +++ b/test/fixtures/transformation/react/adds-appropriate-newlines-when-using-spread-attribute/expected.js @@ -1,3 +1,2 @@ React.createElement(Component, React.__spread({}, this.props, { - sound: "moo" -})); + sound: "moo" })); diff --git a/test/fixtures/transformation/react/compat-convert-component/expected.js b/test/fixtures/transformation/react/compat-convert-component/expected.js index e603231871..efb2cef82a 100644 --- a/test/fixtures/transformation/react/compat-convert-component/expected.js +++ b/test/fixtures/transformation/react/compat-convert-component/expected.js @@ -1,3 +1 @@ -var x = Component({ - foo: "bar" -}, Namespace.Component(null)); +var x = Component({ foo: "bar" }, Namespace.Component(null)); diff --git a/test/fixtures/transformation/react/compat-convert-tags/expected.js b/test/fixtures/transformation/react/compat-convert-tags/expected.js index 742c1cab89..5abbe81784 100644 --- a/test/fixtures/transformation/react/compat-convert-tags/expected.js +++ b/test/fixtures/transformation/react/compat-convert-tags/expected.js @@ -1,3 +1 @@ -var x = React.DOM.div({ - foo: "bar" -}, React.DOM["font-face"](null)); +var x = React.DOM.div({ foo: "bar" }, React.DOM["font-face"](null)); diff --git a/test/fixtures/transformation/react/should-allow-constructor-as-prop/expected.js b/test/fixtures/transformation/react/should-allow-constructor-as-prop/expected.js index 5bda6d624f..d1636ac3c6 100644 --- a/test/fixtures/transformation/react/should-allow-constructor-as-prop/expected.js +++ b/test/fixtures/transformation/react/should-allow-constructor-as-prop/expected.js @@ -1,3 +1 @@ -React.createElement(Component, { - constructor: "foo" -}); +React.createElement(Component, { constructor: "foo" }); diff --git a/test/fixtures/transformation/react/should-insert-commas-after-expressions-before-whitespace/expected.js b/test/fixtures/transformation/react/should-insert-commas-after-expressions-before-whitespace/expected.js index f7a07f8b6c..6d2188c75c 100644 --- a/test/fixtures/transformation/react/should-insert-commas-after-expressions-before-whitespace/expected.js +++ b/test/fixtures/transformation/react/should-insert-commas-after-expressions-before-whitespace/expected.js @@ -2,5 +2,4 @@ var x = React.createElement("div", { attr1: "foo" + "bar", attr2: "foo" + "bar" + "baz" + "bug", attr3: "foo" + "bar" + "baz" + "bug", - attr4: "baz" -}); + attr4: "baz" }); diff --git a/test/fixtures/transformation/react/wraps-props-in-react-spread-for-first-spread-attributes/expected.js b/test/fixtures/transformation/react/wraps-props-in-react-spread-for-first-spread-attributes/expected.js index 5e5b8e74a0..f6ef0b74ea 100644 --- a/test/fixtures/transformation/react/wraps-props-in-react-spread-for-first-spread-attributes/expected.js +++ b/test/fixtures/transformation/react/wraps-props-in-react-spread-for-first-spread-attributes/expected.js @@ -1,4 +1 @@ -React.createElement(Component, React.__spread({}, x, { - y: 2, - z: true -})); +React.createElement(Component, React.__spread({}, x, { y: 2, z: true })); diff --git a/test/fixtures/transformation/react/wraps-props-in-react-spread-for-last-spread-attributes/expected.js b/test/fixtures/transformation/react/wraps-props-in-react-spread-for-last-spread-attributes/expected.js index 6b897925d0..a1651713c2 100644 --- a/test/fixtures/transformation/react/wraps-props-in-react-spread-for-last-spread-attributes/expected.js +++ b/test/fixtures/transformation/react/wraps-props-in-react-spread-for-last-spread-attributes/expected.js @@ -1,4 +1 @@ -React.createElement(Component, React.__spread({ - y: 2, - z: true -}, x)); +React.createElement(Component, React.__spread({ y: 2, z: true }, x)); diff --git a/test/fixtures/transformation/react/wraps-props-in-react-spread-for-middle-spread-attributes/expected.js b/test/fixtures/transformation/react/wraps-props-in-react-spread-for-middle-spread-attributes/expected.js index 53595afca9..9f3084024a 100644 --- a/test/fixtures/transformation/react/wraps-props-in-react-spread-for-middle-spread-attributes/expected.js +++ b/test/fixtures/transformation/react/wraps-props-in-react-spread-for-middle-spread-attributes/expected.js @@ -1,5 +1 @@ -React.createElement(Component, React.__spread({ - y: 2 -}, x, { - z: true -})); +React.createElement(Component, React.__spread({ y: 2 }, x, { z: true }));