@putout/plugin-variables 1.4.0 → 1.6.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
|
@@ -17,8 +17,10 @@ npm i @putout/plugin-variables -D
|
|
|
17
17
|
|
|
18
18
|
## Rules
|
|
19
19
|
|
|
20
|
+
- ✅ [apply-declarations-order](#apply-declarations-order);
|
|
20
21
|
- ✅ [convert-const-to-let](#convert-const-to-let');
|
|
21
22
|
- ✅ [extract-keywords](#extract-keywords');
|
|
23
|
+
- ✅ [reuse-duplicate-init](#reuse-duplicate-init);
|
|
22
24
|
- ✅ [remove-useless-assignment](#remove-useless-assignmenn);
|
|
23
25
|
- ✅ [remove-useless-declaration](#remove-useless-declaration);
|
|
24
26
|
- ✅ [remove-useless-duplicate](#remove-useless-duplicate);
|
|
@@ -32,8 +34,10 @@ npm i @putout/plugin-variables -D
|
|
|
32
34
|
```json
|
|
33
35
|
{
|
|
34
36
|
"rules": {
|
|
37
|
+
"variables/apply-declarations-order": "on",
|
|
35
38
|
"variables/convert-const-to-let": "on",
|
|
36
39
|
"variables/extract-keywords": "on",
|
|
40
|
+
"variables/reuse-duplicate-init": "on",
|
|
37
41
|
"variables/remove-useless-assignment": "on",
|
|
38
42
|
"variables/remove-useless-declaration": ["on", {
|
|
39
43
|
"maxLength": 20
|
|
@@ -47,6 +51,24 @@ npm i @putout/plugin-variables -D
|
|
|
47
51
|
}
|
|
48
52
|
```
|
|
49
53
|
|
|
54
|
+
## apply-declarations-order
|
|
55
|
+
|
|
56
|
+
Helps to [reuse duplicate init](#reuse-duplicate-init'). Checkout in 🐊[**Putout Editor**](https://putout.vercel.app/#/gist/b70ff926b36e1e97ec7129aa0e0458a7/ece0a706de2fd24a66b4671284f7f75017f3c268).
|
|
57
|
+
|
|
58
|
+
### ❌ Example of incorrect code
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
const {env} = require('node:process');
|
|
62
|
+
const process = require('node:process');
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### ✅ Example of correct code
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
const process = require('node:process');
|
|
69
|
+
const {env} = process;
|
|
70
|
+
```
|
|
71
|
+
|
|
50
72
|
## assignment
|
|
51
73
|
|
|
52
74
|
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/a2f7fe5e2c294443576f95dce6fde67e/0699ccb4f7335e8e3f80de891913a8e3ad4f35e3).
|
|
@@ -63,6 +85,42 @@ while (!(files = readDirectory(parentDir)).length) {}
|
|
|
63
85
|
while (!readDirectory(parentDir).length) {}
|
|
64
86
|
```
|
|
65
87
|
|
|
88
|
+
## reuse-duplicate-init
|
|
89
|
+
|
|
90
|
+
> Functions are one of the fundamental building blocks it contains set of statements that performs a calculations, takes some input and returns an output. To use a function, you must define it somewhere in the scope from which you wish to call it.
|
|
91
|
+
>
|
|
92
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions)
|
|
93
|
+
|
|
94
|
+
🐊[**Putout**](https://github.com/coderaiser/putout) plugin adds ability to reuse duplicate init.
|
|
95
|
+
|
|
96
|
+
### ❌ Example of incorrect code
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
const putout = require('putout');
|
|
100
|
+
|
|
101
|
+
const {
|
|
102
|
+
a,
|
|
103
|
+
b,
|
|
104
|
+
operator,
|
|
105
|
+
} = putout;
|
|
106
|
+
|
|
107
|
+
const {replaceWith} = operator;
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### ✅ Example of correct code
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
const putout = require('putout');
|
|
114
|
+
|
|
115
|
+
const {
|
|
116
|
+
a,
|
|
117
|
+
b,
|
|
118
|
+
operator,
|
|
119
|
+
} = putout;
|
|
120
|
+
|
|
121
|
+
const {replaceWith} = operator;
|
|
122
|
+
```
|
|
123
|
+
|
|
66
124
|
## remove-useless-rename
|
|
67
125
|
|
|
68
126
|
### ❌ Example of incorrect code
|
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
isCallExpression,
|
|
14
|
+
isObjectPattern,
|
|
15
|
+
} = types;
|
|
16
|
+
|
|
17
|
+
export const report = () => `Apply declarations order`;
|
|
18
|
+
|
|
19
|
+
export const fix = ({path, current}) => {
|
|
20
|
+
const {node} = current;
|
|
21
|
+
remove(current);
|
|
22
|
+
insertAfter(path, node);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const traverse = ({push}) => ({
|
|
26
|
+
'const __a = __b': (path) => {
|
|
27
|
+
const {__a, __b} = getTemplateValues(path, 'const __a = __b');
|
|
28
|
+
|
|
29
|
+
if (!isIdentifier(__a))
|
|
30
|
+
return;
|
|
31
|
+
|
|
32
|
+
if (!isCallExpression(__b))
|
|
33
|
+
return;
|
|
34
|
+
|
|
35
|
+
const prev = path.getAllPrevSiblings();
|
|
36
|
+
|
|
37
|
+
for (const current of prev.filter(isObjectDestructuring)) {
|
|
38
|
+
const {__d} = getTemplateValues(current, 'const __c = __d');
|
|
39
|
+
|
|
40
|
+
if (compare(__b, __d))
|
|
41
|
+
push({
|
|
42
|
+
current,
|
|
43
|
+
path,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
function isObjectDestructuring(path) {
|
|
50
|
+
if (!isVariableDeclaration(path))
|
|
51
|
+
return false;
|
|
52
|
+
|
|
53
|
+
return isObjectPattern(path.node.declarations[0].id);
|
|
54
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import * as applyDeclarationsOrder from './apply-declarations-order/index.js';
|
|
1
2
|
import * as convertConstToLet from './convert-const-to-let/index.js';
|
|
2
3
|
import * as extractKeywords from './extract-keywords/index.js';
|
|
4
|
+
import * as reuseDuplicateInit from './reuse-duplicate-init/index.js';
|
|
3
5
|
import * as removeUseless from './remove-useless/index.js';
|
|
4
6
|
import * as removeUselessAssignment from './remove-useless-assignment/index.js';
|
|
5
7
|
import * as removeUselessDeclarations from './remove-useless-declarations/index.js';
|
|
@@ -10,8 +12,10 @@ import * as removeUnused from './remove-unused/index.js';
|
|
|
10
12
|
import * as splitDeclarations from './split-declarations/index.js';
|
|
11
13
|
|
|
12
14
|
export const rules = {
|
|
15
|
+
'apply-declarations-order': applyDeclarationsOrder,
|
|
13
16
|
'convert-const-to-let': convertConstToLet,
|
|
14
17
|
'extract-keywords': extractKeywords,
|
|
18
|
+
'reuse-duplicate-init': reuseDuplicateInit,
|
|
15
19
|
'remove-useless': removeUseless,
|
|
16
20
|
'remove-useless-assignment': removeUselessAssignment,
|
|
17
21
|
'remove-useless-declarations': removeUselessDeclarations,
|
|
@@ -23,12 +23,12 @@ const {
|
|
|
23
23
|
isFunctionDeclaration,
|
|
24
24
|
isArrayExpression,
|
|
25
25
|
isRestElement,
|
|
26
|
+
isArrayPattern,
|
|
26
27
|
} = types;
|
|
27
28
|
|
|
28
29
|
export default ({use, declare, addParams}) => {
|
|
29
30
|
const traverseObj = traverseObjectExpression(use);
|
|
30
|
-
|
|
31
|
-
const processObj = processObjectPattern({
|
|
31
|
+
const declareObject = processObjectPattern({
|
|
32
32
|
use,
|
|
33
33
|
declare,
|
|
34
34
|
});
|
|
@@ -64,7 +64,7 @@ export default ({use, declare, addParams}) => {
|
|
|
64
64
|
const param = path.get('param');
|
|
65
65
|
|
|
66
66
|
if (param.isObjectPattern())
|
|
67
|
-
return
|
|
67
|
+
return declareObject(param.get('properties'));
|
|
68
68
|
|
|
69
69
|
if (!param.isIdentifier())
|
|
70
70
|
return;
|
|
@@ -141,7 +141,7 @@ export default ({use, declare, addParams}) => {
|
|
|
141
141
|
|
|
142
142
|
for (const elPath of elements) {
|
|
143
143
|
if (elPath.isObjectPattern()) {
|
|
144
|
-
|
|
144
|
+
declareObject(elPath.get('properties'));
|
|
145
145
|
continue;
|
|
146
146
|
}
|
|
147
147
|
|
|
@@ -213,7 +213,7 @@ export default ({use, declare, addParams}) => {
|
|
|
213
213
|
use(rightPath, rightPath.node.name);
|
|
214
214
|
},
|
|
215
215
|
|
|
216
|
-
|
|
216
|
+
ArrayExpression(path) {
|
|
217
217
|
const {elements} = path.node;
|
|
218
218
|
|
|
219
219
|
for (const el of elements) {
|
|
@@ -557,7 +557,12 @@ export default ({use, declare, addParams}) => {
|
|
|
557
557
|
|
|
558
558
|
/* istanbul ignore else */
|
|
559
559
|
if (isObjectPattern(node)) {
|
|
560
|
-
|
|
560
|
+
declareObject(paramPath.get('properties'));
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (isArrayPattern(node)) {
|
|
565
|
+
declareArray(paramPath.get('elements'));
|
|
561
566
|
continue;
|
|
562
567
|
}
|
|
563
568
|
}
|
|
@@ -125,6 +125,9 @@ export const traverseArrayExpression = (use) => {
|
|
|
125
125
|
for (const elementPath of elementsPaths) {
|
|
126
126
|
const {node} = elementPath;
|
|
127
127
|
|
|
128
|
+
if (isIdentifier(node))
|
|
129
|
+
use(elementPath, node.name);
|
|
130
|
+
|
|
128
131
|
if (node.properties)
|
|
129
132
|
traverseObjExpression(elementPath.get('properties'));
|
|
130
133
|
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {operator} from 'putout';
|
|
2
|
+
|
|
3
|
+
const {replaceWith} = operator;
|
|
4
|
+
|
|
5
|
+
export const report = () => 'Reuse duplicate init';
|
|
6
|
+
|
|
7
|
+
export const fix = ({path, newPath}) => {
|
|
8
|
+
replaceWith(path.get('declarations.0.init'), newPath);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const exclude = () => [
|
|
12
|
+
'const __a = __identifier',
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
export const traverse = ({push, store}) => ({
|
|
16
|
+
'const __identifier = __b'(path) {
|
|
17
|
+
const idPath = path.get('declarations.0.id');
|
|
18
|
+
const initPath = path.get('declarations.0.init');
|
|
19
|
+
const initName = initPath.toString();
|
|
20
|
+
|
|
21
|
+
store(initName, idPath);
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
'const __object = __b'(path) {
|
|
25
|
+
const initPath = path.get('declarations.0.init');
|
|
26
|
+
const initName = initPath.toString();
|
|
27
|
+
|
|
28
|
+
for (const propPath of path.get('declarations.0.id.properties')) {
|
|
29
|
+
if (propPath.isRestElement())
|
|
30
|
+
continue;
|
|
31
|
+
|
|
32
|
+
const nestedPath = propPath.get('key');
|
|
33
|
+
const nestedName = nestedPath.toString();
|
|
34
|
+
|
|
35
|
+
store(`${initName}.${nestedName}`, nestedPath);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const newPath = store(initName);
|
|
39
|
+
|
|
40
|
+
if (!newPath || !newPath.node)
|
|
41
|
+
return;
|
|
42
|
+
|
|
43
|
+
if (path === newPath.parentPath.parentPath)
|
|
44
|
+
return;
|
|
45
|
+
|
|
46
|
+
if (path.scope.uid !== newPath.scope.uid)
|
|
47
|
+
return;
|
|
48
|
+
|
|
49
|
+
push({
|
|
50
|
+
path,
|
|
51
|
+
newPath,
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-variables",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.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 find and remove useless",
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"@putout/plugin-nodejs": "*",
|
|
43
43
|
"@putout/plugin-putout": "*",
|
|
44
44
|
"@putout/plugin-reuse-duplicate-init": "*",
|
|
45
|
+
"@putout/plugin-tape": "*",
|
|
45
46
|
"@putout/test": "^14.0.0",
|
|
46
47
|
"c8": "^10.0.0",
|
|
47
48
|
"eslint": "^10.0.0-alpha.0",
|