@putout/plugin-for-of 12.6.5 → 12.7.1
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-each/index.js
CHANGED
|
@@ -5,13 +5,14 @@ import {
|
|
|
5
5
|
} from 'putout';
|
|
6
6
|
|
|
7
7
|
const {
|
|
8
|
+
isFunction,
|
|
8
9
|
isIdentifier,
|
|
9
10
|
continueStatement,
|
|
10
|
-
isReturnStatement,
|
|
11
11
|
} = types;
|
|
12
12
|
|
|
13
13
|
const {
|
|
14
14
|
compare,
|
|
15
|
+
createTypeChecker,
|
|
15
16
|
getTemplateValues,
|
|
16
17
|
replaceWith,
|
|
17
18
|
replaceWithMultiple,
|
|
@@ -29,7 +30,19 @@ const forOfEntriesTemplate = template(`
|
|
|
29
30
|
`);
|
|
30
31
|
|
|
31
32
|
const {keys} = Object;
|
|
32
|
-
|
|
33
|
+
|
|
34
|
+
const isRoot = createTypeChecker([
|
|
35
|
+
['+', isFunction],
|
|
36
|
+
['+: -> Program'],
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
const isSupportedParent = createTypeChecker([
|
|
40
|
+
'-: parentPath -> ReturnStatement',
|
|
41
|
+
'-: parentPath -> SequenceExpression',
|
|
42
|
+
'-: parentPath -> ConditionalExpression',
|
|
43
|
+
'-: parentPath -> VariableDeclarator',
|
|
44
|
+
'+: -> +',
|
|
45
|
+
]);
|
|
33
46
|
|
|
34
47
|
export const report = () => `Use 'for-of' instead of 'forEach()'`;
|
|
35
48
|
|
|
@@ -93,18 +106,7 @@ export const fix = (path) => {
|
|
|
93
106
|
export const traverse = ({push}) => ({
|
|
94
107
|
'__.forEach.call(__a, (__b) => __body)': push,
|
|
95
108
|
'__.forEach(__args)': (path) => {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (isReturnStatement(parentPath))
|
|
99
|
-
return;
|
|
100
|
-
|
|
101
|
-
if (parentPath.isSequenceExpression())
|
|
102
|
-
return;
|
|
103
|
-
|
|
104
|
-
if (parentPath.isConditionalExpression())
|
|
105
|
-
return;
|
|
106
|
-
|
|
107
|
-
if (parentPath.isVariableDeclarator())
|
|
109
|
+
if (!isSupportedParent(path))
|
|
108
110
|
return;
|
|
109
111
|
|
|
110
112
|
const objectPath = path.get('callee.object');
|
|
@@ -5,7 +5,11 @@ const {
|
|
|
5
5
|
isObjectProperty,
|
|
6
6
|
} = types;
|
|
7
7
|
|
|
8
|
-
const {
|
|
8
|
+
const {
|
|
9
|
+
remove,
|
|
10
|
+
createTypeChecker,
|
|
11
|
+
} = operator;
|
|
12
|
+
|
|
9
13
|
const getValue = ({value}) => value;
|
|
10
14
|
|
|
11
15
|
export const report = (path) => {
|
|
@@ -29,9 +33,24 @@ function matchForOf(vars, path) {
|
|
|
29
33
|
const idPath = path.get('left.declarations.0.id');
|
|
30
34
|
const elements = getElements(idPath);
|
|
31
35
|
|
|
32
|
-
|
|
36
|
+
if (isAllReferenced(idPath, elements))
|
|
37
|
+
return false;
|
|
38
|
+
|
|
39
|
+
return isSupportedForOf(idPath, {
|
|
40
|
+
variables: getVariables(path),
|
|
41
|
+
});
|
|
33
42
|
}
|
|
34
43
|
|
|
44
|
+
const hasOneProperty = ({node}) => node.properties.length === 1;
|
|
45
|
+
const isFullyUnreferenced = ({node}, {variables}) => variables.length === node.properties.length;
|
|
46
|
+
|
|
47
|
+
const isSupportedForOf = createTypeChecker([
|
|
48
|
+
['+: -> !ObjectPattern'],
|
|
49
|
+
['+', hasOneProperty],
|
|
50
|
+
['-', isFullyUnreferenced],
|
|
51
|
+
['+: -> ObjectPattern'],
|
|
52
|
+
]);
|
|
53
|
+
|
|
35
54
|
function replaceForOf(vars, path) {
|
|
36
55
|
const variables = getVariables(path);
|
|
37
56
|
|
package/package.json
CHANGED