@putout/printer 18.37.1 → 18.38.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/ChangeLog
CHANGED
|
@@ -20,6 +20,12 @@ export const chain = (path) => {
|
|
|
20
20
|
];
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
+
function isPartOfSameChain(current) {
|
|
24
|
+
const {operator} = current.node;
|
|
25
|
+
|
|
26
|
+
return operator === '||' || operator === '&&';
|
|
27
|
+
}
|
|
28
|
+
|
|
23
29
|
function down(current) {
|
|
24
30
|
let count = 0;
|
|
25
31
|
|
|
@@ -32,9 +38,7 @@ function down(current) {
|
|
|
32
38
|
|
|
33
39
|
current = current.get('left');
|
|
34
40
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (operator !== '||' && operator !== '&&')
|
|
41
|
+
if (!isPartOfSameChain(current))
|
|
38
42
|
break;
|
|
39
43
|
} while (current.isLogicalExpression());
|
|
40
44
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {types} from '@putout/babel';
|
|
2
|
+
import {createTypeChecker} from '#type-checker';
|
|
2
3
|
import {hasTrailingComment} from '#is';
|
|
3
4
|
|
|
4
5
|
const {
|
|
@@ -8,21 +9,13 @@ const {
|
|
|
8
9
|
|
|
9
10
|
const {assign} = Object;
|
|
10
11
|
|
|
11
|
-
const isCallee = (call, member) => {
|
|
12
|
-
if (!isCallExpression(call))
|
|
13
|
-
return false;
|
|
14
|
-
|
|
15
|
-
return call.node.callee === member.node;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
12
|
export const chain = (path) => {
|
|
19
|
-
const
|
|
13
|
+
const properties = [
|
|
20
14
|
...down(path),
|
|
21
15
|
...up(path),
|
|
22
16
|
];
|
|
23
17
|
|
|
24
|
-
const
|
|
25
|
-
const root = all.at(-1);
|
|
18
|
+
const root = properties.pop();
|
|
26
19
|
|
|
27
20
|
return [root, properties];
|
|
28
21
|
};
|
|
@@ -33,13 +26,8 @@ function down(path) {
|
|
|
33
26
|
let current = path.get('object');
|
|
34
27
|
|
|
35
28
|
while (!current.isIdentifier()) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
if (isFn)
|
|
40
|
-
current = current.get('callee');
|
|
41
|
-
|
|
42
|
-
properties.unshift(prop);
|
|
29
|
+
properties.unshift(buildProperty(current));
|
|
30
|
+
current = stepIntoCallee(current);
|
|
43
31
|
|
|
44
32
|
if (!current.isMemberExpression())
|
|
45
33
|
break;
|
|
@@ -50,38 +38,54 @@ function down(path) {
|
|
|
50
38
|
return properties;
|
|
51
39
|
}
|
|
52
40
|
|
|
41
|
+
function stepIntoCallee(current) {
|
|
42
|
+
if (current.isCallExpression())
|
|
43
|
+
return current.get('callee');
|
|
44
|
+
|
|
45
|
+
return current;
|
|
46
|
+
}
|
|
47
|
+
|
|
53
48
|
function up(current) {
|
|
54
49
|
const properties = [];
|
|
55
50
|
|
|
56
51
|
while (current.isMemberExpression()) {
|
|
57
52
|
if (hasTrailingComment(current.get('object')))
|
|
58
|
-
properties.push(
|
|
59
|
-
type: 'CommentLine',
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
const member = current;
|
|
63
|
-
current = current.parentPath;
|
|
53
|
+
properties.push(commentProperty());
|
|
64
54
|
|
|
65
|
-
|
|
66
|
-
properties.push(build(current));
|
|
67
|
-
current = current.parentPath;
|
|
68
|
-
}
|
|
55
|
+
current = stepOutOfChain(current, properties);
|
|
69
56
|
|
|
70
57
|
if (!isMemberExpression(current))
|
|
71
58
|
break;
|
|
72
59
|
}
|
|
73
60
|
|
|
74
|
-
properties.push(
|
|
75
|
-
type: current.type,
|
|
76
|
-
});
|
|
61
|
+
properties.push(rootProperty(current));
|
|
77
62
|
|
|
78
63
|
return properties;
|
|
79
64
|
}
|
|
80
65
|
|
|
81
|
-
function
|
|
82
|
-
const
|
|
66
|
+
function stepOutOfChain(member, properties) {
|
|
67
|
+
const parent = member.parentPath;
|
|
68
|
+
|
|
69
|
+
if (!isMemberOwnCallee(member))
|
|
70
|
+
return parent;
|
|
71
|
+
|
|
72
|
+
properties.push(buildProperty(parent));
|
|
73
|
+
|
|
74
|
+
return parent.parentPath;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function isPathOwnCallee({node, parentPath}) {
|
|
78
|
+
return node === parentPath.node.callee;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const isMemberOwnCallee = createTypeChecker([
|
|
82
|
+
['-: parentPath -> !CallExpression'],
|
|
83
|
+
['+', isPathOwnCallee],
|
|
84
|
+
]);
|
|
85
|
+
|
|
86
|
+
function buildProperty(path) {
|
|
83
87
|
const prop = {
|
|
84
|
-
type,
|
|
88
|
+
type: path.type,
|
|
85
89
|
};
|
|
86
90
|
|
|
87
91
|
if (isCallExpression(path)) {
|
|
@@ -100,3 +104,10 @@ function build(path) {
|
|
|
100
104
|
return prop;
|
|
101
105
|
}
|
|
102
106
|
|
|
107
|
+
const commentProperty = () => ({
|
|
108
|
+
type: 'CommentLine',
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const rootProperty = (path) => ({
|
|
112
|
+
type: path.type,
|
|
113
|
+
});
|
|
@@ -6,6 +6,7 @@ const {isFunction} = types;
|
|
|
6
6
|
export const isNeedIndent = createTypeChecker([
|
|
7
7
|
['-', hasComplexAttribute],
|
|
8
8
|
['+: parentPath -> JSXElement'],
|
|
9
|
+
['+: parentPath -> CallExpression'],
|
|
9
10
|
['-: parentPath.parentPath -> !CallExpression'],
|
|
10
11
|
['+: parentPath -> ArrowFunctionExpression'],
|
|
11
12
|
]);
|
|
@@ -17,6 +17,7 @@ const condition = createTypeChecker([
|
|
|
17
17
|
const {
|
|
18
18
|
isJSXElement,
|
|
19
19
|
isArrowFunctionExpression,
|
|
20
|
+
isCallExpression,
|
|
20
21
|
} = types;
|
|
21
22
|
|
|
22
23
|
const isInsideArrow = ({parentPath}) => isArrowFunctionExpression(parentPath);
|
|
@@ -36,15 +37,19 @@ export const JSXElement = {
|
|
|
36
37
|
}
|
|
37
38
|
},
|
|
38
39
|
print(path, {print, traverse, maybe}) {
|
|
40
|
+
const {parentPath} = path;
|
|
39
41
|
const needIndent = isNeedIndent(path);
|
|
42
|
+
const parentCall = isCallExpression(parentPath);
|
|
43
|
+
|
|
40
44
|
maybe.indent.inc(needIndent);
|
|
45
|
+
maybe.print.breakline(parentCall);
|
|
41
46
|
|
|
42
47
|
print('__openingElement');
|
|
43
48
|
path.get('children').map(traverse);
|
|
44
|
-
|
|
45
49
|
print('__closingElement');
|
|
46
50
|
|
|
47
51
|
maybe.indent.dec(needIndent);
|
|
52
|
+
maybe.print.breakline(parentCall);
|
|
48
53
|
},
|
|
49
54
|
after(path, {write, indent, maybe}) {
|
|
50
55
|
const {leadingComments} = path.node;
|
|
@@ -61,3 +66,4 @@ export const JSXElement = {
|
|
|
61
66
|
maybe.write(!leadingComments?.length, ')');
|
|
62
67
|
},
|
|
63
68
|
};
|
|
69
|
+
|
package/package.json
CHANGED