[transform-react-jsx] Add useSpread option to transform JSX#10572
[transform-react-jsx] Add useSpread option to transform JSX#10572nicolo-ribaudo merged 12 commits intobabel:masterfrom
Conversation
nicolo-ribaudo
left a comment
There was a problem hiding this comment.
Can you also add a test where the spread isn't the first property?
| let _props = []; | ||
| const objs = []; | ||
|
|
||
| const useSpread = file.opts.useSpread || false; |
There was a problem hiding this comment.
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.
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.
JLHwung
left a comment
There was a problem hiding this comment.
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.
| let _props = []; | ||
| const objs = []; | ||
|
|
||
| const useSpread = file.opts.useSpread || false; |
There was a problem hiding this comment.
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.
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.
So. Should I go back to the previous version?
There was a problem hiding this comment.
Yes 👍 but only here, not for useSpread.
| const objs = []; | ||
|
|
||
| const useBuiltIns = file.opts.useBuiltIns || false; | ||
| const useSpread = setDefaultValue(file.opts.useSpread, false); |
There was a problem hiding this comment.
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 |
nicolo-ribaudo
left a comment
There was a problem hiding this comment.
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).
Uh oh!
There was an error while loading. Please reload this page.