@putout/printer 14.7.1 → 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 CHANGED
@@ -1,3 +1,8 @@
1
+ 2025.05.03, v14.7.2
2
+
3
+ feature:
4
+ - ec635c3 @putout/printer: ImportDeclaration: printTrailingCommentsBlock
5
+
1
6
  2025.05.03, v14.7.1
2
7
 
3
8
  feature:
@@ -1,19 +1,50 @@
1
1
  'use strict';
2
2
 
3
- const createPrintComment = (fn, value) => () => fn(`//${value}`);
3
+ const createPrintCommentLine = (fn, value) => () => fn(`//${value}`);
4
+ const createPrintCommentBlock = (fn, value) => () => fn(`/*${value}*/\n`);
4
5
 
5
- module.exports.hasCommentsPrinter = (currentTraverse) => currentTraverse.printLeadingCommentLine;
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.printComments = (path, printer, semantics, {currentTraverse}) => {
18
+ module.exports.printLeadingComments = (path, printer, semantics, {currentTraverse}) => {
8
19
  const {print} = printer;
9
- const {leadingComments} = path.node;
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: createPrintComment(print, value),
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
- hasCommentsPrinter,
15
- printComments,
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 (hasCommentsPrinter(currentTraverse))
111
- return printComments(path, printer, semantics, {
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, {write, maybe, indent}, semantics) => {
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) || isImportNotBeforeExport(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,10 +173,3 @@ function isPrevCall(path) {
159
173
 
160
174
  return !isCallExpression(expression.arguments[0]);
161
175
  }
162
-
163
- function isImportNotBeforeExport(path) {
164
- if (!isImportDeclaration(path))
165
- return false;
166
-
167
- return !path.getNextSibling().isExportDeclaration();
168
- }
@@ -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
+
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "14.7.1",
3
+ "version": "14.7.2",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Simplest possible opinionated Babel AST printer for 🐊Putout",