From 11f1e0d1b412f3a3a096ebd44e31870f6336531a Mon Sep 17 00:00:00 2001 From: "qing.deng" Date: Fri, 26 Aug 2022 14:01:47 +0800 Subject: [PATCH] refactor: remove instead of not transform --- .../src/jsx-runtime/babel-restore-jsx.spec.ts | 18 +++------- .../src/jsx-runtime/babel-restore-jsx.ts | 34 ++++++++----------- 2 files changed, 19 insertions(+), 33 deletions(-) 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..5590bfb9d7fa5f 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 @@ -117,12 +117,9 @@ describe('babel-restore-jsx', () => { }) it('should not handle contains __self prop', () => { - expect(jsx('React.createElement(Provider, { __self: this })')) - .toMatchInlineSnapshot(` - "React.createElement(Provider, { - __self: this - });" - `) + expect( + jsx('React.createElement(Provider, { __self: this })') + ).toMatchInlineSnapshot('";"') }) it('should not handle contains __source prop', () => { @@ -130,13 +127,6 @@ describe('babel-restore-jsx', () => { jsx( '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..527057e5e86a87 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 + .map((prop: any) => + t.isObjectProperty(prop) + ? t.jsxAttribute( + getJSXIdentifier(prop.key)!, + getJSXAttributeValue(prop.value) + ) + : t.jsxSpreadAttribute(prop.argument) + ) + .filter((prop: any) => + t.isJSXIdentifier(prop.name) + ? prop.name.name !== '__self' && prop.name.name !== '__source' + : true + ) } function getJSXChild(node: any) {