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 all 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
24 changes: 24 additions & 0 deletions packages/babel-helper-builder-react-jsx/src/index.js
Expand Up @@ -87,6 +87,10 @@ You can turn on the 'throwIfNamespace' flag to bypass this warning.`,
function convertAttribute(node) {
const value = convertAttributeValue(node.value || t.booleanLiteral(true));

if (t.isJSXSpreadAttribute(node)) {
return t.spreadElement(node.argument);
}

if (t.isStringLiteral(value) && !t.isJSXExpressionContainer(node.value)) {
value.value = value.value.replace(/\n\s+/g, " ");

Expand Down Expand Up @@ -172,6 +176,14 @@ You can turn on the 'throwIfNamespace' flag to bypass this warning.`,
let _props = [];
const objs = [];

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 = file.opts.useBuiltIns || false;
if (typeof useBuiltIns !== "boolean") {
throw new Error(
Expand All @@ -180,6 +192,18 @@ You can turn on the 'throwIfNamespace' flag to bypass this warning.`,
);
}

if (useSpread && useBuiltIns) {
throw new Error(
"transform-react-jsx currently only accepts useBuiltIns or useSpread " +
"but not both",
);
}

if (useSpread) {
const props = attribs.map(convertAttribute);
return t.objectExpression(props);
}

while (attribs.length) {
const prop = attribs.shift();
if (t.isJSXSpreadAttribute(prop)) {
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,6 @@
{
"plugins": [
["transform-react-jsx", { "useSpread": true, "useBuiltIns": true }]
],
"throws": "transform-react-jsx currently only accepts useBuiltIns or useSpread but not both"
}
@@ -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 }]]
}
3 changes: 2 additions & 1 deletion packages/babel-preset-react/src/index.js
Expand Up @@ -13,6 +13,7 @@ export default declare((api, opts) => {
opts.throwIfNamespace === undefined ? true : !!opts.throwIfNamespace;
const development = !!opts.development;
const useBuiltIns = !!opts.useBuiltIns;
const { useSpread } = opts;

if (typeof development !== "boolean") {
throw new Error(
Expand All @@ -24,7 +25,7 @@ export default declare((api, opts) => {
plugins: [
[
transformReactJSX,
{ pragma, pragmaFrag, throwIfNamespace, useBuiltIns },
{ pragma, pragmaFrag, throwIfNamespace, useBuiltIns, useSpread },
],
transformReactDisplayName,

Expand Down