@putout/plugin-for-of 7.0.0 → 7.1.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
@@ -31,6 +31,7 @@ npm i @putout/plugin-for-of
31
31
  - ✅ [remove-useless](#remove-useless);
32
32
  - ✅ [remove-useless-array-from](#remove-useless-array-from);
33
33
  - ✅ [remove-useless-variables](#remove-useless-variables);
34
+ - ✅ [to-for-n](#to-for-n);
34
35
 
35
36
  ## Configuration
36
37
 
@@ -48,7 +49,8 @@ npm i @putout/plugin-for-of
48
49
  "for-of/remove-useless-variables": ["on", {
49
50
  "maxProperties": 4
50
51
  }],
51
- "for-of/for": "on"
52
+ "for-of/for": "on",
53
+ "for-of/to-for-n": "on"
52
54
  }
53
55
  }
54
56
  ```
@@ -349,6 +351,32 @@ for (const [i, item] of array.entries()) {
349
351
  }
350
352
  ```
351
353
 
354
+ ### to-for-n
355
+
356
+ > The `entries()` method of `Array` instances returns a new array iterator object that contains the key/value pairs for each index in the array.
357
+ >
358
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries)
359
+
360
+ Index overwrites on each iteration so when it modified inside loop it has no effect so `for` should be used in this case. Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/635e6c8e99c1969a731e1502a70abf75/5a25ec70cf329da098ae971f46003bbe2567a111).
361
+
362
+ #### ❌ Example of incorrect code
363
+
364
+ ```js
365
+ for (let [i, token] of tokens.entries()) {
366
+ ++i;
367
+ }
368
+ ```
369
+
370
+ #### ✅ Example of correct code
371
+
372
+ ```js
373
+ const n = tokens.length;
374
+
375
+ for (let i = 0; i < n; i++) {
376
+ ++i;
377
+ }
378
+ ```
379
+
352
380
  ## License
353
381
 
354
382
  MIT
package/lib/index.js CHANGED
@@ -14,6 +14,7 @@ const removeUselessVariables = require('./remove-useless-variables');
14
14
  const removeUselessArrayFrom = require('./remove-useless-array-from');
15
15
  const removeUnusedVariables = require('./remove-unused-variables');
16
16
  const addMissingDeclaration = require('./add-missing-declaration');
17
+ const toForN = require('./to-for-n');
17
18
 
18
19
  module.exports.rules = {
19
20
  map,
@@ -30,4 +31,5 @@ module.exports.rules = {
30
31
  'remove-useless-array-from': removeUselessArrayFrom,
31
32
  'remove-unused-variables': removeUnusedVariables,
32
33
  'add-missing-declaration': addMissingDeclaration,
34
+ 'to-for-n': toForN,
33
35
  };
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => `Use 'for' instead of 'for...of' with '.entries()' when change index`;
4
+
5
+ module.exports.match = () => ({
6
+ 'for (let [__a, __b] of __c.entries())__body': ({__a}, path) => {
7
+ const binding = path.scope.bindings[__a.name];
8
+ return binding.constantViolations.length;
9
+ },
10
+ });
11
+
12
+ module.exports.replace = () => ({
13
+ 'for (let [__a, __b] of __c.entries())__body': (vars, path) => {
14
+ const {n} = path.parentPath.scope.bindings;
15
+
16
+ if (n)
17
+ return 'for (let i = 0; i < tokens.length; i++) __body';
18
+
19
+ return '{const n = __c.length; for (let i = 0; i < n; i++) __body}';
20
+ },
21
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-for-of",
3
- "version": "7.0.0",
3
+ "version": "7.1.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin adds ability to apply for...of",