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
Changes from 6 commits
345654d
c547265
48d6c09
51fd79d
60aad23
3afabbf
d905397
617dd72
3e32a4e
ec9f24b
7b0f831
35ec82b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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); | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So. Should I go back to the previous version? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes 👍 but only here, not for There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 " + | ||
|
@@ -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)); | ||
} | ||
|
@@ -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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
var div = <Component {...props} foo="bar" /> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
var div = <Component {...props} foo="bar" /> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var div = React.createElement(Component, { ...props, | ||
foo: "bar" | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": [["transform-react-jsx", { "useSpread": true }]] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use