@putout/printer 12.8.0 → 12.10.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 +10 -0
- package/lib/tokenize/expressions/member-expression/is-looks-like-chain.js +65 -0
- package/lib/tokenize/expressions/member-expression/member-expressions.js +3 -71
- package/lib/tokenize/expressions/new-expression/new-expression.js +11 -2
- package/lib/tokenize/expressions/object-expression/object-expression.js +2 -2
- package/lib/tokenize/statements/block-statement/block-statement.js +7 -0
- package/lib/tokenize/statements/block-statement/is-call-inside-chain.js +42 -0
- package/lib/tokenize/statements/expression-statement/expression-statement.js +1 -0
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
2025.01.23, v12.10.0
|
|
2
|
+
|
|
3
|
+
feature:
|
|
4
|
+
- 262925b @putout/printer: BlockStatement: call inside chain
|
|
5
|
+
|
|
6
|
+
2025.01.23, v12.9.0
|
|
7
|
+
|
|
8
|
+
feature:
|
|
9
|
+
- 750b6cd @putout/printer: NewExpression: round braces (coderiaser/minify#135)
|
|
10
|
+
|
|
1
11
|
2025.01.22, v12.8.0
|
|
2
12
|
|
|
3
13
|
feature:
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {types} = require('@putout/babel');
|
|
4
|
+
const {chain} = require('./chain');
|
|
5
|
+
const {satisfy} = require('../../is');
|
|
6
|
+
|
|
7
|
+
const {
|
|
8
|
+
isUnaryExpression,
|
|
9
|
+
isIfStatement,
|
|
10
|
+
isCallExpression,
|
|
11
|
+
} = types;
|
|
12
|
+
|
|
13
|
+
const isArgOfCall = (path) => path.parentPath.isCallExpression() && path.parentPath.get('arguments.0') === path;
|
|
14
|
+
const isCall = (a) => a.type === 'CallExpression';
|
|
15
|
+
|
|
16
|
+
const isExcludedFromChain = satisfy([isUnaryExpression, isIfStatement]);
|
|
17
|
+
|
|
18
|
+
module.exports.isLooksLikeChain = (path) => {
|
|
19
|
+
const [root, properties] = chain(path);
|
|
20
|
+
|
|
21
|
+
if (isExcludedFromChain(root))
|
|
22
|
+
return false;
|
|
23
|
+
|
|
24
|
+
if (properties.find(isPathGet))
|
|
25
|
+
return false;
|
|
26
|
+
|
|
27
|
+
if (path.find(isIfUp))
|
|
28
|
+
return false;
|
|
29
|
+
|
|
30
|
+
const calls = properties.filter(isCall);
|
|
31
|
+
const [firstCall] = calls;
|
|
32
|
+
|
|
33
|
+
if (calls.length === 2 && !firstCall.name)
|
|
34
|
+
return false;
|
|
35
|
+
|
|
36
|
+
if (isArgOfCall(path))
|
|
37
|
+
return false;
|
|
38
|
+
|
|
39
|
+
return calls.length > 1;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const isPathGet = (property) => {
|
|
43
|
+
return isCallExpression(property, {
|
|
44
|
+
name: 'get',
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const isIfUp = (path) => {
|
|
49
|
+
const ifPath = path.find(isIfStatement);
|
|
50
|
+
let is = false;
|
|
51
|
+
|
|
52
|
+
if (!ifPath)
|
|
53
|
+
return is;
|
|
54
|
+
|
|
55
|
+
ifPath.get('test').traverse({
|
|
56
|
+
MemberExpression(currentPath) {
|
|
57
|
+
if (path === currentPath) {
|
|
58
|
+
is = true;
|
|
59
|
+
path.stop();
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
return is;
|
|
65
|
+
};
|
|
@@ -1,20 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {types} = require('@putout/babel');
|
|
4
|
-
|
|
5
|
-
const {chain} = require('./chain');
|
|
6
|
-
const {satisfy} = require('../../is');
|
|
7
|
-
|
|
8
3
|
const {maybePrintComputed} = require('../object-expression/maybe-print-computed');
|
|
9
4
|
const {maybeParens} = require('../../maybe/maybe-parens');
|
|
10
|
-
|
|
11
|
-
const {
|
|
12
|
-
isUnaryExpression,
|
|
13
|
-
isIfStatement,
|
|
14
|
-
isCallExpression,
|
|
15
|
-
} = types;
|
|
16
|
-
|
|
17
|
-
const isArgOfCall = (path) => path.parentPath.isCallExpression() && path.parentPath.get('arguments.0') === path;
|
|
5
|
+
const {isLooksLikeChain} = require('./is-looks-like-chain');
|
|
18
6
|
|
|
19
7
|
module.exports.MemberExpression = (path, printer) => {
|
|
20
8
|
const {
|
|
@@ -35,12 +23,10 @@ module.exports.MemberExpression = (path, printer) => {
|
|
|
35
23
|
if (computed)
|
|
36
24
|
return maybePrintComputed(path, property, printer);
|
|
37
25
|
|
|
38
|
-
const isChain =
|
|
26
|
+
const isChain = isLooksLikeChain(path);
|
|
39
27
|
|
|
40
28
|
maybe.indent.inc(isChain);
|
|
41
|
-
|
|
42
|
-
if (isChain)
|
|
43
|
-
print.breakline();
|
|
29
|
+
maybe.print.breakline(isChain);
|
|
44
30
|
|
|
45
31
|
print('.');
|
|
46
32
|
print('__property');
|
|
@@ -65,57 +51,3 @@ module.exports.OptionalMemberExpression = maybeParens((path, {print, maybe}) =>
|
|
|
65
51
|
|
|
66
52
|
print('__property');
|
|
67
53
|
});
|
|
68
|
-
|
|
69
|
-
const isCall = (a) => a.type === 'CallExpression';
|
|
70
|
-
|
|
71
|
-
const isPathGet = (property) => {
|
|
72
|
-
return isCallExpression(property, {
|
|
73
|
-
name: 'get',
|
|
74
|
-
});
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const isExcludedFromChain = satisfy([isUnaryExpression, isIfStatement]);
|
|
78
|
-
|
|
79
|
-
const isIfUp = (path) => {
|
|
80
|
-
const ifPath = path.find(isIfStatement);
|
|
81
|
-
let is = false;
|
|
82
|
-
|
|
83
|
-
if (!ifPath)
|
|
84
|
-
return is;
|
|
85
|
-
|
|
86
|
-
ifPath.get('test').traverse({
|
|
87
|
-
MemberExpression(currentPath) {
|
|
88
|
-
if (path === currentPath) {
|
|
89
|
-
is = true;
|
|
90
|
-
path.stop();
|
|
91
|
-
}
|
|
92
|
-
},
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
return is;
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
module.exports.likeChain = likeChain;
|
|
99
|
-
function likeChain(path) {
|
|
100
|
-
const [root, properties] = chain(path);
|
|
101
|
-
|
|
102
|
-
if (isExcludedFromChain(root))
|
|
103
|
-
return false;
|
|
104
|
-
|
|
105
|
-
if (properties.find(isPathGet))
|
|
106
|
-
return false;
|
|
107
|
-
|
|
108
|
-
if (path.find(isIfUp))
|
|
109
|
-
return false;
|
|
110
|
-
|
|
111
|
-
const calls = properties.filter(isCall);
|
|
112
|
-
const [firstCall] = calls;
|
|
113
|
-
|
|
114
|
-
if (calls.length === 2 && !firstCall.name)
|
|
115
|
-
return false;
|
|
116
|
-
|
|
117
|
-
if (isArgOfCall(path))
|
|
118
|
-
return false;
|
|
119
|
-
|
|
120
|
-
return calls.length > 1;
|
|
121
|
-
}
|
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const {types} = require('@putout/babel');
|
|
3
4
|
const {exists} = require('../../is');
|
|
4
5
|
const {isMarkedAfter} = require('../../mark');
|
|
5
|
-
|
|
6
|
+
|
|
7
|
+
const {
|
|
8
|
+
isExpressionStatement,
|
|
9
|
+
isMemberExpression,
|
|
10
|
+
} = types;
|
|
11
|
+
|
|
12
|
+
const isInsideExpressionStatement = ({parentPath}) => isExpressionStatement(parentPath);
|
|
6
13
|
const notFirst = ({parentPath}) => exists(parentPath.getPrevSibling());
|
|
14
|
+
const isInsideMember = ({parentPath}) => isMemberExpression(parentPath);
|
|
7
15
|
|
|
8
16
|
const getPrev = ({parentPath}) => {
|
|
9
17
|
const prev = parentPath.getPrevSibling();
|
|
@@ -66,8 +74,9 @@ function maybePrintCloseBrace(path, printer, semantics) {
|
|
|
66
74
|
function maybePrintBrace(brace, path, printer, semantics) {
|
|
67
75
|
const {maybe, print} = printer;
|
|
68
76
|
const {roundBraces} = semantics;
|
|
77
|
+
const {length} = path.node.arguments;
|
|
69
78
|
|
|
70
|
-
if (path
|
|
79
|
+
if (length || isInsideMember(path))
|
|
71
80
|
return print(brace);
|
|
72
81
|
|
|
73
82
|
maybe.print(roundBraces.new, brace);
|
|
@@ -13,8 +13,8 @@ const {
|
|
|
13
13
|
} = require('../../is');
|
|
14
14
|
|
|
15
15
|
const {parseComments} = require('../../comment/comment');
|
|
16
|
-
const {likeChain} = require('../member-expression/member-expressions');
|
|
17
16
|
const {isInsideTuple} = require('./is-inside-tuple');
|
|
17
|
+
const {isLooksLikeChain} = require('../member-expression/is-looks-like-chain');
|
|
18
18
|
|
|
19
19
|
const {
|
|
20
20
|
isStringLiteral,
|
|
@@ -35,7 +35,7 @@ const isMemberExpressionCallee = ({parentPath}) => {
|
|
|
35
35
|
if (!callee.isMemberExpression())
|
|
36
36
|
return false;
|
|
37
37
|
|
|
38
|
-
return
|
|
38
|
+
return isLooksLikeChain(callee);
|
|
39
39
|
};
|
|
40
40
|
|
|
41
41
|
const isInsideCall = ({parentPath}) => parentPath.isCallExpression();
|
|
@@ -14,6 +14,8 @@ const {parseComments} = require('../../comment/comment');
|
|
|
14
14
|
const {insideIfWithNoBody} = require('./inside-if-with-no-body');
|
|
15
15
|
const {getDirectives} = require('./get-directives');
|
|
16
16
|
|
|
17
|
+
const {isCallInsideChain} = require('./is-call-inside-chain');
|
|
18
|
+
|
|
17
19
|
const {
|
|
18
20
|
isArrowFunctionExpression,
|
|
19
21
|
isObjectMethod,
|
|
@@ -60,6 +62,9 @@ module.exports.BlockStatement = {
|
|
|
60
62
|
}
|
|
61
63
|
|
|
62
64
|
maybe.write.linebreak(directives.length && body.length);
|
|
65
|
+
const callInsideChain = isCallInsideChain(path);
|
|
66
|
+
|
|
67
|
+
maybe.indent.inc(callInsideChain);
|
|
63
68
|
|
|
64
69
|
for (const element of body) {
|
|
65
70
|
traverse(element);
|
|
@@ -71,6 +76,8 @@ module.exports.BlockStatement = {
|
|
|
71
76
|
maybe.indent(body.length);
|
|
72
77
|
write('}');
|
|
73
78
|
|
|
79
|
+
maybe.indent.dec(callInsideChain);
|
|
80
|
+
|
|
74
81
|
if (path.parentPath.isObjectMethod())
|
|
75
82
|
write(',');
|
|
76
83
|
},
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {types} = require('@putout/babel');
|
|
4
|
+
const {isLooksLikeChain} = require('../../expressions/member-expression/is-looks-like-chain');
|
|
5
|
+
const {
|
|
6
|
+
isExpressionStatement,
|
|
7
|
+
isMemberExpression,
|
|
8
|
+
isCallExpression,
|
|
9
|
+
} = types;
|
|
10
|
+
|
|
11
|
+
module.exports.isCallInsideChain = (path) => {
|
|
12
|
+
if (!isCallExpression(path.parentPath.parentPath))
|
|
13
|
+
return false;
|
|
14
|
+
|
|
15
|
+
const member = path.find(isTopMemberInsideCall);
|
|
16
|
+
|
|
17
|
+
if (member)
|
|
18
|
+
return isLooksLikeChain(member);
|
|
19
|
+
|
|
20
|
+
const callPath = path.find(isTopCall);
|
|
21
|
+
|
|
22
|
+
if (!callPath)
|
|
23
|
+
return false;
|
|
24
|
+
|
|
25
|
+
const calleeMember = callPath.get('callee');
|
|
26
|
+
|
|
27
|
+
return isLooksLikeChain(calleeMember);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function isTopMemberInsideCall(path) {
|
|
31
|
+
if (!isMemberExpression(path))
|
|
32
|
+
return false;
|
|
33
|
+
|
|
34
|
+
return isExpressionStatement(path.parentPath.parentPath);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function isTopCall(path) {
|
|
38
|
+
if (!isCallExpression(path))
|
|
39
|
+
return false;
|
|
40
|
+
|
|
41
|
+
return isExpressionStatement(path.parentPath);
|
|
42
|
+
}
|
package/package.json
CHANGED