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
Conversation
ivandevp
commented
Oct 18, 2019
•
edited
edited
Q | A |
---|---|
Fixed Issues? | Fixes #9652 |
Patch: Bug Fix? | No |
Major: Breaking Change? | No |
Minor: New Feature? | Yes |
Tests Added + Pass? | Yes |
Documentation PR Link | babel/website#2110 |
Any Dependency Changes? | No |
License | MIT |
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.
Can you also add a test where the spread isn't the first property?
@@ -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; |
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.
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.
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.
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
.
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.
I think when useSpread
is enabled, we can further simplify the logic to
return attribs.map(convertAttribute)
because objs
is used to support breaking at spreadElement only.
And we may teach convertAttribute
to replace JSXSpreadAttribute
by spreadElement
.
@@ -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; |
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.
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
.
…nto feat/use-spread
); | ||
} | ||
|
||
const useBuiltIns = setDefaultValue(file.opts.useBuiltIns, false); |
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.
While this makes 100% sense, it is a breaking change (for example, if you pass 0
) and we should defer it to Babel 8.
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.
So. Should I go back to the previous version?
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.
Yes 👍 but only here, not for useSpread
.
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.
Oh ok, got it.
@@ -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); |
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
const { useSpread = false } = file.opts;
Hey @JLHwung , do you mean to add a condition before if (useSpread) {
const props = attribs.map(convertAttribute);
return t.objectExpression(props);
} |
@ivandevp correct, I missed the |
I've made the requested changes guys, let me know if you think we can do any other enhancement. |
@ivandevp Could you add |
Sure @JLHwung! I forgot that one 😅 |
Hey @JLHwung ! I've tried to follow the |
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.
The CI failure is unrelated
Co-Authored-By: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
A new `useSpread` option was added in Babel 7.7.0 (babel/babel#10572): https://babeljs.io/docs/en/babel-preset-react#usespread Enabling this means that the JSX-> JS conversion will use an inline object with spread elements (for code that spreads props), rather than Babel's extend helper or `Object.assign`. `@babel/env` will still transpile these down if required for the target browsers (the only reason `useSpread` is not already enabled by default is that for the upstream project this is a breaking change, since they cannot assume that users of `babel-preset-react` are always using it alongside `@babel/env`, even though most likely are).