-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
fix: skip flattening spread object with __proto__ #14759
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
Changes from all commits
35da60b
9e3b5c8
f88943e
04d88f2
da9ecda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, shorthand: false }) && | ||
| (t.isIdentifier(value.key, { name: "__proto__" }) || | ||
| t.isStringLiteral(value.key, { value: "__proto__" })), | ||
| ); | ||
| } | ||
|
|
||
| export interface Options { | ||
| filter?: (node: t.Node, pass: PluginPass) => boolean; | ||
| importSource?: string; | ||
|
|
@@ -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)) { | ||
| array.push(...arg.properties); | ||
| } else { | ||
| array.push(t.spreadElement(arg)); | ||
|
|
@@ -718,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) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The check here is necessary if we have pattern |
||
| ) | ||
| ) { | ||
| return objs[0]; | ||
| } | ||
| } | ||
|
|
||
| // looks like we have multiple objects | ||
|
|
@@ -755,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) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <p {...{__proto__: null}}>text</p>; | ||
|
|
||
| <div {...{"__proto__": null}}>{contents}</div>; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { jsx as _jsx } from "react/jsx-runtime"; | ||
|
|
||
| /*#__PURE__*/ | ||
| _jsx("p", { ...{ | ||
| __proto__: null | ||
| }, | ||
| children: "text" | ||
| }); | ||
|
|
||
| /*#__PURE__*/ | ||
| _jsx("div", { ...{ | ||
| "__proto__": null | ||
| }, | ||
| children: contents | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <p {...{__proto__: null}}>text</p>; | ||
|
|
||
| <div {...{"__proto__": null}}>{contents}</div>; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "BABEL_8_BREAKING": false | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /*#__PURE__*/ | ||
| React.createElement("p", babelHelpers.extends({ | ||
| __proto__: null | ||
| }), "text"); | ||
|
|
||
| /*#__PURE__*/ | ||
| React.createElement("div", babelHelpers.extends({ | ||
| "__proto__": null | ||
| }), contents); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <p {...{__proto__: null}}>text</p>; | ||
|
|
||
| <div {...{"__proto__": null}}>{contents}</div>; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "BABEL_8_BREAKING": true | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /*#__PURE__*/ | ||
| React.createElement("p", { ...{ | ||
| __proto__: null | ||
| } | ||
| }, "text"); | ||
|
|
||
| /*#__PURE__*/ | ||
| React.createElement("div", { ...{ | ||
| "__proto__": null | ||
| } | ||
| }, contents); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <p __proto__={null} class="bar">text</p>; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both TS and Babel interpret This PR does not change such behaviour, I added a new test case. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /*#__PURE__*/ | ||
| React.createElement("p", { | ||
| __proto__: null, | ||
| class: "bar" | ||
| }, "text"); |
Uh oh!
There was an error while loading. Please reload this page.