@putout/plugin-for-of 1.2.3 → 2.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/README.md CHANGED
@@ -97,9 +97,11 @@ for (const item of Object.keys(object)) {
97
97
  ### ❌ Example of incorrect code
98
98
 
99
99
  ```js
100
- Object.keys(json).forEach((name) => {
101
- manage(name, json[name]);
102
- });
100
+ Object
101
+ .keys(json)
102
+ .forEach((name) => {
103
+ manage(name, json[name]);
104
+ });
103
105
 
104
106
  [].forEach.call(arguments, (item) => {
105
107
  console.log(item);
@@ -199,8 +201,7 @@ for (const a of b) {
199
201
  ### ✅ Example of correct code
200
202
 
201
203
  ```js
202
- for (const {c} of b) {
203
- }
204
+ for (const {c} of b) {}
204
205
  ```
205
206
 
206
207
  ## remove-useless-array-from
@@ -36,11 +36,17 @@ module.exports.replace = () => ({
36
36
  '__.forEach.call(__a, (__b) => __body)': 'for (const __b of __a) __body',
37
37
  '__.forEach(__args)': (vars, path) => {
38
38
  const {params, body} = path.node.arguments[0];
39
+
39
40
  const item = getItem(params);
40
41
 
41
42
  delete item.typeAnnotation;
43
+
42
44
  const {length} = params;
43
- const thisPassed = isIdentifier(params[0], {name: 'this'});
45
+
46
+ const thisPassed = isIdentifier(params[0], {
47
+ name: 'this',
48
+ });
49
+
44
50
  const items = path.node.callee.object;
45
51
 
46
52
  if (length === 1 || length === 2 && thisPassed) {
@@ -51,11 +57,13 @@ module.exports.replace = () => ({
51
57
  }));
52
58
 
53
59
  fixReturn(newPath);
60
+
54
61
  return;
55
62
  }
56
63
 
57
64
  if (params.length === 2) {
58
65
  const [, index] = params;
66
+
59
67
  const newPath = replaceWith(path, forOfEntriesTemplate({
60
68
  index,
61
69
  item,
@@ -98,16 +106,16 @@ module.exports.match = () => ({
98
106
 
99
107
  const rootPath = path.findParent(isRoot);
100
108
 
101
- if (isBoundVars(rootPath, fnPath))
102
- return false;
103
-
104
- return true;
109
+ return !isBoundVars(rootPath, fnPath);
105
110
  },
106
111
  });
107
112
 
108
113
  function isSameNames(paramPath, objectPath) {
109
114
  const {name} = paramPath.node;
110
- return objectPath.isIdentifier({name});
115
+
116
+ return objectPath.isIdentifier({
117
+ name,
118
+ });
111
119
  }
112
120
 
113
121
  function fixReturn(path) {
@@ -121,10 +129,7 @@ function fixReturn(path) {
121
129
  if (!argument)
122
130
  return replaceWith(path, ContinueStatement());
123
131
 
124
- replaceWithMultiple(path, [
125
- argument,
126
- ContinueStatement(),
127
- ]);
132
+ replaceWithMultiple(path, [argument, ContinueStatement()]);
128
133
  },
129
134
  });
130
135
  }
@@ -184,4 +189,3 @@ function isForBeforeFnUp(path) {
184
189
 
185
190
  return true;
186
191
  }
187
-
@@ -5,6 +5,7 @@ const {
5
5
  types,
6
6
  template,
7
7
  } = require('putout');
8
+
8
9
  const {
9
10
  compare,
10
11
  getTemplateValues,
@@ -28,7 +29,6 @@ module.exports.filter = (path) => {
28
29
 
29
30
  const [first] = body.body;
30
31
  const {__i, __e} = getTemplateValues(path, forLoop);
31
-
32
32
  const {bindings} = path.scope;
33
33
  const iBinding = bindings[__i.name];
34
34
 
@@ -58,4 +58,3 @@ module.exports.replace = () => ({
58
58
  });
59
59
  },
60
60
  });
61
-
@@ -5,6 +5,7 @@ const {
5
5
  template,
6
6
  types,
7
7
  } = require('putout');
8
+
8
9
  const {
9
10
  compare,
10
11
  getTemplateValues,
@@ -75,4 +76,3 @@ function comparePrevSiblings(prev, node) {
75
76
  return true;
76
77
  }
77
78
  }
78
-
@@ -1,9 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- generate,
5
- operator,
6
- } = require('putout');
3
+ const {generate, operator} = require('putout');
7
4
 
8
5
  const {
9
6
  contains,
@@ -21,9 +18,7 @@ module.exports.match = () => ({
21
18
 
22
19
  const {name} = declaration.__a;
23
20
 
24
- return contains(__body, [
25
- `if (!${__b.name}.hasOwnProperty(${name})) __c`,
26
- ]);
21
+ return contains(__body, [`if (!${__b.name}.hasOwnProperty(${name})) __c`]);
27
22
  },
28
23
  });
29
24
 
@@ -33,9 +28,9 @@ module.exports.replace = () => ({
33
28
  const condition = getTemplateValues(first, 'if (!__b.hasOwnProperty(__a)) __c');
34
29
 
35
30
  __body.body.shift();
31
+
36
32
  const {code} = generate(__body);
37
33
 
38
34
  return `for (const ${condition.__a.name} of Object.keys(${__b.name})) ${code}`;
39
35
  },
40
36
  });
41
-
@@ -1,9 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- generate,
5
- operator,
6
- } = require('putout');
3
+ const {generate, operator} = require('putout');
7
4
 
8
5
  const {
9
6
  contains,
@@ -21,9 +18,7 @@ module.exports.match = () => ({
21
18
 
22
19
  const {name} = declaration.__a;
23
20
 
24
- return contains(__body, [
25
- `if (${__b.name}.hasOwnProperty(${name})) __body`,
26
- ]);
21
+ return contains(__body, [`if (${__b.name}.hasOwnProperty(${name})) __body`]);
27
22
  },
28
23
  });
29
24
 
@@ -36,4 +31,3 @@ module.exports.replace = () => ({
36
31
  return `for (const ${condition.__a.name} of Object.keys(${__b.name})) ${code}`;
37
32
  },
38
33
  });
39
-
@@ -5,6 +5,7 @@ const {
5
5
  types,
6
6
  template,
7
7
  } = require('putout');
8
+
8
9
  const {
9
10
  compare,
10
11
  getTemplateValues,
@@ -50,4 +51,3 @@ module.exports.replace = () => ({
50
51
  });
51
52
  },
52
53
  });
53
-
@@ -5,6 +5,7 @@ const {
5
5
  template,
6
6
  types,
7
7
  } = require('putout');
8
+
8
9
  const {
9
10
  compare,
10
11
  getTemplateValues,
@@ -42,10 +43,7 @@ module.exports.filter = (path) => {
42
43
 
43
44
  const {references} = path.scope.bindings[__i.name];
44
45
 
45
- if (references > 3)
46
- return false;
47
-
48
- return true;
46
+ return !(references > 3);
49
47
  };
50
48
 
51
49
  module.exports.replace = () => ({
@@ -72,4 +70,3 @@ function comparePrevSiblings(prev, node) {
72
70
  return true;
73
71
  }
74
72
  }
75
-
package/lib/index.js CHANGED
@@ -19,4 +19,3 @@ module.exports.rules = {
19
19
  ...getRule('remove-useless-array-from'),
20
20
  ...getRule('remove-unused-variables'),
21
21
  };
22
-
package/lib/map/index.js CHANGED
@@ -1,10 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- print,
5
- types,
6
- } = require('putout');
7
-
3
+ const {types} = require('putout');
8
4
  const {
9
5
  isReturnStatement,
10
6
  isBlockStatement,
@@ -28,19 +24,15 @@ module.exports.match = () => ({
28
24
  if (isTryStatement(last))
29
25
  return false;
30
26
 
31
- if (isReturnStatement(last))
32
- return false;
33
-
34
- return true;
27
+ return !isReturnStatement(last);
35
28
  },
36
29
  });
37
30
 
38
31
  module.exports.replace = () => ({
39
- '__a.map((__b) => __c)': ({__a, __b}) => {
40
- const a = print(__a);
41
- const b = print(__b);
32
+ '__a.map((__b) => __c)': (vars, path) => {
33
+ const a = path.get('callee.object');
34
+ const b = path.get('arguments.0.params.0');
42
35
 
43
36
  return `for (const ${b} of ${a}) __c`;
44
37
  },
45
38
  });
46
-
@@ -30,6 +30,7 @@ module.exports.match = () => ({
30
30
 
31
31
  module.exports.replace = () => ({
32
32
  'const __a = __b.reduce((__c, __d) => __e)': ({__a, __d}, path) => {
33
+ path.__putout_for_of_reduce = true;
33
34
  rename(path, __d, __a);
34
35
 
35
36
  return `{
@@ -40,6 +41,7 @@ module.exports.replace = () => ({
40
41
  }`;
41
42
  },
42
43
  'const __a = __b.reduceRight((__c, __d) => __e)': ({__a, __d}, path) => {
44
+ path.__putout_for_of_reduce = true;
43
45
  rename(path, __d, __a);
44
46
 
45
47
  return `{
@@ -49,8 +51,8 @@ module.exports.replace = () => ({
49
51
  }
50
52
  }`;
51
53
  },
52
-
53
- 'const __a = __b.reduce((__c, __d) => __e, __f)': () => {
54
+ 'const __a = __b.reduce((__c, __d) => __e, __f)': (vars, path) => {
55
+ path.__putout_for_of_reduce = true;
54
56
  return `{
55
57
  let __a = __f;
56
58
  for (const __d of __b) {
@@ -62,7 +64,8 @@ module.exports.replace = () => ({
62
64
 
63
65
  function rename(path, from, to) {
64
66
  const fnScope = path.get('declarations.0.init.arguments.0').scope;
67
+
65
68
  fnScope.rename(from.name, to.name);
69
+
66
70
  delete path.scope.bindings[from.name];
67
71
  }
68
-
@@ -1,9 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- types,
5
- operator,
6
- } = require('putout');
3
+ const {types, operator} = require('putout');
7
4
 
8
5
  const {
9
6
  isIdentifier,
@@ -34,10 +31,7 @@ function match(path) {
34
31
  const idPath = path.get('left.declarations.0.id');
35
32
  const elements = getElements(idPath);
36
33
 
37
- if (isAllReferenced(idPath, elements))
38
- return false;
39
-
40
- return true;
34
+ return !isAllReferenced(idPath, elements);
41
35
  }
42
36
 
43
37
  function replace(path) {
@@ -106,15 +100,14 @@ function isAllReferenced(path, nodes) {
106
100
  }
107
101
 
108
102
  function getElements({node}) {
109
- const {
110
- properties,
111
- elements,
112
- } = node;
103
+ const {properties, elements} = node;
113
104
 
114
105
  if (elements)
115
106
  return elements.filter(Boolean);
116
107
 
117
- return properties.map(getValue).filter(Boolean);
108
+ return properties
109
+ .map(getValue)
110
+ .filter(Boolean);
118
111
  }
119
112
 
120
113
  function getPathElements(path) {
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const {types, operator} = require('putout');
4
+
4
5
  const {isIdentifier} = types;
5
6
  const {replaceWith} = operator;
6
7
 
@@ -13,7 +14,6 @@ module.exports.match = () => ({
13
14
 
14
15
  return !references;
15
16
  },
16
-
17
17
  'for (const __a of __array) __c': ({__a, __array}, path) => {
18
18
  if (__array.elements.length >= 2)
19
19
  return false;
@@ -24,10 +24,7 @@ module.exports.match = () => ({
24
24
  const {name} = __a;
25
25
  const {references} = path.scope.bindings[name];
26
26
 
27
- if (references >= 2)
28
- return false;
29
-
30
- return true;
27
+ return !(references >= 2);
31
28
  },
32
29
  });
33
30
 
@@ -40,10 +37,8 @@ module.exports.replace = () => ({
40
37
  return null;
41
38
 
42
39
  const {name} = __a;
43
- const {
44
- references,
45
- referencePaths,
46
- } = path.scope.bindings[name];
40
+
41
+ const {references, referencePaths} = path.scope.bindings[name];
47
42
 
48
43
  if (!references)
49
44
  return replaceWith(path, __c);
@@ -57,4 +52,3 @@ module.exports.replace = () => ({
57
52
  return path;
58
53
  },
59
54
  });
60
-
@@ -5,4 +5,3 @@ module.exports.report = () => `'Array.from()' has no sense inside 'for...of'`;
5
5
  module.exports.replace = () => ({
6
6
  'for (const __a of Array.from(__b)) __c': 'for (const __a of __b) __c',
7
7
  });
8
-
@@ -1,16 +1,10 @@
1
1
  'use strict';
2
2
 
3
- const {
4
- types,
5
- operator,
6
- } = require('putout');
7
- const {
8
- replaceWith,
9
- remove,
10
- } = operator;
3
+ const {types, operator} = require('putout');
11
4
 
12
- const {isAssignmentPattern} = types;
5
+ const {replaceWith, remove} = operator;
13
6
 
7
+ const {isAssignmentPattern} = types;
14
8
  const isToManyProperties = (a, {maxProperties}) => a.isObjectPattern() && a.node.properties.length > maxProperties;
15
9
  const isAssignment = (a) => isAssignmentPattern(a.value);
16
10
 
@@ -30,21 +24,22 @@ module.exports.traverse = ({push, options}) => ({
30
24
  return;
31
25
 
32
26
  const {scope, node} = varPath;
27
+
33
28
  const {name} = node;
34
29
 
35
- const {
36
- references,
37
- referencePaths,
38
- } = scope.bindings[name];
30
+ const {references, referencePaths} = scope.bindings[name];
39
31
 
40
32
  if (references !== 1)
41
33
  return;
42
34
 
43
35
  const [referencePath] = referencePaths;
44
36
  const {parentPath} = referencePath;
37
+
45
38
  const isSameName = parentPath
46
39
  .get('init')
47
- .isIdentifier({name});
40
+ .isIdentifier({
41
+ name,
42
+ });
48
43
 
49
44
  if (!isSameName)
50
45
  return;
@@ -66,4 +61,3 @@ module.exports.traverse = ({push, options}) => ({
66
61
  });
67
62
  },
68
63
  });
69
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-for-of",
3
- "version": "1.2.3",
3
+ "version": "2.0.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",
@@ -36,20 +36,22 @@
36
36
  "@putout/plugin-convert-const-to-let": "*",
37
37
  "@putout/plugin-remove-empty": "*",
38
38
  "@putout/plugin-remove-nested-blocks": "*",
39
+ "@putout/plugin-remove-unreferenced-variables": "*",
39
40
  "@putout/plugin-remove-unused-variables": "*",
40
41
  "@putout/plugin-remove-useless-arguments": "*",
41
42
  "@putout/plugin-remove-useless-continue": "*",
42
- "@putout/test": "^6.0.0",
43
- "c8": "^7.5.0",
43
+ "@putout/test": "^7.0.0",
44
+ "c8": "^8.0.0",
44
45
  "eslint": "^8.0.1",
45
- "eslint-plugin-n": "^15.2.4",
46
- "eslint-plugin-putout": "^17.0.0",
46
+ "eslint-plugin-n": "^16.0.0",
47
+ "eslint-plugin-putout": "^18.0.0",
47
48
  "lerna": "^6.0.1",
48
49
  "madrun": "^9.0.0",
50
+ "montag": "^1.2.1",
49
51
  "nodemon": "^2.0.1"
50
52
  },
51
53
  "peerDependencies": {
52
- "putout": ">=29"
54
+ "putout": ">=30"
53
55
  },
54
56
  "license": "MIT",
55
57
  "engines": {