diff --git a/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.spec.ts b/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.spec.ts index e3ffc7703b40c4..e923af5393a2c2 100644 --- a/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.spec.ts +++ b/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.spec.ts @@ -36,14 +36,14 @@ describe('babel-restore-jsx', () => { it('should handle props without children', () => { expect(jsx('React.createElement("h1", {hi: there})')).toMatchInlineSnapshot( - `"

;"` + '"

;"' ) expect( jsx('React.createElement("h2", {"hi": there})') - ).toMatchInlineSnapshot(`"

;"`) + ).toMatchInlineSnapshot('"

;"') expect( jsx('React.createElement("h3", {hi: "there"})') - ).toMatchInlineSnapshot(`"

;"`) + ).toMatchInlineSnapshot('"

;"') }) it('should handle spread props', () => { @@ -58,19 +58,19 @@ describe('babel-restore-jsx', () => { it('should handle mixed props', () => { expect( jsx('React.createElement("h1", _extends({ hi: "there" }, props))') - ).toMatchInlineSnapshot(`"

;"`) + ).toMatchInlineSnapshot('"

;"') expect( jsx('React.createElement("h1", _extends({}, props, { hi: "there" }))') - ).toMatchInlineSnapshot(`"

;"`) + ).toMatchInlineSnapshot('"

;"') expect( jsx('React.createElement("h1", { ...props, hi: "there" })') - ).toMatchInlineSnapshot(`"

;"`) + ).toMatchInlineSnapshot('"

;"') }) it('should handle props and ignore “null”/“undefined” children', () => { expect( jsx('React.createElement("h1", {hi: there}, null, undefined)') - ).toMatchInlineSnapshot(`"

;"`) + ).toMatchInlineSnapshot('"

;"') }) it('should ignore “null”/“undefined” props and handle children', () => { @@ -93,7 +93,7 @@ describe('babel-restore-jsx', () => { //we extensively tested props and children separately, so only sth. basic expect( jsx('React.createElement("h1", {hi: there}, "Header")') - ).toMatchInlineSnapshot(`"

Header

;"`) + ).toMatchInlineSnapshot('"

Header

;"') }) it('should ignore intermingled “null”/“undefined” children', () => { @@ -116,27 +116,17 @@ describe('babel-restore-jsx', () => { ) }) - it('should not handle contains __self prop', () => { - expect(jsx('React.createElement(Provider, { __self: this })')) - .toMatchInlineSnapshot(` - "React.createElement(Provider, { - __self: this - });" - `) + it('should remove __self prop', () => { + expect( + jsx('React.createElement(Provider, { __self: this })') + ).toMatchInlineSnapshot('";"') }) - it('should not handle contains __source prop', () => { + it('should remove __source prop', () => { expect( jsx( - 'React.createElement(Provider, { __source: { fileName: _jsxFileName, lineNumber: 133 }})' + `React.createElement(Provider, { __source: { fileName: 'jsxFileName', lineNumber: 133 }})` ) - ).toMatchInlineSnapshot(` - "React.createElement(Provider, { - __source: { - fileName: _jsxFileName, - lineNumber: 133 - } - });" - `) + ).toMatchInlineSnapshot('";"') }) }) diff --git a/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.ts b/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.ts index b38ae08dbc7dfd..cf429207a48ad7 100644 --- a/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.ts +++ b/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.ts @@ -18,16 +18,6 @@ import type * as babel from '@babel/core' * Any of those arguments might also be missing (undefined) and/or invalid. */ export default function ({ types: t }: typeof babel): babel.PluginObj { - /** - * If the props contains '__self' or '__source', it is not transform to JSX. - */ - const isInvalidProps = (props: any[]) => - props.some( - (prop) => - t.isJSXIdentifier(prop.name) && - (prop.name.name === '__self' || prop.name.name === '__source') - ) - /** * Get a `JSXElement` from a `CallExpression`. * Returns `null` if this impossible. @@ -46,7 +36,7 @@ export default function ({ types: t }: typeof babel): babel.PluginObj { } const props = getJSXProps(propsNode) - if (props == null || isInvalidProps(props)) { + if (props == null) { return null //no props → [], invalid → null } @@ -129,14 +119,20 @@ export default function ({ types: t }: typeof babel): babel.PluginObj { if (!isPlainObjectExpression(node)) { return null } - return node.properties.map((prop: any) => - t.isObjectProperty(prop) - ? t.jsxAttribute( - getJSXIdentifier(prop.key)!, - getJSXAttributeValue(prop.value) - ) - : t.jsxSpreadAttribute(prop.argument) - ) + return node.properties + .filter( + (prop: any) => + t.isJSXIdentifier(prop.name) && + (prop.name.name === '__self' || prop.name.name === '__source') + ) + .map((prop: any) => + t.isObjectProperty(prop) + ? t.jsxAttribute( + getJSXIdentifier(prop.key)!, + getJSXAttributeValue(prop.value) + ) + : t.jsxSpreadAttribute(prop.argument) + ) } function getJSXChild(node: any) {