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(plugin-react): duplicate __self prop and __source prop #9387

Merged
merged 2 commits into from Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions packages/plugin-react/src/jsx-runtime/babel-restore-jsx.spec.ts
Expand Up @@ -115,4 +115,28 @@ describe('babel-restore-jsx', () => {
`"React.createElement(aaa);"`
)
})

it('should not handle contains __self prop', () => {
expect(jsx('React.createElement(Provider, { __self: this })'))
.toMatchInlineSnapshot(`
"React.createElement(Provider, {
__self: this
});"
`)
})

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
}
});"
`)
})
})
12 changes: 11 additions & 1 deletion packages/plugin-react/src/jsx-runtime/babel-restore-jsx.ts
Expand Up @@ -18,6 +18,16 @@ 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 @@ -36,7 +46,7 @@ export default function ({ types: t }: typeof babel): babel.PluginObj {
}

const props = getJSXProps(propsNode)
if (props == null) {
if (props == null || isInvalidProps(props)) {
aleclarson marked this conversation as resolved.
Show resolved Hide resolved
return null //no props → [], invalid → null
}

Expand Down