@putout/plugin-for-of 12.3.0 β†’ 12.4.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 CHANGED
@@ -87,6 +87,8 @@ for (const name of names) {
87
87
  >
88
88
  > (c) MDN
89
89
 
90
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/227db63f003df008d27455d01fcaec9d/6f1be22c96baa54b9abe34a93ccdcf09a343ff19).
91
+
90
92
  ### ❌ Example of incorrect code
91
93
 
92
94
  ```js
package/lib/map/index.js CHANGED
@@ -1,11 +1,20 @@
1
- import {types} from 'putout';
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,
7
14
  } = types;
8
15
 
16
+ const createPush = template('%%ARRAY%%.push(%%ARGUMENT%%)');
17
+
9
18
  const tail = (body) => body.at(-1);
10
19
 
11
20
  export const report = () => `Use 'for...of' instead of map when 'return' absent`;
@@ -25,6 +34,12 @@ export const match = () => ({
25
34
 
26
35
  return !isReturnStatement(last);
27
36
  },
37
+ 'const __a = __b.map((__c) => __d)': ({__a, __c, __d}) => {
38
+ if (compare(__c, __d))
39
+ return false;
40
+
41
+ return !isArrayPattern(__a);
42
+ },
28
43
  });
29
44
 
30
45
  export const replace = () => ({
@@ -34,4 +49,35 @@ export const replace = () => ({
34
49
 
35
50
  return `for (const ${b} of ${a}) __c`;
36
51
  },
52
+ 'const __a = __b.map((__c) => __d)': ({__a, __d}, path) => {
53
+ if (isBlockStatement(__d)) {
54
+ path.traverse({
55
+ ReturnStatement(path) {
56
+ const {argument} = path.node;
57
+
58
+ replaceWith(path, createPush({
59
+ ARRAY: __a,
60
+ ARGUMENT: argument,
61
+ }));
62
+ },
63
+ });
64
+ return `{
65
+ const __a = [];
66
+ for (const __c of __b) __d;
67
+ }`;
68
+ }
69
+
70
+ return `{
71
+ const __a = [];
72
+ for (const __c of __b) {
73
+ __a.push(__d);
74
+ }
75
+ }`;
76
+ },
77
+ '__a = __b.map((__c) => __d)': `{
78
+ __a = [];
79
+ for (const __c of __b) {
80
+ __a.push(__d);
81
+ }
82
+ }`,
37
83
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-for-of",
3
- "version": "12.3.0",
3
+ "version": "12.4.0",
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",