@putout/plugin-for-of 6.1.1 → 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 +29 -1
- package/lib/for-entries-n/index.js +8 -2
- package/lib/for-n/index.js +1 -1
- package/lib/index.js +2 -0
- package/lib/to-for-n/index.js +21 -0
- package/package.json +5 -5
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
|
|
@@ -35,6 +35,14 @@ module.exports.filter = (path) => {
|
|
|
35
35
|
const [first] = body.body;
|
|
36
36
|
const {__i, __n} = getTemplateValues(path, forLoopToN);
|
|
37
37
|
|
|
38
|
+
const {
|
|
39
|
+
constantViolations,
|
|
40
|
+
references,
|
|
41
|
+
} = path.scope.bindings[__i.name];
|
|
42
|
+
|
|
43
|
+
if (constantViolations.length > 1)
|
|
44
|
+
return false;
|
|
45
|
+
|
|
38
46
|
if (!compare(first, assignIterable(__i)))
|
|
39
47
|
return false;
|
|
40
48
|
|
|
@@ -46,8 +54,6 @@ module.exports.filter = (path) => {
|
|
|
46
54
|
if (nBinding.references > 1)
|
|
47
55
|
return false;
|
|
48
56
|
|
|
49
|
-
const {references} = path.scope.bindings[__i.name];
|
|
50
|
-
|
|
51
57
|
return references > 3;
|
|
52
58
|
};
|
|
53
59
|
|
package/lib/for-n/index.js
CHANGED
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": "
|
|
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",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"changelog": false,
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
14
|
-
"url": "git://github.com/coderaiser/putout.git"
|
|
14
|
+
"url": "git+https://github.com/coderaiser/putout.git"
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
17
|
"test": "madrun test",
|
|
@@ -40,18 +40,18 @@
|
|
|
40
40
|
"@putout/plugin-remove-unused-variables": "*",
|
|
41
41
|
"@putout/plugin-remove-useless-arguments": "*",
|
|
42
42
|
"@putout/plugin-remove-useless-continue": "*",
|
|
43
|
-
"@putout/test": "^
|
|
43
|
+
"@putout/test": "^11.0.0",
|
|
44
44
|
"c8": "^10.0.0",
|
|
45
45
|
"eslint": "^9.0.0",
|
|
46
46
|
"eslint-plugin-n": "^17.0.0",
|
|
47
|
-
"eslint-plugin-putout": "^
|
|
47
|
+
"eslint-plugin-putout": "^23.0.0",
|
|
48
48
|
"lerna": "^6.0.1",
|
|
49
49
|
"madrun": "^10.0.0",
|
|
50
50
|
"montag": "^1.2.1",
|
|
51
51
|
"nodemon": "^3.0.1"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
|
-
"putout": ">=
|
|
54
|
+
"putout": ">=37"
|
|
55
55
|
},
|
|
56
56
|
"license": "MIT",
|
|
57
57
|
"engines": {
|