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): restore-jsx bug when component name is lowercase #6110

Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,10 @@ describe('babel-restore-jsx', () => {
)
).toMatchInlineSnapshot(`"<h1>{foo ? <p /> : null}</h1>;"`)
})

it('should handle lowercase component names', () => {
expect(jsx('React.createElement(aaa)')).toMatchInlineSnapshot(
`"React.createElement(aaa);"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure if leaving it untouched will work. Have you tested this in a real scenario?

Another option is to not use libraries that minify themselves ;P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure if leaving it untouched will work. Have you tested this in a real scenario?

I tried in dev and build, in SSR and in the browser on a mid-sized project, works for me™ :)

In any case, I would consider it a bug if it didn't. It's not illegal to mix the new JSX runtime and React.createElement. In fact, createElement is still the recommended way in some cases. The blog post that introduced the new runtime specifically says:

If you need to manually create elements in your code, you should keep using React.createElement.

So things like React.createElement(...args) should (and does) keep working.

Another option is to not use libraries that minify themselves ;P

Fair, but they're still widespread in the ecosystem :)

)
})
})
6 changes: 3 additions & 3 deletions packages/plugin-react/src/jsx-runtime/babel-restore-jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default function ({ types: t }: typeof babel): babel.PluginObj {
return null
}

const name = getJSXIdentifier(node)
const name = getJSXIdentifier(node, true)
if (name != null) {
return name
}
Expand Down Expand Up @@ -152,9 +152,9 @@ export default function ({ types: t }: typeof babel): babel.PluginObj {
return children
}

function getJSXIdentifier(node: any) {
function getJSXIdentifier(node: any, tag = false) {
//TODO: JSXNamespacedName
if (t.isIdentifier(node)) {
if (t.isIdentifier(node) && (!tag || node.name.match(/^[A-Z]/))) {
return t.jsxIdentifier(node.name)
}
if (t.isStringLiteral(node)) {
Expand Down