@putout/plugin-for-of 12.6.4 → 12.6.5
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/lib/map/index.js +29 -14
- package/package.json +1 -1
package/lib/map/index.js
CHANGED
|
@@ -4,7 +4,11 @@ import {
|
|
|
4
4
|
template,
|
|
5
5
|
} from 'putout';
|
|
6
6
|
|
|
7
|
-
const {
|
|
7
|
+
const {
|
|
8
|
+
compare,
|
|
9
|
+
replaceWith,
|
|
10
|
+
insertAfter,
|
|
11
|
+
} = operator;
|
|
8
12
|
|
|
9
13
|
const {
|
|
10
14
|
isReturnStatement,
|
|
@@ -19,6 +23,21 @@ const {
|
|
|
19
23
|
} = types;
|
|
20
24
|
|
|
21
25
|
const createPush = template('%%ARRAY%%.push(%%ARGUMENT%%)');
|
|
26
|
+
const createLoop = template('for (const %%ITEM%% of %%LIST%%) %%BODY%%');
|
|
27
|
+
|
|
28
|
+
const createBody = (array, body) => {
|
|
29
|
+
if (isBlockStatement(body))
|
|
30
|
+
return body;
|
|
31
|
+
|
|
32
|
+
const push = createPush({
|
|
33
|
+
ARRAY: array,
|
|
34
|
+
ARGUMENT: body,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return blockStatement([
|
|
38
|
+
expressionStatement(push),
|
|
39
|
+
]);
|
|
40
|
+
};
|
|
22
41
|
|
|
23
42
|
const tail = (body) => body.at(-1);
|
|
24
43
|
|
|
@@ -63,21 +82,17 @@ export const replace = () => ({
|
|
|
63
82
|
|
|
64
83
|
return `for (const ${b} of ${a}) __c`;
|
|
65
84
|
},
|
|
66
|
-
'const __a = __b.map((__c) => __d)': ({__a, __d}, path) => {
|
|
67
|
-
if (isBlockStatement(__d))
|
|
85
|
+
'const __a = __b.map((__c) => __d)': ({__a, __b, __c, __d}, path) => {
|
|
86
|
+
if (isBlockStatement(__d))
|
|
68
87
|
path.traverse(createVisitor(__a, __d));
|
|
69
|
-
return `{
|
|
70
|
-
const __a = [];
|
|
71
|
-
for (const __c of __b) __d;
|
|
72
|
-
}`;
|
|
73
|
-
}
|
|
74
88
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
89
|
+
insertAfter(path, createLoop({
|
|
90
|
+
ITEM: __c,
|
|
91
|
+
LIST: __b,
|
|
92
|
+
BODY: createBody(__a, __d),
|
|
93
|
+
}));
|
|
94
|
+
|
|
95
|
+
return 'const __a = []';
|
|
81
96
|
},
|
|
82
97
|
'__a = __b.map((__c) => __d)': ({__a, __d}, path) => {
|
|
83
98
|
if (isBlockStatement(__d)) {
|
package/package.json
CHANGED