@putout/plugin-for-of 12.6.1 → 12.6.2
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/lib/for-entries-n/index.js +19 -18
- package/package.json +1 -1
|
@@ -1,16 +1,29 @@
|
|
|
1
1
|
import {
|
|
2
2
|
operator,
|
|
3
3
|
template,
|
|
4
|
-
types,
|
|
5
4
|
} from 'putout';
|
|
6
5
|
|
|
7
6
|
const {
|
|
8
7
|
compare,
|
|
9
8
|
getTemplateValues,
|
|
10
9
|
getBinding,
|
|
10
|
+
createTypeChecker,
|
|
11
11
|
} = operator;
|
|
12
12
|
|
|
13
|
-
const
|
|
13
|
+
const isForWithBlock = createTypeChecker([
|
|
14
|
+
['-: body -> !BlockStatement'],
|
|
15
|
+
['+: -> ForStatement'],
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
const isIndexUsedOften = createTypeChecker([
|
|
19
|
+
['-: constantViolations.length', '>', 1],
|
|
20
|
+
['+: references', '>', 3],
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
const isUsedOnce = createTypeChecker([
|
|
24
|
+
['-: references', '>', 1],
|
|
25
|
+
['+: path -> VariableDeclarator'],
|
|
26
|
+
]);
|
|
14
27
|
|
|
15
28
|
export const report = () => `Use 'for..of' instead of 'for'`;
|
|
16
29
|
|
|
@@ -26,20 +39,13 @@ export const filter = (path) => {
|
|
|
26
39
|
if (!prevPath.node)
|
|
27
40
|
return false;
|
|
28
41
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (!isBlockStatement(body))
|
|
42
|
+
if (!isForWithBlock(node))
|
|
32
43
|
return false;
|
|
33
44
|
|
|
34
|
-
const [first] = body.body;
|
|
45
|
+
const [first] = node.body.body;
|
|
35
46
|
const {__i, __n} = getTemplateValues(path, forLoopToN);
|
|
36
47
|
|
|
37
|
-
|
|
38
|
-
constantViolations,
|
|
39
|
-
references,
|
|
40
|
-
} = path.scope.bindings[__i.name];
|
|
41
|
-
|
|
42
|
-
if (constantViolations.length > 1)
|
|
48
|
+
if (!isIndexUsedOften(path.scope.bindings[__i.name]))
|
|
43
49
|
return false;
|
|
44
50
|
|
|
45
51
|
if (!compare(first, assignIterable(__i)))
|
|
@@ -48,12 +54,7 @@ export const filter = (path) => {
|
|
|
48
54
|
if (!comparePrevSiblings(path, assignN(__n)))
|
|
49
55
|
return false;
|
|
50
56
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (nBinding.references > 1)
|
|
54
|
-
return false;
|
|
55
|
-
|
|
56
|
-
return references > 3;
|
|
57
|
+
return isUsedOnce(getBinding(path, __n.name));
|
|
57
58
|
};
|
|
58
59
|
|
|
59
60
|
export const replace = () => ({
|
package/package.json
CHANGED