Skip to content

Commit

Permalink
refactor: remove instead of not transform
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Aug 26, 2022
1 parent 9950295 commit 11f1e0d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 33 deletions.
18 changes: 4 additions & 14 deletions packages/plugin-react/src/jsx-runtime/babel-restore-jsx.spec.ts
Expand Up @@ -117,26 +117,16 @@ 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('"<Provider />;"')
})

it('should not handle contains __source prop', () => {
expect(
jsx(
'React.createElement(Provider, { __source: { fileName: _jsxFileName, lineNumber: 133 }})'
)
).toMatchInlineSnapshot(`
"React.createElement(Provider, {
__source: {
fileName: _jsxFileName,
lineNumber: 133
}
});"
`)
).toMatchInlineSnapshot('"<Provider />;"')
})
})
34 changes: 15 additions & 19 deletions packages/plugin-react/src/jsx-runtime/babel-restore-jsx.ts
Expand Up @@ -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.
Expand All @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 11f1e0d

Please sign in to comment.