@putout/plugin-for-of 12.1.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
@@ -7,12 +7,13 @@ import {
7
7
  const {
8
8
  isIdentifier,
9
9
  continueStatement,
10
+ isReturnStatement,
10
11
  } = types;
11
12
 
12
13
  const {
13
14
  replaceWith,
14
15
  replaceWithMultiple,
15
- traverse,
16
+ superTraverse,
16
17
  } = operator;
17
18
 
18
19
  const forOfTemplate = template(`
@@ -81,6 +82,9 @@ export const match = () => ({
81
82
  '__.forEach(__args)': (vars, path) => {
82
83
  const {parentPath} = path;
83
84
 
85
+ if (isReturnStatement(parentPath))
86
+ return false;
87
+
84
88
  if (parentPath.isSequenceExpression())
85
89
  return false;
86
90
 
@@ -130,7 +134,7 @@ function isSameNames(paramPath, objectPath) {
130
134
  }
131
135
 
132
136
  function fixReturn(path) {
133
- traverse(path, {
137
+ superTraverse(path, {
134
138
  'return __'(path) {
135
139
  if (!isForBeforeFnUp(path))
136
140
  return;
@@ -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.1.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",
@@ -41,10 +41,9 @@
41
41
  "@putout/plugin-variables": "*",
42
42
  "@putout/test": "^15.0.0",
43
43
  "eslint": "^10.0.0",
44
- "eslint-plugin-n": "^17.0.0",
45
44
  "eslint-plugin-putout": "^31.0.0",
46
45
  "madrun": "^13.0.0",
47
- "montag": "^1.2.1",
46
+ "montag": "^2.0.0",
48
47
  "nodemon": "^3.0.1",
49
48
  "superc8": "^12.0.0"
50
49
  },