@putout/plugin-destructuring 1.3.0 → 1.4.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 +24 -6
- package/lib/apply-declarations-order/index.js +40 -0
- package/lib/index.js +3 -1
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -17,8 +17,9 @@ npm i @putout/plugin-destructuring
|
|
|
17
17
|
|
|
18
18
|
## Rules
|
|
19
19
|
|
|
20
|
-
- ✅ [apply-object](#apply-object);
|
|
21
20
|
- ✅ [apply-array](#apply-array);
|
|
21
|
+
- ✅ [apply-object](#apply-object);
|
|
22
|
+
- ✅ [apply-declarations-order](#apply-declarations-order);
|
|
22
23
|
- ✅ [convert-object-to-array](#convert-object-to-array);
|
|
23
24
|
- ✅ [extract-properties](#extract-properties);
|
|
24
25
|
- ✅ [remove-useless-object](#remove-useless-object);
|
|
@@ -33,8 +34,9 @@ npm i @putout/plugin-destructuring
|
|
|
33
34
|
```json
|
|
34
35
|
{
|
|
35
36
|
"rules": {
|
|
36
|
-
"destructuring/apply-object": "on",
|
|
37
37
|
"destructuring/apply-array": "on",
|
|
38
|
+
"destructuring/apply-object": "on",
|
|
39
|
+
"destructuring/apply-declarations-order": "on",
|
|
38
40
|
"destructuring/convert-object-to-array": "on",
|
|
39
41
|
"destructuring/extract-properties": "on",
|
|
40
42
|
"destructuring/remove-useless-object": "on",
|
|
@@ -55,7 +57,7 @@ npm i @putout/plugin-destructuring
|
|
|
55
57
|
const first = array[0];
|
|
56
58
|
```
|
|
57
59
|
|
|
58
|
-
|
|
60
|
+
### ✅ Example of correct code
|
|
59
61
|
|
|
60
62
|
```js
|
|
61
63
|
const [first] = array;
|
|
@@ -79,6 +81,24 @@ const {name} = user;
|
|
|
79
81
|
({hello} = world);
|
|
80
82
|
```
|
|
81
83
|
|
|
84
|
+
## apply-declarations-order
|
|
85
|
+
|
|
86
|
+
Helps to [extract-properties](#extract-properties'). Checkout in 🐊[**Putout Editor**](https://putout.vercel.app/#/gist/b70ff926b36e1e97ec7129aa0e0458a7/ece0a706de2fd24a66b4671284f7f75017f3c268).
|
|
87
|
+
|
|
88
|
+
### ❌ Example of incorrect code
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
const {env} = require('node:process');
|
|
92
|
+
const process = require('node:process');
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### ✅ Example of correct code
|
|
96
|
+
|
|
97
|
+
```js
|
|
98
|
+
const process = require('node:process');
|
|
99
|
+
const {env} = process;
|
|
100
|
+
```
|
|
101
|
+
|
|
82
102
|
## remove-useless-object
|
|
83
103
|
|
|
84
104
|
Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/c9ed04b421d75ae39e58038fa6e14630/4c097e3173990ec7e5ebabbe2cedf8e952092ebf).
|
|
@@ -120,8 +140,6 @@ const [a, b] = c;
|
|
|
120
140
|
>
|
|
121
141
|
> (c) [Destructuring in JavaScript: the not so good parts](https://goodguydaniel.com/blog/destructuring-not-so-good-parts)
|
|
122
142
|
|
|
123
|
-
🐊[**Putout**](https://github.com/coderaiser/putout) plugin adds ability to split nested destructuring.
|
|
124
|
-
|
|
125
143
|
### ❌ Example of incorrect code
|
|
126
144
|
|
|
127
145
|
```js
|
|
@@ -198,7 +216,7 @@ const {one, two} = require('numbers');
|
|
|
198
216
|
} = data);
|
|
199
217
|
```
|
|
200
218
|
|
|
201
|
-
|
|
219
|
+
## remove-useless-arguments
|
|
202
220
|
|
|
203
221
|
### ❌ Example of incorrect code
|
|
204
222
|
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {types, operator} from 'putout';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
insertAfter,
|
|
5
|
+
remove,
|
|
6
|
+
compare,
|
|
7
|
+
getTemplateValues,
|
|
8
|
+
} = operator;
|
|
9
|
+
|
|
10
|
+
const {
|
|
11
|
+
isVariableDeclaration,
|
|
12
|
+
isIdentifier,
|
|
13
|
+
} = types;
|
|
14
|
+
|
|
15
|
+
export const report = () => `Apply declarations order`;
|
|
16
|
+
|
|
17
|
+
export const fix = ({path, current}) => {
|
|
18
|
+
const {node} = current;
|
|
19
|
+
remove(current);
|
|
20
|
+
insertAfter(path, node);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const traverse = ({push}) => ({
|
|
24
|
+
'const __a = __b': (path) => {
|
|
25
|
+
const {__a, __b} = getTemplateValues(path, 'const __a = __b');
|
|
26
|
+
|
|
27
|
+
if (!isIdentifier(__a))
|
|
28
|
+
return;
|
|
29
|
+
|
|
30
|
+
const prev = path.getAllPrevSiblings();
|
|
31
|
+
|
|
32
|
+
for (const current of prev.filter(isVariableDeclaration)) {
|
|
33
|
+
if (compare(__b, current.node.declarations[0].init))
|
|
34
|
+
push({
|
|
35
|
+
current,
|
|
36
|
+
path,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
});
|
package/lib/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as applyDeclarationsOrder from './apply-declarations-order/index.js';
|
|
1
2
|
import * as applyObject from './apply-object/index.js';
|
|
2
3
|
import * as applyArray from './apply-array/index.js';
|
|
3
4
|
import * as extractPropertiesEqualDeep from './extract-properties-equal-deep/index.js';
|
|
@@ -11,8 +12,9 @@ import * as splitCall from './split-call/index.js';
|
|
|
11
12
|
import * as mergeProperties from './merge-properties/index.js';
|
|
12
13
|
|
|
13
14
|
export const rules = {
|
|
14
|
-
'apply-object': applyObject,
|
|
15
15
|
'apply-array': applyArray,
|
|
16
|
+
'apply-object': applyObject,
|
|
17
|
+
'apply-declarations-order': applyDeclarationsOrder,
|
|
16
18
|
'convert-object-to-array': convertObjectToArray,
|
|
17
19
|
'extract-properties-equal-deep': extractPropertiesEqualDeep,
|
|
18
20
|
'extract-properties-not-equal-deep': extractPropertiesNotEqualDeep,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-destructuring",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.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",
|
|
@@ -23,7 +23,9 @@
|
|
|
23
23
|
"coverage": "madrun coverage",
|
|
24
24
|
"report": "madrun report"
|
|
25
25
|
},
|
|
26
|
-
"dependencies": {
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"redput": "^3.6.0"
|
|
28
|
+
},
|
|
27
29
|
"keywords": [
|
|
28
30
|
"putout",
|
|
29
31
|
"putout-plugin",
|
|
@@ -42,7 +44,7 @@
|
|
|
42
44
|
"@putout/plugin-variables": "*",
|
|
43
45
|
"@putout/test": "^14.0.0",
|
|
44
46
|
"c8": "^10.0.0",
|
|
45
|
-
"eslint": "
|
|
47
|
+
"eslint": "^10.0.0-alpha.0",
|
|
46
48
|
"eslint-plugin-n": "^17.0.0",
|
|
47
49
|
"eslint-plugin-putout": "^29.0.0",
|
|
48
50
|
"madrun": "^11.0.0",
|