@putout/plugin-variables 1.0.0 → 1.1.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 +28 -0
- package/lib/index.js +2 -0
- package/lib/remove-unreferenced/index.js +98 -0
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -155,6 +155,34 @@ function DestructuringErrors(a, b) {
|
|
|
155
155
|
bc = b.c.replace('x', 'y');
|
|
156
156
|
```
|
|
157
157
|
|
|
158
|
+
## remove-unreferenced
|
|
159
|
+
|
|
160
|
+
> A **variable** is a named reference to a **value**.
|
|
161
|
+
>
|
|
162
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Glossary/Variable)
|
|
163
|
+
|
|
164
|
+
### ❌ Example of incorrect code
|
|
165
|
+
|
|
166
|
+
```js
|
|
167
|
+
let a;
|
|
168
|
+
let b;
|
|
169
|
+
|
|
170
|
+
a = 5;
|
|
171
|
+
b = 6;
|
|
172
|
+
|
|
173
|
+
console.log(a);
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### ✅ Example of correct code
|
|
177
|
+
|
|
178
|
+
```js
|
|
179
|
+
let a;
|
|
180
|
+
|
|
181
|
+
a = 5;
|
|
182
|
+
|
|
183
|
+
console.log(a);
|
|
184
|
+
```
|
|
185
|
+
|
|
158
186
|
## License
|
|
159
187
|
|
|
160
188
|
MIT
|
package/lib/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import * as removeUselessAssignment from './remove-useless-assignment/index.js';
|
|
|
3
3
|
import * as removeUselessDeclarations from './remove-useless-declarations/index.js';
|
|
4
4
|
import * as removeUselessDuplicates from './remove-useless-duplicates/index.js';
|
|
5
5
|
import * as removeUselessRename from './remove-useless-rename/index.js';
|
|
6
|
+
import * as removeUnreferenced from './remove-unreferenced/index.js';
|
|
6
7
|
|
|
7
8
|
export const rules = {
|
|
8
9
|
'remove-useless': removeUseless,
|
|
@@ -10,4 +11,5 @@ export const rules = {
|
|
|
10
11
|
'remove-useless-declarations': removeUselessDeclarations,
|
|
11
12
|
'remove-useless-duplicates': removeUselessDuplicates,
|
|
12
13
|
'remove-useless-rename': removeUselessRename,
|
|
14
|
+
'remove-unreferenced': removeUnreferenced,
|
|
13
15
|
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import {operator} from 'putout';
|
|
2
|
+
|
|
3
|
+
const {remove} = operator;
|
|
4
|
+
|
|
5
|
+
export const report = () => 'Avoid unreferenced variables';
|
|
6
|
+
|
|
7
|
+
export const fix = (path) => remove(path);
|
|
8
|
+
|
|
9
|
+
export const traverse = ({push}) => ({
|
|
10
|
+
'return __a'(path) {
|
|
11
|
+
const arg = path.get('argument');
|
|
12
|
+
|
|
13
|
+
if (!arg.isIdentifier())
|
|
14
|
+
return false;
|
|
15
|
+
|
|
16
|
+
const binding = path.scope.bindings[arg.node.name];
|
|
17
|
+
|
|
18
|
+
if (!binding)
|
|
19
|
+
return false;
|
|
20
|
+
|
|
21
|
+
if (binding.constantViolations.length)
|
|
22
|
+
return false;
|
|
23
|
+
|
|
24
|
+
if (binding.referencePaths.length !== 1)
|
|
25
|
+
return false;
|
|
26
|
+
|
|
27
|
+
if (!binding.path.isVariableDeclarator())
|
|
28
|
+
return false;
|
|
29
|
+
|
|
30
|
+
if (binding.path.node.init)
|
|
31
|
+
return false;
|
|
32
|
+
|
|
33
|
+
push(path);
|
|
34
|
+
},
|
|
35
|
+
'__identifier = __a'(path) {
|
|
36
|
+
const {parentPath} = path;
|
|
37
|
+
|
|
38
|
+
if (parentPath.isMemberExpression())
|
|
39
|
+
return;
|
|
40
|
+
|
|
41
|
+
if (parentPath.isConditionalExpression())
|
|
42
|
+
return;
|
|
43
|
+
|
|
44
|
+
const {name} = path.node.left;
|
|
45
|
+
const binding = path.scope.getAllBindings()[name];
|
|
46
|
+
|
|
47
|
+
if (!binding)
|
|
48
|
+
return;
|
|
49
|
+
|
|
50
|
+
if (path.find(isInsideForOf))
|
|
51
|
+
return;
|
|
52
|
+
|
|
53
|
+
const {referenced} = binding;
|
|
54
|
+
|
|
55
|
+
if (referenced)
|
|
56
|
+
return;
|
|
57
|
+
|
|
58
|
+
if (binding.path.isObjectPattern()) {
|
|
59
|
+
const propPath = getPropertyPath(binding.path, name);
|
|
60
|
+
|
|
61
|
+
push(path);
|
|
62
|
+
push(propPath);
|
|
63
|
+
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const idPath = binding.path.get('id');
|
|
68
|
+
|
|
69
|
+
if (binding.path.isVariableDeclarator() && idPath.isObjectPattern()) {
|
|
70
|
+
const propPath = getPropertyPath(idPath, name);
|
|
71
|
+
|
|
72
|
+
push(path);
|
|
73
|
+
push(propPath);
|
|
74
|
+
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
push(path);
|
|
79
|
+
push(binding.path);
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
function getPropertyPath(path, name) {
|
|
84
|
+
let propPath;
|
|
85
|
+
|
|
86
|
+
for (propPath of path.get('properties')) {
|
|
87
|
+
const {key, shorthand} = propPath.node;
|
|
88
|
+
|
|
89
|
+
if (shorthand && key.name !== name)
|
|
90
|
+
continue;
|
|
91
|
+
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return propPath;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const isInsideForOf = (path) => path.__putout_for_of_reduce;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-variables",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.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",
|
|
@@ -37,7 +37,9 @@
|
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@putout/eslint-flat": "^3.0.0",
|
|
39
39
|
"@putout/plugin-declare": "*",
|
|
40
|
+
"@putout/plugin-for-of": "*",
|
|
40
41
|
"@putout/plugin-maybe": "*",
|
|
42
|
+
"@putout/plugin-minify": "*",
|
|
41
43
|
"@putout/plugin-remove-useless-array-from": "*",
|
|
42
44
|
"@putout/plugin-reuse-duplicate-init": "*",
|
|
43
45
|
"@putout/test": "^14.0.0",
|