Skip to content

Commit a05cea4

Browse files
committed
feature: @putout/plugin-destructuring: apply-declarations-order: add
1 parent 6060cea commit a05cea4

File tree

10 files changed

+99
-4
lines changed

10 files changed

+99
-4
lines changed

packages/plugin-destructuring/README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ npm i @putout/plugin-destructuring
1717

1818
## Rules
1919

20-
-[apply-object](#apply-object);
2120
-[apply-array](#apply-array);
21+
-[apply-object](#apply-object);
22+
-[apply-declarations-order](#apply-declarations-order);
2223
-[convert-object-to-array](#convert-object-to-array);
2324
-[extract-properties](#extract-properties);
2425
-[remove-useless-object](#remove-useless-object);
@@ -33,8 +34,9 @@ npm i @putout/plugin-destructuring
3334
```json
3435
{
3536
"rules": {
36-
"destructuring/apply-object": "on",
3737
"destructuring/apply-array": "on",
38+
"destructuring/apply-object": "on",
39+
"destructuring/apply-declarations-order": "on",
3840
"destructuring/convert-object-to-array": "on",
3941
"destructuring/extract-properties": "on",
4042
"destructuring/remove-useless-object": "on",
@@ -79,6 +81,24 @@ const {name} = user;
7981
({hello} = world);
8082
```
8183

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+
82102
## remove-useless-object
83103

84104
Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/c9ed04b421d75ae39e58038fa6e14630/4c097e3173990ec7e5ebabbe2cedf8e952092ebf).
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const process = require('node:process');
2+
const {env} = require('node:process');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const {env} = require('node:process');
2+
const process = require('node:process');
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {createTest} from '@putout/test';
2+
import * as plugin from './index.js';
3+
4+
const test = createTest(import.meta.url, {
5+
plugins: [
6+
['apply-declarations-order', plugin],
7+
],
8+
});
9+
10+
test('destructuring: apply-declarations-order: report', (t) => {
11+
t.report('apply-declarations-order', `Apply declarations order`);
12+
t.end();
13+
});
14+
15+
test('destructuring: apply-declarations-order: transform', (t) => {
16+
t.transform('apply-declarations-order');
17+
t.end();
18+
});

packages/plugin-destructuring/lib/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as applyDeclarationsOrder from './apply-declarations-order/index.js';
12
import * as applyObject from './apply-object/index.js';
23
import * as applyArray from './apply-array/index.js';
34
import * as extractPropertiesEqualDeep from './extract-properties-equal-deep/index.js';
@@ -11,8 +12,9 @@ import * as splitCall from './split-call/index.js';
1112
import * as mergeProperties from './merge-properties/index.js';
1213

1314
export const rules = {
14-
'apply-object': applyObject,
1515
'apply-array': applyArray,
16+
'apply-object': applyObject,
17+
'apply-declarations-order': applyDeclarationsOrder,
1618
'convert-object-to-array': convertObjectToArray,
1719
'extract-properties-equal-deep': extractPropertiesEqualDeep,
1820
'extract-properties-not-equal-deep': extractPropertiesNotEqualDeep,

packages/plugin-destructuring/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"coverage": "madrun coverage",
2424
"report": "madrun report"
2525
},
26-
"dependencies": {},
26+
"dependencies": {
27+
"redput": "^3.6.0"
28+
},
2729
"keywords": [
2830
"putout",
2931
"putout-plugin",

packages/plugin-destructuring/test/destructuring.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,8 @@ test('plugin-destructuring: transform: extract-properties-not-equal-deep', (t) =
9191
t.transform('extract-properties-not-equal-deep');
9292
t.end();
9393
});
94+
95+
test('plugin-destructuring: transform: apply-declarations-order', (t) => {
96+
t.transform('apply-declarations-order');
97+
t.end();
98+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const process = require('node:process');
2+
const {env} = require('node:process');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const {env} = require('node:process');
2+
const process = require('node:process');

0 commit comments

Comments
 (0)