@putout/plugin-for-of 12.6.3 → 12.6.4

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 +36 -10
  2. package/package.json +1 -1
package/lib/map/index.js CHANGED
@@ -12,6 +12,10 @@ const {
12
12
  isTryStatement,
13
13
  isArrayPattern,
14
14
  isCallExpression,
15
+ blockStatement,
16
+ continueStatement,
17
+ expressionStatement,
18
+ identifier,
15
19
  } = types;
16
20
 
17
21
  const createPush = template('%%ARRAY%%.push(%%ARGUMENT%%)');
@@ -61,9 +65,7 @@ export const replace = () => ({
61
65
  },
62
66
  'const __a = __b.map((__c) => __d)': ({__a, __d}, path) => {
63
67
  if (isBlockStatement(__d)) {
64
- path.traverse({
65
- ReturnStatement: createReplaceWithPush(__a),
66
- });
68
+ path.traverse(createVisitor(__a, __d));
67
69
  return `{
68
70
  const __a = [];
69
71
  for (const __c of __b) __d;
@@ -79,9 +81,7 @@ export const replace = () => ({
79
81
  },
80
82
  '__a = __b.map((__c) => __d)': ({__a, __d}, path) => {
81
83
  if (isBlockStatement(__d)) {
82
- path.traverse({
83
- ReturnStatement: createReplaceWithPush(__a),
84
- });
84
+ path.traverse(createVisitor(__a, __d));
85
85
  return `{
86
86
  __a = [];
87
87
  for (const __c of __b) __d;
@@ -97,11 +97,37 @@ export const replace = () => ({
97
97
  },
98
98
  });
99
99
 
100
- const createReplaceWithPush = (__a) => (path) => {
101
- const {argument} = path.node;
100
+ const isTail = (path, body) => {
101
+ if (path.parentPath.node !== body)
102
+ return false;
102
103
 
103
- replaceWith(path, createPush({
104
+ return tail(body.body) === path.node;
105
+ };
106
+
107
+ const skipNested = (body) => (path) => {
108
+ if (path.node.body !== body)
109
+ path.skip();
110
+ };
111
+
112
+ const createVisitor = (__a, body) => ({
113
+ Function: skipNested(body),
114
+ ReturnStatement: createReplaceWithPush(__a, body),
115
+ });
116
+
117
+ const createReplaceWithPush = (__a, body) => (path) => {
118
+ const argument = path.node.argument || identifier('undefined');
119
+ const push = createPush({
104
120
  ARRAY: __a,
105
121
  ARGUMENT: argument,
106
- }));
122
+ });
123
+
124
+ if (isTail(path, body)) {
125
+ replaceWith(path, push);
126
+ return;
127
+ }
128
+
129
+ replaceWith(path, blockStatement([
130
+ expressionStatement(push),
131
+ continueStatement(),
132
+ ]));
107
133
  };
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.4",
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",