Skip to content

Commit f7d55e3

Browse files
committed
feature: @putout/plugin-putout: convert-include-to-traverse: add
1 parent d6a3892 commit f7d55e3

File tree

10 files changed

+131
-0
lines changed

10 files changed

+131
-0
lines changed

packages/plugin-putout/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ npm i @putout/plugin-putout -D
5151
-[convert-replace-with-multiple](#convert-replace-with-multiple);
5252
-[convert-report-to-function](#convert-report-to-function);
5353
-[convert-to-no-transform-code](#convert-to-no-transform-code);
54+
-[convert-include-to-traverse](#convert-include-to-traverse);
5455
-[convert-traverse-to-include](#convert-traverse-to-include);
5556
-[convert-traverse-to-replace](#convert-traverse-to-replace);
5657
-[convert-traverse-to-scan](#convert-traverse-to-scan);
@@ -104,6 +105,7 @@ npm i @putout/plugin-putout -D
104105
"putout/convert-babel-types": "on",
105106
"putout/convert-destructuring-to-identifier": "on",
106107
"putout/convert-node-to-path-in-get-template-values": "on",
108+
"putout/convert-include-to-traverse": "on",
107109
"putout/convert-traverse-to-include": "on",
108110
"putout/convert-traverse-to-replace": "on",
109111
"putout/convert-traverse-to-scan": "on",
@@ -623,6 +625,26 @@ const parseOptions = require('putout/lib/parse-options');
623625
const parseOptions = require('putout/parse-options');
624626
```
625627

628+
## convert-include-to-traverse
629+
630+
Checkout in 🐊[**Putout Editor*](https://putout.cloudcmd.io/#/gist/a41b5c943a74b59a6c18f31fc6d31937/d79c6084f49b6444536840bc2fc1fba725ba83f8).
631+
632+
### ❌ Example of incorrect code
633+
634+
```js
635+
export const include = () => ({
636+
ClassDeclaration(path) {},
637+
});
638+
```
639+
640+
### ✅ Example of correct code
641+
642+
```js
643+
export const traverse = () => ({
644+
ClassDeclaration(path) {},
645+
});
646+
```
647+
626648
## convert-traverse-to-include
627649

628650
### ❌ Example of incorrect code
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const report = () => ``;
2+
3+
export const fix = () => {};
4+
5+
module.exports.traverse = () => ({
6+
ClassDeclaration(path) {},
7+
});
8+
9+
export const traverse = () => ({
10+
ClassDeclaration(path) {},
11+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const report = () => ``;
2+
3+
export const fix = () => {
4+
}
5+
6+
module.exports.include = () => ({
7+
ClassDeclaration(path) {
8+
}
9+
});
10+
11+
export const include = () => ({
12+
ClassDeclaration(path) {
13+
}
14+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports.include = () => [
2+
'ClassDeclaration',
3+
];
4+
export const include = () => [
5+
'ClassDeclaration',
6+
];
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const {types} = require('putout');
4+
const {isObjectExpression} = types;
5+
6+
module.exports.report = () => `Use 'traverse' instead of 'include'`;
7+
8+
const check = ({__a}) => {
9+
return isObjectExpression(__a.body);
10+
};
11+
12+
module.exports.match = () => ({
13+
'const include = __a': check,
14+
'module.exports.include = __a': check,
15+
});
16+
17+
module.exports.replace = () => ({
18+
'const include = __a': 'const traverse = __a',
19+
'module.exports.include = __a': 'module.exports.traverse = __a',
20+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
const {createTest} = require('@putout/test');
4+
const plugin = require('.');
5+
6+
const test = createTest(__dirname, {
7+
printer: 'putout',
8+
plugins: [
9+
['convert-include-to-traverse', plugin],
10+
],
11+
});
12+
13+
test('putout: convert-include-to-traverse: report', (t) => {
14+
t.report('convert-include-to-traverse', `Use 'traverse' instead of 'include'`);
15+
t.end();
16+
});
17+
18+
test('putout: convert-include-to-traverse: transform', (t) => {
19+
t.transform('convert-include-to-traverse');
20+
t.end();
21+
});
22+
23+
test('putout: convert-include-to-traverse: no transform: not-object', (t) => {
24+
t.noTransform('not-object');
25+
t.end();
26+
});

packages/plugin-putout/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const simplifyReplaceTemplate = require('./simplify-replace-template');
5353
const removeEmptyArrayFromProcess = require('./remove-empty-array-from-process');
5454
const addPlacesToComparePlaces = require('./add-places-to-compare-places');
5555
const addPathArgToFix = require('./add-path-arg-to-fix');
56+
const convertIncludeToTraverse = require('./convert-include-to-traverse');
5657

5758
module.exports.rules = {
5859
'apply-processors-destructuring': applyProcessorsDestructuring,
@@ -108,4 +109,5 @@ module.exports.rules = {
108109
'remove-empty-array-from-process': removeEmptyArrayFromProcess,
109110
'add-places-to-compare-places': addPlacesToComparePlaces,
110111
'add-path-arg-to-fix': addPathArgToFix,
112+
'convert-include-to-traverse': convertIncludeToTraverse,
111113
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const report = () => ``;
2+
3+
export const fix = () => {};
4+
5+
module.exports.traverse = () => ({
6+
ClassDeclaration(path) {},
7+
});
8+
9+
export const traverse = () => ({
10+
ClassDeclaration(path) {},
11+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const report = () => ``;
2+
3+
export const fix = () => {
4+
}
5+
6+
module.exports.include = () => ({
7+
ClassDeclaration(path) {
8+
}
9+
});
10+
11+
export const include = () => ({
12+
ClassDeclaration(path) {
13+
}
14+
});

packages/plugin-putout/test/putout.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,8 @@ test('plugin-putout: transform: add-path-arg-to-fix', (t) => {
262262
t.transform('add-path-arg-to-fix');
263263
t.end();
264264
});
265+
266+
test('plugin-putout: transform: convert-include-to-traverse', (t) => {
267+
t.transform('convert-include-to-traverse');
268+
t.end();
269+
});

0 commit comments

Comments
 (0)