@putout/plugin-for-of 6.0.0 → 6.1.1
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
|
@@ -17,6 +17,7 @@ npm i @putout/plugin-for-of
|
|
|
17
17
|
|
|
18
18
|
## Rules
|
|
19
19
|
|
|
20
|
+
- ✅ [add-missing-declaration](#add-missing-declaration);
|
|
20
21
|
- ✅ [for-each](#for-each);
|
|
21
22
|
- ✅ [for-entries](#for-entries);
|
|
22
23
|
- ✅ [for-entries-n](#for-entries-n);
|
|
@@ -36,6 +37,7 @@ npm i @putout/plugin-for-of
|
|
|
36
37
|
```json
|
|
37
38
|
{
|
|
38
39
|
"rules": {
|
|
40
|
+
"for-of/add-missing-declaration": "on",
|
|
39
41
|
"for-of/map": "on",
|
|
40
42
|
"for-of/for-in": "on",
|
|
41
43
|
"for-of/for-each": "on",
|
|
@@ -51,6 +53,30 @@ npm i @putout/plugin-for-of
|
|
|
51
53
|
}
|
|
52
54
|
```
|
|
53
55
|
|
|
56
|
+
## add-missing-declaration
|
|
57
|
+
|
|
58
|
+
> The [`const`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const) declaration declares block-scoped local variables.
|
|
59
|
+
>
|
|
60
|
+
> (c) MDN
|
|
61
|
+
|
|
62
|
+
Chechout in 🐊[**Putout Editor**](https://putout.vercel.app/#/gist/68d114f01384084cc3bec0a5d75f2dfa/0bb5201d36ed27f74728921aca95a04efa35c82d).
|
|
63
|
+
|
|
64
|
+
### ❌ Example of incorrect code
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
for (name of names) {
|
|
68
|
+
alert(`hello ${name}`);
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### ✅ Example of correct code
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
for (const name of names) {
|
|
76
|
+
alert(`hello ${name}`);
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
54
80
|
## map
|
|
55
81
|
|
|
56
82
|
> The [`map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) method creates a new array populated with the results of calling a provided function on every element in the calling array.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {types} = require('putout');
|
|
4
|
+
const {isVariableDeclaration} = types;
|
|
5
|
+
|
|
6
|
+
module.exports.report = () => `Add missing declaration`;
|
|
7
|
+
|
|
8
|
+
module.exports.match = () => ({
|
|
9
|
+
'for (__a of __b) __c': ({__a}, path) => {
|
|
10
|
+
if (isVariableDeclaration(__a))
|
|
11
|
+
return false;
|
|
12
|
+
|
|
13
|
+
const bindings = path.scope.getAllBindings();
|
|
14
|
+
|
|
15
|
+
return !bindings[__a.name];
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
module.exports.replace = () => ({
|
|
20
|
+
'for (__a of __b) __c': 'for (const __a of __b) __c',
|
|
21
|
+
});
|
package/lib/index.js
CHANGED
|
@@ -13,6 +13,7 @@ const removeUseless = require('./remove-useless');
|
|
|
13
13
|
const removeUselessVariables = require('./remove-useless-variables');
|
|
14
14
|
const removeUselessArrayFrom = require('./remove-useless-array-from');
|
|
15
15
|
const removeUnusedVariables = require('./remove-unused-variables');
|
|
16
|
+
const addMissingDeclaration = require('./add-missing-declaration');
|
|
16
17
|
|
|
17
18
|
module.exports.rules = {
|
|
18
19
|
map,
|
|
@@ -28,4 +29,5 @@ module.exports.rules = {
|
|
|
28
29
|
'remove-useless-variables': removeUselessVariables,
|
|
29
30
|
'remove-useless-array-from': removeUselessArrayFrom,
|
|
30
31
|
'remove-unused-variables': removeUnusedVariables,
|
|
32
|
+
'add-missing-declaration': addMissingDeclaration,
|
|
31
33
|
};
|
|
@@ -18,23 +18,23 @@ module.exports.report = (path) => {
|
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
module.exports.match = () => ({
|
|
21
|
-
'for (const __array of __b) __c':
|
|
22
|
-
'for (const __object of __b) __c':
|
|
21
|
+
'for (const __array of __b) __c': matchForOf,
|
|
22
|
+
'for (const __object of __b) __c': matchForOf,
|
|
23
23
|
});
|
|
24
24
|
|
|
25
25
|
module.exports.replace = () => ({
|
|
26
|
-
'for (const __array of __b) __c':
|
|
27
|
-
'for (const __object of __b) __c':
|
|
26
|
+
'for (const __array of __b) __c': replaceForOf,
|
|
27
|
+
'for (const __object of __b) __c': replaceForOf,
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
-
function
|
|
30
|
+
function matchForOf(vars, path) {
|
|
31
31
|
const idPath = path.get('left.declarations.0.id');
|
|
32
32
|
const elements = getElements(idPath);
|
|
33
33
|
|
|
34
34
|
return !isAllReferenced(idPath, elements);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
function
|
|
37
|
+
function replaceForOf(vars, path) {
|
|
38
38
|
const variables = getVariables(path);
|
|
39
39
|
|
|
40
40
|
for (const currentPath of variables) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-for-of",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.1.1",
|
|
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",
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
"@putout/plugin-remove-unused-variables": "*",
|
|
41
41
|
"@putout/plugin-remove-useless-arguments": "*",
|
|
42
42
|
"@putout/plugin-remove-useless-continue": "*",
|
|
43
|
-
"@putout/test": "^
|
|
44
|
-
"c8": "^
|
|
43
|
+
"@putout/test": "^10.0.0",
|
|
44
|
+
"c8": "^10.0.0",
|
|
45
45
|
"eslint": "^9.0.0",
|
|
46
46
|
"eslint-plugin-n": "^17.0.0",
|
|
47
47
|
"eslint-plugin-putout": "^22.0.0",
|