@putout/plugin-for-of 12.2.0 → 12.4.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
@@ -32,6 +32,7 @@ npm i @putout/plugin-for-of
32
32
  - ✅ [remove-useless-array-from](#remove-useless-array-from);
33
33
  - ✅ [remove-useless-variables](#remove-useless-variables);
34
34
  - ✅ [to-for-n](#to-for-n);
35
+ - ✅ [from-while](#from-while);
35
36
 
36
37
  ## Configuration
37
38
 
@@ -50,7 +51,8 @@ npm i @putout/plugin-for-of
50
51
  "maxProperties": 4
51
52
  }],
52
53
  "for-of/for": "on",
53
- "for-of/to-for-n": "on"
54
+ "for-of/to-for-n": "on",
55
+ "for-of/from-while": "on"
54
56
  }
55
57
  }
56
58
  ```
@@ -85,6 +87,8 @@ for (const name of names) {
85
87
  >
86
88
  > (c) MDN
87
89
 
90
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/227db63f003df008d27455d01fcaec9d/6f1be22c96baa54b9abe34a93ccdcf09a343ff19).
91
+
88
92
  ### ❌ Example of incorrect code
89
93
 
90
94
  ```js
@@ -165,7 +169,7 @@ for (const item of arguments) {
165
169
 
166
170
  ## reduce
167
171
 
168
- > - The `reduce()` method executes a user-supplied **reducer** callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
172
+ > The `reduce()` method executes a user-supplied **reducer** callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
169
173
  >
170
174
  > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)
171
175
 
@@ -377,6 +381,36 @@ for (let i = 0; i < n; i++) {
377
381
  }
