Skip to content

Commit 5af46b3

Browse files
committed
feature: @putout/variables: apply-declarations-order: add
1 parent 3eb3f46 commit 5af46b3

File tree

14 files changed

+67
-42
lines changed

14 files changed

+67
-42
lines changed

packages/plugin-destructuring/README.md

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ npm i @putout/plugin-destructuring
1919

2020
-[apply-array](#apply-array);
2121
-[apply-object](#apply-object);
22-
-[apply-declarations-order](#apply-declarations-order);
2322
-[convert-object-to-array](#convert-object-to-array);
2423
-[extract-properties](#extract-properties);
2524
-[remove-useless-object](#remove-useless-object);
@@ -36,7 +35,6 @@ npm i @putout/plugin-destructuring
3635
"rules": {
3736
"destructuring/apply-array": "on",
3837
"destructuring/apply-object": "on",
39-
"destructuring/apply-declarations-order": "on",
4038
"destructuring/convert-object-to-array": "on",
4139
"destructuring/extract-properties": "on",
4240
"destructuring/remove-useless-object": "on",
@@ -81,24 +79,6 @@ const {name} = user;
8179
({hello} = world);
8280
```
8381

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-
10282
## remove-useless-object
10383

10484
Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/c9ed04b421d75ae39e58038fa6e14630/4c097e3173990ec7e5ebabbe2cedf8e952092ebf).

packages/plugin-destructuring/lib/apply-declarations-order/index.spec.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

packages/plugin-destructuring/lib/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as applyDeclarationsOrder from './apply-declarations-order/index.js';
21
import * as applyObject from './apply-object/index.js';
32
import * as applyArray from './apply-array/index.js';
43
import * as extractPropertiesEqualDeep from './extract-properties-equal-deep/index.js';
@@ -14,7 +13,6 @@ import * as mergeProperties from './merge-properties/index.js';
1413
export const rules = {
1514
'apply-array': applyArray,
1615
'apply-object': applyObject,
17-
'apply-declarations-order': applyDeclarationsOrder,
1816
'convert-object-to-array': convertObjectToArray,
1917
'extract-properties-equal-deep': extractPropertiesEqualDeep,
2018
'extract-properties-not-equal-deep': extractPropertiesNotEqualDeep,

packages/plugin-destructuring/test/fixture/apply-declarations-order-fix.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/plugin-variables/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ npm i @putout/plugin-variables -D
1717

1818
## Rules
1919

20+
-[apply-declarations-order](#apply-declarations-order);
2021
-[convert-const-to-let](#convert-const-to-let');
2122
-[extract-keywords](#extract-keywords');
2223
-[remove-useless-assignment](#remove-useless-assignmenn);
@@ -32,6 +33,7 @@ npm i @putout/plugin-variables -D
3233
```json
3334
{
3435
"rules": {
36+
"variables/apply-declarations-order": "on",
3537
"variables/convert-const-to-let": "on",
3638
"variables/extract-keywords": "on",
3739
"variables/remove-useless-assignment": "on",
@@ -47,6 +49,24 @@ npm i @putout/plugin-variables -D
4749
}
4850
```
4951

52+
## apply-declarations-order
53+
54+
Helps to [reuse duplicate init](#reuse-duplicate-init'). Checkout in 🐊[**Putout Editor**](https://putout.vercel.app/#/gist/b70ff926b36e1e97ec7129aa0e0458a7/ece0a706de2fd24a66b4671284f7f75017f3c268).
55+
56+
### ❌ Example of incorrect code
57+
58+
```js
59+
const {env} = require('node:process');
60+
const process = require('node:process');
61+
```
62+
63+
### ✅ Example of correct code
64+
65+
```js
66+
const process = require('node:process');
67+
const {env} = process;
68+
```
69+
5070
## assignment
5171

5272
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/a2f7fe5e2c294443576f95dce6fde67e/0699ccb4f7335e8e3f80de891913a8e3ad4f35e3).

packages/plugin-destructuring/lib/apply-declarations-order/fixture/apply-declarations-order-fix.js renamed to packages/plugin-variables/lib/apply-declarations-order/fixture/apply-declarations-order-fix.js

File renamed without changes.

packages/plugin-destructuring/lib/apply-declarations-order/fixture/apply-declarations-order.js renamed to packages/plugin-variables/lib/apply-declarations-order/fixture/apply-declarations-order.js

File renamed without changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const {declarations} = 5;
2+
3+
if (parentPath.isExportDeclaration())
4+
return;
5+
6+
const init = 5;

packages/plugin-destructuring/lib/apply-declarations-order/index.js renamed to packages/plugin-variables/lib/apply-declarations-order/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
const {
1111
isVariableDeclaration,
1212
isIdentifier,
13+
isCallExpression,
1314
} = types;
1415

1516
export const report = () => `Apply declarations order`;
@@ -27,6 +28,9 @@ export const traverse = ({push}) => ({
2728
if (!isIdentifier(__a))
2829
return;
2930

31+
if (!isCallExpression(__b))
32+
return;
33+
3034
const prev = path.getAllPrevSiblings();
3135

3236
for (const current of prev.filter(isVariableDeclaration)) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {createTest} from '@putout/test';
2+
import * as plugin from './index.js';
3+
4+
const test = createTest(import.meta.url, {
5+
plugins: [
6+
['variables/apply-declarations-order', plugin],
7+
],
8+
});
9+
10+
test('variables: apply-declarations-order: report', (t) => {
11+
t.report('apply-declarations-order', `Apply declarations order`);
12+
t.end();
13+
});
14+
15+
test('variables: apply-declarations-order: transform', (t) => {
16+
t.transform('apply-declarations-order');
17+
t.end();
18+
});
19+
20+
test('variables: apply-declarations-order: no report: identifier', (t) => {
21+
t.noReport('identifier');
22+
t.end();
23+
});
24+
25+
test('variables: apply-declarations-order: no report: literal', (t) => {
26+
t.noReport('literal');
27+
t.end();
28+
});
29+

0 commit comments

Comments
 (0)