@putout/plugin-for-of 12.5.2 → 12.6.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 CHANGED
@@ -40,9 +40,14 @@ npm i @putout/plugin-for-of
40
40
  {
41
41
  "rules": {
42
42
  "for-of/add-missing-declaration": "on",
43
- "for-of/map": "on",
44
- "for-of/for-in": "on",
43
+
44
+ "for-of/for": "on",
45
45
  "for-of/for-each": "on",
46
+ "for-of/for-in": "on",
47
+
48
+ "for-of/from-while": "on",
49
+ "for-of/map": "on",
50
+
46
51
  "for-of/reduce": "on",
47
52
  "for-of/remove-unused-variables": "on",
48
53
  "for-of/remove-useless": "on",
@@ -50,9 +55,8 @@ npm i @putout/plugin-for-of
50
55
  "for-of/remove-useless-variables": ["on", {
51
56
  "maxProperties": 4
52
57
  }],
53
- "for-of/for": "on",
54
- "for-of/to-for-n": "on",
55
- "for-of/from-while": "on"
58
+
59
+ "for-of/to-for-n": "on"
56
60
  }
57
61
  }
58
62
  ```
@@ -11,6 +11,8 @@ const {
11
11
  } = types;
12
12
 
13
13
  const {
14
+ compare,
15
+ getTemplateValues,
14
16
  replaceWith,
15
17
  replaceWithMultiple,
16
18
  superTraverse,
@@ -31,97 +33,119 @@ const isRoot = (path) => path.isFunction() || path.isProgram();
31
33
 
32
34
  export const report = () => `Use 'for-of' instead of 'forEach()'`;
33
35
 
34
- export const replace = () => ({
35
- '__.forEach.call(__a, (__b) => __body)': 'for (const __b of __a) __body',
36
- '__.forEach(__args)': (vars, path) => {
37
- const {params, body} = path.node.arguments[0];
38
- const item = getItem(params);
39
-
40
- delete item.typeAnnotation;
41
-
42
- const {length} = params;
43
-
44
- for (const param of params) {
45
- delete param.typeAnnotation;
46
- }
47
-
48
- const thisPassed = isIdentifier(params[0], {
49
- name: 'this',
50
- });
51
-
52
- const items = path.node.callee.object;
53
-
54
- if (length === 1 || length === 2 && thisPassed) {
55
- const newPath = replaceWith(path, forOfTemplate({
56
- item,
57
- items,
58
- body,
59
- }));
60
-
61
- fixReturn(newPath);
62
-
63
- return;
64
- }
65
-
66
- if (params.length === 2) {
67
- const [, index] = params;
68
-
69
- const newPath = replaceWith(path, forOfEntriesTemplate({
70
- index,
71
- item,
72
- items,
73
- body,
74
- }));
75
-
76
- fixReturn(newPath);
77
- }
78
- },
79
- });
36
+ export const fix = (path) => {
37
+ if (compare(path, '__.forEach.call(__a, (__b) => __body)')) {
38
+ const {
39
+ __a,
40
+ __b,
41
+ __body,
42
+ } = getTemplateValues(path, '__.forEach.call(__a, (__b) => __body)');
43
+
44
+ replaceWith(path, forOfTemplate({
45
+ item: __b,
46
+ items: __a,
47
+ body: __body,
48
+ }));
49
+
50
+ return;
51
+ }
52
+
53
+ const {params, body} = path.node.arguments[0];
54
+ const item = getItem(params);
55
+
56
+ delete item.typeAnnotation;
57
+
58
+ const {length} = params;
59
+
60
+ for (const param of params) {
61
+ delete param.typeAnnotation;
62
+ }
63
+
64
+ const thisPassed = isIdentifier(params[0], {
65
+ name: 'this',
66
+ });
67
+
68
+ const items = path.node.callee.object;
69
+
70
+ if (length === 1 || length === 2 && thisPassed) {
71
+ const newPath = replaceWith(path, forOfTemplate({
72
+ item,
73
+ items,
74
+ body,
75
+ }));
76
+
77
+ fixReturn(newPath);
78
+ return;
79
+ }
80
+
81
+ const [, index] = params;
82
+
83
+ const newPath = replaceWith(path, forOfEntriesTemplate({
84
+ index,
85
+ item,
86
+ items,
87
+ body,
88
+ }));
89
+
90
+ fixReturn(newPath);
91
+ };
80
92
 
81
- export const match = () => ({
82
- '__.forEach(__args)': (vars, path) => {
93
+ export const traverse = ({push}) => ({
94
+ '__.forEach.call(__a, (__b) => __body)': push,
95
+ '__.forEach(__args)': (path) => {
83
96
  const {parentPath} = path;
84
97
 
85
98
  if (isReturnStatement(parentPath))
86
- return false;
99
+ return;
87
100
 
88
101
  if (parentPath.isSequenceExpression())
89
- return false;
102
+ return;
90
103
 
91
104
  if (parentPath.isConditionalExpression())
92
- return false;
105
+ return;
93
106
 
94
107
  if (parentPath.isVariableDeclarator())
95
- return false;
108
+ return;
96
109
 
97
110
  const objectPath = path.get('callee.object');
98
111
  const fnPath = path.get('arguments.0');
99
112
 
100
113
  if (!fnPath.isFunction())
101
- return false;
114
+ return;
102
115
 
103
116
  const params = fnPath.get('params');
104
117
 
105
118
  if (!params.length)
106
- return false;
119
+ return;
107
120
 
108
121
  if (isParentContainsFunctionArgument(objectPath))
109
- return false;
122
+ return;
110
123
 
111
- if (path.node.arguments.length === 2 && !path.get('arguments.1').isThisExpression())
112
- return false;
124
+ const {length} = path.node.arguments;
125
+
126
+ if (length === 2 && !path.get('arguments.1').isThisExpression())
127
+ return;
113
128
 
114
129
  if (isIndexWithThis(params))
115
- return false;
130
+ return;
116
131
 
117
132
  const [paramPath] = params;
118
133
 
119
134
  if (isSameNames(paramPath, objectPath))
120
- return false;
135
+ return;
121
136
 
122
137
  const rootPath = path.findParent(isRoot);
123
138
 
124
- return !isBoundVars(rootPath, fnPath);
139
+ if (isBoundVars(rootPath, fnPath))
140
+ return;
141
+
142
+ if (length === 1) {
143
+ push(path);
144
+ return;
145
+ }
146
+
147
+ if (params.length === 2)
148
+ push(path);
125
149
  },
126
150
  });
127
151
 
@@ -153,7 +177,7 @@ function isBoundVars(parentPath, path) {
153
177
  const currentBindings = keys(parentPath.scope.bindings);
154
178
  const fnBindings = keys(path.scope.bindings);
155
179
 
156
- return compare(currentBindings, fnBindings);
180
+ return compareBindings(currentBindings, fnBindings);
157
181
  }
158
182
 
159
183
  function isParentContainsFunctionArgument(objectPath) {
@@ -175,7 +199,7 @@ function getItem(params) {
175
199
  return thisItem;
176
200
  }
177
201
 
178
- function compare(a, b) {
202
+ function compareBindings(a, b) {
179
203
  for (const el of a) {
180
204
  if (b.includes(el))
181
205
  return true;
@@ -62,8 +62,10 @@ export const replace = () => ({
62
62
  }
63
63
  }`;
64
64
  },
65
- 'const __a = __b.reduce((__c, __d) => __e, __f)': (vars, path) => {
65
+ 'const __a = __b.reduce((__c, __d) => __e, __f)': ({__a, __c}, path) => {
66
66
  path.__putout_for_of_reduce = true;
67
+ rename(path, __c, __a);
68
+
67
69
  return `{
68
70
  let __a = __f;
69
71
  for (const __d of __b) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-for-of",
3
- "version": "12.5.2",
3
+ "version": "12.6.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",