@putout/printer 12.27.0 → 12.27.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 CHANGED
@@ -1,3 +1,8 @@
1
+ 2025.02.08, v12.27.1
2
+
3
+ feature:
4
+ - bd1a8cd @putout/printer: decorators: comments: improve
5
+
1
6
  2025.02.08, v12.27.0
2
7
 
3
8
  feature:
@@ -37,8 +37,9 @@ module.exports.printParams = (path, printer, semantics, customization = {}) => {
37
37
 
38
38
  for (let i = 0; i <= n; i++) {
39
39
  const isLast = i === n;
40
+ const current = params[i];
40
41
 
41
- traverse(params[i]);
42
+ traverse(current);
42
43
 
43
44
  if (!isLast) {
44
45
  print(',');
@@ -3,36 +3,51 @@
3
3
  const {printParams} = require('./params');
4
4
  const {hasLeadingComment} = require('../../is');
5
5
 
6
- const isDecoratorHasLeadingComment = (path) => {
7
- const [firstParam] = path.get('params');
8
- const decorators = firstParam.get('decorators');
6
+ const isAllParamsHasLeadingComments = (path) => {
7
+ const params = path.get('params');
8
+ let commentsCount = 0;
9
+ let decoratorsCount = 0;
9
10
 
10
- if (!decorators.length)
11
- return false;
11
+ for (const param of params) {
12
+ const decorators = param.get('decorators');
13
+
14
+ if (!decorators.length)
15
+ continue;
16
+
17
+ const [firstDecorator] = decorators;
18
+ ++decoratorsCount;
19
+
20
+ if (hasLeadingComment(firstDecorator))
21
+ ++commentsCount;
22
+ }
12
23
 
13
- const [firstDecorator] = decorators;
14
-
15
- return hasLeadingComment(firstDecorator);
24
+ return commentsCount === decoratorsCount;
16
25
  };
17
26
 
18
27
  const hasDecorators = ({decorators}) => decorators?.length;
19
28
 
20
29
  module.exports.printFunctionParams = (path, printer, semantics) => {
21
- const {print} = printer;
22
30
  const {params} = path.node;
23
31
  const isNewline = params.filter(hasDecorators).length;
24
- const printSpace = isNewline ? print.breakline : print.space;
32
+ const isAllHasComments = isAllParamsHasLeadingComments(path);
25
33
 
26
- const printBeforeFirst = createPrint(path, {
34
+ const printSpace = createPrintSpace({
35
+ isNewline,
36
+ printer,
37
+ });
38
+
39
+ const printBeforeFirst = createPrintBeforeFirst(path, {
27
40
  type: 'inc',
28
41
  printer,
29
42
  isNewline,
43
+ isAllHasComments,
30
44
  });
31
45
 
32
- const printAfterLast = createPrint(path, {
46
+ const printAfterLast = createPrintAfterLast({
33
47
  type: 'dec',
34
48
  printer,
35
49
  isNewline,
50
+ isAllHasComments,
36
51
  });
37
52
 
38
53
  printParams(path, printer, semantics, {
@@ -42,14 +57,40 @@ module.exports.printFunctionParams = (path, printer, semantics) => {
42
57
  });
43
58
  };
44
59
 
45
- const createPrint = (path, {isNewline, printer, type}) => () => {
60
+ const createPrintBeforeFirst = (path, {isNewline, isAllHasComments, printer, type}) => () => {
46
61
  if (!isNewline)
47
62
  return;
48
63
 
49
64
  const {indent, print} = printer;
50
65
 
51
- if (!isDecoratorHasLeadingComment(path))
66
+ if (!isAllHasComments)
52
67
  indent[type]();
53
68
 
54
69
  print.breakline();
70
+
71
+ const [first] = path.get('params');
72
+
73
+ if (!first.node.decorators)
74
+ indent();
75
+ };
76
+
77
+ const createPrintAfterLast = ({isNewline, printer, isAllHasComments, type}) => () => {
78
+ if (!isNewline)
79
+ return;
80
+
81
+ const {indent, print} = printer;
82
+
83
+ if (!isAllHasComments)
84
+ indent[type]();
85
+
86
+ print.breakline();
87
+ };
88
+
89
+ const createPrintSpace = ({isNewline, printer}) => () => {
90
+ const {print} = printer;
91
+
92
+ if (!isNewline)
93
+ return print.space();
94
+
95
+ print.breakline();
55
96
  };
@@ -40,6 +40,5 @@ module.exports.TSParameterProperty = (path, {print, maybe, indent}) => {
40
40
  }
41
41
 
42
42
  print('__parameter');
43
- maybe.print(isNewline, ',');
44
43
  maybe.print.breakline(decoratorsLength);
45
44
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "12.27.0",
3
+ "version": "12.27.1",
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",