@putout/plugin-for-of 1.0.0 → 1.2.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 +176 -3
- package/lib/for-each/index.js +187 -0
- package/lib/for-in-negative/index.js +41 -0
- package/lib/for-in-positive/index.js +39 -0
- package/lib/index.js +8 -0
- package/lib/reduce/index.js +68 -0
- package/lib/remove-unused-variables/index.js +139 -0
- package/lib/remove-useless/index.js +60 -0
- package/lib/remove-useless-array-from/index.js +8 -0
- package/lib/remove-useless-variables/index.js +69 -0
- package/package.json +9 -2
package/README.md
CHANGED
|
@@ -21,6 +21,15 @@ npm i @putout/plugin-for-of
|
|
|
21
21
|
{
|
|
22
22
|
"rules": {
|
|
23
23
|
"for-of/map": "on",
|
|
24
|
+
"for-of/for-in": "on",
|
|
25
|
+
"for-of/for-each": "on",
|
|
26
|
+
"for-of/reduce": "on",
|
|
27
|
+
"for-of/remove-unused-variables": "on",
|
|
28
|
+
"for-of/remove-useless": "on",
|
|
29
|
+
"for-of/remove-useless-array-from": "on",
|
|
30
|
+
"for-of/remove-useless-variables": ["on", {
|
|
31
|
+
"maxProperties": 4
|
|
32
|
+
}],
|
|
24
33
|
"for-of/for": "on"
|
|
25
34
|
}
|
|
26
35
|
}
|
|
@@ -32,7 +41,7 @@ npm i @putout/plugin-for-of
|
|
|
32
41
|
>
|
|
33
42
|
> (c) MDN
|
|
34
43
|
|
|
35
|
-
|
|
44
|
+
### ❌ Example of incorrect code
|
|
36
45
|
|
|
37
46
|
```js
|
|
38
47
|
names.map((name) => {
|
|
@@ -40,7 +49,7 @@ names.map((name) => {
|
|
|
40
49
|
});
|
|
41
50
|
```
|
|
42
51
|
|
|
43
|
-
|
|
52
|
+
### ✅ Example of correct code
|
|
44
53
|
|
|
45
54
|
```js
|
|
46
55
|
for (const name of names) {
|
|
@@ -48,11 +57,174 @@ for (const name of names) {
|
|
|
48
57
|
}
|
|
49
58
|
```
|
|
50
59
|
|
|
60
|
+
## for-in
|
|
61
|
+
|
|
62
|
+
> The [`for...in`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) statement iterates over all enumerable properties of an object that are keyed by strings.
|
|
63
|
+
>
|
|
64
|
+
> (c) MDN
|
|
65
|
+
|
|
66
|
+
### ❌ Example of incorrect code
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
for (const item in object) {
|
|
70
|
+
if (object.hasOwnProperty(item)) {
|
|
71
|
+
log(item);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
for (const item in object) {
|
|
76
|
+
if (!object.hasOwnProperty(item))
|
|
77
|
+
continue;
|
|
78
|
+
|
|
79
|
+
log(item);
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### ✅ Example of correct code
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
for (const item of Object.keys(object)) {
|
|
87
|
+
log(item);
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## for-each
|
|
92
|
+
|
|
93
|
+
> The [`forEach()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) method executes a provided function once for each array element.
|
|
94
|
+
>
|
|
95
|
+
> (c) MDN
|
|
96
|
+
|
|
97
|
+
### ❌ Example of incorrect code
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
Object.keys(json).forEach((name) => {
|
|
101
|
+
manage(name, json[name]);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
[].forEach.call(arguments, (item) => {
|
|
105
|
+
console.log(item);
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### ✅ Example of correct code
|
|
110
|
+
|
|
111
|
+
```js
|
|
112
|
+
for (const name of Object.keys(json)) {
|
|
113
|
+
manage(name, json[name]);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
for (const item of arguments) {
|
|
117
|
+
console.log(item);
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## reduce
|
|
122
|
+
|
|
123
|
+
> - The [`reduce()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) method executes a user-supplied **reducer** callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
|
|
124
|
+
>
|
|
125
|
+
> (c) MDN
|
|
126
|
+
|
|
127
|
+
You should always look at second argument of a **reducer** since it changes logic drastically and should read back and forth a couple times to understand what is going on.
|
|
128
|
+
|
|
129
|
+
> Recursive functions like `.reduce()` can be powerful but sometimes difficult to understand, especially for less experienced **JavaScript** developers. If code becomes clearer when using other array methods, developers must weigh the readability tradeoff against the other benefits of using `.reduce()`. In cases where `.reduce()` is the best choice, documentation and semantic variable naming can help mitigate readability drawbacks.
|
|
130
|
+
>
|
|
131
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#when_to_not_use_reduce)
|
|
132
|
+
|
|
133
|
+
Check it out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/107751499a4bbdd83b9181444d8bdbbd/8d3b646a3df0025d564e2bb141cc7f6a6bb1b3a5).
|
|
134
|
+
|
|
135
|
+
## ❌ Example of incorrect code
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
const result = list.reduce((a, b) => a + b, 1);
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## ✅ Example of correct code
|
|
142
|
+
|
|
143
|
+
```js
|
|
144
|
+
let sum = 1;
|
|
145
|
+
|
|
146
|
+
for (const a of list) {
|
|
147
|
+
sum += a;
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## remove-unused-variables
|
|
152
|
+
|
|
153
|
+
### ❌ Example of incorrect code
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
for (const {a, b} of c) {
|
|
157
|
+
console.log(a);
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### ✅ Example of correct code
|
|
162
|
+
|
|
163
|
+
```js
|
|
164
|
+
for (const {a} of c) {
|
|
165
|
+
console.log(a);
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## remove-useless
|
|
170
|
+
|
|
171
|
+
> The `Array` enables storing a collection of multiple items under a single variable name.
|
|
172
|
+
>
|
|
173
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
|
|
174
|
+
|
|
175
|
+
### ❌ Example of incorrect code
|
|
176
|
+
|
|
177
|
+
```js
|
|
178
|
+
for (const a of ['hello']) {
|
|
179
|
+
console.log(a);
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### ✅ Example of correct code
|
|
184
|
+
|
|
185
|
+
```js
|
|
186
|
+
console.log('hello');
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## remove-useless-variables
|
|
190
|
+
|
|
191
|
+
### ❌ Example of incorrect code
|
|
192
|
+
|
|
193
|
+
```js
|
|
194
|
+
for (const a of b) {
|
|
195
|
+
const {c} = a;
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### ✅ Example of correct code
|
|
200
|
+
|
|
201
|
+
```js
|
|
202
|
+
for (const {c} of b) {
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## remove-useless-array-from
|
|
207
|
+
|
|
208
|
+
> The `Array.from()` static method creates a new, shallow-copied `Array` instance from an iterable or array-like object.
|
|
209
|
+
>
|
|
210
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from)
|
|
211
|
+
|
|
212
|
+
### ❌ Example of incorrect code
|
|
213
|
+
|
|
214
|
+
```js
|
|
215
|
+
for (const x of Array.from(y)) {}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### ✅ Example of correct code
|
|
219
|
+
|
|
220
|
+
```js
|
|
221
|
+
for (const x of y) {}
|
|
222
|
+
```
|
|
223
|
+
|
|
51
224
|
## for
|
|
52
225
|
|
|
53
226
|
> The [`for`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for) statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement to be executed in the loop.
|
|
54
227
|
>
|
|
55
|
-
>
|
|
56
228
|
> (c) MDN
|
|
57
229
|
|
|
58
230
|
### for-n
|
|
@@ -120,6 +292,7 @@ for (const [i, item] of array.entries()) {
|
|
|
120
292
|
|
|
121
293
|
```js
|
|
122
294
|
const n = array.length;
|
|
295
|
+
|
|
123
296
|
for (let i = 0; i < n; i++) {
|
|
124
297
|
const item = array[i];
|
|
125
298
|
console.log(i, item);
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
template,
|
|
5
|
+
operator,
|
|
6
|
+
types,
|
|
7
|
+
} = require('putout');
|
|
8
|
+
|
|
9
|
+
const {
|
|
10
|
+
ContinueStatement,
|
|
11
|
+
isIdentifier,
|
|
12
|
+
} = types;
|
|
13
|
+
|
|
14
|
+
const {
|
|
15
|
+
replaceWith,
|
|
16
|
+
replaceWithMultiple,
|
|
17
|
+
traverse,
|
|
18
|
+
} = operator;
|
|
19
|
+
|
|
20
|
+
const forOfTemplate = template(`
|
|
21
|
+
for (const %%item%% of %%items%%)
|
|
22
|
+
%%body%%
|
|
23
|
+
`);
|
|
24
|
+
|
|
25
|
+
const forOfEntriesTemplate = template(`
|
|
26
|
+
for (const [%%index%%, %%item%%] of %%items%%.entries())
|
|
27
|
+
%%body%%
|
|
28
|
+
`);
|
|
29
|
+
|
|
30
|
+
const {keys} = Object;
|
|
31
|
+
const isRoot = (path) => path.isFunction() || path.isProgram();
|
|
32
|
+
|
|
33
|
+
module.exports.report = () => `for-of should be used instead of forEach`;
|
|
34
|
+
|
|
35
|
+
module.exports.replace = () => ({
|
|
36
|
+
'__.forEach.call(__a, (__b) => __body)': 'for (const __b of __a) __body',
|
|
37
|
+
'__.forEach(__args)': (vars, path) => {
|
|
38
|
+
const {params, body} = path.node.arguments[0];
|
|
39
|
+
const item = getItem(params);
|
|
40
|
+
|
|
41
|
+
delete item.typeAnnotation;
|
|
42
|
+
const {length} = params;
|
|
43
|
+
const thisPassed = isIdentifier(params[0], {name: 'this'});
|
|
44
|
+
const items = path.node.callee.object;
|
|
45
|
+
|
|
46
|
+
if (length === 1 || length === 2 && thisPassed) {
|
|
47
|
+
const newPath = replaceWith(path, forOfTemplate({
|
|
48
|
+
item,
|
|
49
|
+
items,
|
|
50
|
+
body,
|
|
51
|
+
}));
|
|
52
|
+
|
|
53
|
+
fixReturn(newPath);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (params.length === 2) {
|
|
58
|
+
const [, index] = params;
|
|
59
|
+
const newPath = replaceWith(path, forOfEntriesTemplate({
|
|
60
|
+
index,
|
|
61
|
+
item,
|
|
62
|
+
items,
|
|
63
|
+
body,
|
|
64
|
+
}));
|
|
65
|
+
|
|
66
|
+
fixReturn(newPath);
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
module.exports.match = () => ({
|
|
72
|
+
'__.forEach(__args)': (vars, path) => {
|
|
73
|
+
const objectPath = path.get('callee.object');
|
|
74
|
+
const fnPath = path.get('arguments.0');
|
|
75
|
+
|
|
76
|
+
if (!fnPath.isFunction())
|
|
77
|
+
return false;
|
|
78
|
+
|
|
79
|
+
const params = fnPath.get('params');
|
|
80
|
+
|
|
81
|
+
if (!params.length)
|
|
82
|
+
return false;
|
|
83
|
+
|
|
84
|
+
if (isParentContainsFunctionArgument(objectPath))
|
|
85
|
+
return false;
|
|
86
|
+
|
|
87
|
+
if (path.node.arguments.length === 2 && !path.get('arguments.1').isThisExpression())
|
|
88
|
+
return false;
|
|
89
|
+
|
|
90
|
+
// this is the case when "i" declared and "this"
|
|
91
|
+
if (params.length >= 3 && params[0].isIdentifier({name: 'this'}))
|
|
92
|
+
return false;
|
|
93
|
+
|
|
94
|
+
const [paramPath] = params;
|
|
95
|
+
|
|
96
|
+
if (isSameNames(paramPath, objectPath))
|
|
97
|
+
return false;
|
|
98
|
+
|
|
99
|
+
const rootPath = path.findParent(isRoot);
|
|
100
|
+
|
|
101
|
+
if (isBoundVars(rootPath, fnPath))
|
|
102
|
+
return false;
|
|
103
|
+
|
|
104
|
+
return true;
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
function isSameNames(paramPath, objectPath) {
|
|
109
|
+
const {name} = paramPath.node;
|
|
110
|
+
return objectPath.isIdentifier({name});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function fixReturn(path) {
|
|
114
|
+
traverse(path, {
|
|
115
|
+
'return __'(path) {
|
|
116
|
+
if (!isForBeforeFnUp(path))
|
|
117
|
+
return;
|
|
118
|
+
|
|
119
|
+
const {argument} = path.node;
|
|
120
|
+
|
|
121
|
+
if (!argument)
|
|
122
|
+
return replaceWith(path, ContinueStatement());
|
|
123
|
+
|
|
124
|
+
replaceWithMultiple(path, [
|
|
125
|
+
argument,
|
|
126
|
+
ContinueStatement(),
|
|
127
|
+
]);
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function isBoundVars(parentPath, path) {
|
|
133
|
+
const currentBindings = keys(parentPath.scope.bindings);
|
|
134
|
+
const fnBindings = keys(path.scope.bindings);
|
|
135
|
+
|
|
136
|
+
return compare(currentBindings, fnBindings);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function isParentContainsFunctionArgument(objectPath) {
|
|
140
|
+
if (!objectPath.isCallExpression())
|
|
141
|
+
return false;
|
|
142
|
+
|
|
143
|
+
for (const argPath of objectPath.get('arguments')) {
|
|
144
|
+
if (argPath.isFunction())
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function getItem(params) {
|
|
150
|
+
const [thisItem, item] = params;
|
|
151
|
+
|
|
152
|
+
if (params[0].name === 'this')
|
|
153
|
+
return item;
|
|
154
|
+
|
|
155
|
+
return thisItem;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function compare(a, b) {
|
|
159
|
+
for (const el of a) {
|
|
160
|
+
if (b.includes(el))
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function isForBeforeFnUp(path) {
|
|
168
|
+
let wasForOf = false;
|
|
169
|
+
let wasFn = false;
|
|
170
|
+
|
|
171
|
+
while (path = path.parentPath) {
|
|
172
|
+
if (path.isFunction())
|
|
173
|
+
wasFn = true;
|
|
174
|
+
|
|
175
|
+
if (path.isForOfStatement())
|
|
176
|
+
wasForOf = true;
|
|
177
|
+
|
|
178
|
+
if (wasForOf && wasFn)
|
|
179
|
+
return true;
|
|
180
|
+
|
|
181
|
+
if (wasFn && !wasForOf)
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
generate,
|
|
5
|
+
operator,
|
|
6
|
+
} = require('putout');
|
|
7
|
+
|
|
8
|
+
const {
|
|
9
|
+
contains,
|
|
10
|
+
getTemplateValues,
|
|
11
|
+
} = operator;
|
|
12
|
+
|
|
13
|
+
module.exports.report = () => `for-of should be used instead of for-in`;
|
|
14
|
+
|
|
15
|
+
module.exports.match = () => ({
|
|
16
|
+
'for (__a in __b) __body': ({__a, __b, __body}) => {
|
|
17
|
+
const declaration = getTemplateValues(__a, 'var __a');
|
|
18
|
+
|
|
19
|
+
if (!declaration.__a)
|
|
20
|
+
return false;
|
|
21
|
+
|
|
22
|
+
const {name} = declaration.__a;
|
|
23
|
+
|
|
24
|
+
return contains(__body, [
|
|
25
|
+
`if (!${__b.name}.hasOwnProperty(${name})) __c`,
|
|
26
|
+
]);
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
module.exports.replace = () => ({
|
|
31
|
+
'for (__a in __b) __body': ({__b, __body}) => {
|
|
32
|
+
const [first] = __body.body;
|
|
33
|
+
const condition = getTemplateValues(first, 'if (!__b.hasOwnProperty(__a)) __c');
|
|
34
|
+
|
|
35
|
+
__body.body.shift();
|
|
36
|
+
const {code} = generate(__body);
|
|
37
|
+
|
|
38
|
+
return `for (const ${condition.__a.name} of Object.keys(${__b.name})) ${code}`;
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
generate,
|
|
5
|
+
operator,
|
|
6
|
+
} = require('putout');
|
|
7
|
+
|
|
8
|
+
const {
|
|
9
|
+
contains,
|
|
10
|
+
getTemplateValues,
|
|
11
|
+
} = operator;
|
|
12
|
+
|
|
13
|
+
module.exports.report = () => `for-of should be used instead of for-in`;
|
|
14
|
+
|
|
15
|
+
module.exports.match = () => ({
|
|
16
|
+
'for (__a in __b) __body': ({__a, __b, __body}) => {
|
|
17
|
+
const declaration = getTemplateValues(__a, 'var __a');
|
|
18
|
+
|
|
19
|
+
if (!declaration.__a)
|
|
20
|
+
return false;
|
|
21
|
+
|
|
22
|
+
const {name} = declaration.__a;
|
|
23
|
+
|
|
24
|
+
return contains(__body, [
|
|
25
|
+
`if (${__b.name}.hasOwnProperty(${name})) __body`,
|
|
26
|
+
]);
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
module.exports.replace = () => ({
|
|
31
|
+
'for (__a in __b) __body': ({__b, __body}) => {
|
|
32
|
+
const [first] = __body.body;
|
|
33
|
+
const condition = getTemplateValues(first, 'if (__b.hasOwnProperty(__a)) __body');
|
|
34
|
+
const {code} = generate(condition.__body);
|
|
35
|
+
|
|
36
|
+
return `for (const ${condition.__a.name} of Object.keys(${__b.name})) ${code}`;
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
package/lib/index.js
CHANGED
|
@@ -6,9 +6,17 @@ const getRule = (a) => ({
|
|
|
6
6
|
|
|
7
7
|
module.exports.rules = {
|
|
8
8
|
...getRule('map'),
|
|
9
|
+
...getRule('for-each'),
|
|
9
10
|
...getRule('for-n'),
|
|
10
11
|
...getRule('for-length'),
|
|
11
12
|
...getRule('for-entries'),
|
|
12
13
|
...getRule('for-entries-n'),
|
|
14
|
+
...getRule('for-in-negative'),
|
|
15
|
+
...getRule('for-in-positive'),
|
|
16
|
+
...getRule('reduce'),
|
|
17
|
+
...getRule('remove-useless'),
|
|
18
|
+
...getRule('remove-useless-variables'),
|
|
19
|
+
...getRule('remove-useless-array-from'),
|
|
20
|
+
...getRule('remove-unused-variables'),
|
|
13
21
|
};
|
|
14
22
|
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {types} = require('putout');
|
|
4
|
+
const {
|
|
5
|
+
isBlockStatement,
|
|
6
|
+
isArrayExpression,
|
|
7
|
+
isCallExpression,
|
|
8
|
+
} = types;
|
|
9
|
+
|
|
10
|
+
module.exports.report = () => `Use 'for...of' instead of '.reduce()'`;
|
|
11
|
+
|
|
12
|
+
module.exports.match = () => ({
|
|
13
|
+
'const __a = __b.reduce((__c, __d) => __e)': ({__b, __e}) => {
|
|
14
|
+
if (isArrayExpression(__b))
|
|
15
|
+
return false;
|
|
16
|
+
|
|
17
|
+
if (isCallExpression(__b))
|
|
18
|
+
return false;
|
|
19
|
+
|
|
20
|
+
return !isBlockStatement(__e);
|
|
21
|
+
},
|
|
22
|
+
'const __a = __b.reduceRight((__c, __d) => __e)': ({__b, __e}) => {
|
|
23
|
+
if (isArrayExpression(__b))
|
|
24
|
+
return false;
|
|
25
|
+
|
|
26
|
+
return !isBlockStatement(__e);
|
|
27
|
+
},
|
|
28
|
+
'const __a = __b.reduce((__c, __d) => __e, __f)': ({__e}) => !isBlockStatement(__e),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
module.exports.replace = () => ({
|
|
32
|
+
'const __a = __b.reduce((__c, __d) => __e)': ({__a, __d}, path) => {
|
|
33
|
+
rename(path, __d, __a);
|
|
34
|
+
|
|
35
|
+
return `{
|
|
36
|
+
let [__a] = __b;
|
|
37
|
+
for (const __c of __b.slice(1)) {
|
|
38
|
+
__a = __e;
|
|
39
|
+
}
|
|
40
|
+
}`;
|
|
41
|
+
},
|
|
42
|
+
'const __a = __b.reduceRight((__c, __d) => __e)': ({__a, __d}, path) => {
|
|
43
|
+
rename(path, __d, __a);
|
|
44
|
+
|
|
45
|
+
return `{
|
|
46
|
+
let [__a] = __b;
|
|
47
|
+
for (const __c of __b.slice(1)) {
|
|
48
|
+
__a = __e;
|
|
49
|
+
}
|
|
50
|
+
}`;
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
'const __a = __b.reduce((__c, __d) => __e, __f)': () => {
|
|
54
|
+
return `{
|
|
55
|
+
let __a = __f;
|
|
56
|
+
for (const __d of __b) {
|
|
57
|
+
__a = __e;
|
|
58
|
+
}
|
|
59
|
+
}`;
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
function rename(path, from, to) {
|
|
64
|
+
const fnScope = path.get('declarations.0.init.arguments.0').scope;
|
|
65
|
+
fnScope.rename(from.name, to.name);
|
|
66
|
+
delete path.scope.bindings[from.name];
|
|
67
|
+
}
|
|
68
|
+
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {types} = require('putout');
|
|
4
|
+
const {
|
|
5
|
+
isIdentifier,
|
|
6
|
+
isObjectProperty,
|
|
7
|
+
} = types;
|
|
8
|
+
|
|
9
|
+
const getValue = ({value}) => value;
|
|
10
|
+
|
|
11
|
+
module.exports.report = (path) => {
|
|
12
|
+
const variables = getVariables(path);
|
|
13
|
+
const names = getNames(variables);
|
|
14
|
+
|
|
15
|
+
return `'${names.join(', ')}' inside 'for...of' loop defined but never used`;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
module.exports.match = () => ({
|
|
19
|
+
'for (const __array of __b) __c': (vars, path) => match(path),
|
|
20
|
+
'for (const __object of __b) __c': (vars, path) => match(path),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
module.exports.replace = () => ({
|
|
24
|
+
'for (const __array of __b) __c': (vars, path) => replace(path),
|
|
25
|
+
'for (const __object of __b) __c': (vars, path) => replace(path),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
function match(path) {
|
|
29
|
+
const idPath = path.get('left.declarations.0.id');
|
|
30
|
+
const elements = getElements(idPath);
|
|
31
|
+
|
|
32
|
+
if (!isAllIdentifiers(elements))
|
|
33
|
+
return false;
|
|
34
|
+
|
|
35
|
+
if (isAllReferenced(idPath, elements))
|
|
36
|
+
return false;
|
|
37
|
+
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function replace(path) {
|
|
42
|
+
const variables = getVariables(path);
|
|
43
|
+
|
|
44
|
+
for (const currentPath of variables) {
|
|
45
|
+
currentPath.remove();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return path;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getVariables(path) {
|
|
52
|
+
const idPath = path.get('left.declarations.0.id');
|
|
53
|
+
const elements = getPathElements(idPath);
|
|
54
|
+
const result = [];
|
|
55
|
+
|
|
56
|
+
for (const el of elements) {
|
|
57
|
+
if (!isReferenced(path, el))
|
|
58
|
+
result.push(el);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function isAllIdentifiers(array) {
|
|
65
|
+
const realArray = array.filter(Boolean);
|
|
66
|
+
|
|
67
|
+
const {length} = realArray;
|
|
68
|
+
const {length: realLength} = realArray.filter(isIdentifier);
|
|
69
|
+
|
|
70
|
+
return length === realLength;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function isReferenced({scope}, path) {
|
|
74
|
+
const {bindings} = scope;
|
|
75
|
+
|
|
76
|
+
if (path.isObjectProperty()) {
|
|
77
|
+
const {name} = path.node.value;
|
|
78
|
+
const current = bindings[name];
|
|
79
|
+
|
|
80
|
+
return !current || current.referenced;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const {node} = path;
|
|
84
|
+
|
|
85
|
+
if (!node)
|
|
86
|
+
return true;
|
|
87
|
+
|
|
88
|
+
const {name} = path.node;
|
|
89
|
+
|
|
90
|
+
return bindings[name].referenced;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function isAllReferenced(path, array) {
|
|
94
|
+
const {bindings} = path.scope;
|
|
95
|
+
|
|
96
|
+
for (const {name} of array) {
|
|
97
|
+
const current = bindings[name];
|
|
98
|
+
|
|
99
|
+
if (!current.referenced)
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function getElements({node}) {
|
|
107
|
+
const {
|
|
108
|
+
properties,
|
|
109
|
+
elements,
|
|
110
|
+
} = node;
|
|
111
|
+
|
|
112
|
+
if (elements)
|
|
113
|
+
return elements.filter(Boolean);
|
|
114
|
+
|
|
115
|
+
return properties.map(getValue).filter(Boolean);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function getPathElements(path) {
|
|
119
|
+
if (path.isObjectPattern())
|
|
120
|
+
return path.get('properties');
|
|
121
|
+
|
|
122
|
+
return path.get('elements');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function getNames(paths) {
|
|
126
|
+
const result = [];
|
|
127
|
+
|
|
128
|
+
for (const {node} of paths) {
|
|
129
|
+
if (isObjectProperty(node)) {
|
|
130
|
+
result.push(node.value.name);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
result.push(node.name);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {types, operator} = require('putout');
|
|
4
|
+
const {isIdentifier} = types;
|
|
5
|
+
const {replaceWith} = operator;
|
|
6
|
+
|
|
7
|
+
module.exports.report = () => `Avoid useless for-of`;
|
|
8
|
+
|
|
9
|
+
module.exports.match = () => ({
|
|
10
|
+
'for (const __identifier__a of __identifier__b) __c': ({__identifier__a}, path) => {
|
|
11
|
+
const {name} = __identifier__a;
|
|
12
|
+
const {references} = path.scope.bindings[name];
|
|
13
|
+
|
|
14
|
+
return !references;
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
'for (const __a of __array) __c': ({__a, __array}, path) => {
|
|
18
|
+
if (__array.elements.length >= 2)
|
|
19
|
+
return false;
|
|
20
|
+
|
|
21
|
+
if (!isIdentifier(__a))
|
|
22
|
+
return false;
|
|
23
|
+
|
|
24
|
+
const {name} = __a;
|
|
25
|
+
const {references} = path.scope.bindings[name];
|
|
26
|
+
|
|
27
|
+
if (references >= 2)
|
|
28
|
+
return false;
|
|
29
|
+
|
|
30
|
+
return true;
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
module.exports.replace = () => ({
|
|
35
|
+
'for (const __identifier__a of __identifier__b) __c': '__c',
|
|
36
|
+
'for (const __a of __array) __c': ({__a, __c, __array}, path) => {
|
|
37
|
+
const {elements} = __array;
|
|
38
|
+
|
|
39
|
+
if (!elements.length)
|
|
40
|
+
return null;
|
|
41
|
+
|
|
42
|
+
const {name} = __a;
|
|
43
|
+
const {
|
|
44
|
+
references,
|
|
45
|
+
referencePaths,
|
|
46
|
+
} = path.scope.bindings[name];
|
|
47
|
+
|
|
48
|
+
if (!references)
|
|
49
|
+
return replaceWith(path, __c);
|
|
50
|
+
|
|
51
|
+
const [el] = elements;
|
|
52
|
+
const [refPath] = referencePaths;
|
|
53
|
+
|
|
54
|
+
replaceWith(refPath, el);
|
|
55
|
+
replaceWith(path, __c);
|
|
56
|
+
|
|
57
|
+
return path;
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
types,
|
|
5
|
+
operator,
|
|
6
|
+
} = require('putout');
|
|
7
|
+
const {
|
|
8
|
+
replaceWith,
|
|
9
|
+
remove,
|
|
10
|
+
} = operator;
|
|
11
|
+
|
|
12
|
+
const {isAssignmentPattern} = types;
|
|
13
|
+
|
|
14
|
+
const isToManyProperties = (a, {maxProperties}) => a.isObjectPattern() && a.node.properties.length > maxProperties;
|
|
15
|
+
const isAssignment = (a) => isAssignmentPattern(a.value);
|
|
16
|
+
|
|
17
|
+
module.exports.report = () => `Use destructuring in head of 'for...of'`;
|
|
18
|
+
|
|
19
|
+
module.exports.fix = ({path, varPath}) => {
|
|
20
|
+
replaceWith(varPath, path.node.id);
|
|
21
|
+
remove(path);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
module.exports.traverse = ({push, options}) => ({
|
|
25
|
+
'for (const __ of __) __'(path) {
|
|
26
|
+
const leftPath = path.get('left');
|
|
27
|
+
const varPath = leftPath.get('declarations.0.id');
|
|
28
|
+
|
|
29
|
+
if (!varPath.isIdentifier())
|
|
30
|
+
return;
|
|
31
|
+
|
|
32
|
+
const {scope, node} = varPath;
|
|
33
|
+
const {name} = node;
|
|
34
|
+
|
|
35
|
+
const {
|
|
36
|
+
references,
|
|
37
|
+
referencePaths,
|
|
38
|
+
} = scope.bindings[name];
|
|
39
|
+
|
|
40
|
+
if (references !== 1)
|
|
41
|
+
return;
|
|
42
|
+
|
|
43
|
+
const [referencePath] = referencePaths;
|
|
44
|
+
const {parentPath} = referencePath;
|
|
45
|
+
const isSameName = parentPath
|
|
46
|
+
.get('init')
|
|
47
|
+
.isIdentifier({name});
|
|
48
|
+
|
|
49
|
+
if (!isSameName)
|
|
50
|
+
return;
|
|
51
|
+
|
|
52
|
+
const idPath = parentPath.get('id');
|
|
53
|
+
const {maxProperties = 4} = options;
|
|
54
|
+
|
|
55
|
+
if (isToManyProperties(idPath, {maxProperties}))
|
|
56
|
+
return;
|
|
57
|
+
|
|
58
|
+
const {properties} = idPath.node;
|
|
59
|
+
|
|
60
|
+
if (idPath.isObjectPattern() && properties.find(isAssignment))
|
|
61
|
+
return;
|
|
62
|
+
|
|
63
|
+
push({
|
|
64
|
+
path: parentPath,
|
|
65
|
+
varPath,
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-for-of",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "🐊Putout plugin adds ability to apply for...of",
|
|
@@ -32,11 +32,18 @@
|
|
|
32
32
|
"try-to-catch"
|
|
33
33
|
],
|
|
34
34
|
"devDependencies": {
|
|
35
|
+
"@putout/plugin-conditions": "*",
|
|
36
|
+
"@putout/plugin-convert-const-to-let": "*",
|
|
37
|
+
"@putout/plugin-remove-empty": "*",
|
|
38
|
+
"@putout/plugin-remove-nested-blocks": "*",
|
|
39
|
+
"@putout/plugin-remove-unused-variables": "*",
|
|
40
|
+
"@putout/plugin-remove-useless-arguments": "*",
|
|
41
|
+
"@putout/plugin-remove-useless-continue": "*",
|
|
35
42
|
"@putout/test": "^6.0.0",
|
|
36
43
|
"c8": "^7.5.0",
|
|
37
44
|
"eslint": "^8.0.1",
|
|
38
45
|
"eslint-plugin-n": "^15.2.4",
|
|
39
|
-
"eslint-plugin-putout": "^
|
|
46
|
+
"eslint-plugin-putout": "^17.0.0",
|
|
40
47
|
"lerna": "^6.0.1",
|
|
41
48
|
"madrun": "^9.0.0",
|
|
42
49
|
"nodemon": "^2.0.1"
|