@putout/plugin-for-of 6.0.0 → 6.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 +26 -0
- package/lib/add-missing-declaration/index.js +21 -0
- package/lib/index.js +2 -0
- package/package.json +1 -1
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
|
};
|
package/package.json
CHANGED