@putout/plugin-destructuring 1.0.1 → 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 CHANGED
@@ -19,10 +19,12 @@ 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);
27
+ - ✅ [merge-properties](#merge-properties);
26
28
 
27
29
  ## Config
28
30
 
@@ -31,8 +33,12 @@ npm i @putout/plugin-destructuring
31
33
  "rules": {
32
34
  "destructuring/apply-object": "on",
33
35
  "destructuring/apply-array": "on",
36
+ "destructuring/convert-object-to-array": "on",
34
37
  "destructuring/remove-useless-object": "on",
35
- "destructuring/convert-object-to-array": "on"
38
+ "destructuring/remove-useless-arguments": "on",
39
+ "destructuring/split-nested": "on",
40
+ "destructuring/split-call": "on",
41
+ "destructuring/merge-properties": "on"
36
42
  }
37
43
  }
38
44
  ```
@@ -188,6 +194,31 @@ const {one, two} = require('numbers');
188
194
  } = data);
189
195
  ```
190
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
+
191
222
  ## License
192
223
 
193
224
  MIT
package/lib/index.js CHANGED
@@ -1,15 +1,17 @@
1
1
  import * as convertObjectToArray from './convert-object-to-array/index.js';
2
- import * as object from './object/index.js';
3
- import * as array from './array/index.js';
4
- import * as falsy from './falsy/index.js';
2
+ import * as applyObject from './apply-object/index.js';
3
+ import * as applyArray from './apply-array/index.js';
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';
8
9
 
9
10
  export const rules = {
10
- 'apply-object': object,
11
- 'apply-array': array,
12
- 'remove-useless-object': falsy,
11
+ 'apply-object': applyObject,
12
+ 'apply-array': applyArray,
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-destructuring",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin adds ability to transform destructuring",
File without changes
File without changes
File without changes