@putout/plugin-for-of 12.6.3 → 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.
Files changed (2) hide show
  1. package/lib/map/index.js +65 -24
  2. 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 {compare, replaceWith} = operator;
7
+ const {
8
+ compare,
9
+ replaceWith,
10
+ insertAfter,
11
+ } = operator;
8
12
 
9
13
  const {
10
14
  isReturnStatement,
@@ -12,9 +16,28 @@ const {
12
16
  isTryStatement,
13
17
  isArrayPattern,
14
18
  isCallExpression,
19
+ blockStatement,
20
+ continueStatement,
21
+ expressionStatement,
22
+ identifier,
15
23
  } = types;
16
24
 
17
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
+ };
18
41
 
19
42
  const tail = (body) => body.at(-1);
20
43
 
@@ -59,29 +82,21 @@ export const replace = () => ({
59
82
 
60
83
  return `for (const ${b} of ${a}) __c`;
61
84
  },
62
- 'const __a = __b.map((__c) => __d)': ({__a, __d}, path) => {
63
- if (isBlockStatement(__d)) {
64
- path.traverse({
65
- ReturnStatement: createReplaceWithPush(__a),
66
- });
67
- return `{
68
- const __a = [];
69
- for (const __c of __b) __d;
70
- }`;
71
- }
85
+ 'const __a = __b.map((__c) => __d)': ({__a, __b, __c, __d}, path) => {
86
+ if (isBlockStatement(__d))
87
+ path.traverse(createVisitor(__a, __d));
72
88
 
73
- return `{
74
- const __a = [];
75
- for (const __c of __b) {
76
- __a.push(__d);
77
- }
78
- }`;
89
+ insertAfter(path, createLoop({
90
+ ITEM: __c,
91
+ LIST: __b,
92
+ BODY: createBody(__a, __d),
93
+ }));
94
+
95
+ return 'const __a = []';
79
96
  },
80
97
  '__a = __b.map((__c) => __d)': ({__a, __d}, path) => {
81
98
  if (isBlockStatement(__d)) {
82
- path.traverse({
83
- ReturnStatement: createReplaceWithPush(__a),
84
- });
99
+ path.traverse(createVisitor(__a, __d));
85
100
  return `{
86
101
  __a = [];
87
102
  for (const __c of __b) __d;
@@ -97,11 +112,37 @@ export const replace = () => ({
97
112
  },
98
113
  });
99
114
 
100
- const createReplaceWithPush = (__a) => (path) => {
101
- const {argument} = path.node;
115
+ const isTail = (path, body) => {
116
+ if (path.parentPath.node !== body)
117
+ return false;
102
118
 
103
- replaceWith(path, createPush({
119
+ return tail(body.body) === path.node;
120
+ };
121
+
122
+ const skipNested = (body) => (path) => {
123
+ if (path.node.body !== body)
124
+ path.skip();
125
+ };
126
+
127
+ const createVisitor = (__a, body) => ({
128
+ Function: skipNested(body),
129
+ ReturnStatement: createReplaceWithPush(__a, body),
130
+ });
131
+
132
+ const createReplaceWithPush = (__a, body) => (path) => {
133
+ const argument = path.node.argument || identifier('undefined');
134
+ const push = createPush({
104
135
  ARRAY: __a,
105
136
  ARGUMENT: argument,
106
- }));
137
+ });
138
+
139
+ if (isTail(path, body)) {
140
+ replaceWith(path, push);
141
+ return;
142
+ }
143
+
144
+ replaceWith(path, blockStatement([
145
+ expressionStatement(push),
146
+ continueStatement(),
147
+ ]));
107
148
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-for-of",
3
- "version": "12.6.3",
3
+ "version": "12.6.5",
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",