Skip to content

Commit dab92ff

Browse files
committed
feature: @putout/plugin-react: rename-jsx-to-js: add
1 parent 1994443 commit dab92ff

File tree

16 files changed

+138
-8
lines changed

16 files changed

+138
-8
lines changed

packages/plugin-react/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Add `.putout.json` with:
3030
-[remove-useless-forward-ref](#remove-useless-forward-ref);
3131
-[remove-implicit-ref-return](#remove-implicit-ref-return);
3232
-[rename-js-to-jsx](#rename-js-to-jsx);
33+
-[rename-jsx-to-js](#rename-jsx-to-js);
3334

3435
## Config
3536

@@ -41,7 +42,9 @@ Here is list of rules:
4142
"react/apply-create-root": "on",
4243
"react/remove-useless-provider": "on",
4344
"react/remove-useless-forward-ref": "on",
44-
"react/remove-implicit-ref-return": "on"
45+
"react/remove-implicit-ref-return": "on",
46+
"react/rename-jsx-to-js": "on",
47+
"react/rename-js-to-jsx": "on"
4548
}
4649
}
4750
```
@@ -184,6 +187,20 @@ Rename `*.js` files to `*.jsx` when they contains JSX.
184187

185188
Check out in 🐊[Putout Editor](https://putout.cloudcmd.io/#/gist/bebaba6a03958effd72f160f9ef8c8ef/e3a275a2d6352183f71415dcd4346f2cd5667748).
186189

190+
## rename-jsx-to-js
191+
192+
Rename `*.jsx` files to `*.js` when they contains JSX.
193+
194+
```diff
195+
/
196+
|-- package.json
197+
`-- lib/
198+
- `-- hello.jsx
199+
+ `-- hello.js
200+
```
201+
202+
Check out in 🐊[Putout Editor](https://putout.cloudcmd.io/#/gist/20bb4c5e3646ebbebccdc23bc93224c1/a0216b8fce6dce41fce16534d80354d9d94c6983).
203+
187204
### ❌ Example of incorrect code
188205

189206
```jsx

packages/plugin-react/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ const removeImplicitRefReturn = require('./remove-implicit-ref-return');
55
const removeUselessForwardRef = require('./remove-useless-forward-ref');
66
const applyCreateRoot = require('./apply-create-root');
77
const renameJsToJsx = require('./rename-js-to-jsx');
8+
const renameJsxToJs = require('./rename-jsx-to-js');
89

910
module.exports.rules = {
1011
'remove-useless-provider': removeUselessProvider,
1112
'remove-implicit-ref-return': removeImplicitRefReturn,
1213
'remove-useless-forward-ref': removeUselessForwardRef,
1314
'apply-create-root': applyCreateRoot,
1415
'rename-js-to-jsx': ['off', renameJsToJsx],
16+
'rename-jsx-to-js': ['off', renameJsxToJs],
1517
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const a = (
2+
<A></A>
3+
);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
const noop = () => {};
4+
5+
module.exports.report = () => '';
6+
module.exports.fix = noop;
7+
module.exports.include = () => ['JSXElement'];
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const {createTest} = require('@putout/test');
4+
const plugin = require('.');
5+
6+
const test = createTest(__dirname, {
7+
plugins: [
8+
['is-jsx', plugin],
9+
],
10+
});
11+
12+
test('rename-js-to-jsx: is-jsx: report', (t) => {
13+
t.report('is-jsx', ``);
14+
t.end();
15+
});
16+
17+
test('rename-js-to-jsx: is-jsx: no transform', (t) => {
18+
t.noTransform('is-jsx');
19+
t.end();
20+
});

packages/plugin-react/lib/rename-js-to-jsx/index.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
'use strict';
22

33
const {operator} = require('putout');
4-
const noop = () => {};
5-
const {matchFiles} = operator;
64

7-
const isJSX = {
8-
report: () => '',
9-
fix: noop,
10-
include: () => ['JSXElement'],
11-
};
5+
const isJSX = require('../is-jsx');
6+
const {matchFiles} = operator;
127

138
module.exports = matchFiles({
149
filename: '*.js',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__putout_processor_filesystem([
2+
"/",
3+
["/1.js", "const a = 5;"],
4+
["/2.js", "Y29uc3QgYSA9ICgKICAgIDxhPjwvYT4KKTsK"]
5+
]);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__putout_processor_filesystem([
2+
'/',
3+
['/1.js', 'const a = 5;'],
4+
['/2.jsx', 'const a = <a></a>;'],
5+
]);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const {operator} = require('putout');
4+
const isJSX = require('../is-jsx');
5+
6+
const {matchFiles} = operator;
7+
8+
module.exports = matchFiles({
9+
filename: '*.jsx',
10+
files: {
11+
'__name.jsx -> __name.js': isJSX,
12+
},
13+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const {createTest} = require('@putout/test');
4+
const plugin = require('.');
5+
6+
const test = createTest(__dirname, {
7+
plugins: [
8+
['rename-jsx-to-js', plugin],
9+
],
10+
});
11+
12+
test('react: rename-jsx-to-js: report', (t) => {
13+
t.report('rename-jsx-to-js', ``);
14+
t.end();
15+
});
16+
17+
test('react: rename-jsx-to-js: transform', (t) => {
18+
t.transform('rename-jsx-to-js');
19+
t.end();
20+
});

0 commit comments

Comments
 (0)