@putout/plugin-for-of 12.6.2 → 12.6.4

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.
@@ -1,7 +1,4 @@
1
- import {
2
- operator,
3
- template,
4
- } from 'putout';
1
+ import {operator, template} from 'putout';
5
2
 
6
3
  const {
7
4
  compare,
@@ -11,8 +8,8 @@ const {
11
8
  } = operator;
12
9
 
13
10
  const isForWithBlock = createTypeChecker([
14
- ['-: body -> !BlockStatement'],
15
- ['+: -> ForStatement'],
11
+ '-: body -> !BlockStatement',
12
+ '+: -> ForStatement',
16
13
  ]);
17
14
 
18
15
  const isIndexUsedOften = createTypeChecker([
package/lib/map/index.js CHANGED
@@ -12,6 +12,10 @@ const {
12
12
  isTryStatement,
13
13
  isArrayPattern,
14
14
  isCallExpression,
15
+ blockStatement,
16
+ continueStatement,
17
+ expressionStatement,
18
+ identifier,
15
19
  } = types;
16
20
 
17
21
  const createPush = template('%%ARRAY%%.push(%%ARGUMENT%%)');
@@ -61,9 +65,7 @@ export const replace = () => ({
61
65
  },
62
66
  'const __a = __b.map((__c) => __d)': ({__a, __d}, path) => {
63
67
  if (isBlockStatement(__d)) {
64
- path.traverse({
65
- ReturnStatement: createReplaceWithPush(__a),
66
- });
68
+ path.traverse(createVisitor(__a, __d));
67
69
  return `{
68
70
  const __a = [];
69
71
  for (const __c of __b) __d;
@@ -79,9 +81,7 @@ export const replace = () => ({
79
81
  },
80
82
  '__a = __b.map((__c) => __d)': ({__a, __d}, path) => {
81
83
  if (isBlockStatement(__d)) {
82
- path.traverse({
83
- ReturnStatement: createReplaceWithPush(__a),
84
- });
84
+ path.traverse(createVisitor(__a, __d));
85
85
  return `{
86
86
  __a = [];
87
87
  for (const __c of __b) __d;
@@ -97,11 +97,37 @@ export const replace = () => ({
97
97
  },
98
98
  });
99
99
 
100
- const createReplaceWithPush = (__a) => (path) => {
101
- const {argument} = path.node;
100
+ const isTail = (path, body) => {
101
+ if (path.parentPath.node !== body)
102
+ return false;
102
103
 
103
- replaceWith(path, createPush({
104
+ return tail(body.body) === path.node;
105
+ };
106
+
107
+ const skipNested = (body) => (path) => {
108
+ if (path.node.body !== body)
109
+ path.skip();
110
+ };
111
+
112
+ const createVisitor = (__a, body) => ({
113
+ Function: skipNested(body),
114
+ ReturnStatement: createReplaceWithPush(__a, body),
115
+ });
116
+
117
+ const createReplaceWithPush = (__a, body) => (path) => {
118
+ const argument = path.node.argument || identifier('undefined');
119
+ const push = createPush({
104
120
  ARRAY: __a,
105
121
  ARGUMENT: argument,
106
- }));
122
+ });
123
+
124
+ if (isTail(path, body)) {
125
+ replaceWith(path, push);
126
+ return;
127
+ }
128
+
129
+ replaceWith(path, blockStatement([
130
+ expressionStatement(push),
131
+ continueStatement(),
132
+ ]));
107
133
  };
@@ -1,13 +1,36 @@
1
1
  import {types, operator} from 'putout';
2
2
 
3
- const {replaceWith, remove} = operator;
3
+ const {
4
+ replaceWith,
5
+ remove,
6
+ createTypeChecker,
7
+ } = operator;
8
+
4
9
  const {
5
10
  isAssignmentPattern,
6
11
  isObjectPattern,
7
12
  } = types;
8
13
 
9
- const isToManyProperties = (a, {maxProperties}) => a.isObjectPattern() && a.node.properties.length > maxProperties;
10
- const isAssignment = (a) => isAssignmentPattern(a.value);
14
+ const isAssignment = ({value}) => isAssignmentPattern(value);
15
+ const isNestedProperty = ({value}) => isObjectPattern(value);
16
+
17
+ const isToManyProperties = ({node}, {maxProperties}) => node.properties.length > maxProperties;
18
+ const isNested = ({node}) => node.properties.some(isNestedProperty);
19
+ const hasAssignment = ({node}) => node.properties.some(isAssignment);
20
+
21
+ const isSupportedBinding = createTypeChecker([
22
+ ['-: references', '!=', 1],
23
+ ['-: referencePaths.0.parentPath -> !VariableDeclarator'],
24
+ ['+: -> +'],
25
+ ]);
26
+
27
+ const isSupportedId = createTypeChecker([
28
+ ['+: -> !ObjectPattern'],
29
+ ['-', isToManyProperties],
30
+ ['-', isNested],
31
+ ['-', hasAssignment],
32
+ ['+: -> ObjectPattern'],
33
+ ]);
11
34
 
12
35
  export const report = () => `Use destructuring in head of 'for..of'`;
13
36
 
@@ -18,37 +41,20 @@ export const fix = ({path, varPath}) => {
18
41
 
19
42
  export const traverse = ({push, options}) => ({
20
43
  'for (const __ of __) __'(path) {
21
- const leftPath = path.get('left');
22
- const varPath = leftPath.get('declarations.0.id');
44
+ const varPath = path.get('left.declarations.0.id');
23
45
 
24
46
  if (!varPath.isIdentifier())
25
47
  return;
26
48
 
27
- const {scope, node} = varPath;
28
- const {name} = node;
29
- const {references, referencePaths} = scope.bindings[name];
30
-
31
- if (references !== 1)
32
- return;
33
-
34
- const [referencePath] = referencePaths;
35
- const {parentPath} = referencePath;
49
+ const binding = varPath.scope.bindings[varPath.node.name];
36
50
 
37
- if (!parentPath.isVariableDeclarator())
51
+ if (!isSupportedBinding(binding))
38
52
  return;
39
53
 
40
- const idPath = parentPath.get('id');
41
54
  const {maxProperties = 4} = options;
55
+ const {parentPath} = binding.referencePaths[0];
42
56
 
43
- if (isToManyProperties(idPath, {maxProperties}))
44
- return;
45
-
46
- if (isNested(idPath))
47
- return;
48
-
49
- const {properties} = idPath.node;
50
-
51
- if (idPath.isObjectPattern() && properties.find(isAssignment))
57
+ if (!isSupportedId(parentPath.get('id'), {maxProperties}))
52
58
  return;
53
59
 
54
60
  push({
@@ -57,15 +63,3 @@ export const traverse = ({push, options}) => ({
57
63
  });
58
64
  },
59
65
  });
60
-
61
- function isNested(path) {
62
- if (!path.isObjectPattern())
63
- return false;
64
-
65
- for (const {value} of path.node.properties) {
66
- if (isObjectPattern(value))
67
- return true;
68
- }
69
-
70
- return false;
71
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-for-of",
3
- "version": "12.6.2",
3
+ "version": "12.6.4",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin adds ability to apply for...of",