@putout/printer 14.7.0 → 14.7.2
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/comments-printer/comments-printer.js +37 -6
- package/lib/tokenize/comment/parse-leading-comments.js +4 -4
- package/lib/tokenize/comment/parse-trailing-comments.js +17 -4
- package/lib/tokenize/statements/import-declaration/import-declaration.js +12 -1
- package/lib/tokenize/tokenize.js +3 -1
- package/lib/tokenize/typescript/ts-parameter-property/ts-parameter-property.js +1 -0
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -1,19 +1,50 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const createPrintCommentLine = (fn, value) => () => fn(`//${value}`);
|
|
4
|
+
const createPrintCommentBlock = (fn, value) => () => fn(`/*${value}*/\n`);
|
|
4
5
|
|
|
5
|
-
module.exports.
|
|
6
|
+
module.exports.hasTrailingCommentsPrinter = (currentTraverse) => {
|
|
7
|
+
const {printTrailingCommentBlock} = currentTraverse;
|
|
8
|
+
|
|
9
|
+
return Boolean(printTrailingCommentBlock);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
module.exports.hasLeadingCommentsPrinter = (currentTraverse) => {
|
|
13
|
+
const {printLeadingCommentLine} = currentTraverse;
|
|
14
|
+
|
|
15
|
+
return Boolean(printLeadingCommentLine);
|
|
16
|
+
};
|
|
6
17
|
|
|
7
|
-
module.exports.
|
|
18
|
+
module.exports.printLeadingComments = (path, printer, semantics, {currentTraverse}) => {
|
|
8
19
|
const {print} = printer;
|
|
9
|
-
const {
|
|
20
|
+
const {
|
|
21
|
+
leadingComments = [],
|
|
22
|
+
} = path.node;
|
|
23
|
+
|
|
10
24
|
const {printLeadingCommentLine} = currentTraverse;
|
|
11
25
|
|
|
12
26
|
for (const [index, {type, value}] of leadingComments.entries()) {
|
|
13
27
|
if (type === 'CommentLine')
|
|
14
|
-
printLeadingCommentLine(path, printer, semantics, {
|
|
28
|
+
printLeadingCommentLine?.(path, printer, semantics, {
|
|
29
|
+
index,
|
|
30
|
+
printComment: createPrintCommentLine(print, value),
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
module.exports.printTrailingComments = (path, printer, semantics, {currentTraverse}) => {
|
|
36
|
+
const {print} = printer;
|
|
37
|
+
const {
|
|
38
|
+
trailingComments = [],
|
|
39
|
+
} = path.node;
|
|
40
|
+
|
|
41
|
+
const {printTrailingCommentBlock} = currentTraverse;
|
|
42
|
+
|
|
43
|
+
for (const [index, {type, value}] of trailingComments.entries()) {
|
|
44
|
+
if (type === 'CommentBlock')
|
|
45
|
+
printTrailingCommentBlock?.(path, printer, semantics, {
|
|
15
46
|
index,
|
|
16
|
-
printComment:
|
|
47
|
+
printComment: createPrintCommentBlock(print, value),
|
|
17
48
|
});
|
|
18
49
|
}
|
|
19
50
|
};
|
|
@@ -11,8 +11,8 @@ const {markBefore} = require('../mark');
|
|
|
11
11
|
const {maybeInsideFn} = require('./maybe-inside-fn');
|
|
12
12
|
|
|
13
13
|
const {
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
printLeadingComments,
|
|
15
|
+
hasLeadingCommentsPrinter,
|
|
16
16
|
} = require('./comments-printer/comments-printer');
|
|
17
17
|
|
|
18
18
|
const {
|
|
@@ -107,8 +107,8 @@ module.exports.parseLeadingComments = (path, printer, semantics, {currentTravers
|
|
|
107
107
|
if (!leadingComments?.length)
|
|
108
108
|
return;
|
|
109
109
|
|
|
110
|
-
if (
|
|
111
|
-
return
|
|
110
|
+
if (hasLeadingCommentsPrinter(currentTraverse))
|
|
111
|
+
return printLeadingComments(path, printer, semantics, {
|
|
112
112
|
currentTraverse,
|
|
113
113
|
});
|
|
114
114
|
|
|
@@ -10,12 +10,16 @@ const {
|
|
|
10
10
|
|
|
11
11
|
const {isLooksLikeChain} = require('../expressions/member-expression/is-looks-like-chain');
|
|
12
12
|
|
|
13
|
+
const {
|
|
14
|
+
printTrailingComments,
|
|
15
|
+
hasTrailingCommentsPrinter,
|
|
16
|
+
} = require('./comments-printer/comments-printer');
|
|
17
|
+
|
|
13
18
|
const {
|
|
14
19
|
isDecorator,
|
|
15
20
|
isMemberExpression,
|
|
16
21
|
isExpressionStatement,
|
|
17
22
|
isCallExpression,
|
|
18
|
-
isImportDeclaration,
|
|
19
23
|
} = types;
|
|
20
24
|
|
|
21
25
|
const hasBody = (path) => {
|
|
@@ -91,8 +95,13 @@ function isCommentOnNextLine(path) {
|
|
|
91
95
|
return isNextLine || isNextLineAfterNewline;
|
|
92
96
|
}
|
|
93
97
|
|
|
94
|
-
module.exports.parseTrailingComments = (path,
|
|
98
|
+
module.exports.parseTrailingComments = (path, printer, semantics, {currentTraverse} = {}) => {
|
|
95
99
|
const {parentPath} = path;
|
|
100
|
+
const {
|
|
101
|
+
write,
|
|
102
|
+
maybe,
|
|
103
|
+
indent,
|
|
104
|
+
} = printer;
|
|
96
105
|
|
|
97
106
|
if (!semantics.comments)
|
|
98
107
|
return;
|
|
@@ -102,6 +111,11 @@ module.exports.parseTrailingComments = (path, {write, maybe, indent}, semantics)
|
|
|
102
111
|
if (!trailingComments?.length)
|
|
103
112
|
return;
|
|
104
113
|
|
|
114
|
+
if (hasTrailingCommentsPrinter(currentTraverse))
|
|
115
|
+
return printTrailingComments(path, printer, semantics, {
|
|
116
|
+
currentTraverse,
|
|
117
|
+
});
|
|
118
|
+
|
|
105
119
|
if (path.isDirective())
|
|
106
120
|
return;
|
|
107
121
|
|
|
@@ -139,7 +153,7 @@ module.exports.parseTrailingComments = (path, {write, maybe, indent}, semantics)
|
|
|
139
153
|
if (type === 'CommentBlock') {
|
|
140
154
|
maybe.write.space(sameLine);
|
|
141
155
|
maybe.indent(!sameLine);
|
|
142
|
-
maybe.print.breakline(isPrevCall(path)
|
|
156
|
+
maybe.print.breakline(isPrevCall(path));
|
|
143
157
|
write(`/*${value}*/`);
|
|
144
158
|
maybe.write.newline(!sameLine || !isFnParam(path) && isCoupleLines(path.parentPath));
|
|
145
159
|
}
|
|
@@ -159,4 +173,3 @@ function isPrevCall(path) {
|
|
|
159
173
|
|
|
160
174
|
return !isCallExpression(expression.arguments[0]);
|
|
161
175
|
}
|
|
162
|
-
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {parseImportSpecifiers} = require('parse-import-specifiers');
|
|
4
|
-
|
|
4
|
+
const {types} = require('@putout/babel');
|
|
5
5
|
const {markAfter} = require('../../mark');
|
|
6
6
|
const {isLast, isNext} = require('../../is');
|
|
7
7
|
|
|
@@ -10,6 +10,8 @@ const {
|
|
|
10
10
|
ImportAttribute,
|
|
11
11
|
} = require('./import-attribute');
|
|
12
12
|
|
|
13
|
+
const {isExportDeclaration} = types;
|
|
14
|
+
|
|
13
15
|
module.exports.ImportAttribute = ImportAttribute;
|
|
14
16
|
module.exports.ImportDeclaration = {
|
|
15
17
|
print(path, printer, semantics) {
|
|
@@ -134,3 +136,12 @@ function parseMaxSpecifiers(imports, semantics) {
|
|
|
134
136
|
|
|
135
137
|
return maxSpecifiersInOneLine;
|
|
136
138
|
}
|
|
139
|
+
|
|
140
|
+
module.exports.ImportDeclaration.printTrailingCommentBlock = (path, printer, semantics, {printComment}) => {
|
|
141
|
+
const {maybe} = printer;
|
|
142
|
+
const next = path.getNextSibling();
|
|
143
|
+
|
|
144
|
+
maybe.print.breakline(!isExportDeclaration(next));
|
|
145
|
+
printComment();
|
|
146
|
+
};
|
|
147
|
+
|
package/lib/tokenize/tokenize.js
CHANGED
|
@@ -231,7 +231,9 @@ module.exports.tokenize = (ast, overrides) => {
|
|
|
231
231
|
|
|
232
232
|
// this is main thing
|
|
233
233
|
maybeVisitor(currentTraverse, path, printer, semantics);
|
|
234
|
-
parseTrailingComments(path, printer, semantics
|
|
234
|
+
parseTrailingComments(path, printer, semantics, {
|
|
235
|
+
currentTraverse,
|
|
236
|
+
});
|
|
235
237
|
maybeThrow(i !== currentIndent, path, `☝️Looks like indent level changed after token visitor: '{{ type }}', for code: '{{ path }}'`);
|
|
236
238
|
|
|
237
239
|
debug(path.type);
|
|
@@ -44,6 +44,7 @@ module.exports.TSParameterProperty = (path, {print, maybe, indent}) => {
|
|
|
44
44
|
if (hasLeadingComment(path) && !path.node.decorators)
|
|
45
45
|
print.breakline();
|
|
46
46
|
};
|
|
47
|
+
|
|
47
48
|
module.exports.TSParameterProperty.printLeadingCommentLine = (path, printer, semantics, {printComment}) => {
|
|
48
49
|
const {indent, print} = printer;
|
|
49
50
|
|
package/package.json
CHANGED