@putout/printer 12.18.0 → 12.19.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/ChangeLog +10 -0
- package/lib/tokenize/comment/parse-trailing-comments.js +16 -6
- package/lib/tokenize/expressions/member-expression/chain.js +6 -0
- package/lib/tokenize/expressions/member-expression/is-looks-like-chain.js +5 -1
- package/lib/tokenize/expressions/member-expression/member-expressions.js +2 -2
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -8,6 +8,8 @@ const {
|
|
|
8
8
|
isNext,
|
|
9
9
|
} = require('../is');
|
|
10
10
|
|
|
11
|
+
const {isLooksLikeChain} = require('../expressions/member-expression/is-looks-like-chain');
|
|
12
|
+
|
|
11
13
|
const {
|
|
12
14
|
isDecorator,
|
|
13
15
|
isMemberExpression,
|
|
@@ -47,15 +49,12 @@ const isNewlineAfter = (path) => {
|
|
|
47
49
|
module.exports.isTrailingIsLeading = isTrailingIsLeading;
|
|
48
50
|
|
|
49
51
|
function isCommentOnNextLine(path) {
|
|
50
|
-
const {node
|
|
52
|
+
const {node} = path;
|
|
51
53
|
const {
|
|
52
54
|
loc,
|
|
53
55
|
trailingComments,
|
|
54
56
|
} = node;
|
|
55
57
|
|
|
56
|
-
if (parentPath.isMemberExpression())
|
|
57
|
-
return false;
|
|
58
|
-
|
|
59
58
|
if (path.isClassMethod())
|
|
60
59
|
return false;
|
|
61
60
|
|
|
@@ -84,7 +83,9 @@ function isCommentOnNextLine(path) {
|
|
|
84
83
|
return isNextLine || isNextLineAfterNewline;
|
|
85
84
|
}
|
|
86
85
|
|
|
87
|
-
module.exports.parseTrailingComments = (path, {write, maybe}, semantics) => {
|
|
86
|
+
module.exports.parseTrailingComments = (path, {write, maybe, indent}, semantics) => {
|
|
87
|
+
const {parentPath} = path;
|
|
88
|
+
|
|
88
89
|
if (!semantics.comments)
|
|
89
90
|
return;
|
|
90
91
|
|
|
@@ -97,15 +98,24 @@ module.exports.parseTrailingComments = (path, {write, maybe}, semantics) => {
|
|
|
97
98
|
return;
|
|
98
99
|
|
|
99
100
|
const n = trailingComments.length - 1;
|
|
101
|
+
const likeChain = isLooksLikeChain(parentPath);
|
|
100
102
|
|
|
101
103
|
for (const {type, value, loc} of trailingComments) {
|
|
102
104
|
const sameLine = isSameLine(path, loc);
|
|
103
105
|
const commentOnNextLine = isCommentOnNextLine(path);
|
|
104
106
|
|
|
105
107
|
if (type === 'CommentLine') {
|
|
108
|
+
const nextLineInChain = commentOnNextLine && likeChain;
|
|
109
|
+
const shouldIndent = !sameLine && (!commentOnNextLine || path.isDecorator());
|
|
110
|
+
|
|
106
111
|
maybe.write.breakline(commentOnNextLine);
|
|
107
112
|
maybe.write.space(sameLine);
|
|
108
|
-
|
|
113
|
+
|
|
114
|
+
if (shouldIndent || nextLineInChain) {
|
|
115
|
+
maybe.indent.inc(nextLineInChain);
|
|
116
|
+
indent();
|
|
117
|
+
maybe.indent.dec(nextLineInChain);
|
|
118
|
+
}
|
|
109
119
|
|
|
110
120
|
if (hasBody(path)) {
|
|
111
121
|
maybe.write.breakline(!isNext(path));
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const {hasTrailingComment} = require('../../is');
|
|
3
4
|
const {assign} = Object;
|
|
4
5
|
|
|
5
6
|
module.exports.chain = (path) => {
|
|
@@ -41,6 +42,11 @@ function up(current) {
|
|
|
41
42
|
const properties = [];
|
|
42
43
|
|
|
43
44
|
while (current.isMemberExpression()) {
|
|
45
|
+
if (hasTrailingComment(current.get('object')))
|
|
46
|
+
properties.push({
|
|
47
|
+
type: 'CommentLine',
|
|
48
|
+
});
|
|
49
|
+
|
|
44
50
|
current = current.parentPath;
|
|
45
51
|
|
|
46
52
|
if (current.isCallExpression()) {
|
|
@@ -10,10 +10,11 @@ const {
|
|
|
10
10
|
isCallExpression,
|
|
11
11
|
} = types;
|
|
12
12
|
|
|
13
|
-
const isArgOfCall = (path) => path.parentPath
|
|
13
|
+
const isArgOfCall = (path) => path.parentPath?.isCallExpression() && path.parentPath.get('arguments.0') === path;
|
|
14
14
|
const isCall = (a) => a.type === 'CallExpression';
|
|
15
15
|
|
|
16
16
|
const isExcludedFromChain = satisfy([isUnaryExpression, isIfStatement]);
|
|
17
|
+
const hasComment = ({type}) => type === 'CommentLine';
|
|
17
18
|
|
|
18
19
|
module.exports.isLooksLikeChain = (path) => {
|
|
19
20
|
const [root, properties] = chain(path);
|
|
@@ -24,6 +25,9 @@ module.exports.isLooksLikeChain = (path) => {
|
|
|
24
25
|
if (properties.find(isPathGet))
|
|
25
26
|
return false;
|
|
26
27
|
|
|
28
|
+
if (properties.find(hasComment))
|
|
29
|
+
return true;
|
|
30
|
+
|
|
27
31
|
if (path.find(isIfUp))
|
|
28
32
|
return false;
|
|
29
33
|
|
|
@@ -16,6 +16,8 @@ module.exports.MemberExpression = (path, printer) => {
|
|
|
16
16
|
const isParens = object.isAssignmentExpression();
|
|
17
17
|
const {computed} = path.node;
|
|
18
18
|
|
|
19
|
+
const isChain = isLooksLikeChain(path);
|
|
20
|
+
|
|
19
21
|
maybe.print(isParens, '(');
|
|
20
22
|
traverse(object);
|
|
21
23
|
maybe.print(isParens, ')');
|
|
@@ -23,8 +25,6 @@ module.exports.MemberExpression = (path, printer) => {
|
|
|
23
25
|
if (computed)
|
|
24
26
|
return maybePrintComputed(path, property, printer);
|
|
25
27
|
|
|
26
|
-
const isChain = isLooksLikeChain(path);
|
|
27
|
-
|
|
28
28
|
maybe.indent.inc(isChain);
|
|
29
29
|
maybe.print.breakline(isChain);
|
|
30
30
|
|
package/package.json
CHANGED