@putout/plugin-for-of 12.2.0 → 12.3.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
  ```
@@ -165,7 +167,7 @@ for (const item of arguments) {
165
167
 
166
168
  ## reduce
167
169
 
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.
170
+ > 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
171
  >
170
172
  > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)
171
173
 
@@ -377,6 +379,36 @@ for (let i = 0; i < n; i++) {
377
379
  }
378
380
  ```
379
381
 
382
+ ### from-while
383
+
384
+ > 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.
385
+ >
386
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while)
387
+
388
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/e4faebbc52bc2410425facc557b9acc7/9db52e3880190fe4f898ee6ac2722598073edf01).
389
+
390
+ #### ❌ Example of incorrect code
391
+
392
+ ```js
393
+ let i = 0;
394
+
395
+ while (i < node.signature.params.length) {
396
+ const {name} = node.signature.params[i];
397
+
398
+ params.push(identifier(name.value));
399
+
400
+ i = i + 1;
401
+ }
402
+ ```
403
+
404
+ #### ✅ Example of correct code
405
+
406
+ ```js
407
+ for (const {name} of node.signature.params) {
408
+ params.push(identifier(name.value));
409
+ }
410
+ ```
411
+
380
412
  ## License
381
413
 
382
414
  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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-for-of",
3
- "version": "12.2.0",
3
+ "version": "12.3.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",