378
382
  ```
379
383
 
384
+ ### from-while
385
+
386
+ > 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
+ >
388
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while)
389
+
390
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/e4faebbc52bc2410425facc557b9acc7/9db52e3880190fe4f898ee6ac2722598073edf01).
391
+
392
+ #### ❌ Example of incorrect code
393
+
394
+ ```js
395
+ let i = 0;
396
+
397
+ while (i < node.signature.params.length) {
398
+ const {name} = node.signature.params[i];
399
+
400
+ params.push(identifier(name.value));
401
+
402
+ i = i + 1;
403
+ }
404
+ ```
405
+
406
+ #### ✅ Example of correct code
407
+
408
+ ```js
409
+ for (const {name} of node.signature.params) {
410
+ params.push(identifier(name.value));
411
+ }
412
+ ```
413
+
380
414
  ## License
381
415
 
382
416
  MIT
@@ -0,0 +1,66 @@
1
+ import {template, operator} from 'putout';
2
+
3
+ const {
4
+ remove,
5
+ getBinding,
6
+ compareAny,
7
+ compare,
8
+ getTemplateValues,
9
+ } = operator;
10
+
11
+ const getForOfLoop = template(`for (const LEFT of RIGHT) BODY`);
12
+ const assignIterable = (__i) => `const __a = __b[${__i.name}]`;
13
+
14
+ const incI = [
15
+ '__i = __i + 1',
16
+ '++__i',
17
+ '__i++',
18
+ ];
19
+
20
+ const WHILE = 'while (__i < __n) {__body}';
21
+
22
+ export const report = () => `Use 'for..of' instead of 'while'`;
23
+
24
+ export const filter = (path) => {
25
+ const {node} = path;
26
+ const prevPath = path.getPrevSibling();
27
+
28
+ if (!prevPath.node)
29
+ return false;
30
+
31
+ const {body} = node;
32
+
33
+ const first = body.body.at(0);
34
+ const last = body.body.at(-1);
35
+
36
+ const {__i} = getTemplateValues(path, WHILE);
37
+
38
+ if (!compare(first, assignIterable(__i)))
39
+ return false;
40
+
41
+ if (!compareAny(last, incI))
42
+ return false;
43
+
44
+ const {references} = getBinding(path, __i.name);
45
+
46
+ return references <= 3;
47
+ };
48
+
49
+ export const replace = () => ({
50
+ [WHILE]: ({__body, __i}, path) => {
51
+ const {__a, __b} = getTemplateValues(__body.body.at(0), assignIterable(__i));
52
+
53
+ __body.body.shift();
54
+ __body.body.pop();
55
+
56
+ const prev = path.getPrevSibling();
57
+
58
+ remove(prev);
59
+
60
+ return getForOfLoop({
61
+ LEFT: __a,
62
+ RIGHT: __b,
63
+ BODY: __body,
64
+ });
65
+ },
66
+ });
package/lib/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import * as fromWhile from './from-while/index.js';
1
2
  import * as map from './map/index.js';
2
3
  import * as forEach from './for-each/index.js';
3
4
  import * as forN from './for-n/index.js';
@@ -30,4 +31,5 @@ export const rules = {
30
31
  'remove-unused-variables': removeUnusedVariables,
31
32
  'add-missing-declaration': addMissingDeclaration,
32
33
  'to-for-n': toForN,
34
+ 'from-while': fromWhile,
33
35
  };
package/lib/map/index.js CHANGED
@@ -1,11 +1,20 @@
1
- import {types} from 'putout';
1
+ import {
2
+ types,
3
+ operator,
4
+ template,
5
+ } from 'putout';
6
+
7
+ const {compare, replaceWith} = operator;
2
8
 
3
9
  const {
4
10
  isReturnStatement,
5
11
  isBlockStatement,
6
12
  isTryStatement,
13
+ isArrayPattern,
7
14
  } = types;
8
15
 
16
+ const createPush = template('%%ARRAY%%.push(%%ARGUMENT%%)');
17
+
9
18
  const tail = (body) => body.at(-1);
10
19
 
11
20
  export const report = () => `Use 'for...of' instead of map when 'return' absent`;
@@ -25,6 +34,12 @@ export const match = () => ({
25
34
 
26
35
  return !isReturnStatement(last);
27
36
  },
37
+ 'const __a = __b.map((__c) => __d)': ({__a, __c, __d}) => {
38
+ if (compare(__c, __d))
39
+ return false;
40
+
41
+ return !isArrayPattern(__a);
42
+ },
28
43
  });
29
44
 
30
45
  export const replace = () => ({
@@ -34,4 +49,35 @@ export const replace = () => ({
34
49
 
35
50
  return `for (const ${b} of ${a}) __c`;
36
51
  },
52
+ 'const __a = __b.map((__c) => __d)': ({__a, __d}, path) => {
53
+ if (isBlockStatement(__d)) {
54
+ path.traverse({
55
+ ReturnStatement(path) {
56
+ const {argument} = path.node;
57
+
58
+ replaceWith(path, createPush({
59
+ ARRAY: __a,
60
+ ARGUMENT: argument,
61
+ }));
62
+ },
63
+ });
64
+ return `{
65
+ const __a = [];
66
+ for (const __c of __b) __d;
67
+ }`;
68
+ }
69
+
70
+ return `{
71
+ const __a = [];
72
+ for (const __c of __b) {
73
+ __a.push(__d);
74
+ }
75
+ }`;
76
+ },
77
+ '__a = __b.map((__c) => __d)': `{
78
+ __a = [];
79
+ for (const __c of __b) {
80
+ __a.push(__d);
81
+ }
82
+ }`,
37
83
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-for-of",
3
- "version": "12.2.0",
3
+ "version": "12.4.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",
@@ -38,6 +38,7 @@
38
38
  "@putout/plugin-remove-empty": "*",
39
39
  "@putout/plugin-remove-nested-blocks": "*",
40
40
  "@putout/plugin-remove-useless-continue": "*",
41
+ "@putout/plugin-remove-useless-map": "*",
41
42
  "@putout/plugin-variables": "*",
42
43
  "@putout/test": "^15.0.0",
43
44
  "eslint": "^10.0.0",