@putout/plugin-destructuring 1.1.0 → 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
@@ -22,6 +22,7 @@ npm i @putout/plugin-destructuring
22
22
  - ✅ [convert-object-to-array](#convert-object-to-array);
23
23
  - ✅ [remove-useless-object](#remove-useless-object);
24
24
  - ✅ [remove-useless-arguments](#remove-useless-arguments);
25
+ - ✅ [remove-useless-variables](#remove-useless-variables);
25
26
  - ✅ [split-nested](#split-nested);
26
27
  - ✅ [split-call](#split-call);
27
28
  - ✅ [merge-properties](#merge-properties);
@@ -36,6 +37,7 @@ npm i @putout/plugin-destructuring
36
37
  "destructuring/convert-object-to-array": "on",
37
38
  "destructuring/remove-useless-object": "on",
38
39
  "destructuring/remove-useless-arguments": "on",
40
+ "destructuring/remove-useless-variables": "on",
39
41
  "destructuring/split-nested": "on",
40
42
  "destructuring/split-call": "on",
41
43
  "destructuring/merge-properties": "on"
@@ -219,6 +221,22 @@ onIfStatement({
219
221
  function onIfStatement({push}) {}
220
222
  ```
221
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
+
222
240
  ## License
223
241
 
224
242
  MIT
package/lib/index.js CHANGED
@@ -3,6 +3,7 @@ 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
5
  import * as removeUselessArguments from './remove-useless-arguments/index.js';
6
+ import * as removeUselessVariables from './remove-useless-variables/index.js';
6
7
  import * as splitNested from './split-nested/index.js';
7
8
  import * as splitCall from './split-call/index.js';
8
9
  import * as mergeProperties from './merge-properties/index.js';
@@ -12,6 +13,7 @@ export const rules = {
12
13
  'apply-array': applyArray,
13
14
  'remove-useless-object': removeUselessObject,
14
15
  'remove-useless-arguments': removeUselessArguments,
16
+ 'remove-useless-variables': removeUselessVariables,
15
17
  'convert-object-to-array': convertObjectToArray,
16
18
  'split-nested': splitNested,
17
19
  'split-call': splitCall,
@@ -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.1.0",
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",