@putout/plugin-destructuring 1.0.2 → 1.1.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 +29 -2
- package/lib/index.js +2 -0
- package/lib/remove-useless-arguments/index.js +132 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,8 +19,9 @@ npm i @putout/plugin-destructuring
|
|
|
19
19
|
|
|
20
20
|
- ✅ [apply-object](#apply-object);
|
|
21
21
|
- ✅ [apply-array](#apply-array);
|
|
22
|
-
- ✅ [remove-useless-object](#remove-useless-object);
|
|
23
22
|
- ✅ [convert-object-to-array](#convert-object-to-array);
|
|
23
|
+
- ✅ [remove-useless-object](#remove-useless-object);
|
|
24
|
+
- ✅ [remove-useless-arguments](#remove-useless-arguments);
|
|
24
25
|
- ✅ [split-nested](#split-nested);
|
|
25
26
|
- ✅ [split-call](#split-call);
|
|
26
27
|
- ✅ [merge-properties](#merge-properties);
|
|
@@ -32,8 +33,9 @@ npm i @putout/plugin-destructuring
|
|
|
32
33
|
"rules": {
|
|
33
34
|
"destructuring/apply-object": "on",
|
|
34
35
|
"destructuring/apply-array": "on",
|
|
35
|
-
"destructuring/remove-useless-object": "on",
|
|
36
36
|
"destructuring/convert-object-to-array": "on",
|
|
37
|
+
"destructuring/remove-useless-object": "on",
|
|
38
|
+
"destructuring/remove-useless-arguments": "on",
|
|
37
39
|
"destructuring/split-nested": "on",
|
|
38
40
|
"destructuring/split-call": "on",
|
|
39
41
|
"destructuring/merge-properties": "on"
|
|
@@ -192,6 +194,31 @@ const {one, two} = require('numbers');
|
|
|
192
194
|
} = data);
|
|
193
195
|
```
|
|
194
196
|
|
|
197
|
+
### remove-useless-arguments
|
|
198
|
+
|
|
199
|
+
### ❌ Example of incorrect code
|
|
200
|
+
|
|
201
|
+
```js
|
|
202
|
+
onIfStatement({
|
|
203
|
+
push,
|
|
204
|
+
generate,
|
|
205
|
+
abc,
|
|
206
|
+
helloworld,
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
function onIfStatement({push}) {}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### ✅ Example of correct code
|
|
213
|
+
|
|
214
|
+
```js
|
|
215
|
+
onIfStatement({
|
|
216
|
+
push,
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
function onIfStatement({push}) {}
|
|
220
|
+
```
|
|
221
|
+
|
|
195
222
|
## License
|
|
196
223
|
|
|
197
224
|
MIT
|
package/lib/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import * as convertObjectToArray from './convert-object-to-array/index.js';
|
|
|
2
2
|
import * as applyObject from './apply-object/index.js';
|
|
3
3
|
import * as applyArray from './apply-array/index.js';
|
|
4
4
|
import * as removeUselessObject from './remove-useless-object/index.js';
|
|
5
|
+
import * as removeUselessArguments from './remove-useless-arguments/index.js';
|
|
5
6
|
import * as splitNested from './split-nested/index.js';
|
|
6
7
|
import * as splitCall from './split-call/index.js';
|
|
7
8
|
import * as mergeProperties from './merge-properties/index.js';
|
|
@@ -10,6 +11,7 @@ export const rules = {
|
|
|
10
11
|
'apply-object': applyObject,
|
|
11
12
|
'apply-array': applyArray,
|
|
12
13
|
'remove-useless-object': removeUselessObject,
|
|
14
|
+
'remove-useless-arguments': removeUselessArguments,
|
|
13
15
|
'convert-object-to-array': convertObjectToArray,
|
|
14
16
|
'split-nested': splitNested,
|
|
15
17
|
'split-call': splitCall,
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import {types, operator} from 'putout';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
isIdentifier,
|
|
5
|
+
isObjectProperty,
|
|
6
|
+
} = types;
|
|
7
|
+
|
|
8
|
+
const {findBinding, remove} = operator;
|
|
9
|
+
|
|
10
|
+
const getKey = ({key}) => key;
|
|
11
|
+
|
|
12
|
+
export const report = ({path, name}) => {
|
|
13
|
+
const {key} = path.node;
|
|
14
|
+
|
|
15
|
+
return `Avoid useless argument '${key.name}' of a function '${name}()'`;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const fix = ({path}) => {
|
|
19
|
+
remove(path);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const traverse = ({push}) => ({
|
|
23
|
+
'__(__object)': processUseless({
|
|
24
|
+
push,
|
|
25
|
+
index: 0,
|
|
26
|
+
}),
|
|
27
|
+
'__(__, __object)': processUseless({
|
|
28
|
+
push,
|
|
29
|
+
index: 1,
|
|
30
|
+
}),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const processUseless = ({push, index}) => (path) => {
|
|
34
|
+
const {
|
|
35
|
+
is,
|
|
36
|
+
name,
|
|
37
|
+
argProps,
|
|
38
|
+
param,
|
|
39
|
+
} = getUseless({
|
|
40
|
+
path,
|
|
41
|
+
index,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
if (!is)
|
|
45
|
+
return;
|
|
46
|
+
|
|
47
|
+
removeUseless({
|
|
48
|
+
push,
|
|
49
|
+
name,
|
|
50
|
+
param,
|
|
51
|
+
argProps,
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
function getUseless({path, index}) {
|
|
56
|
+
const NOT_OK = {
|
|
57
|
+
is: false,
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const {name} = path.node.callee;
|
|
61
|
+
|
|
62
|
+
const argument = path.get('arguments').at(index);
|
|
63
|
+
const argProps = argument.get('properties').filter(isObjectProperty);
|
|
64
|
+
|
|
65
|
+
const refPath = findBinding(path, name);
|
|
66
|
+
|
|
67
|
+
if (!refPath)
|
|
68
|
+
return NOT_OK;
|
|
69
|
+
|
|
70
|
+
const binding = refPath.scope.bindings[name];
|
|
71
|
+
const params = getParams(binding.path);
|
|
72
|
+
|
|
73
|
+
if (!params.length)
|
|
74
|
+
return NOT_OK;
|
|
75
|
+
|
|
76
|
+
if (params.length <= index)
|
|
77
|
+
return NOT_OK;
|
|
78
|
+
|
|
79
|
+
const param = params.at(index);
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
is: true,
|
|
83
|
+
name,
|
|
84
|
+
param,
|
|
85
|
+
argProps,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function removeUseless({push, name, param, argProps}) {
|
|
90
|
+
if (!param.isObjectPattern())
|
|
91
|
+
return;
|
|
92
|
+
|
|
93
|
+
const {properties} = param.node;
|
|
94
|
+
|
|
95
|
+
for (const prop of properties) {
|
|
96
|
+
if (!isIdentifier(prop.value))
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const propKeys = properties.map(getKey);
|
|
101
|
+
|
|
102
|
+
for (const propPath of argProps) {
|
|
103
|
+
let is = false;
|
|
104
|
+
|
|
105
|
+
const {key} = propPath.node;
|
|
106
|
+
|
|
107
|
+
for (const {name} of propKeys) {
|
|
108
|
+
if (name === key.name) {
|
|
109
|
+
is = true;
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (!is)
|
|
115
|
+
push({
|
|
116
|
+
name,
|
|
117
|
+
path: propPath,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function getParams(path) {
|
|
123
|
+
if (path.isFunction())
|
|
124
|
+
return path.get('params');
|
|
125
|
+
|
|
126
|
+
const fnPath = path.get('init');
|
|
127
|
+
|
|
128
|
+
if (!fnPath.isFunction())
|
|
129
|
+
return [];
|
|
130
|
+
|
|
131
|
+
return fnPath.get('params');
|
|
132
|
+
}
|
package/package.json
CHANGED