@putout/plugin-for-of 12.5.1 → 12.6.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
@@ -34,15 +34,20 @@ npm i @putout/plugin-for-of
34
34
  - ✅ [remove-useless-variables](#remove-useless-variables);
35
35
  - ✅ [to-for-n](#to-for-n);
36
36
 
37
- ## Configuration
37
+ ## Config
38
38
 
39
39
  ```json
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
  ```
@@ -269,15 +273,13 @@ for (const x of Array.from(y)) {}
269
273
  for (const x of y) {}
270
274
  ```
271
275
 
272
- ## for
276
+ ## for-n
273
277
 
274
278
  > The `for` statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement to be executed in the loop.
275
279
  >
276
280
  > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for)
277
281
 
278
- ### for-n
279
-
280
- #### ❌ Example of incorrect code
282
+ ### ❌ Example of incorrect code
281
283
 
282
284
  ```js
283
285
  const n = items.length;
@@ -288,7 +290,7 @@ for (let i = 0; i < n; i++) {
288
290
  }
289
291
  ```
290
292
 
291
- #### ✅ Example of correct code
293
+ ### ✅ Example of correct code
292
294
 
293
295
  ```js
294
296
  for (const item of items) {
@@ -296,9 +298,9 @@ for (const item of items) {
296
298
  }
297
299
  ```
298
300
 
299
- ### for-length
301
+ ## for-length
300
302
 
301
- #### ❌ Example of incorrect code
303
+ ### ❌ Example of incorrect code
302
304
 
303
305
  ```js
304
306
  for (let i = 0; i < array.length; i++) {
@@ -307,7 +309,7 @@ for (let i = 0; i < array.length; i++) {
307
309
  }
308
310
  ```
309
311
 
310
- #### ✅ Example of correct code
312
+ ### ✅ Example of correct code
311
313
 
312
314
  ```js
313
315
  for (const item of items) {
@@ -315,9 +317,9 @@ for (const item of items) {
315
317
  }
316
318
  ```
317
319
 
318
- ### for-entries
320
+ ## for-entries
319
321
 
320
- #### ❌ Example of incorrect code
322
+ ### ❌ Example of incorrect code
321
323
 
322
324
  ```js
323
325
  for (let i = 0; i < array.length; i++) {
@@ -326,7 +328,7 @@ for (let i = 0; i < array.length; i++) {
326
328
  }
327
329
  ```
328
330
 
329
- #### ✅ Example of correct code
331
+ ### ✅ Example of correct code
330
332
 
331
333
  ```js
332
334
  for (const [i, item] of array.entries()) {
@@ -334,9 +336,9 @@ for (const [i, item] of array.entries()) {
334
336
  }
335
337
  ```
336
338
 
337
- ### for-entries-n
339
+ ## for-entries-n
338
340
 
339
- #### ❌ Example of incorrect code
341
+ ### ❌ Example of incorrect code
340
342
 
341
343
  ```js
342
344
  const n = array.length;
@@ -347,7 +349,7 @@ for (let i = 0; i < n; i++) {
347
349
  }
348
350
  ```
349
351
 
350
- #### ✅ Example of correct code
352
+ ### ✅ Example of correct code
351
353
 
352
354
  ```js
353
355
  for (const [i, item] of array.entries()) {
@@ -381,7 +383,7 @@ for (let i = 0; i < n; i++) {
381
383
  }
382
384
  ```
383
385
 
384
- ### from-while
386
+ ## from-while
385
387
 
386
388
  > The `while` statement creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated before executing the statement.
387
389
  >
@@ -389,7 +391,7 @@ for (let i = 0; i < n; i++) {
389
391
 
390
392
  Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/e4faebbc52bc2410425facc557b9acc7/9db52e3880190fe4f898ee6ac2722598073edf01).
391
393
 
392
- #### ❌ Example of incorrect code
394
+ ### ❌ Example of incorrect code
393
395
 
394
396
  ```js
395
397
  let i = 0;
@@ -403,7 +405,7 @@ while (i < node.signature.params.length) {
403
405
  }
404
406
  ```
405
407
 
406
- #### ✅ Example of correct code
408
+ ### ✅ Example of correct code
407
409
 
408
410
  ```js
409
411
  for (const {name} of node.signature.params) {
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-for-of",
3
- "version": "12.5.1",
3
+ "version": "12.6.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",