Skip to content

Commit dd8671c

Browse files
authored
add update-react-import codemod (#262)
1 parent 1f04dd8 commit dd8671c

22 files changed

+217
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ guide](https://github.com/airbnb/javascript/blob/7684892951ef663e1c4e62ad57d662e
143143
npx react-codemod sort-comp <path>
144144
```
145145

146+
#### `update-react-imports`
147+
148+
[As of Babel 7.9.0](https://babeljs.io/blog/2020/03/16/7.9.0#a-new-jsx-transform-11154-https-githubcom-babel-babel-pull-11154), when using `runtime: automatic` in `@babel/preset-react` or `@babel/plugin-transform-react-jsx`, you will not need to explicitly import React for compiling jsx. This codemod removes the redundant import statements. It also converts (`import React from 'react'`) to named imports (`import * as React from 'react`).
149+
150+
```sh
151+
npx react-codemod update-react-imports <path>
152+
```
153+
146154
### Explanation of the new ES2015 class transform with property initializers
147155
1. Determine if mixins are convertible. We only transform a `createClass` call to an ES6 class component when:
148156
- There are no mixins on the class, or

bin/cli.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ const TRANSFORMER_INQUIRER_CHOICES = [
181181
name:
182182
'Reorders React component methods to match the ESLint react/sort-comp rule.',
183183
value: 'sort-comp'
184+
},
185+
{
186+
name: 'update-react-imports: Removes redundant import statements from explicitly importing React to compile JSX and converts default imports to named imports',
187+
value: 'update-react-imports',
184188
}
185189
];
186190

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React, { createElement, useState } from "react";
2+
3+
React.createElement('div', {});
4+
5+
<div>Hi</div>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { createElement, useState } from "react";
2+
import * as React from "react";
3+
4+
React.createElement('div', {});
5+
6+
<div>Hi</div>;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React, { type Element, createElement, useState } from "react";
2+
3+
<div>Hi</div>;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import type { Element } from "react";
2+
import { createElement, useState } from "react";
3+
4+
<div>Hi</div>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React, { type Element, type Component } from "react";
2+
3+
React.createElement('div', {});
4+
5+
<div>Hi</div>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { Element, Component } from "react";
2+
import * as React from "react";
3+
4+
React.createElement('div', {});
5+
6+
<div>Hi</div>;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React, { type Element } from "react";
2+
3+
<div>Hi</div>;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import type { Element } from "react";
2+
3+
<div>Hi</div>;

0 commit comments

Comments
 (0)