Skip to content

Commit

Permalink
fix single spread element optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jul 15, 2022
1 parent 9e3b5c8 commit f88943e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
19 changes: 17 additions & 2 deletions packages/babel-plugin-transform-react-jsx/src/create-plugin.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
@@ -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({
Expand Down
@@ -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__*/
Expand Down
@@ -0,0 +1 @@
<p __proto__={null} class="bar">text</p>;
@@ -0,0 +1,5 @@
/*#__PURE__*/
React.createElement("p", {
__proto__: null,
class: "bar"
}, "text");

0 comments on commit f88943e

Please sign in to comment.