@putout/plugin-destructuring 1.0.2 → 1.2.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,8 +19,10 @@ 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);
25
+ - ✅ [remove-useless-variables](#remove-useless-variables);
24
26
  - ✅ [split-nested](#split-nested);
25
27
  - ✅ [split-call](#split-call);
26
28
  - ✅ [merge-properties](#merge-properties);
@@ -32,8 +34,10 @@ npm i @putout/plugin-destructuring
32
34
  "rules": {
33
35
  "destructuring/apply-object": "on",
34
36
  "destructuring/apply-array": "on",
35
- "destructuring/remove-useless-object": "on",
36
37
  "destructuring/convert-object-to-array": "on",
38
+ "destructuring/remove-useless-object": "on",
39
+ "destructuring/remove-useless-arguments": "on",
40
+ "destructuring/remove-useless-variables": "on",
37
41
  "destructuring/split-nested": "on",
38
42
  "destructuring/split-call": "on",
39
43
  "destructuring/merge-properties": "on"
@@ -192,6 +196,47 @@ const {one, two} = require('numbers');
192
196
  } = data);
193
197
  ```
194
198
 
199
+ ### remove-useless-arguments
200
+
201
+ ### ❌ Example of incorrect code
202
+
203
+ ```js
204
+ onIfStatement({
205
+ push,
206
+ generate,
207
+ abc,
208
+ helloworld,
209
+ });
210
+
211
+ function onIfStatement({push}) {}
212
+ ```
213
+
214
+ ### ✅ Example of correct code
215
+
216
+ ```js
217
+ onIfStatement({
218
+ push,
219
+ });
220
+
221
+ function onIfStatement({push}) {}
222
+ ```
223
+
224
+ ## remove-useless-variables
225
+
226
+ ### ❌ Example of incorrect code
227
+
228
+ ```js
229
+ function hi(c) {
230
+ const {a, b} = c;
231
+ }
232
+ ```
233
+
234
+ ### ✅ Example of correct code
235
+
236
+ ```js
237
+ function hi({a, b}) {}
238
+ ```
239
+
195
240
  ## License
196
241
 
197
242
  MIT
package/lib/index.js CHANGED
@@ -2,6 +2,8 @@ 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';
6
+ import * as removeUselessVariables from './remove-useless-variables/index.js';
5
7
  import * as splitNested from './split-nested/index.js';
6
8
  import * as splitCall from './split-call/index.js';
7
9
  import * as mergeProperties from './merge-properties/index.js';
@@ -10,6 +12,8 @@ export const rules = {
10
12
  'apply-object': applyObject,
11
13
  'apply-array': applyArray,
12
14
  'remove-useless-object': removeUselessObject,
15
+ 'remove-useless-arguments': removeUselessArguments,
16
+ 'remove-useless-variables': removeUselessVariables,
13
17
  'convert-object-to-array': convertObjectToArray,
14
18
  'split-nested': splitNested,
15
19
  '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
+ }
@@ -0,0 +1,78 @@
1
+ import {types, operator} from 'putout';
2
+
3
+ const {
4
+ isIdentifier,
5
+ isRestElement,
6
+ isAssignmentPattern,
7
+ } = types;
8
+
9
+ const {replaceWith} = operator;
10
+
11
+ const MAX_LENGTH = 20;
12
+
13
+ const getKeyLength = (a) => {
14
+ const {key, value} = a;
15
+
16
+ if (!isAssignmentPattern(value) && isIdentifier(key))
17
+ return a.key.name.length;
18
+
19
+ if (isRestElement(a) && isIdentifier(a.argument))
20
+ return a.argument.name.length + 3;
21
+
22
+ return MAX_LENGTH;
23
+ };
24
+
25
+ const sum = (a, b) => a + getKeyLength(b);
26
+
27
+ export const report = (path) => {
28
+ return `Avoid useless variable '${path.node.declarations[0].init.name}'`;
29
+ };
30
+
31
+ export const match = () => ({
32
+ 'const __object = __a': ({__a, __object}, path) => {
33
+ const {parentPath} = path.parentPath;
34
+
35
+ if (!parentPath)
36
+ return false;
37
+
38
+ if (!parentPath.isFunction())
39
+ return false;
40
+
41
+ if (path.node !== parentPath.node.body.body[0])
42
+ return false;
43
+
44
+ const {params} = parentPath.node;
45
+
46
+ if (params.length !== 1)
47
+ return false;
48
+
49
+ const [first] = params;
50
+
51
+ if (__object.properties.length > 3)
52
+ return false;
53
+
54
+ if (!isIdentifier(first))
55
+ return false;
56
+
57
+ if (__a.name !== first.name)
58
+ return false;
59
+
60
+ const binding = path.scope.bindings[first.name];
61
+
62
+ if (binding.path.node.typeAnnotation)
63
+ return false;
64
+
65
+ if (binding.references > 1)
66
+ return false;
67
+
68
+ const namesLength = __object.properties.reduce(sum, 0);
69
+
70
+ return namesLength < MAX_LENGTH;
71
+ },
72
+ });
73
+
74
+ export const replace = () => ({
75
+ 'const __object = __a': ({__object}, path) => {
76
+ replaceWith(path.parentPath.parentPath.get('params.0'), __object);
77
+ },
78
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-destructuring",
3
- "version": "1.0.2",
3
+ "version": "1.2.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",