Skip to content

chore: add react/create-element-to-jsx codemod to codemods and react-utils to project tooling #14

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

Merged
merged 5 commits into from
Dec 18, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions codemods/react/create-element-to-jsx/.codemodrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json",
"name": "react/create-element-to-jsx",
"description": "Transform React.createElement to JSX",
"version": "1.0.0",
"engine": "jscodeshift",
"private": false,
"arguments": [],
"meta": {
"tags": ["react", "migration"],
"git": "https://github.com/reactjs/react-codemod/tree/master/transforms"
}
}
2 changes: 2 additions & 0 deletions codemods/react/create-element-to-jsx/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
21 changes: 21 additions & 0 deletions codemods/react/create-element-to-jsx/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2015-present, Facebook, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
25 changes: 25 additions & 0 deletions codemods/react/create-element-to-jsx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
This codemod transforms React.createElement calls into JSX syntax, making your code more readable and maintainable.

## Example

### Before

```tsx
return React.createElement(
"div",
{ className: "container" },
React.createElement("h1", null, "Hello World"),
React.createElement("p", { style: { color: "blue" } }, "Welcome to React")
);
```

### After

```tsx
return (
<div className="container">
<h1>Hello World</h1>
<p style={{ color: "blue" }}>Welcome to React</p>
</div>
);
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

function App() {
return React.createElement('div', null, 'Hello World');
}

export default App;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';

function App() {
return (
<div>
Hello World
</div>
);
}

export default App;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';

function Button({ disabled }) {
return React.createElement(
'button',
{
className: 'btn',
onClick: () => console.log('clicked'),
disabled
},
'Click me'
);
}

export default Button;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';

function Button({ disabled }) {
return (
<button
className="btn"
onClick={() => console.log('clicked')}
disabled={disabled}>
Click me
</button>
);
}

export default Button;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';

const Button = ({ onClick, children }) => React.createElement(
'button',
{ onClick },
children
);

function App() {
return React.createElement(
'div',
{ className: 'container', id: 'main' },
React.createElement(
'h1',
{ style: { color: 'blue' } },
'Hello World'
),
React.createElement(
Button,
{ onClick: () => console.log('clicked') },
'Click me'
)
);
}

export default App;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';

const Button = ({ onClick, children }) => <button onClick={onClick}>
{children}
</button>;

function App() {
return (
<div className="container" id="main">
<h1 style={{ color: 'blue' }}>
Hello World
</h1>
<Button onClick={() => console.log('clicked')}>
Click me
</Button>
</div>
);
}

export default App;
Loading
Loading