@putout/plugin-putout 16.9.0 β 17.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -0
- package/lib/convert-get-rule-to-require/index.js +49 -22
- package/lib/convert-traverse-to-scan/index.js +128 -0
- package/lib/declare/get-rule.js +12 -0
- package/lib/declare/index.js +1 -4
- package/lib/index.js +2 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -43,6 +43,7 @@ npm i @putout/plugin-putout -D
|
|
|
43
43
|
"putout/convert-node-to-path-in-get-template-values": "on",
|
|
44
44
|
"putout/convert-traverse-to-include": "on",
|
|
45
45
|
"putout/convert-traverse-to-replace": "on",
|
|
46
|
+
"putout/convert-traverse-to-scan": "on",
|
|
46
47
|
"putout/convert-process-to-find": "on",
|
|
47
48
|
"putout/convert-method-to-property": "on",
|
|
48
49
|
"putout/convert-add-argument-to-add-args": "on",
|
|
@@ -583,6 +584,49 @@ module.exports.replace = () => ({
|
|
|
583
584
|
});
|
|
584
585
|
```
|
|
585
586
|
|
|
587
|
+
## convert-traverse-to-scan
|
|
588
|
+
|
|
589
|
+
Checkout in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/afe988c8e53ae70e50bf26512672f3cd/a6677d53b996e85880a4af18250c00e849322bbe).
|
|
590
|
+
|
|
591
|
+
### β Example of incorrect code
|
|
592
|
+
|
|
593
|
+
```js
|
|
594
|
+
module.exports.traverse = ({push, options}) => ({
|
|
595
|
+
[__filesystem](path) {
|
|
596
|
+
const {names} = options;
|
|
597
|
+
|
|
598
|
+
for (const name of names) {
|
|
599
|
+
const files = findFile(path, name);
|
|
600
|
+
|
|
601
|
+
for (const file of files) {
|
|
602
|
+
push({
|
|
603
|
+
name,
|
|
604
|
+
path: file,
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
},
|
|
609
|
+
});
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
### β
Example of correct code
|
|
613
|
+
|
|
614
|
+
```js
|
|
615
|
+
module.exports.scan = (path, {push, options}) => {
|
|
616
|
+
const {names} = options;
|
|
617
|
+
|
|
618
|
+
for (const name of names) {
|
|
619
|
+
const files = findFile(path, name);
|
|
620
|
+
|
|
621
|
+
for (const file of files) {
|
|
622
|
+
push(file, {
|
|
623
|
+
name,
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
```
|
|
629
|
+
|
|
586
630
|
## convert-process-to-find
|
|
587
631
|
|
|
588
632
|
### β Example of incorrect code
|
|
@@ -17,6 +17,7 @@ const {
|
|
|
17
17
|
ObjectProperty,
|
|
18
18
|
Identifier,
|
|
19
19
|
StringLiteral,
|
|
20
|
+
ArrayExpression,
|
|
20
21
|
} = types;
|
|
21
22
|
|
|
22
23
|
module.exports.report = () => `Use top-level 'require()' instead of '...getRule()'`;
|
|
@@ -27,14 +28,8 @@ const createRequire = template(REQUIRE, {
|
|
|
27
28
|
});
|
|
28
29
|
|
|
29
30
|
module.exports.match = () => ({
|
|
30
|
-
'getRule(__a)':
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (!path.parentPath.isSpreadElement())
|
|
34
|
-
return false;
|
|
35
|
-
|
|
36
|
-
return !path.scope.getAllBindings()[name];
|
|
37
|
-
},
|
|
31
|
+
'getRule(__a)': match,
|
|
32
|
+
'getRule(__a, __b)': match,
|
|
38
33
|
});
|
|
39
34
|
|
|
40
35
|
module.exports.replace = () => ({
|
|
@@ -45,23 +40,26 @@ module.exports.replace = () => ({
|
|
|
45
40
|
|
|
46
41
|
replaceWith(path.parentPath, node);
|
|
47
42
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const nodeRequire = createRequire({
|
|
53
|
-
__a: id,
|
|
54
|
-
__b: StringLiteral(`./${__a.value}`),
|
|
43
|
+
addRequire({
|
|
44
|
+
__a,
|
|
45
|
+
id,
|
|
46
|
+
path,
|
|
55
47
|
});
|
|
56
48
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
49
|
+
return path;
|
|
50
|
+
},
|
|
51
|
+
'getRule(__a, __b)': ({__a, __b}, path) => {
|
|
52
|
+
const name = camel(__a.value);
|
|
53
|
+
const id = Identifier(name);
|
|
54
|
+
const node = ObjectProperty(__a, ArrayExpression([__b, id]));
|
|
55
|
+
|
|
56
|
+
replaceWith(path.parentPath, node);
|
|
63
57
|
|
|
64
|
-
|
|
58
|
+
addRequire({
|
|
59
|
+
__a,
|
|
60
|
+
id,
|
|
61
|
+
path,
|
|
62
|
+
});
|
|
65
63
|
|
|
66
64
|
return path;
|
|
67
65
|
},
|
|
@@ -76,3 +74,32 @@ function getLatest(body) {
|
|
|
76
74
|
|
|
77
75
|
return path;
|
|
78
76
|
}
|
|
77
|
+
|
|
78
|
+
function addRequire({__a, id, path}) {
|
|
79
|
+
const programPath = path.scope.getProgramParent().path;
|
|
80
|
+
const body = programPath.get('body');
|
|
81
|
+
const [first] = body;
|
|
82
|
+
|
|
83
|
+
const nodeRequire = createRequire({
|
|
84
|
+
__a: id,
|
|
85
|
+
__b: StringLiteral(`./${__a.value}`),
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
if (compare(first, REQUIRE)) {
|
|
89
|
+
const latest = getLatest(body.slice(1));
|
|
90
|
+
insertBefore(latest, nodeRequire);
|
|
91
|
+
|
|
92
|
+
return path;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
programPath.node.body.unshift(nodeRequire);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function match({__a}, path) {
|
|
99
|
+
const name = __a.value;
|
|
100
|
+
|
|
101
|
+
if (!path.parentPath.isSpreadElement())
|
|
102
|
+
return false;
|
|
103
|
+
|
|
104
|
+
return !path.scope.getAllBindings()[name];
|
|
105
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {operator, types} = require('putout');
|
|
4
|
+
const {
|
|
5
|
+
traverse,
|
|
6
|
+
remove,
|
|
7
|
+
rename,
|
|
8
|
+
getProperty,
|
|
9
|
+
replaceWith,
|
|
10
|
+
compare,
|
|
11
|
+
} = operator;
|
|
12
|
+
|
|
13
|
+
const {
|
|
14
|
+
Identifier,
|
|
15
|
+
isReturnStatement,
|
|
16
|
+
isObjectMethod,
|
|
17
|
+
isObjectProperty,
|
|
18
|
+
isObjectExpression,
|
|
19
|
+
} = types;
|
|
20
|
+
|
|
21
|
+
module.exports.report = () => `Use Scanner instead of Traverser`;
|
|
22
|
+
|
|
23
|
+
module.exports.fix = ({path, pathProperty}) => {
|
|
24
|
+
if (path.isObjectMethod()) {
|
|
25
|
+
replaceWith(path.parentPath, path.get('body'));
|
|
26
|
+
path.parentPath.parentPath.node.params.unshift(Identifier('path'));
|
|
27
|
+
|
|
28
|
+
path.parentPath.parentPath.parentPath.node.left.property.name = 'scan';
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (path.isObjectProperty()) {
|
|
33
|
+
replaceWith(path.parentPath, path.get('value.body'));
|
|
34
|
+
path.parentPath.parentPath.node.params.unshift(Identifier('path'));
|
|
35
|
+
|
|
36
|
+
const {left} = path.parentPath.parentPath.parentPath.node;
|
|
37
|
+
|
|
38
|
+
left.property.name = 'scan';
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (path.isCallExpression()) {
|
|
43
|
+
const {value} = pathProperty.node;
|
|
44
|
+
pathProperty.parentPath.parentPath.node.arguments.unshift(value);
|
|
45
|
+
|
|
46
|
+
const {path: programPath} = path.scope.getProgramParent();
|
|
47
|
+
|
|
48
|
+
traverse(programPath, {
|
|
49
|
+
'module.exports.fix = (__object) => __': (path) => {
|
|
50
|
+
const rightPath = path.get('right');
|
|
51
|
+
const [argPath] = rightPath.get('params');
|
|
52
|
+
|
|
53
|
+
rightPath.node.params.unshift(value);
|
|
54
|
+
|
|
55
|
+
const pathPropertyFix = getProperty(argPath, 'path');
|
|
56
|
+
rename(pathPropertyFix, 'path', value.name);
|
|
57
|
+
remove(pathPropertyFix);
|
|
58
|
+
},
|
|
59
|
+
'module.exports.report = (__object) => __': (path) => {
|
|
60
|
+
const rightPath = path.get('right');
|
|
61
|
+
const [argPath] = rightPath.get('params');
|
|
62
|
+
|
|
63
|
+
rightPath.node.params.unshift(value);
|
|
64
|
+
|
|
65
|
+
const pathPropertyFix = getProperty(argPath, 'path');
|
|
66
|
+
|
|
67
|
+
if (pathPropertyFix) {
|
|
68
|
+
rename(pathPropertyFix, 'path', value.name);
|
|
69
|
+
remove(pathPropertyFix);
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
remove(pathProperty);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
module.exports.traverse = ({push}) => ({
|
|
79
|
+
'ObjectMethod|ObjectProperty'(path) {
|
|
80
|
+
if (!isFilesystemPath(path))
|
|
81
|
+
return;
|
|
82
|
+
|
|
83
|
+
push({
|
|
84
|
+
path,
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
'push(__a)'(path) {
|
|
88
|
+
const __aPath = path.get('arguments.0');
|
|
89
|
+
|
|
90
|
+
if (!__aPath.isObjectExpression())
|
|
91
|
+
return;
|
|
92
|
+
|
|
93
|
+
if (path.find(isReturnStatement))
|
|
94
|
+
return;
|
|
95
|
+
|
|
96
|
+
if (!path.find(isFilesystemPath) && !path.find(isScan))
|
|
97
|
+
return;
|
|
98
|
+
|
|
99
|
+
const pathProperty = getProperty(__aPath, 'path');
|
|
100
|
+
|
|
101
|
+
push({
|
|
102
|
+
path,
|
|
103
|
+
pathProperty,
|
|
104
|
+
});
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
function isScan(path) {
|
|
109
|
+
return compare(path, 'module.exports.scan = __');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function isFilesystemPath(path) {
|
|
113
|
+
if (!isObjectMethod(path) && !isObjectProperty(path))
|
|
114
|
+
return false;
|
|
115
|
+
|
|
116
|
+
if (isObjectExpression(path.parentPath) && path.parentPath.node.properties.length > 1)
|
|
117
|
+
return false;
|
|
118
|
+
|
|
119
|
+
const {computed} = path.node;
|
|
120
|
+
|
|
121
|
+
if (!computed)
|
|
122
|
+
return false;
|
|
123
|
+
|
|
124
|
+
if (path.node.key.name !== '__filesystem')
|
|
125
|
+
return false;
|
|
126
|
+
|
|
127
|
+
return !path.parentPath.parentPath.isReturnStatement();
|
|
128
|
+
}
|
package/lib/declare/index.js
CHANGED
|
@@ -2,10 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const types = require('./types');
|
|
4
4
|
const operator = require('./operator');
|
|
5
|
-
|
|
6
|
-
const getRule = (a) => ({
|
|
7
|
-
[a]: require(`./${a}`),
|
|
8
|
-
});
|
|
5
|
+
const {getRule} = require('./get-rule');
|
|
9
6
|
|
|
10
7
|
module.exports.declare = () => ({
|
|
11
8
|
template: `import {template} from 'putout'`,
|
package/lib/index.js
CHANGED
|
@@ -22,6 +22,7 @@ const convertBabelTypes = require('./convert-babel-types');
|
|
|
22
22
|
const convertNodeToPathInGetTemplateValues = require('./convert-node-to-path-in-get-template-values');
|
|
23
23
|
const convertTraverseToInclude = require('./convert-traverse-to-include');
|
|
24
24
|
const convertTraverseToReplace = require('./convert-traverse-to-replace');
|
|
25
|
+
const convertTraverseToScan = require('./convert-traverse-to-scan');
|
|
25
26
|
const convertProcessToFind = require('./convert-process-to-find');
|
|
26
27
|
const convertMethodToProperty = require('./convert-method-to-property');
|
|
27
28
|
const convertAddArgumentToAddArgs = require('./convert-add-argument-to-add-args');
|
|
@@ -88,4 +89,5 @@ module.exports.rules = {
|
|
|
88
89
|
'add-index-to-import': addIndexToImport,
|
|
89
90
|
'apply-rename': applyRename,
|
|
90
91
|
'apply-short-processors': applyShortProcessors,
|
|
92
|
+
'convert-traverse-to-scan': convertTraverseToScan,
|
|
91
93
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-putout",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "17.0.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "πPutout plugin helps with plugins development",
|
|
@@ -45,7 +45,8 @@
|
|
|
45
45
|
"lerna": "^6.0.1",
|
|
46
46
|
"madrun": "^9.0.0",
|
|
47
47
|
"montag": "^1.2.1",
|
|
48
|
-
"nodemon": "^3.0.1"
|
|
48
|
+
"nodemon": "^3.0.1",
|
|
49
|
+
"supertape": "^8.7.0"
|
|
49
50
|
},
|
|
50
51
|
"peerDependencies": {
|
|
51
52
|
"putout": ">=33"
|