- Notifications
You must be signed in to change notification settings - Fork 300
Adding a codemod for transforming createElement function calls to JSX #9
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
Conversation
d02fb83
to b18da9b
Compare There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following alphabetical order
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
test/create-element-to-jsx-props.js Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add these two cases:
React.createElement(Foo, {foo: 'bar', ...someObject})
to<Foo foo="bar" {...someObject}>
– where the order of properties should be preserved, of course.- Convert children-less components to
<Foo />
instead of<Foo></Foo>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it correct that
var a = React.createElement(Foo, null); var b = React.createElement("div", null);
should convert to:
var a = <Foo />; var b = <div></div>;
Or should div be a self closing element too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they should both convert to a single tag that closes, like <div />
.
This is awesome! I left a couple of inline comments and I'll set up eslint for this repo. Two more minor things:
|
b18da9b
to e6e0097
Compare
|
oh yeah, can you also flatten your commits into a single one? |
4ccb2c1
to 11b4bd8
Compare transforms/create-element-to-jsx.js Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can just inline this replaceWith
below:
root.find(...).replaceWith(...).size()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As in combining this and the stuff on line 76?
EDIT replaceWith
will automatically iterate over the items? No need for the forEach
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, you don't need the call to j
, replaceWith
is part of every collection so you can just call it directly instead of forEach
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat.
I like no-parens around single-argument-identifiers in arrows but I'm definitely not going to fight you over that :P @zpao will love you, actually, so this is just about who you wanna score points with. |
If single-argument-identifiers was enforced one way or the other by eslint I'd be happy to follow it. :) |
11b4bd8
to b5f84d3
Compare transforms/create-element-to-jsx.js Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rm this empty line please :)
Alright this looks really solid. I left a couple of inline comments but take your pick on what you actually wanna update – these are mostly just suggestions. The only thing I'd like to see is to copy this: https://github.com/reactjs/react-codemod/blob/master/transforms/class.js#L500 to make sure it only runs on code that actually has React in scope and doesn't have a |
I'll fix the blank line, and ignore the destructuring. Can you provide a test case that would fail without that check on React in scope? |
It should be something like doing |
I also promise to consolidate all that setup code some time, it sucks to copy-paste it. |
b5f84d3
to 2b85bb1
Compare Currently failing for the case of
Should my test expect that to return the original contents untransformed, or an empty string as it is now? Also, I don't see any tests for any of the other transforms that test the behavior of |
yeah it is ok not to test it. If the transform returns null it means that it won't apply any transformations, so you should test for an empty string (and maybe add the coercion from null to an empty string in |
2b85bb1
to 6106750
Compare 6106750
to 968a6e8
Compare Adding a codemod for transforming createElement function calls to JSX
yay! |
Transforms
to
This codemod is useful for people who start out without using JSX and want to convert their codebase to JSX later.