@putout/plugin-for-of 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) coderaiser
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,139 @@
1
+ # @putout/plugin-for-of [![NPM version][NPMIMGURL]][NPMURL]
2
+
3
+ [NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-for-of.svg?style=flat&longCache=true
4
+ [NPMURL]: https://npmjs.org/package/@putout/plugin-for-of "npm"
5
+
6
+ > The [`for...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) statement creates a loop which invokes a custom iteration hook with statements to be executed for the value of each element of an array.
7
+ >
8
+ > (c) MDN
9
+
10
+ 🐊[**Putout**](https://github.com/coderaiser/putout) plugin adds support of transformation `for...of` statements.
11
+
12
+ ## Install
13
+
14
+ ```
15
+ npm i @putout/plugin-for-of
16
+ ```
17
+
18
+ ## Configuration
19
+
20
+ ```json
21
+ {
22
+ "rules": {
23
+ "for-of/map": "on",
24
+ "for-of/for": "on"
25
+ }
26
+ }
27
+ ```
28
+
29
+ ## map
30
+
31
+ > 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.
32
+ >
33
+ > (c) MDN
34
+
35
+ ## ❌ Example of incorrect code
36
+
37
+ ```js
38
+ names.map((name) => {
39
+ alert(`hello ${name}`);
40
+ });
41
+ ```
42
+
43
+ ## βœ… Example of correct code
44
+
45
+ ```js
46
+ for (const name of names) {
47
+ alert(`hello ${name}`);
48
+ }
49
+ ```
50
+
51
+ ## for
52
+
53
+ > The [`for`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for) statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement to be executed in the loop.
54
+ >
55
+ >
56
+ > (c) MDN
57
+
58
+ ### for-n
59
+
60
+ #### ❌ Example of incorrect code
61
+
62
+ ```js
63
+ const n = items.length;
64
+
65
+ for (let i = 0; i < n; i++) {
66
+ const item = items[i];
67
+ log(item);
68
+ }
69
+ ```
70
+
71
+ #### βœ… Example of correct code
72
+
73
+ ```js
74
+ for (const item of items) {
75
+ log(item);
76
+ }
77
+ ```
78
+
79
+ ### for-length
80
+
81
+ #### ❌ Example of incorrect code
82
+
83
+ ```js
84
+ for (let i = 0; i < array.length; i++) {
85
+ const item = array[i];
86
+ console.log(item);
87
+ }
88
+ ```
89
+
90
+ #### βœ… Example of correct code
91
+
92
+ ```js
93
+ for (const item of items) {
94
+ log(item);
95
+ }
96
+ ```
97
+
98
+ ### for-entries
99
+
100
+ #### ❌ Example of incorrect code
101
+
102
+ ```js
103
+ for (let i = 0; i < array.length; i++) {
104
+ const item = array[i];
105
+ console.log(i, item);
106
+ }
107
+ ```
108
+
109
+ #### βœ… Example of correct code
110
+
111
+ ```js
112
+ for (const [i, item] of array.entries()) {
113
+ console.log(i, item);
114
+ }
115
+ ```
116
+
117
+ ### for-entries-n
118
+
119
+ ##### ❌ Example of incorrect code
120
+
121
+ ```js
122
+ const n = array.length;
123
+ for (let i = 0; i < n; i++) {
124
+ const item = array[i];
125
+ console.log(i, item);
126
+ }
127
+ ```
128
+
129
+ #### βœ… Example of correct code
130
+
131
+ ```js
132
+ for (const [i, item] of array.entries()) {
133
+ console.log(i, item);
134
+ }
135
+ ```
136
+
137
+ ## License
138
+
139
+ MIT
@@ -0,0 +1,61 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ operator,
5
+ types,
6
+ template,
7
+ } = require('putout');
8
+ const {
9
+ compare,
10
+ getTemplateValues,
11
+ } = operator;
12
+
13
+ const {isBlockStatement} = types;
14
+
15
+ module.exports.report = () => `Use 'for...of' instead of 'for'`;
16
+
17
+ const forLoop = 'for (let __i = 0; __i < __e.length; __i++) __c';
18
+ const getForOfLoop = template(`for (const [INDEX, ITEM] of ITEMS.entries()) BODY`);
19
+ const assignIterable = (__i) => `const __a = __b[${__i.name}]`;
20
+ const assignIterableWithName = (__i, __e) => `const __a = ${__e.name}[${__i.name}]`;
21
+
22
+ module.exports.filter = (path) => {
23
+ const {node} = path;
24
+ const {body} = node;
25
+
26
+ if (!isBlockStatement(body))
27
+ return false;
28
+
29
+ const [first] = body.body;
30
+ const {__i, __e} = getTemplateValues(path, forLoop);
31
+
32
+ const {bindings} = path.scope;
33
+ const iBinding = bindings[__i.name];
34
+
35
+ if (iBinding.constantViolations.length > 1)
36
+ return false;
37
+
38
+ const {references} = path.scope.bindings[__i.name];
39
+
40
+ if (references < 4)
41
+ return false;
42
+
43
+ return compare(first, assignIterableWithName(__i, __e));
44
+ };
45
+
46
+ module.exports.replace = () => ({
47
+ [forLoop]: ({__c, __i}) => {
48
+ const [node] = __c.body;
49
+ const {__a, __b} = getTemplateValues(node, assignIterable(__i));
50
+
51
+ __c.body.shift();
52
+
53
+ return getForOfLoop({
54
+ INDEX: __i,
55
+ ITEM: __a,
56
+ ITEMS: __b,
57
+ BODY: __c,
58
+ });
59
+ },
60
+ });
61
+
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ operator,
5
+ template,
6
+ types,
7
+ } = require('putout');
8
+ const {
9
+ compare,
10
+ getTemplateValues,
11
+ } = operator;
12
+
13
+ const {isBlockStatement} = types;
14
+
15
+ module.exports.report = () => `Use 'for...of' instead of 'for'`;
16
+
17
+ const forLoopToN = 'for (let __i = 0; __i < __n; __i++) __c';
18
+ const getForOfLoop = template(`for (const [INDEX, LEFT] of RIGHT.entries()) BODY`);
19
+ const assignIterable = (__i) => `const __a = __b[${__i.name}]`;
20
+ const assignN = (__n) => `const ${__n.name} = __e.length`;
21
+
22
+ module.exports.filter = (path) => {
23
+ const {node} = path;
24
+ const prevPath = path.getPrevSibling();
25
+
26
+ if (!prevPath.node)
27
+ return false;
28
+
29
+ const {body} = node;
30
+
31
+ if (!isBlockStatement(body))
32
+ return false;
33
+
34
+ const [first] = body.body;
35
+ const {__i, __n} = getTemplateValues(path, forLoopToN);
36
+
37
+ if (!compare(first, assignIterable(__i)))
38
+ return false;
39
+
40
+ if (!comparePrevSiblings(path, assignN(__n)))
41
+ return false;
42
+
43
+ const nBinding = path.scope.getBinding(__n.name);
44
+
45
+ if (nBinding.references > 1)
46
+ return false;
47
+
48
+ const {references} = path.scope.bindings[__i.name];
49
+
50
+ return references > 3;
51
+ };
52
+
53
+ module.exports.replace = () => ({
54
+ [forLoopToN]: ({__c, __i}) => {
55
+ const [node] = __c.body;
56
+ const {__a, __b} = getTemplateValues(node, assignIterable(__i));
57
+
58
+ __c.body.shift();
59
+
60
+ return getForOfLoop({
61
+ INDEX: __i,
62
+ LEFT: __a,
63
+ RIGHT: __b,
64
+ BODY: __c,
65
+ });
66
+ },
67
+ });
68
+
69
+ function comparePrevSiblings(prev, node) {
70
+ while (prev = prev.getPrevSibling()) {
71
+ if (!prev.node)
72
+ return false;
73
+
74
+ if (compare(prev.node, node))
75
+ return true;
76
+ }
77
+ }
78
+
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ operator,
5
+ types,
6
+ template,
7
+ } = require('putout');
8
+ const {
9
+ compare,
10
+ getTemplateValues,
11
+ } = operator;
12
+
13
+ const {isBlockStatement} = types;
14
+
15
+ module.exports.report = () => `Use 'for...of' instead of 'for'`;
16
+
17
+ const forLoop = 'for (let __i = 0; __i < __e.length; __i++) __c';
18
+ const getForOfLoop = template(`for (const LEFT of RIGHT) BODY`);
19
+ const assignIterable = (__i) => `const __a = __b[${__i.name}]`;
20
+ const assignIterableWithName = (__i, __e) => `const __a = ${__e.name}[${__i.name}]`;
21
+
22
+ module.exports.filter = (path) => {
23
+ const {node} = path;
24
+ const {body} = node;
25
+
26
+ if (!isBlockStatement(body))
27
+ return false;
28
+
29
+ const [first] = body.body;
30
+ const {__i, __e} = getTemplateValues(path, forLoop);
31
+ const {references} = path.scope.bindings[__i.name];
32
+
33
+ if (references > 3)
34
+ return false;
35
+
36
+ return compare(first, assignIterableWithName(__i, __e));
37
+ };
38
+
39
+ module.exports.replace = () => ({
40
+ [forLoop]: ({__c, __i}) => {
41
+ const [node] = __c.body;
42
+ const {__a, __b} = getTemplateValues(node, assignIterable(__i));
43
+
44
+ __c.body.shift();
45
+
46
+ return getForOfLoop({
47
+ LEFT: __a,
48
+ RIGHT: __b,
49
+ BODY: __c,
50
+ });
51
+ },
52
+ });
53
+
@@ -0,0 +1,75 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ operator,
5
+ template,
6
+ types,
7
+ } = require('putout');
8
+ const {
9
+ compare,
10
+ getTemplateValues,
11
+ } = operator;
12
+
13
+ const {isBlockStatement} = types;
14
+
15
+ module.exports.report = () => `Use 'for...of' instead of 'for'`;
16
+
17
+ const forLoopToN = 'for (let __i = 0; __i < __n; __i++) __c';
18
+ const getForOfLoop = template(`for (const LEFT of RIGHT) BODY`);
19
+ const assignIterable = (__i) => `const __a = __b[${__i.name}]`;
20
+ const assignN = (__n) => `const ${__n.name} = __e.length`;
21
+
22
+ module.exports.filter = (path) => {
23
+ const {node} = path;
24
+ const prevPath = path.getPrevSibling();
25
+
26
+ if (!prevPath.node)
27
+ return false;
28
+
29
+ const {body} = node;
30
+
31
+ if (!isBlockStatement(body))
32
+ return false;
33
+
34
+ const [first] = body.body;
35
+ const {__i, __n} = getTemplateValues(path, forLoopToN);
36
+
37
+ if (!compare(first, assignIterable(__i)))
38
+ return false;
39
+
40
+ if (!comparePrevSiblings(path, assignN(__n)))
41
+ return false;
42
+
43
+ const {references} = path.scope.bindings[__i.name];
44
+
45
+ if (references > 3)
46
+ return false;
47
+
48
+ return true;
49
+ };
50
+
51
+ module.exports.replace = () => ({
52
+ [forLoopToN]: ({__c, __i}) => {
53
+ const [node] = __c.body;
54
+ const {__a, __b} = getTemplateValues(node, assignIterable(__i));
55
+
56
+ __c.body.shift();
57
+
58
+ return getForOfLoop({
59
+ LEFT: __a,
60
+ RIGHT: __b,
61
+ BODY: __c,
62
+ });
63
+ },
64
+ });
65
+
66
+ function comparePrevSiblings(prev, node) {
67
+ while (prev = prev.getPrevSibling()) {
68
+ if (!prev.node)
69
+ return false;
70
+
71
+ if (compare(prev.node, node))
72
+ return true;
73
+ }
74
+ }
75
+
package/lib/index.js ADDED
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ const getRule = (a) => ({
4
+ [a]: require(`./${a}`),
5
+ });
6
+
7
+ module.exports.rules = {
8
+ ...getRule('map'),
9
+ ...getRule('for-n'),
10
+ ...getRule('for-length'),
11
+ ...getRule('for-entries'),
12
+ ...getRule('for-entries-n'),
13
+ };
14
+
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ print,
5
+ types,
6
+ } = require('putout');
7
+
8
+ const {
9
+ isReturnStatement,
10
+ isBlockStatement,
11
+ isTryStatement,
12
+ } = types;
13
+
14
+ const tail = (body) => body.at(-1);
15
+
16
+ module.exports.report = () => `Use 'for...of' instead of map when 'return' absent`;
17
+
18
+ module.exports.match = () => ({
19
+ '__a.map((__b) => __c)': ({__c}, path) => {
20
+ if (!path.parentPath.isExpressionStatement())
21
+ return false;
22
+
23
+ if (!isBlockStatement(__c))
24
+ return false;
25
+
26
+ const last = tail(__c.body);
27
+
28
+ if (isTryStatement(last))
29
+ return false;
30
+
31
+ if (isReturnStatement(last))
32
+ return false;
33
+
34
+ return true;
35
+ },
36
+ });
37
+
38
+ module.exports.replace = () => ({
39
+ '__a.map((__b) => __c)': ({__a, __b}) => {
40
+ const a = print(__a);
41
+ const b = print(__b);
42
+
43
+ return `for (const ${b} of ${a}) __c`;
44
+ },
45
+ });
46
+
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@putout/plugin-for-of",
3
+ "version": "1.0.0",
4
+ "type": "commonjs",
5
+ "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
+ "description": "🐊Putout plugin adds ability to apply for...of",
7
+ "homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-for-of#readme",
8
+ "main": "lib/index.js",
9
+ "release": false,
10
+ "tag": false,
11
+ "changelog": false,
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git://github.com/coderaiser/putout.git"
15
+ },
16
+ "scripts": {
17
+ "test": "madrun test",
18
+ "watch:test": "madrun watch:test",
19
+ "lint": "madrun lint",
20
+ "fresh:lint": "madrun fresh:lint",
21
+ "lint:fresh": "madrun lint:fresh",
22
+ "fix:lint": "madrun fix:lint",
23
+ "coverage": "madrun coverage",
24
+ "report": "madrun report"
25
+ },
26
+ "dependencies": {},
27
+ "keywords": [
28
+ "putout",
29
+ "putout-plugin",
30
+ "plugin",
31
+ "for-of",
32
+ "try-to-catch"
33
+ ],
34
+ "devDependencies": {
35
+ "@putout/test": "^6.0.0",
36
+ "c8": "^7.5.0",
37
+ "eslint": "^8.0.1",
38
+ "eslint-plugin-n": "^15.2.4",
39
+ "eslint-plugin-putout": "^16.0.0",
40
+ "lerna": "^6.0.1",
41
+ "madrun": "^9.0.0",
42
+ "nodemon": "^2.0.1"
43
+ },
44
+ "peerDependencies": {
45
+ "putout": ">=29"
46
+ },
47
+ "license": "MIT",
48
+ "engines": {
49
+ "node": ">=16"
50
+ },
51
+ "publishConfig": {
52
+ "access": "public"
53
+ }
54
+ }