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

transform-react-jsx with runtime: automatic should always using jsx-runtime #12177

Closed
morlay opened this issue Oct 15, 2020 · 6 comments
Closed
Labels
i: bug i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@morlay
Copy link

morlay commented Oct 15, 2020

Bug Report

"@babel/plugin-transform-react-jsx" ^7.10.4

[ "@babel/plugin-transform-react-jsx", { "runtime": "automatic"  }]

Current behavior
fallbacked classic runtime when jsx use spread with key exists

_createElement("span", {...obj, key: "key"})

Input Code

<span {...obj} key={"key"} />

Expected behavior

_jsx("span", obj, "key")

Environment

  • Babel version(s): 7.12.0
  • Node/npm version: 14.12.0
  • OS: [e.g. OSX 10.13.4, Windows 10]
  • Monorepo: yes
  • How you are using Babel: cli, register, loader

Possible Solution

may transform codes like

<span key={key} {...obj} /> => jsx("span", obj, key)
<span {...obj} key={key} /> => jsx("span", {...obj, key}, key)

to support https://github.com/babel/babel/blob/master/packages/babel-helper-builder-react-jsx-experimental/src/index.js#L270

it will be friendly for custom jsx-runtime and some react-like lib

Additional context

@babel-bot
Copy link
Collaborator

Hey @morlay! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@AjayPoshak
Copy link
Contributor

key is also a prop, so it need to be merged with obj prop before passing it down to JSX function call. I believe this is the correct behavior.

@morlay
Copy link
Author

morlay commented Oct 15, 2020

I got it by the comments.

  // We want to use React.createElement, even in the case of
  // jsx, for <div {...props} key={key} /> to distinguish it
  // from <div key={key} {...props} />. This is an intermediary
  // step while we deprecate key spread from props. Afterwards,
  // we will stop using createElement in the transform.

https://github.com/babel/babel/blob/master/packages/babel-helper-builder-react-jsx-experimental/src/index.js#L270

But it will be confused for adding custom jsx-runtime like emotion-js/emotion#1970

have to expose as

import { createElement } from "@emotion/core"
import { jsx, jsxs, Fragment } from "@emotion/core/jsx-runtime"

@morlay
Copy link
Author

morlay commented Oct 15, 2020

key prop not be used in react component for now
i think transfroming to _jsx("span", obj, "key") is safe.

When we want to use key from prop.
we should migrate to jsx-runtime first. then just put key into obj for spread, like

const obj = {
    key: "key"
}

<div {...obj} key={key} />

The rule <div key={key} {...obj} /> using jsx but <div {...obj} key={key} /> using createElement
i think we need a new eslint-rule to lint it 😆

@morlay
Copy link
Author

morlay commented Oct 15, 2020

or like this:

<div key={key} {...obj} /> => jsx("div", obj, key)
<div {...obj} key={key} /> => jsx("div", {...obj, key}, key)

Should got same behavior as the comments expect

@morlay morlay changed the title transform-react-jsx with runtime: automatic fallback createElement when jsx use spread with key exists. transform-react-jsx with runtime: automatic should not createElement when jsx use spread with key exists. Oct 15, 2020
@morlay morlay changed the title transform-react-jsx with runtime: automatic should not createElement when jsx use spread with key exists. transform-react-jsx with runtime: automatic should always using jsx-runtime Oct 15, 2020
@morlay
Copy link
Author

morlay commented Oct 17, 2020

clear enough facebook/react#20031 (comment)

@morlay morlay closed this as completed Oct 17, 2020
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jan 16, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug i: needs triage outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

3 participants