@putout/plugin-for-of 12.6.4 → 12.7.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.
@@ -5,13 +5,14 @@ import {
5
5
  } from 'putout';
6
6
 
7
7
  const {
8
+ isFunction,
8
9
  isIdentifier,
9
10
  continueStatement,
10
- isReturnStatement,
11
11
  } = types;
12
12
 
13
13
  const {
14
14
  compare,
15
+ createTypeChecker,
15
16
  getTemplateValues,
16
17
  replaceWith,
17
18
  replaceWithMultiple,
@@ -29,7 +30,19 @@ const forOfEntriesTemplate = template(`
29
30
  `);
30
31
 
31
32
  const {keys} = Object;
32
- const isRoot = (path) => path.isFunction() || path.isProgram();
33
+
34
+ const isRoot = createTypeChecker([
35
+ ['+', isFunction],
36
+ ['+: -> Program'],
37
+ ]);
38
+
39
+ const isSupportedParent = createTypeChecker([
40
+ '-: parentPath -> ReturnStatement',
41
+ '-: parentPath -> SequenceExpression',
42
+ '-: parentPath -> ConditionalExpression',
43
+ '-: parentPath -> VariableDeclarator',
44
+ '+: -> +',
45
+ ]);
33
46
 
34
47
  export const report = () => `Use 'for-of' instead of 'forEach()'`;
35
48
 
@@ -93,18 +106,7 @@ export const fix = (path) => {
93
106
  export const traverse = ({push}) => ({
94
107
  '__.forEach.call(__a, (__b) => __body)': push,
95
108
  '__.forEach(__args)': (path) => {
96
- const {parentPath} = path;
97
-
98
- if (isReturnStatement(parentPath))
99
- return;
100
-
101
- if (parentPath.isSequenceExpression())
102
- return;
103
-
104
- if (parentPath.isConditionalExpression())
105
- return;
106
-
107
- if (parentPath.isVariableDeclarator())
109
+ if (!isSupportedParent(path))
108
110
  return;
109
111
 
110
112
  const objectPath = path.get('callee.object');
package/lib/map/index.js CHANGED
@@ -4,7 +4,11 @@ import {
4
4
  template,
5
5
  } from 'putout';
6
6
 
7
- const {compare, replaceWith} = operator;
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
- return `{
76
- const __a = [];
77
- for (const __c of __b) {
78
- __a.push(__d);
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-for-of",
3
- "version": "12.6.4",
3
+ "version": "12.7.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",