@putout/plugin-for-of 5.0.0 → 5.2.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 +33 -0
- package/lib/add-missing-star/index.js +47 -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-star](#add-missing-star);
|
|
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-star": "on",
|
|
39
41
|
"for-of/map": "on",
|
|
40
42
|
"for-of/for-in": "on",
|
|
41
43
|
"for-of/for-each": "on",
|
|
@@ -51,6 +53,37 @@ npm i @putout/plugin-for-of
|
|
|
51
53
|
}
|
|
52
54
|
```
|
|
53
55
|
|
|
56
|
+
## add-missing-star
|
|
57
|
+
|
|
58
|
+
> The `function*` declaration creates a binding of a new generator function to a given name.
|
|
59
|
+
>
|
|
60
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*)
|
|
61
|
+
|
|
62
|
+
### ❌ Example of incorrect code
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
function hello() {
|
|
66
|
+
yield;
|
|
67
|
+
'world';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function func2() {
|
|
71
|
+
yield * func1();
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### ✅ Example of correct code
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
function* hello() {
|
|
79
|
+
yield 'world';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function* func2() {
|
|
83
|
+
yield* func1();
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
54
87
|
## map
|
|
55
88
|
|
|
56
89
|
> 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,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {types, operator} = require('putout');
|
|
4
|
+
const {remove, replaceWith} = operator;
|
|
5
|
+
const {YieldExpression} = types;
|
|
6
|
+
|
|
7
|
+
const DELEGATE = true;
|
|
8
|
+
|
|
9
|
+
module.exports.report = () => `Add missing '*' in generator function`;
|
|
10
|
+
|
|
11
|
+
module.exports.fix = (path) => {
|
|
12
|
+
const fnPath = path.getFunctionParent();
|
|
13
|
+
|
|
14
|
+
fnPath.node.generator = true;
|
|
15
|
+
|
|
16
|
+
if (path.parentPath.isExpressionStatement()) {
|
|
17
|
+
const next = path.parentPath.getNextSibling();
|
|
18
|
+
const {expression} = next.node;
|
|
19
|
+
|
|
20
|
+
replaceWith(path, YieldExpression(expression));
|
|
21
|
+
remove(next);
|
|
22
|
+
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const {parentPath} = path;
|
|
27
|
+
const {right} = parentPath.node;
|
|
28
|
+
|
|
29
|
+
replaceWith(parentPath, YieldExpression(right, DELEGATE));
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
module.exports.traverse = ({push}) => ({
|
|
33
|
+
Identifier(path) {
|
|
34
|
+
if (path.node.name !== 'yield')
|
|
35
|
+
return;
|
|
36
|
+
|
|
37
|
+
if (path.parentPath.isBinaryExpression({operator: '*'})) {
|
|
38
|
+
push(path);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!path.parentPath.isExpressionStatement())
|
|
43
|
+
return;
|
|
44
|
+
|
|
45
|
+
push(path);
|
|
46
|
+
},
|
|
47
|
+
});
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const addMissingStar = require('./add-missing-star');
|
|
3
4
|
const map = require('./map');
|
|
4
5
|
const forEach = require('./for-each');
|
|
5
6
|
const forN = require('./for-n');
|
|
@@ -15,6 +16,7 @@ const removeUselessArrayFrom = require('./remove-useless-array-from');
|
|
|
15
16
|
const removeUnusedVariables = require('./remove-unused-variables');
|
|
16
17
|
|
|
17
18
|
module.exports.rules = {
|
|
19
|
+
'add-missing-star': addMissingStar,
|
|
18
20
|
map,
|
|
19
21
|
'for-each': forEach,
|
|
20
22
|
'for-n': forN,
|
package/package.json
CHANGED