@putout/printer 15.7.0 → 15.8.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 +5 -0
- package/lib/tokenize/comment/comments-printer/comments-printer.js +31 -3
- package/lib/tokenize/statements/expression-statement/comments.js +44 -0
- package/lib/tokenize/statements/expression-statement/expression-statement.js +8 -0
- package/lib/tokenize/statements/import-declaration/comments.js +18 -0
- package/lib/tokenize/statements/import-declaration/import-declaration.js +7 -9
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -21,13 +21,27 @@ module.exports.printLeadingComments = (path, printer, semantics, {currentTravers
|
|
|
21
21
|
leadingComments = [],
|
|
22
22
|
} = path.node;
|
|
23
23
|
|
|
24
|
-
const {
|
|
24
|
+
const {
|
|
25
|
+
printLeadingCommentLine,
|
|
26
|
+
printLeadingCommentBlock,
|
|
27
|
+
} = currentTraverse;
|
|
28
|
+
|
|
29
|
+
const n = leadingComments.length - 1;
|
|
25
30
|
|
|
26
31
|
for (const [index, {type, value}] of leadingComments.entries()) {
|
|
27
|
-
if (type === 'CommentLine')
|
|
32
|
+
if (type === 'CommentLine') {
|
|
28
33
|
printLeadingCommentLine?.(path, printer, semantics, {
|
|
29
34
|
index,
|
|
30
35
|
printComment: createPrintCommentLine(print, value),
|
|
36
|
+
isLast: index === n,
|
|
37
|
+
});
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (type === 'CommentBlock')
|
|
42
|
+
printLeadingCommentBlock?.(path, printer, semantics, {
|
|
43
|
+
index,
|
|
44
|
+
printComment: createPrintCommentBlock(print, value),
|
|
31
45
|
});
|
|
32
46
|
}
|
|
33
47
|
};
|
|
@@ -38,9 +52,23 @@ module.exports.printTrailingComments = (path, printer, semantics, {currentTraver
|
|
|
38
52
|
trailingComments = [],
|
|
39
53
|
} = path.node;
|
|
40
54
|
|
|
41
|
-
const {
|
|
55
|
+
const {
|
|
56
|
+
printTrailingCommentLine,
|
|
57
|
+
printTrailingCommentBlock,
|
|
58
|
+
} = currentTraverse;
|
|
59
|
+
|
|
60
|
+
const n = trailingComments.length - 1;
|
|
42
61
|
|
|
43
62
|
for (const [index, {type, value}] of trailingComments.entries()) {
|
|
63
|
+
if (type === 'CommentLine') {
|
|
64
|
+
printTrailingCommentLine?.(path, printer, semantics, {
|
|
65
|
+
index,
|
|
66
|
+
printComment: createPrintCommentLine(print, value),
|
|
67
|
+
isLast: index === n,
|
|
68
|
+
});
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
|
|
44
72
|
if (type === 'CommentBlock')
|
|
45
73
|
printTrailingCommentBlock?.(path, printer, semantics, {
|
|
46
74
|
index,
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {types} = require('@putout/babel');
|
|
4
|
+
const {hasTrailingComment} = require('#is');
|
|
5
|
+
|
|
6
|
+
const {
|
|
7
|
+
isBlockStatement,
|
|
8
|
+
isProgram,
|
|
9
|
+
isIfStatement,
|
|
10
|
+
isClassMethod,
|
|
11
|
+
} = types;
|
|
12
|
+
|
|
13
|
+
module.exports.printLeadingCommentLine = (path, printer, semantics, {index, isLast, printComment}) => {
|
|
14
|
+
const {print, indent} = printer;
|
|
15
|
+
const prev = path.getPrevSibling();
|
|
16
|
+
const {parentPath} = path;
|
|
17
|
+
const parentParentPath = parentPath.parentPath;
|
|
18
|
+
|
|
19
|
+
if (hasTrailingComment(prev))
|
|
20
|
+
return;
|
|
21
|
+
|
|
22
|
+
if (!index && !prev.node && (isIfStatement(parentPath) || isClassMethod(parentParentPath)))
|
|
23
|
+
indent();
|
|
24
|
+
|
|
25
|
+
printComment();
|
|
26
|
+
|
|
27
|
+
print.newline();
|
|
28
|
+
|
|
29
|
+
if (!isLast && !path.parentPath.isIfStatement())
|
|
30
|
+
print.indent();
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
module.exports.printLeadingCommentBlock = (path, printer, semantics, {printComment}) => {
|
|
34
|
+
const {indent} = printer;
|
|
35
|
+
const prev = path.getPrevSibling();
|
|
36
|
+
|
|
37
|
+
if (hasTrailingComment(prev))
|
|
38
|
+
return;
|
|
39
|
+
|
|
40
|
+
if (isBlockStatement(path.parentPath) && !isProgram(path.parentPath.parentPath))
|
|
41
|
+
indent();
|
|
42
|
+
|
|
43
|
+
printComment();
|
|
44
|
+
};
|
|
@@ -15,6 +15,12 @@ const {
|
|
|
15
15
|
} = require('../../is');
|
|
16
16
|
|
|
17
17
|
const {isInsideAssignNextAssignFunction} = require('./is-inside-assign-next-assign-function');
|
|
18
|
+
|
|
19
|
+
const {
|
|
20
|
+
printLeadingCommentLine,
|
|
21
|
+
printLeadingCommentBlock,
|
|
22
|
+
} = require('./comments');
|
|
23
|
+
|
|
18
24
|
const isCommentBlock = (a) => a?.type === 'CommentBlock';
|
|
19
25
|
|
|
20
26
|
const {
|
|
@@ -117,6 +123,8 @@ module.exports.ExpressionStatement = {
|
|
|
117
123
|
}
|
|
118
124
|
},
|
|
119
125
|
};
|
|
126
|
+
module.exports.ExpressionStatement.printLeadingCommentLine = printLeadingCommentLine;
|
|
127
|
+
module.exports.ExpressionStatement.printLeadingCommentBlock = printLeadingCommentBlock;
|
|
120
128
|
|
|
121
129
|
function isTopParentLast({parentPath}) {
|
|
122
130
|
if (!parentPath.isIfStatement())
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {types} = require('@putout/babel');
|
|
4
|
+
const {isExportDeclaration} = types;
|
|
5
|
+
|
|
6
|
+
module.exports.printTrailingCommentLine = (path, printer, semantics, {printComment}) => {
|
|
7
|
+
const {print} = printer;
|
|
8
|
+
printComment();
|
|
9
|
+
print.breakline();
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
module.exports.printTrailingCommentBlock = (path, printer, semantics, {printComment}) => {
|
|
13
|
+
const {maybe} = printer;
|
|
14
|
+
const next = path.getNextSibling();
|
|
15
|
+
|
|
16
|
+
maybe.print.breakline(!isExportDeclaration(next));
|
|
17
|
+
printComment();
|
|
18
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {parseImportSpecifiers} = require('parse-import-specifiers');
|
|
4
|
-
|
|
4
|
+
|
|
5
5
|
const {markAfter} = require('../../mark');
|
|
6
6
|
const {isLast, isNext} = require('../../is');
|
|
7
7
|
|
|
@@ -10,7 +10,10 @@ const {
|
|
|
10
10
|
ImportAttribute,
|
|
11
11
|
} = require('./import-attribute');
|
|
12
12
|
|
|
13
|
-
const {
|
|
13
|
+
const {
|
|
14
|
+
printTrailingCommentBlock,
|
|
15
|
+
printTrailingCommentLine,
|
|
16
|
+
} = require('./comments');
|
|
14
17
|
|
|
15
18
|
module.exports.ImportAttribute = ImportAttribute;
|
|
16
19
|
module.exports.ImportDeclaration = {
|
|
@@ -137,10 +140,5 @@ function parseMaxSpecifiers(imports, semantics) {
|
|
|
137
140
|
return maxSpecifiersInOneLine;
|
|
138
141
|
}
|
|
139
142
|
|
|
140
|
-
module.exports.ImportDeclaration.printTrailingCommentBlock =
|
|
141
|
-
|
|
142
|
-
const next = path.getNextSibling();
|
|
143
|
-
|
|
144
|
-
maybe.print.breakline(!isExportDeclaration(next));
|
|
145
|
-
printComment();
|
|
146
|
-
};
|
|
143
|
+
module.exports.ImportDeclaration.printTrailingCommentBlock = printTrailingCommentBlock;
|
|
144
|
+
module.exports.ImportDeclaration.printTrailingCommentLine = printTrailingCommentLine;
|
package/package.json
CHANGED