@putout/plugin-for-of 12.5.2 → 12.6.1
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 +9 -5
- package/lib/for-each/index.js +86 -62
- package/lib/reduce/index.js +3 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -40,9 +40,14 @@ npm i @putout/plugin-for-of
|
|
|
40
40
|
{
|
|
41
41
|
"rules": {
|
|
42
42
|
"for-of/add-missing-declaration": "on",
|
|
43
|
-
|
|
44
|
-
"for-of/for
|
|
43
|
+
|
|
44
|
+
"for-of/for": "on",
|
|
45
45
|
"for-of/for-each": "on",
|
|
46
|
+
"for-of/for-in": "on",
|
|
47
|
+
|
|
48
|
+
"for-of/from-while": "on",
|
|
49
|
+
"for-of/map": "on",
|
|
50
|
+
|
|
46
51
|
"for-of/reduce": "on",
|
|
47
52
|
"for-of/remove-unused-variables": "on",
|
|
48
53
|
"for-of/remove-useless": "on",
|
|
@@ -50,9 +55,8 @@ npm i @putout/plugin-for-of
|
|
|
50
55
|
"for-of/remove-useless-variables": ["on", {
|
|
51
56
|
"maxProperties": 4
|
|
52
57
|
}],
|
|
53
|
-
|
|
54
|
-
"for-of/to-for-n": "on"
|
|
55
|
-
"for-of/from-while": "on"
|
|
58
|
+
|
|
59
|
+
"for-of/to-for-n": "on"
|
|
56
60
|
}
|
|
57
61
|
}
|
|
58
62
|
```
|
package/lib/for-each/index.js
CHANGED
|
@@ -11,6 +11,8 @@ const {
|
|
|
11
11
|
} = types;
|
|
12
12
|
|
|
13
13
|
const {
|
|
14
|
+
compare,
|
|
15
|
+
getTemplateValues,
|
|
14
16
|
replaceWith,
|
|
15
17
|
replaceWithMultiple,
|
|
16
18
|
superTraverse,
|
|
@@ -31,97 +33,119 @@ const isRoot = (path) => path.isFunction() || path.isProgram();
|
|
|
31
33
|
|
|
32
34
|
export const report = () => `Use 'for-of' instead of 'forEach()'`;
|
|
33
35
|
|
|
34
|
-
export const
|
|
35
|
-
'__.forEach.call(__a, (__b) => __body)'
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
36
|
+
export const fix = (path) => {
|
|
37
|
+
if (compare(path, '__.forEach.call(__a, (__b) => __body)')) {
|
|
38
|
+
const {
|
|
39
|
+
__a,
|
|
40
|
+
__b,
|
|
41
|
+
__body,
|
|
42
|
+
} = getTemplateValues(path, '__.forEach.call(__a, (__b) => __body)');
|
|
43
|
+
|
|
44
|
+
replaceWith(path, forOfTemplate({
|
|
45
|
+
item: __b,
|
|
46
|
+
items: __a,
|
|
47
|
+
body: __body,
|
|
48
|
+
}));
|
|
49
|
+
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const {params, body} = path.node.arguments[0];
|
|
54
|
+
const item = getItem(params);
|
|
55
|
+
|
|
56
|
+
delete item.typeAnnotation;
|
|
57
|
+
|
|
58
|
+
const {length} = params;
|
|
59
|
+
|
|
60
|
+
for (const param of params) {
|
|
61
|
+
delete param.typeAnnotation;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const thisPassed = isIdentifier(params[0], {
|
|
65
|
+
name: 'this',
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const items = path.node.callee.object;
|
|
69
|
+
|
|
70
|
+
if (length === 1 || length === 2 && thisPassed) {
|
|
71
|
+
const newPath = replaceWith(path, forOfTemplate({
|
|
72
|
+
item,
|
|
73
|
+
items,
|
|
74
|
+
body,
|
|
75
|
+
}));
|
|
76
|
+
|
|
77
|
+
fixReturn(newPath);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const [, index] = params;
|
|
82
|
+
|
|
83
|
+
const newPath = replaceWith(path, forOfEntriesTemplate({
|
|
84
|
+
index,
|
|
85
|
+
item,
|
|
86
|
+
items,
|
|
87
|
+
body,
|
|
88
|
+
}));
|
|
89
|
+
|
|
90
|
+
fixReturn(newPath);
|
|
91
|
+
};
|
|
80
92
|
|
|
81
|
-
export const
|
|
82
|
-
'__.forEach(
|
|
93
|
+
export const traverse = ({push}) => ({
|
|
94
|
+
'__.forEach.call(__a, (__b) => __body)': push,
|
|
95
|
+
'__.forEach(__args)': (path) => {
|
|
83
96
|
const {parentPath} = path;
|
|
84
97
|
|
|
85
98
|
if (isReturnStatement(parentPath))
|
|
86
|
-
return
|
|
99
|
+
return;
|
|
87
100
|
|
|
88
101
|
if (parentPath.isSequenceExpression())
|
|
89
|
-
return
|
|
102
|
+
return;
|
|
90
103
|
|
|
91
104
|
if (parentPath.isConditionalExpression())
|
|
92
|
-
return
|
|
105
|
+
return;
|
|
93
106
|
|
|
94
107
|
if (parentPath.isVariableDeclarator())
|
|
95
|
-
return
|
|
108
|
+
return;
|
|
96
109
|
|
|
97
110
|
const objectPath = path.get('callee.object');
|
|
98
111
|
const fnPath = path.get('arguments.0');
|
|
99
112
|
|
|
100
113
|
if (!fnPath.isFunction())
|
|
101
|
-
return
|
|
114
|
+
return;
|
|
102
115
|
|
|
103
116
|
const params = fnPath.get('params');
|
|
104
117
|
|
|
105
118
|
if (!params.length)
|
|
106
|
-
return
|
|
119
|
+
return;
|
|
107
120
|
|
|
108
121
|
if (isParentContainsFunctionArgument(objectPath))
|
|
109
|
-
return
|
|
122
|
+
return;
|
|
110
123
|
|
|
111
|
-
|
|
112
|
-
|
|
124
|
+
const {length} = path.node.arguments;
|
|
125
|
+
|
|
126
|
+
if (length === 2 && !path.get('arguments.1').isThisExpression())
|
|
127
|
+
return;
|
|
113
128
|
|
|
114
129
|
if (isIndexWithThis(params))
|
|
115
|
-
return
|
|
130
|
+
return;
|
|
116
131
|
|
|
117
132
|
const [paramPath] = params;
|
|
118
133
|
|
|
119
134
|
if (isSameNames(paramPath, objectPath))
|
|
120
|
-
return
|
|
135
|
+
return;
|
|
121
136
|
|
|
122
137
|
const rootPath = path.findParent(isRoot);
|
|
123
138
|
|
|
124
|
-
|
|
139
|
+
if (isBoundVars(rootPath, fnPath))
|
|
140
|
+
return;
|
|
141
|
+
|
|
142
|
+
if (length === 1) {
|
|
143
|
+
push(path);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (params.length === 2)
|
|
148
|
+
push(path);
|
|
125
149
|
},
|
|
126
150
|
});
|
|
127
151
|
|
|
@@ -153,7 +177,7 @@ function isBoundVars(parentPath, path) {
|
|
|
153
177
|
const currentBindings = keys(parentPath.scope.bindings);
|
|
154
178
|
const fnBindings = keys(path.scope.bindings);
|
|
155
179
|
|
|
156
|
-
return
|
|
180
|
+
return compareBindings(currentBindings, fnBindings);
|
|
157
181
|
}
|
|
158
182
|
|
|
159
183
|
function isParentContainsFunctionArgument(objectPath) {
|
|
@@ -175,7 +199,7 @@ function getItem(params) {
|
|
|
175
199
|
return thisItem;
|
|
176
200
|
}
|
|
177
201
|
|
|
178
|
-
function
|
|
202
|
+
function compareBindings(a, b) {
|
|
179
203
|
for (const el of a) {
|
|
180
204
|
if (b.includes(el))
|
|
181
205
|
return true;
|
package/lib/reduce/index.js
CHANGED
|
@@ -62,8 +62,10 @@ export const replace = () => ({
|
|
|
62
62
|
}
|
|
63
63
|
}`;
|
|
64
64
|
},
|
|
65
|
-
'const __a = __b.reduce((__c, __d) => __e, __f)': (
|
|
65
|
+
'const __a = __b.reduce((__c, __d) => __e, __f)': ({__a, __c}, path) => {
|
|
66
66
|
path.__putout_for_of_reduce = true;
|
|
67
|
+
rename(path, __c, __a);
|
|
68
|
+
|
|
67
69
|
return `{
|
|
68
70
|
let __a = __f;
|
|
69
71
|
for (const __d of __b) {
|
package/package.json
CHANGED