diff --git a/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts b/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts index 352507157f21..6562844486df 100644 --- a/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts +++ b/packages/babel-plugin-transform-react-jsx/src/create-plugin.ts @@ -727,7 +727,17 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`, } if (objs.length === 1) { - return objs[0]; + if ( + !( + t.isSpreadElement(props[0]) && + // If an object expression is spread element's argument + // it is very likely to contain __proto__ and we should stop + // optimizing spread element + t.isObjectExpression(props[0].argument) + ) + ) { + return objs[0]; + } } // looks like we have multiple objects @@ -764,7 +774,12 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`, accumulateAttribute(props, attr); } - return props.length === 1 && t.isSpreadElement(props[0]) + return props.length === 1 && + t.isSpreadElement(props[0]) && + // If an object expression is spread element's argument + // it is very likely to contain __proto__ and we should stop + // optimizing spread element + !t.isObjectExpression(props[0].argument) ? props[0].argument : props.length > 0 ? t.objectExpression(props) diff --git a/packages/babel-plugin-transform-react-jsx/test/fixtures/react/handle-spread-with-proto-babel-7/output.js b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/handle-spread-with-proto-babel-7/output.js index 456dad6b23ff..21ff33f71bf5 100644 --- a/packages/babel-plugin-transform-react-jsx/test/fixtures/react/handle-spread-with-proto-babel-7/output.js +++ b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/handle-spread-with-proto-babel-7/output.js @@ -1,14 +1,14 @@ var __proto__ = null; /*#__PURE__*/ -React.createElement("p", { +React.createElement("p", babelHelpers.extends({ __proto__: null -}, "text"); +}), "text"); /*#__PURE__*/ -React.createElement("div", { +React.createElement("div", babelHelpers.extends({ "__proto__": null -}, contents); +}), contents); /*#__PURE__*/ React.createElement("img", babelHelpers.extends({ diff --git a/packages/babel-plugin-transform-react-jsx/test/fixtures/react/handle-spread-with-proto/output.js b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/handle-spread-with-proto/output.js index 8cbd2578a629..bd4035962194 100644 --- a/packages/babel-plugin-transform-react-jsx/test/fixtures/react/handle-spread-with-proto/output.js +++ b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/handle-spread-with-proto/output.js @@ -1,13 +1,15 @@ var __proto__ = null; /*#__PURE__*/ -React.createElement("p", { - __proto__: null +React.createElement("p", { ...{ + __proto__: null + } }, "text"); /*#__PURE__*/ -React.createElement("div", { - "__proto__": null +React.createElement("div", { ...{ + "__proto__": null + } }, contents); /*#__PURE__*/ diff --git a/packages/babel-plugin-transform-react-jsx/test/fixtures/react/proto-in-jsx-attribute/input.js b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/proto-in-jsx-attribute/input.js new file mode 100644 index 000000000000..15228285e30e --- /dev/null +++ b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/proto-in-jsx-attribute/input.js @@ -0,0 +1 @@ +

text

; diff --git a/packages/babel-plugin-transform-react-jsx/test/fixtures/react/proto-in-jsx-attribute/output.js b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/proto-in-jsx-attribute/output.js new file mode 100644 index 000000000000..a975dc74f39e --- /dev/null +++ b/packages/babel-plugin-transform-react-jsx/test/fixtures/react/proto-in-jsx-attribute/output.js @@ -0,0 +1,5 @@ +/*#__PURE__*/ +React.createElement("p", { + __proto__: null, + class: "bar" +}, "text");