-
Notifications
You must be signed in to change notification settings - Fork 47.9k
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
[babel-plugin-react-jsx] Avoid duplicate "children" key in props object #17094
Conversation
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.
Let's fix this more broadly, incl. <A x={1} x={2} />
or more exotic ones like <A x={1} {...obj} x={2} />
.
@gaearon Can you document why you accepted even though your comment isn't addressed? There's some insight there. (It seems ok to me to only fix children, since the general case isn't solved for any other object literal and we just need parity with the previous transform.) |
@sebmarkbage The current JSX transform does not fix this case either, the fix actually comes from the ES2015 Babel preset that has a plugin that handles object literals with duplicate keys. I should have replied to the comment on here, but I forgot (we spoke about in-person though). |
Yeah I initially thought that old transform handled this but we later realized it didn’t either so it seemed ok |
This PR fixes a duplicate "children" key bug in the
TransformJSXToReactJSX
Babel transform. Specifically, when we have JSX that looks like this:Before this PR, the output would have been:
In the above output, the
children
property is duplicated in the object literal expression. In JS strict mode, this can cause errors in certain browsers (notably IE) where an object cannot contain duplicate property keys.This PR changed the logic, so when this pattern is encountered (explicitly with
children
only) we create a new object and put thechildren
property in it and then useObject.assign
(or whatever the native helper is) to ensure the correct object is formed as an end-result. So the above would output: