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 6 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
36 changes: 29 additions & 7 deletions packages/babel-helper-builder-react-jsx/src/index.js
Expand Up @@ -161,6 +161,14 @@ You can turn on the 'throwIfNamespace' flag to bypass this warning.`,
return [];
}

function setDefaultValue(option, defaultValue) {
if (typeof option === "undefined") {
return defaultValue;
}

return option;
}

/**
* The logic for this is quite terse. It's because we need to
* support spread elements. We loop over all attributes,
Expand All @@ -172,7 +180,15 @@ You can turn on the 'throwIfNamespace' flag to bypass this warning.`,
let _props = [];
const objs = [];

const useBuiltIns = file.opts.useBuiltIns || false;
const useSpread = setDefaultValue(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.

You can use

const { useSpread = false } = file.opts;

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

const useBuiltIns = setDefaultValue(file.opts.useBuiltIns, false);
Copy link
Member

Choose a reason for hiding this comment

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

While this makes 100% sense, it is a breaking change (for example, if you pass 0) and we should defer it to Babel 8.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So. Should I go back to the previous version?

Copy link
Member

Choose a reason for hiding this comment

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

Yes 👍 but only here, not for useSpread.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh ok, got it.

if (typeof useBuiltIns !== "boolean") {
throw new Error(
"transform-react-jsx currently only accepts a boolean option for " +
Expand All @@ -185,6 +201,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 +220,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": 0 }]],
"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 }]]
}