Skip to content

Commit 58436d2

Browse files
committed
feature: @putout/plugin-putout: add-path-arg-to-visitors: add
1 parent 2590231 commit 58436d2

File tree

12 files changed

+199
-0
lines changed

12 files changed

+199
-0
lines changed

packages/plugin-putout/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ npm i @putout/plugin-putout -D
1717
-[add-index-to-import](#add-index-to-import);
1818
-[add-places-to-compare-places](#add-places-to-compare-places);
1919
-[add-path-arg-to-fix](#add-path-arg-to-fix);
20+
-[add-path-arg-to-visitors](#add-path-arg-to-visitors);
2021
-[add-test-args](#add-test-args);
2122
-[add-traverse-args](#add-traverse-args);
2223
-[add-track-file](#add-track-file);
@@ -76,6 +77,7 @@ npm i @putout/plugin-putout -D
7677
"rules": {
7778
"putout/add-places-to-compare-places": "on",
7879
"putout/add-path-arg-to-fix": "on",
80+
"putout/add-path-arg-to-visitors": "on",
7981
"putout/add-test-args": "on",
8082
"putout/add-traverse-args": "on",
8183
"putout/add-track-file": "on",
@@ -866,6 +868,30 @@ export const fix = (path) => {
866868
};
867869
```
868870

871+
## add-path-arg-to-visitors
872+
873+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/e20eb16668b2ebbceca1a03cde859e93/fee08d6dbb6aa4d835adacc8e9de4d994fd34848).
874+
875+
### ❌ Example of incorrect code
876+
877+
```js
878+
export const traverse = () => ({
879+
TSUnionType() {
880+
console.log(path);
881+
},
882+
});
883+
```
884+
885+
### ✅ Example of correct code
886+
887+
```js
888+
export const traverse = () => ({
889+
TSUnionType(path) {
890+
console.log(path);
891+
},
892+
});
893+
```
894+
869895
## add-test-args
870896

871897
### ❌ Example of incorrect code
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const traverse = () => ({
2+
TSUnionType(path) {
3+
console.log(path);
4+
},
5+
});
6+
7+
module.exports.traverse = () => ({
8+
TSUnionType(path) {
9+
console.log(path);
10+
},
11+
});
12+
13+
module.exports.traverse = () => ({
14+
[`export const traverse = ${TRAVERSE}`]: traverseMethods({
15+
where: 'declaration.declarations.0.init',
16+
push,
17+
}),
18+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export const traverse = () => ({
2+
TSUnionType() {
3+
console.log(path);
4+
}
5+
});
6+
7+
8+
module.exports.traverse = () => ({
9+
TSUnionType() {
10+
console.log(path);
11+
}
12+
});
13+
14+
15+
module.exports.traverse = () => ({
16+
[`export const traverse = ${TRAVERSE}`]: traverseMethods({
17+
where: 'declaration.declarations.0.init',
18+
push,
19+
}),
20+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const traverse = () => ({
2+
'module.exports = __a'(path) {
3+
}
4+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const traverse = () => ({
2+
'module.exports = __a': (path) => {
3+
console.log(path);
4+
},
5+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const traverse = () => ({
2+
'module.exports = __a': () => {
3+
console.log(path);
4+
}
5+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
const {types} = require('putout');
4+
const {Identifier} = types;
5+
6+
module.exports.report = () => `Add 'path' argument to 'traverse' visitors`;
7+
8+
const TRAVERSE = '(__args) => __object';
9+
10+
module.exports.fix = (path) => {
11+
path.node.params.push(Identifier('path'));
12+
};
13+
14+
module.exports.traverse = ({push}) => ({
15+
[`export const traverse = ${TRAVERSE}`]: traverseMethods({
16+
where: 'declaration.declarations.0.init',
17+
push,
18+
}),
19+
[`module.exports.traverse = ${TRAVERSE}`]: traverseMethods({
20+
where: 'right',
21+
push,
22+
}),
23+
});
24+
25+
const traverseMethods = ({where, push}) => (path) => {
26+
const initPath = path.get(where);
27+
const objectPath = initPath.get('body');
28+
29+
for (let prop of objectPath.get('properties')) {
30+
if (prop.isObjectProperty())
31+
prop = prop.get('value');
32+
33+
if (!prop.isFunction())
34+
continue;
35+
36+
if (prop.node.params.length)
37+
continue;
38+
39+
prop.traverse({
40+
ReferencedIdentifier(path) {
41+
if (path.node.name === 'path')
42+
push(prop);
43+
},
44+
});
45+
}
46+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const {createTest} = require('@putout/test');
4+
const plugin = require('.');
5+
6+
const test = createTest(__dirname, {
7+
plugins: [
8+
['add-path-arg-to-visitors', plugin],
9+
],
10+
});
11+
12+
test('putout: add-path-arg-to-visitors: report', (t) => {
13+
t.report('add-path-arg-to-visitors', `Add 'path' argument to 'traverse' visitors`);
14+
t.end();
15+
});
16+
17+
test('putout: add-path-arg-to-visitors: transform', (t) => {
18+
t.transform('add-path-arg-to-visitors');
19+
t.end();
20+
});
21+
22+
test('putout: add-path-arg-to-visitors: transform: property', (t) => {
23+
t.transform('property');
24+
t.end();
25+
});
26+
27+
test('putout: add-path-arg-to-visitors: no report: not referenced', (t) => {
28+
t.noReport('not-referenced');
29+
t.end();
30+
});

packages/plugin-putout/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const addPlacesToComparePlaces = require('./add-places-to-compare-places');
5555
const addPathArgToFix = require('./add-path-arg-to-fix');
5656
const convertIncludeToTraverse = require('./convert-include-to-traverse');
5757
const removeUselessPrinterOption = require('./remove-useless-printer-option');
58+
const addPathArgToVisitors = require('./add-path-arg-to-visitors');
5859

5960
module.exports.rules = {
6061
'apply-processors-destructuring': applyProcessorsDestructuring,
@@ -112,4 +113,5 @@ module.exports.rules = {
112113
'add-path-arg-to-fix': addPathArgToFix,
113114
'convert-include-to-traverse': convertIncludeToTraverse,
114115
'remove-useless-printer-option': removeUselessPrinterOption,
116+
'add-path-arg-to-visitors': addPathArgToVisitors,
115117
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const traverse = () => ({
2+
TSUnionType(path) {
3+
console.log(path);
4+
},
5+
});
6+
7+
module.exports.traverse = () => ({
8+
TSUnionType(path) {
9+
console.log(path);
10+
},
11+
});
12+
13+
module.exports.traverse = ({push}) => ({
14+
[`export const traverse = ${TRAVERSE}`]: traverseMethods({
15+
where: 'declaration.declarations.0.init',
16+
push,
17+
}),
18+
});

0 commit comments

Comments
 (0)