@putout/plugin-for-of 5.2.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 CHANGED
@@ -17,7 +17,7 @@ npm i @putout/plugin-for-of
17
17
 
18
18
  ## Rules
19
19
 
20
- - ✅ [add-missing-star](#add-missing-star);
20
+ - ✅ [add-missing-declaration](#add-missing-declaration);
21
21
  - ✅ [for-each](#for-each);
22
22
  - ✅ [for-entries](#for-entries);
23
23
  - ✅ [for-entries-n](#for-entries-n);
@@ -37,7 +37,7 @@ npm i @putout/plugin-for-of
37
37
  ```json
38
38
  {
39
39
  "rules": {
40
- "for-of/add-missing-star": "on",
40
+ "for-of/add-missing-declaration": "on",
41
41
  "for-of/map": "on",
42
42
  "for-of/for-in": "on",
43
43
  "for-of/for-each": "on",
@@ -53,34 +53,27 @@ npm i @putout/plugin-for-of
53
53
  }
54
54
  ```
55
55
 
56
- ## add-missing-star
56
+ ## add-missing-declaration
57
57
 
58
- > The `function*` declaration creates a binding of a new generator function to a given name.
58
+ > The [`const`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const) declaration declares block-scoped local variables.
59
59
  >
60
- > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*)
60
+ > (c) MDN
61
+
62
+ Chechout in 🐊[**Putout Editor**](https://putout.vercel.app/#/gist/68d114f01384084cc3bec0a5d75f2dfa/0bb5201d36ed27f74728921aca95a04efa35c82d).
61
63
 
62
64
  ### ❌ Example of incorrect code
63
65
 
64
66
  ```js
65
- function hello() {
66
- yield;
67
- 'world';
68
- }
69
-
70
- function func2() {
71
- yield * func1();
67
+ for (name of names) {
68
+ alert(`hello ${name}`);
72
69
  }
73
70
  ```
74
71
 
75
72
  ### ✅ Example of correct code
76
73
 
77
74
  ```js
78
- function* hello() {
79
- yield 'world';
80
- }
81
-
82
- function* func2() {
83
- yield* func1();
75
+ for (const name of names) {
76
+ alert(`hello ${name}`);
84
77
  }
85
78
  ```
86
79
 
@@ -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
@@ -1,6 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const addMissingStar = require('./add-missing-star');
4
3
  const map = require('./map');
5
4
  const forEach = require('./for-each');
6
5
  const forN = require('./for-n');
@@ -14,9 +13,9 @@ const removeUseless = require('./remove-useless');
14
13
  const removeUselessVariables = require('./remove-useless-variables');
15
14
  const removeUselessArrayFrom = require('./remove-useless-array-from');
16
15
  const removeUnusedVariables = require('./remove-unused-variables');
16
+ const addMissingDeclaration = require('./add-missing-declaration');
17
17
 
18
18
  module.exports.rules = {
19
- 'add-missing-star': addMissingStar,
20
19
  map,
21
20
  'for-each': forEach,
22
21
  'for-n': forN,
@@ -30,4 +29,5 @@ module.exports.rules = {
30
29
  'remove-useless-variables': removeUselessVariables,
31
30
  'remove-useless-array-from': removeUselessArrayFrom,
32
31
  'remove-unused-variables': removeUnusedVariables,
32
+ 'add-missing-declaration': addMissingDeclaration,
33
33
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-for-of",
3
- "version": "5.2.0",
3
+ "version": "6.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",
@@ -1,47 +0,0 @@
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
- });