@putout/plugin-for-of 1.3.0 → 2.0.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 +6 -5
- package/lib/for-each/index.js +15 -11
- package/lib/for-entries/index.js +1 -2
- package/lib/for-entries-n/index.js +1 -1
- package/lib/for-in-negative/index.js +3 -8
- package/lib/for-in-positive/index.js +2 -8
- package/lib/for-length/index.js +1 -1
- package/lib/for-n/index.js +2 -5
- package/lib/index.js +0 -1
- package/lib/map/index.js +1 -5
- package/lib/reduce/index.js +6 -3
- package/lib/remove-unused-variables/index.js +6 -13
- package/lib/remove-useless/index.js +4 -10
- package/lib/remove-useless-array-from/index.js +0 -1
- package/lib/remove-useless-variables/index.js +9 -15
- package/package.json +8 -6
package/README.md
CHANGED
|
@@ -97,9 +97,11 @@ for (const item of Object.keys(object)) {
|
|
|
97
97
|
### ❌ Example of incorrect code
|
|
98
98
|
|
|
99
99
|
```js
|
|
100
|
-
Object
|
|
101
|
-
|
|
102
|
-
|
|
100
|
+
Object
|
|
101
|
+
.keys(json)
|
|
102
|
+
.forEach((name) => {
|
|
103
|
+
manage(name, json[name]);
|
|
104
|
+
});
|
|
103
105
|
|
|
104
106
|
[].forEach.call(arguments, (item) => {
|
|
105
107
|
console.log(item);
|
|
@@ -199,8 +201,7 @@ for (const a of b) {
|
|
|
199
201
|
### ✅ Example of correct code
|
|
200
202
|
|
|
201
203
|
```js
|
|
202
|
-
for (const {c} of b) {
|
|
203
|
-
}
|
|
204
|
+
for (const {c} of b) {}
|
|
204
205
|
```
|
|
205
206
|
|
|
206
207
|
## remove-useless-array-from
|
package/lib/for-each/index.js
CHANGED
|
@@ -36,11 +36,17 @@ module.exports.replace = () => ({
|
|
|
36
36
|
'__.forEach.call(__a, (__b) => __body)': 'for (const __b of __a) __body',
|
|
37
37
|
'__.forEach(__args)': (vars, path) => {
|
|
38
38
|
const {params, body} = path.node.arguments[0];
|
|
39
|
+
|
|
39
40
|
const item = getItem(params);
|
|
40
41
|
|
|
41
42
|
delete item.typeAnnotation;
|
|
43
|
+
|
|
42
44
|
const {length} = params;
|
|
43
|
-
|
|
45
|
+
|
|
46
|
+
const thisPassed = isIdentifier(params[0], {
|
|
47
|
+
name: 'this',
|
|
48
|
+
});
|
|
49
|
+
|
|
44
50
|
const items = path.node.callee.object;
|
|
45
51
|
|
|
46
52
|
if (length === 1 || length === 2 && thisPassed) {
|
|
@@ -51,11 +57,13 @@ module.exports.replace = () => ({
|
|
|
51
57
|
}));
|
|
52
58
|
|
|
53
59
|
fixReturn(newPath);
|
|
60
|
+
|
|
54
61
|
return;
|
|
55
62
|
}
|
|
56
63
|
|
|
57
64
|
if (params.length === 2) {
|
|
58
65
|
const [, index] = params;
|
|
66
|
+
|
|
59
67
|
const newPath = replaceWith(path, forOfEntriesTemplate({
|
|
60
68
|
index,
|
|
61
69
|
item,
|
|
@@ -98,16 +106,16 @@ module.exports.match = () => ({
|
|
|
98
106
|
|
|
99
107
|
const rootPath = path.findParent(isRoot);
|
|
100
108
|
|
|
101
|
-
|
|
102
|
-
return false;
|
|
103
|
-
|
|
104
|
-
return true;
|
|
109
|
+
return !isBoundVars(rootPath, fnPath);
|
|
105
110
|
},
|
|
106
111
|
});
|
|
107
112
|
|
|
108
113
|
function isSameNames(paramPath, objectPath) {
|
|
109
114
|
const {name} = paramPath.node;
|
|
110
|
-
|
|
115
|
+
|
|
116
|
+
return objectPath.isIdentifier({
|
|
117
|
+
name,
|
|
118
|
+
});
|
|
111
119
|
}
|
|
112
120
|
|
|
113
121
|
function fixReturn(path) {
|
|
@@ -121,10 +129,7 @@ function fixReturn(path) {
|
|
|
121
129
|
if (!argument)
|
|
122
130
|
return replaceWith(path, ContinueStatement());
|
|
123
131
|
|
|
124
|
-
replaceWithMultiple(path, [
|
|
125
|
-
argument,
|
|
126
|
-
ContinueStatement(),
|
|
127
|
-
]);
|
|
132
|
+
replaceWithMultiple(path, [argument, ContinueStatement()]);
|
|
128
133
|
},
|
|
129
134
|
});
|
|
130
135
|
}
|
|
@@ -184,4 +189,3 @@ function isForBeforeFnUp(path) {
|
|
|
184
189
|
|
|
185
190
|
return true;
|
|
186
191
|
}
|
|
187
|
-
|
package/lib/for-entries/index.js
CHANGED
|
@@ -5,6 +5,7 @@ const {
|
|
|
5
5
|
types,
|
|
6
6
|
template,
|
|
7
7
|
} = require('putout');
|
|
8
|
+
|
|
8
9
|
const {
|
|
9
10
|
compare,
|
|
10
11
|
getTemplateValues,
|
|
@@ -28,7 +29,6 @@ module.exports.filter = (path) => {
|
|
|
28
29
|
|
|
29
30
|
const [first] = body.body;
|
|
30
31
|
const {__i, __e} = getTemplateValues(path, forLoop);
|
|
31
|
-
|
|
32
32
|
const {bindings} = path.scope;
|
|
33
33
|
const iBinding = bindings[__i.name];
|
|
34
34
|
|
|
@@ -58,4 +58,3 @@ module.exports.replace = () => ({
|
|
|
58
58
|
});
|
|
59
59
|
},
|
|
60
60
|
});
|
|
61
|
-
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
generate,
|
|
5
|
-
operator,
|
|
6
|
-
} = require('putout');
|
|
3
|
+
const {generate, operator} = require('putout');
|
|
7
4
|
|
|
8
5
|
const {
|
|
9
6
|
contains,
|
|
@@ -21,9 +18,7 @@ module.exports.match = () => ({
|
|
|
21
18
|
|
|
22
19
|
const {name} = declaration.__a;
|
|
23
20
|
|
|
24
|
-
return contains(__body, [
|
|
25
|
-
`if (!${__b.name}.hasOwnProperty(${name})) __c`,
|
|
26
|
-
]);
|
|
21
|
+
return contains(__body, [`if (!${__b.name}.hasOwnProperty(${name})) __c`]);
|
|
27
22
|
},
|
|
28
23
|
});
|
|
29
24
|
|
|
@@ -33,9 +28,9 @@ module.exports.replace = () => ({
|
|
|
33
28
|
const condition = getTemplateValues(first, 'if (!__b.hasOwnProperty(__a)) __c');
|
|
34
29
|
|
|
35
30
|
__body.body.shift();
|
|
31
|
+
|
|
36
32
|
const {code} = generate(__body);
|
|
37
33
|
|
|
38
34
|
return `for (const ${condition.__a.name} of Object.keys(${__b.name})) ${code}`;
|
|
39
35
|
},
|
|
40
36
|
});
|
|
41
|
-
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
generate,
|
|
5
|
-
operator,
|
|
6
|
-
} = require('putout');
|
|
3
|
+
const {generate, operator} = require('putout');
|
|
7
4
|
|
|
8
5
|
const {
|
|
9
6
|
contains,
|
|
@@ -21,9 +18,7 @@ module.exports.match = () => ({
|
|
|
21
18
|
|
|
22
19
|
const {name} = declaration.__a;
|
|
23
20
|
|
|
24
|
-
return contains(__body, [
|
|
25
|
-
`if (${__b.name}.hasOwnProperty(${name})) __body`,
|
|
26
|
-
]);
|
|
21
|
+
return contains(__body, [`if (${__b.name}.hasOwnProperty(${name})) __body`]);
|
|
27
22
|
},
|
|
28
23
|
});
|
|
29
24
|
|
|
@@ -36,4 +31,3 @@ module.exports.replace = () => ({
|
|
|
36
31
|
return `for (const ${condition.__a.name} of Object.keys(${__b.name})) ${code}`;
|
|
37
32
|
},
|
|
38
33
|
});
|
|
39
|
-
|
package/lib/for-length/index.js
CHANGED
package/lib/for-n/index.js
CHANGED
|
@@ -5,6 +5,7 @@ const {
|
|
|
5
5
|
template,
|
|
6
6
|
types,
|
|
7
7
|
} = require('putout');
|
|
8
|
+
|
|
8
9
|
const {
|
|
9
10
|
compare,
|
|
10
11
|
getTemplateValues,
|
|
@@ -42,10 +43,7 @@ module.exports.filter = (path) => {
|
|
|
42
43
|
|
|
43
44
|
const {references} = path.scope.bindings[__i.name];
|
|
44
45
|
|
|
45
|
-
|
|
46
|
-
return false;
|
|
47
|
-
|
|
48
|
-
return true;
|
|
46
|
+
return !(references > 3);
|
|
49
47
|
};
|
|
50
48
|
|
|
51
49
|
module.exports.replace = () => ({
|
|
@@ -72,4 +70,3 @@ function comparePrevSiblings(prev, node) {
|
|
|
72
70
|
return true;
|
|
73
71
|
}
|
|
74
72
|
}
|
|
75
|
-
|
package/lib/index.js
CHANGED
package/lib/map/index.js
CHANGED
|
@@ -24,10 +24,7 @@ module.exports.match = () => ({
|
|
|
24
24
|
if (isTryStatement(last))
|
|
25
25
|
return false;
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
return false;
|
|
29
|
-
|
|
30
|
-
return true;
|
|
27
|
+
return !isReturnStatement(last);
|
|
31
28
|
},
|
|
32
29
|
});
|
|
33
30
|
|
|
@@ -39,4 +36,3 @@ module.exports.replace = () => ({
|
|
|
39
36
|
return `for (const ${b} of ${a}) __c`;
|
|
40
37
|
},
|
|
41
38
|
});
|
|
42
|
-
|
package/lib/reduce/index.js
CHANGED
|
@@ -30,6 +30,7 @@ module.exports.match = () => ({
|
|
|
30
30
|
|
|
31
31
|
module.exports.replace = () => ({
|
|
32
32
|
'const __a = __b.reduce((__c, __d) => __e)': ({__a, __d}, path) => {
|
|
33
|
+
path.__putout_for_of_reduce = true;
|
|
33
34
|
rename(path, __d, __a);
|
|
34
35
|
|
|
35
36
|
return `{
|
|
@@ -40,6 +41,7 @@ module.exports.replace = () => ({
|
|
|
40
41
|
}`;
|
|
41
42
|
},
|
|
42
43
|
'const __a = __b.reduceRight((__c, __d) => __e)': ({__a, __d}, path) => {
|
|
44
|
+
path.__putout_for_of_reduce = true;
|
|
43
45
|
rename(path, __d, __a);
|
|
44
46
|
|
|
45
47
|
return `{
|
|
@@ -49,8 +51,8 @@ module.exports.replace = () => ({
|
|
|
49
51
|
}
|
|
50
52
|
}`;
|
|
51
53
|
},
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
'const __a = __b.reduce((__c, __d) => __e, __f)': (vars, path) => {
|
|
55
|
+
path.__putout_for_of_reduce = true;
|
|
54
56
|
return `{
|
|
55
57
|
let __a = __f;
|
|
56
58
|
for (const __d of __b) {
|
|
@@ -62,7 +64,8 @@ module.exports.replace = () => ({
|
|
|
62
64
|
|
|
63
65
|
function rename(path, from, to) {
|
|
64
66
|
const fnScope = path.get('declarations.0.init.arguments.0').scope;
|
|
67
|
+
|
|
65
68
|
fnScope.rename(from.name, to.name);
|
|
69
|
+
|
|
66
70
|
delete path.scope.bindings[from.name];
|
|
67
71
|
}
|
|
68
|
-
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
types,
|
|
5
|
-
operator,
|
|
6
|
-
} = require('putout');
|
|
3
|
+
const {types, operator} = require('putout');
|
|
7
4
|
|
|
8
5
|
const {
|
|
9
6
|
isIdentifier,
|
|
@@ -34,10 +31,7 @@ function match(path) {
|
|
|
34
31
|
const idPath = path.get('left.declarations.0.id');
|
|
35
32
|
const elements = getElements(idPath);
|
|
36
33
|
|
|
37
|
-
|
|
38
|
-
return false;
|
|
39
|
-
|
|
40
|
-
return true;
|
|
34
|
+
return !isAllReferenced(idPath, elements);
|
|
41
35
|
}
|
|
42
36
|
|
|
43
37
|
function replace(path) {
|
|
@@ -106,15 +100,14 @@ function isAllReferenced(path, nodes) {
|
|
|
106
100
|
}
|
|
107
101
|
|
|
108
102
|
function getElements({node}) {
|
|
109
|
-
const {
|
|
110
|
-
properties,
|
|
111
|
-
elements,
|
|
112
|
-
} = node;
|
|
103
|
+
const {properties, elements} = node;
|
|
113
104
|
|
|
114
105
|
if (elements)
|
|
115
106
|
return elements.filter(Boolean);
|
|
116
107
|
|
|
117
|
-
return properties
|
|
108
|
+
return properties
|
|
109
|
+
.map(getValue)
|
|
110
|
+
.filter(Boolean);
|
|
118
111
|
}
|
|
119
112
|
|
|
120
113
|
function getPathElements(path) {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {types, operator} = require('putout');
|
|
4
|
+
|
|
4
5
|
const {isIdentifier} = types;
|
|
5
6
|
const {replaceWith} = operator;
|
|
6
7
|
|
|
@@ -13,7 +14,6 @@ module.exports.match = () => ({
|
|
|
13
14
|
|
|
14
15
|
return !references;
|
|
15
16
|
},
|
|
16
|
-
|
|
17
17
|
'for (const __a of __array) __c': ({__a, __array}, path) => {
|
|
18
18
|
if (__array.elements.length >= 2)
|
|
19
19
|
return false;
|
|
@@ -24,10 +24,7 @@ module.exports.match = () => ({
|
|
|
24
24
|
const {name} = __a;
|
|
25
25
|
const {references} = path.scope.bindings[name];
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
return false;
|
|
29
|
-
|
|
30
|
-
return true;
|
|
27
|
+
return !(references >= 2);
|
|
31
28
|
},
|
|
32
29
|
});
|
|
33
30
|
|
|
@@ -40,10 +37,8 @@ module.exports.replace = () => ({
|
|
|
40
37
|
return null;
|
|
41
38
|
|
|
42
39
|
const {name} = __a;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
referencePaths,
|
|
46
|
-
} = path.scope.bindings[name];
|
|
40
|
+
|
|
41
|
+
const {references, referencePaths} = path.scope.bindings[name];
|
|
47
42
|
|
|
48
43
|
if (!references)
|
|
49
44
|
return replaceWith(path, __c);
|
|
@@ -57,4 +52,3 @@ module.exports.replace = () => ({
|
|
|
57
52
|
return path;
|
|
58
53
|
},
|
|
59
54
|
});
|
|
60
|
-
|
|
@@ -1,16 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
types,
|
|
5
|
-
operator,
|
|
6
|
-
} = require('putout');
|
|
7
|
-
const {
|
|
8
|
-
replaceWith,
|
|
9
|
-
remove,
|
|
10
|
-
} = operator;
|
|
3
|
+
const {types, operator} = require('putout');
|
|
11
4
|
|
|
12
|
-
const {
|
|
5
|
+
const {replaceWith, remove} = operator;
|
|
13
6
|
|
|
7
|
+
const {isAssignmentPattern} = types;
|
|
14
8
|
const isToManyProperties = (a, {maxProperties}) => a.isObjectPattern() && a.node.properties.length > maxProperties;
|
|
15
9
|
const isAssignment = (a) => isAssignmentPattern(a.value);
|
|
16
10
|
|
|
@@ -30,21 +24,22 @@ module.exports.traverse = ({push, options}) => ({
|
|
|
30
24
|
return;
|
|
31
25
|
|
|
32
26
|
const {scope, node} = varPath;
|
|
27
|
+
|
|
33
28
|
const {name} = node;
|
|
34
29
|
|
|
35
|
-
const {
|
|
36
|
-
references,
|
|
37
|
-
referencePaths,
|
|
38
|
-
} = scope.bindings[name];
|
|
30
|
+
const {references, referencePaths} = scope.bindings[name];
|
|
39
31
|
|
|
40
32
|
if (references !== 1)
|
|
41
33
|
return;
|
|
42
34
|
|
|
43
35
|
const [referencePath] = referencePaths;
|
|
44
36
|
const {parentPath} = referencePath;
|
|
37
|
+
|
|
45
38
|
const isSameName = parentPath
|
|
46
39
|
.get('init')
|
|
47
|
-
.isIdentifier({
|
|
40
|
+
.isIdentifier({
|
|
41
|
+
name,
|
|
42
|
+
});
|
|
48
43
|
|
|
49
44
|
if (!isSameName)
|
|
50
45
|
return;
|
|
@@ -66,4 +61,3 @@ module.exports.traverse = ({push, options}) => ({
|
|
|
66
61
|
});
|
|
67
62
|
},
|
|
68
63
|
});
|
|
69
|
-
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-for-of",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.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",
|
|
@@ -36,20 +36,22 @@
|
|
|
36
36
|
"@putout/plugin-convert-const-to-let": "*",
|
|
37
37
|
"@putout/plugin-remove-empty": "*",
|
|
38
38
|
"@putout/plugin-remove-nested-blocks": "*",
|
|
39
|
+
"@putout/plugin-remove-unreferenced-variables": "*",
|
|
39
40
|
"@putout/plugin-remove-unused-variables": "*",
|
|
40
41
|
"@putout/plugin-remove-useless-arguments": "*",
|
|
41
42
|
"@putout/plugin-remove-useless-continue": "*",
|
|
42
|
-
"@putout/test": "^
|
|
43
|
-
"c8": "^
|
|
43
|
+
"@putout/test": "^7.0.0",
|
|
44
|
+
"c8": "^8.0.0",
|
|
44
45
|
"eslint": "^8.0.1",
|
|
45
|
-
"eslint-plugin-n": "^
|
|
46
|
-
"eslint-plugin-putout": "^
|
|
46
|
+
"eslint-plugin-n": "^16.0.0",
|
|
47
|
+
"eslint-plugin-putout": "^18.0.0",
|
|
47
48
|
"lerna": "^6.0.1",
|
|
48
49
|
"madrun": "^9.0.0",
|
|
50
|
+
"montag": "^1.2.1",
|
|
49
51
|
"nodemon": "^2.0.1"
|
|
50
52
|
},
|
|
51
53
|
"peerDependencies": {
|
|
52
|
-
"putout": ">=
|
|
54
|
+
"putout": ">=30"
|
|
53
55
|
},
|
|
54
56
|
"license": "MIT",
|
|
55
57
|
"engines": {
|