Repository navigation
Fix: incorrect transform jsx-runtime in development - #2741
Donny/강동윤 (kdy1) merged 46 commits into
Conversation
Donny/강동윤 (kdy1)
left a comment
There was a problem hiding this comment.
This is not enough.
You have to handle special attributes like __source, too.
19065d3 to
e0fe330
Compare
|
Oh! Sorry, I overlooked it 🙇 I updated code with passing |
Donny/강동윤 (kdy1)
left a comment
There was a problem hiding this comment.
I think there's are small issues with jsx directives.
| @@ -596,13 +600,62 @@ where | |||
|
|
|||
| self.top_level_node = top_level_node; | |||
|
|
|||
There was a problem hiding this comment.
I think this code will error if /* @jsxRuntime automatic */ is used.
There was a problem hiding this comment.
Because of this, the babel plugin detect attributes named __source and __self and change those into arguments.
There was a problem hiding this comment.
I’m sorry for that I did not know about jsx directives.
Because of this, the babel plugin detect attributes named __source and __self and change those into arguments.
Is this the process below?
I am worried if my understanding is correct, but should I fix the code to set __source and __self props as JSX attribute in development mode as usual?
There was a problem hiding this comment.
Sorry for late response, I missed the email.
Is this the process below?
Yes.
Should I fix the code to set __source and __self props as JSX attribute in development mode as usual?
I think so, as it's what babel does
There was a problem hiding this comment.
Thank you for your reply and help!
I fixed code like below
- set
__sourceand__selfto props withjsx_srcandjsx_self(as usual) - As babel plugin does, with
runtime: automaticanddevelopment: trueconfiguration, swc extract__sourceand__selffrom JSX’s attributes and set it as_jsxDEV’s arguments.
it looks like there are not test settings to test react module(= jsx with jsx_src and jsx_self) with a specific configuration, so I added integration testing directory to test react module with a specific configuration.
If there is yet something I missed, I will be happy if you pointed it out.
fddc954 to
6351e01
Compare
| } | ||
|
|
||
| if !use_create_element | ||
| && i.sym == String::from("__source") |
There was a problem hiding this comment.
String::from("__source") is very inefficient.
You can compare JsWord to str like *i.sym === "__source ".
| }; | ||
|
|
||
| let mut key = None; | ||
| let mut __source = None; |
There was a problem hiding this comment.
I think variable name should not use _ as a prefix as it has special meaning in rust.
There was a problem hiding this comment.
👍 7fcafd2
I replaced __source with source_props, and __self with self_props.
| Some(key) => key, | ||
| None => ExprOrSpread { | ||
| spread: None, | ||
| expr: Box::new(build_undefined_literal_value()), |
There was a problem hiding this comment.
Please use undefined(DUMMY_SP) instead.
There was a problem hiding this comment.
I did not notice undefined util module 🙇
👍 0ac5ded
| }) | ||
| .as_arg(), | ||
| )) | ||
| .chain(Some(__source)) |
There was a problem hiding this comment.
There's std::iter::once and it's better than using Some to create an iterator.
|
Code changes look fine to me, but can you add some more tests? |
|
Thank you! I've added some tests on this, but I am wondering what more tests to add. Can you share what happened or error logs when Next.js was broken? (I searched for the Next.js issue but couldn't find it) |
|
Shinobu Hayashi (@Shinyaigeek) vercel/next.js#30302 (comment) |
Donny/강동윤 (kdy1)
left a comment
There was a problem hiding this comment.
Thanks!
I found some tiny issues.
| ] | ||
| }, | ||
| void 0, | ||
| true); No newline at end of file |
There was a problem hiding this comment.
I got different output from babel.
var _jsxFileName = "/Users/kdy1/projects/lab/src/input.js";
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
import { Fragment as _Fragment } from "react/jsx-dev-runtime";
var x = /*#__PURE__*/_jsxDEV(_Fragment, {
children: [/*#__PURE__*/_jsxDEV("div", {
children: "hoge"
}, void 0, false, {
fileName: _jsxFileName,
lineNumber: 3,
columnNumber: 9
}, this), /*#__PURE__*/_jsxDEV("div", {
children: "fuga"
}, void 0, false, {
fileName: _jsxFileName,
lineNumber: 4,
columnNumber: 9
}, this)]
}, void 0, true);Seems like children of fragments are not visited?
There was a problem hiding this comment.
Can you add e.visit_mut_children_with(self) to
There was a problem hiding this comment.
I think this works. jsxdev-args-with-gragment test is placed in fixture, not in integration. This means jsx-src and jsx-self does not work while this test run. This output will be the same with babel’s output if we move this test into integration. I think this is a little confusing, so I moved this test case from fixture into integration in fdaaaa2
|
I’m sorry I did not understand why #2542 broke next.js (vercel/next.js#30302 (comment)). I think there are enough test cases for this change, but I am also worried that this change will break next.js as vercel/next.js#30302 (comment) . Is there any documentation for running Next.js tests with the local version of swc? (I rewrote Cargo.toml of |
|
Shinobu Hayashi (@Shinyaigeek) Same here. I don't understand the reason. I think in this case it will be fine, as you added tests for all input. But if you want to try it, you can patch all swc dependencies in https://github.com/vercel/next.js/tree/c004322d138e1949437efb0b80e0973f3a3775cf/packages/next-swc/crates to use git or path dependency targeting your own branch and create a PR. |
|
But I really think it will be fine in this case, as we tested all input. |
|
Donny/강동윤 (@kdy1) I see. It seems to be a tough task to test with |
|
Yeah, I think it is better. Thanks for the pr again and I'll review it soon |
Donny/강동윤 (kdy1)
left a comment
There was a problem hiding this comment.
Thanks! I found one perf/code style issue.
|
|
||
| let args = if self.development { | ||
| args.into_iter() | ||
| .chain( |
There was a problem hiding this comment.
I think .chain(once(undefined(DUMMY_SP).as_arg())).chain(once(Lit::Bool(Bool { span: DUMMY_SP, value: use_jsxs, }) .as_arg())) would be better.
There was a problem hiding this comment.
OK 👍 Thank you for helping! I fixed in 5be5cf0
## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint` This PR applies - swc-project/swc#2741 This fixes `development` mode of jsx.
Motivation 🔥
Fix: #2656
Currently, swc does not transform JSX correctly with new JSX Transform in development mode. It is importing
jsxfromjsx-runtimewhen it should be importingjsxDEVfromjsx-dev-runtime. (The same applies tojsxs).I fixed this.