GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [babel-plugin-react-jsx] Avoid duplicate "children" key in props obje… · react/react@e7704e2

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/babel-plugin-react-jsx
    • __tests__
      • TransformJSXToReactJSX-test.js
      • __snapshots__
        • TransformJSXToReactJSX-test.js.snap
    • src
      • TransformJSXToReactBabelPlugin.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/babel-plugin-react-jsx/__tests__/TransformJSXToReactJSX-test.js‎

Copy file name to clipboardExpand all lines: packages/babel-plugin-react-jsx/__tests__/TransformJSXToReactJSX-test.js
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,4 +480,10 @@ describe('transform react to jsx', () => {
480480
transform(`<Component y={2} {...x} />`, {useBuiltIns: false})
481481
).toMatchSnapshot();
482482
});
483+
484+
it('should not contain duplicate children key in props object', () => {
485+
expect(
486+
transform(`<Component children={1}>2</Component>`)
487+
).toMatchSnapshot();
488+
});
483489
});
Collapse file

‎packages/babel-plugin-react-jsx/__tests__/__snapshots__/TransformJSXToReactJSX-test.js.snap‎

Copy file name to clipboardExpand all lines: packages/babel-plugin-react-jsx/__tests__/__snapshots__/TransformJSXToReactJSX-test.js.snap
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ var e = React.jsx(F, {
250250
});
251251
`;
252252

253+
exports[`transform react to jsx should not contain duplicate children key in props object 1`] = `
254+
React.jsx(Component, Object.assign({
255+
children: 1
256+
}, {
257+
children: "2"
258+
}));
259+
`;
260+
253261
exports[`transform react to jsx should not strip nbsp even couple with other whitespace 1`] = `
254262
React.jsx("div", {
255263
children: "\\xA0 "
Collapse file

‎packages/babel-plugin-react-jsx/src/TransformJSXToReactBabelPlugin.js‎

Copy file name to clipboardExpand all lines: packages/babel-plugin-react-jsx/src/TransformJSXToReactBabelPlugin.js
+18-1Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,20 @@ You can turn on the 'throwIfNamespace' flag to bypass this warning.`,
268268
);
269269
}
270270

271+
function isChildrenProp(prop) {
272+
return (
273+
t.isJSXAttribute(prop) &&
274+
t.isJSXIdentifier(prop.name) &&
275+
prop.name.name === 'children'
276+
);
277+
}
278+
271279
// Builds props for React.jsx. This function adds children into the props
272280
// and ensures that props is always an object
273281
function buildJSXOpeningElementAttributes(attribs, file, children) {
274282
let _props = [];
275283
const objs = [];
284+
const hasChildren = children && children.length > 0;
276285

277286
const useBuiltIns = file.opts.useBuiltIns || false;
278287
if (typeof useBuiltIns !== 'boolean') {
@@ -287,14 +296,22 @@ You can turn on the 'throwIfNamespace' flag to bypass this warning.`,
287296
if (t.isJSXSpreadAttribute(prop)) {
288297
_props = pushProps(_props, objs);
289298
objs.push(prop.argument);
299+
} else if (hasChildren && isChildrenProp(prop)) {
300+
// In order to avoid having duplicate "children" keys, we avoid
301+
// pushing the "children" prop if we have actual children. Instead
302+
// we put the children into a separate object and then rely on
303+
// the Object.assign logic below to ensure the correct object is
304+
// formed.
305+
_props = pushProps(_props, objs);
306+
objs.push(t.objectExpression([convertAttribute(prop)]));
290307
} else {
291308
_props.push(convertAttribute(prop));
292309
}
293310
}
294311

295312
// In React.JSX, children is no longer a separate argument, but passed in
296313
// through the argument object
297-
if (children && children.length > 0) {
314+
if (hasChildren) {
298315
if (children.length === 1) {
299316
_props.push(t.objectProperty(t.identifier('children'), children[0]));
300317
} else {

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.