Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[transform-react-jsx] Add useSpread option to transform JSX #10572

Merged
merged 12 commits into from Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 20 additions & 6 deletions packages/babel-helper-builder-react-jsx/src/index.js
Expand Up @@ -172,6 +172,14 @@ You can turn on the 'throwIfNamespace' flag to bypass this warning.`,
let _props = [];
const objs = [];

const useSpread = file.opts.useSpread || false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that this accepts 0, but let's keep it as-is for consistency with the other options of this file 🤷‍♀️

We should revisit it in Babel 8.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, we could begin on useSpread now because it is a new option. useBuiltins: 0 is strange enough and it does not justify that you can even use useSpread: 0.

Copy link
Contributor

@JLHwung JLHwung Oct 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also add useSpread to preset-react, too.

Another thought is that may be we should give a warning (or even error) when both useBuiltIns and useSpread are true because useSpread precedes useBuiltIns.

if (typeof useSpread !== "boolean") {
throw new Error(
"transform-react-jsx currently only accepts a boolean option for " +
"useSpread (defaults to false)",
);
}

const useBuiltIns = file.opts.useBuiltIns || false;
if (typeof useBuiltIns !== "boolean") {
throw new Error(
Expand All @@ -185,6 +193,9 @@ You can turn on the 'throwIfNamespace' flag to bypass this warning.`,
if (t.isJSXSpreadAttribute(prop)) {
_props = pushProps(_props, objs);
objs.push(prop.argument);
if (useSpread) {
_props.push(t.spreadElement(prop.argument));
}
} else {
_props.push(convertAttribute(prop));
}
Expand All @@ -201,12 +212,15 @@ You can turn on the 'throwIfNamespace' flag to bypass this warning.`,
objs.unshift(t.objectExpression([]));
}

const helper = useBuiltIns
? t.memberExpression(t.identifier("Object"), t.identifier("assign"))
: file.addHelper("extends");

// spread it
attribs = t.callExpression(helper, objs);
if (useSpread) {
attribs = t.objectExpression(_props);
} else {
const helper = useBuiltIns
? t.memberExpression(t.identifier("Object"), t.identifier("assign"))
: file.addHelper("extends");
// spread it
attribs = t.callExpression(helper, objs);
}
}

return attribs;
Expand Down
@@ -0,0 +1 @@
var div = <Component {...props} foo="bar" />
@@ -0,0 +1,4 @@
{
"plugins": [["transform-react-jsx", { "useSpread": "invalidOption" }]],
"throws": "transform-react-jsx currently only accepts a boolean option for useSpread (defaults to false)"
}
@@ -0,0 +1 @@
var div = <Component {...props} foo="bar" />
@@ -0,0 +1,3 @@
var div = React.createElement(Component, { ...props,
foo: "bar"
});
@@ -0,0 +1,3 @@
{
"plugins": [["transform-react-jsx", { "useSpread": true }]]
}