@putout/plugin-for-of 12.6.2 → 12.6.3
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
|
-
|
|
15
|
-
|
|
11
|
+
'-: body -> !BlockStatement',
|
|
12
|
+
'+: -> ForStatement',
|
|
16
13
|
]);
|
|
17
14
|
|
|
18
15
|
const isIndexUsedOften = createTypeChecker([
|
|
@@ -1,13 +1,36 @@
|
|
|
1
1
|
import {types, operator} from 'putout';
|
|
2
2
|
|
|
3
|
-
const {
|
|
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
|
|
10
|
-
const
|
|
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
|
|
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
|
|
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 (!
|
|
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 (
|
|
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