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

[babel-plugin-react-jsx] Avoid duplicate "children" key in props object #17094

Merged
merged 2 commits into from
Oct 15, 2019

Conversation

trueadm
Copy link
Contributor

@trueadm trueadm commented Oct 15, 2019

This PR fixes a duplicate "children" key bug in the TransformJSXToReactJSX Babel transform. Specifically, when we have JSX that looks like this:

<Component children={1}>2</Component>

Before this PR, the output would have been:

React.jsx(Component, {\n  children: 1,\n  children: "2"\n})

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 the children property in it and then use Object.assign (or whatever the native helper is) to ensure the correct object is formed as an end-result. So the above would output:

React.jsx(Component, Object.assign({
  children: 1
}, {
  children: "2"
}));

@sizebot
Copy link

sizebot commented Oct 15, 2019

No significant bundle size changes to report.

Generated by 🚫 dangerJS against 1ea3dc4

Copy link
Collaborator

@gaearon gaearon left a 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} />.

@trueadm trueadm merged commit e7704e2 into facebook:master Oct 15, 2019
@trueadm trueadm deleted the fix-duplicate-children-keys branch October 15, 2019 13:41
@sebmarkbage
Copy link
Collaborator

sebmarkbage commented Oct 15, 2019

@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.)

@trueadm
Copy link
Contributor Author

trueadm commented Oct 15, 2019

@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).

@gaearon
Copy link
Collaborator

gaearon commented Oct 15, 2019

Yeah I initially thought that old transform handled this but we later realized it didn’t either so it seemed ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants