@putout/plugin-for-of 12.3.0 β 12.4.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 +2 -0
- package/lib/map/index.js +52 -2
- package/package.json +2 -1
package/README.md
CHANGED
package/lib/map/index.js
CHANGED
|
@@ -1,14 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
types,
|
|
3
|
+
operator,
|
|
4
|
+
template,
|
|
5
|
+
} from 'putout';
|
|
6
|
+
|
|
7
|
+
const {compare, replaceWith} = operator;
|
|
2
8
|
|
|
3
9
|
const {
|
|
4
10
|
isReturnStatement,
|
|
5
11
|
isBlockStatement,
|
|
6
12
|
isTryStatement,
|
|
13
|
+
isArrayPattern,
|
|
14
|
+
isCallExpression,
|
|
7
15
|
} = types;
|
|
8
16
|
|
|
17
|
+
const createPush = template('%%ARRAY%%.push(%%ARGUMENT%%)');
|
|
18
|
+
|
|
9
19
|
const tail = (body) => body.at(-1);
|
|
10
20
|
|
|
11
|
-
export const report = () => `Use 'for
|
|
21
|
+
export const report = () => `Use 'for..of' instead of 'map'`;
|
|
12
22
|
|
|
13
23
|
export const match = () => ({
|
|
14
24
|
'__a.map((__b) => __c)': ({__c}, path) => {
|
|
@@ -25,6 +35,15 @@ export const match = () => ({
|
|
|
25
35
|
|
|
26
36
|
return !isReturnStatement(last);
|
|
27
37
|
},
|
|
38
|
+
'const __a = __b.map((__c) => __d)': ({__a, __b, __c, __d}) => {
|
|
39
|
+
if (isCallExpression(__b))
|
|
40
|
+
return false;
|
|
41
|
+
|
|
42
|
+
if (compare(__c, __d))
|
|
43
|
+
return false;
|
|
44
|
+
|
|
45
|
+
return !isArrayPattern(__a);
|
|
46
|
+
},
|
|
28
47
|
});
|
|
29
48
|
|
|
30
49
|
export const replace = () => ({
|
|
@@ -34,4 +53,35 @@ export const replace = () => ({
|
|
|
34
53
|
|
|
35
54
|
return `for (const ${b} of ${a}) __c`;
|
|
36
55
|
},
|
|
56
|
+
'const __a = __b.map((__c) => __d)': ({__a, __d}, path) => {
|
|
57
|
+
if (isBlockStatement(__d)) {
|
|
58
|
+
path.traverse({
|
|
59
|
+
ReturnStatement(path) {
|
|
60
|
+
const {argument} = path.node;
|
|
61
|
+
|
|
62
|
+
replaceWith(path, createPush({
|
|
63
|
+
ARRAY: __a,
|
|
64
|
+
ARGUMENT: argument,
|
|
65
|
+
}));
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
return `{
|
|
69
|
+
const __a = [];
|
|
70
|
+
for (const __c of __b) __d;
|
|
71
|
+
}`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return `{
|
|
75
|
+
const __a = [];
|
|
76
|
+
for (const __c of __b) {
|
|
77
|
+
__a.push(__d);
|
|
78
|
+
}
|
|
79
|
+
}`;
|
|
80
|
+
},
|
|
81
|
+
'__a = __b.map((__c) => __d)': `{
|
|
82
|
+
__a = [];
|
|
83
|
+
for (const __c of __b) {
|
|
84
|
+
__a.push(__d);
|
|
85
|
+
}
|
|
86
|
+
}`,
|
|
37
87
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-for-of",
|
|
3
|
-
"version": "12.
|
|
3
|
+
"version": "12.4.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "πPutout plugin adds ability to apply for...of",
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"@putout/plugin-remove-empty": "*",
|
|
39
39
|
"@putout/plugin-remove-nested-blocks": "*",
|
|
40
40
|
"@putout/plugin-remove-useless-continue": "*",
|
|
41
|
+
"@putout/plugin-remove-useless-map": "*",
|
|
41
42
|
"@putout/plugin-variables": "*",
|
|
42
43
|
"@putout/test": "^15.0.0",
|
|
43
44
|
"eslint": "^10.0.0",
|