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

fix: skip flattening spread object with __proto__ #14759

Merged
merged 5 commits into from Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/babel-plugin-transform-react-jsx/src/create-plugin.ts
Expand Up @@ -39,6 +39,15 @@ const get = (pass: PluginPass, name: string) =>
const set = (pass: PluginPass, name: string, v: any) =>
pass.set(`@babel/plugin-react-jsx/${name}`, v);

function hasProto(node: t.ObjectExpression) {
return node.properties.some(
value =>
t.isObjectProperty(value, { computed: false }) &&
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
(t.isIdentifier(value.key, { name: "__proto__" }) ||
t.isStringLiteral(value.key, { value: "__proto__" })),
);
}

export interface Options {
filter?: (node: t.Node, pass: PluginPass) => boolean;
importSource?: string;
Expand Down Expand Up @@ -422,7 +431,7 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`,
if (t.isJSXSpreadAttribute(attribute.node)) {
const arg = attribute.node.argument;
// Collect properties into props array if spreading object expression
if (t.isObjectExpression(arg)) {
if (t.isObjectExpression(arg) && !hasProto(arg)) {
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
array.push(...arg.properties);
} else {
array.push(t.spreadElement(arg));
Expand Down
Expand Up @@ -5,3 +5,7 @@
<img alt="" {...{src, title}} />;

<blockquote {...{cite}}>{items}</blockquote>;

<pre {...{["__proto__"]: null }}></pre>;

<code {...{[__proto__]: null }}></code>;
Expand Up @@ -22,3 +22,13 @@ _jsx("blockquote", {
cite,
children: items
});

/*#__PURE__*/
_jsx("pre", {
["__proto__"]: null
});

/*#__PURE__*/
_jsx("code", {
[__proto__]: null
});
@@ -0,0 +1,7 @@
var __proto__ = null;

<p {...{__proto__: null}}>text</p>;

<div {...{"__proto__": null}}>{contents}</div>;

<img alt="" {...{__proto__}} />;
@@ -0,0 +1,24 @@
import { jsx as _jsx } from "react/jsx-runtime";
var __proto__ = null;

/*#__PURE__*/
_jsx("p", { ...{
__proto__: null
},
children: "text"
});

/*#__PURE__*/
_jsx("div", { ...{
"__proto__": null
},
children: contents
});

/*#__PURE__*/
_jsx("img", {
alt: "",
...{
__proto__
}
});
Expand Up @@ -5,3 +5,7 @@
<img alt="" {...{src, title}} />;

<blockquote {...{cite}}>{items}</blockquote>;

<pre {...{["__proto__"]: null }}></pre>;

<code {...{[__proto__]: null }}></code>;
Expand Up @@ -15,3 +15,13 @@ React.createElement("img", {
React.createElement("blockquote", {
cite
}, items);

/*#__PURE__*/
React.createElement("pre", {
["__proto__"]: null
});

/*#__PURE__*/
React.createElement("code", {
[__proto__]: null
});
@@ -0,0 +1,7 @@
var __proto__ = null;

<p {...{__proto__: null}}>text</p>;

<div {...{"__proto__": null}}>{contents}</div>;

<img alt="" {...{__proto__}} />;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should behave like the computed fields, could you move it to packages/babel-plugin-transform-react-jsx/test/fixtures/react/flattens-spread/input.js?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I was gonna update test fixtures when I read your last comment, but then I got a busy weekend.

@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}
@@ -0,0 +1,18 @@
var __proto__ = null;

/*#__PURE__*/
React.createElement("p", {
__proto__: null
}, "text");

/*#__PURE__*/
React.createElement("div", {
"__proto__": null
}, contents);

/*#__PURE__*/
React.createElement("img", babelHelpers.extends({
alt: ""
}, {
__proto__
}));
@@ -0,0 +1,7 @@
var __proto__ = null;

<p {...{__proto__: null}}>text</p>;

<div {...{"__proto__": null}}>{contents}</div>;

<img alt="" {...{__proto__}} />;
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}
@@ -0,0 +1,19 @@
var __proto__ = null;

/*#__PURE__*/
React.createElement("p", {
__proto__: null
}, "text");

/*#__PURE__*/
React.createElement("div", {
"__proto__": null
}, contents);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should spreadElement be used here?
It seems to be the expected behavior in #14756 (comment).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Yeah the spread element should not be stripped. I will investigate.


/*#__PURE__*/
React.createElement("img", {
alt: "",
...{
__proto__
}
});