@putout/printer 12.26.0 → 12.27.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 CHANGED
@@ -1,3 +1,8 @@
1
+ 2025.02.08, v12.27.0
2
+
3
+ feature:
4
+ - 7775296 @putout/printer: Decorator: trailing comments
5
+
1
6
  2025.02.08, v12.26.0
2
7
 
3
8
  feature:
@@ -20,6 +20,7 @@ const {
20
20
  isClassBody,
21
21
  isBinaryExpression,
22
22
  isClassMethod,
23
+ isDecorator,
23
24
  } = types;
24
25
 
25
26
  const isProperty = satisfy([
@@ -42,13 +43,14 @@ const isInsideVar = (path) => {
42
43
  return isVariableDeclarator(parentPath.parentPath);
43
44
  };
44
45
 
45
- const hasDecorators = (path) => {
46
- const {parentPath} = path;
46
+ const hasDecoratorsWithComments = (path) => {
47
+ if (isDecorator(path))
48
+ return false;
47
49
 
48
- if (path.node.decorators?.length)
50
+ if (path.node.decorators)
49
51
  return true;
50
52
 
51
- return parentPath.node.decorators?.length;
53
+ return path?.parentPath.node?.decorators;
52
54
  };
53
55
 
54
56
  function isCommentOfPrevious(path) {
@@ -97,7 +99,7 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
97
99
  if (!leadingComments?.length)
98
100
  return;
99
101
 
100
- if (hasDecorators(path))
102
+ if (hasDecoratorsWithComments(path))
101
103
  return;
102
104
 
103
105
  const looksLikeSwitchCase = path.isSwitchCase();
@@ -139,7 +141,7 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
139
141
 
140
142
  if (isClassMethod(path)) {
141
143
  indent();
142
- } else if (isBinaryExpression(path)) {
144
+ } else if (isBinaryExpression(path) || isDecorator(path)) {
143
145
  indent.inc();
144
146
  indent();
145
147
  indent.dec();
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const {parseComments} = require('../../comment/comment');
4
+
4
5
  const noop = () => {};
5
6
  const parseParams = (path) => path.get('params');
6
7
 
@@ -1,6 +1,19 @@
1
1
  'use strict';
2
2
 
3
3
  const {printParams} = require('./params');
4
+ const {hasLeadingComment} = require('../../is');
5
+
6
+ const isDecoratorHasLeadingComment = (path) => {
7
+ const [firstParam] = path.get('params');
8
+ const decorators = firstParam.get('decorators');
9
+
10
+ if (!decorators.length)
11
+ return false;
12
+
13
+ const [firstDecorator] = decorators;
14
+
15
+ return hasLeadingComment(firstDecorator);
16
+ };
4
17
 
5
18
  const hasDecorators = ({decorators}) => decorators?.length;
6
19
 
@@ -10,13 +23,13 @@ module.exports.printFunctionParams = (path, printer, semantics) => {
10
23
  const isNewline = params.filter(hasDecorators).length;
11
24
  const printSpace = isNewline ? print.breakline : print.space;
12
25
 
13
- const printBeforeFirst = createPrint({
26
+ const printBeforeFirst = createPrint(path, {
14
27
  type: 'inc',
15
28
  printer,
16
29
  isNewline,
17
30
  });
18
31
 
19
- const printAfterLast = createPrint({
32
+ const printAfterLast = createPrint(path, {
20
33
  type: 'dec',
21
34
  printer,
22
35
  isNewline,
@@ -29,11 +42,14 @@ module.exports.printFunctionParams = (path, printer, semantics) => {
29
42
  });
30
43
  };
31
44
 
32
- const createPrint = ({isNewline, printer, type}) => () => {
45
+ const createPrint = (path, {isNewline, printer, type}) => () => {
33
46
  if (!isNewline)
34
47
  return;
35
48
 
36
49
  const {indent, print} = printer;
37
- indent[type]();
50
+
51
+ if (!isDecoratorHasLeadingComment(path))
52
+ indent[type]();
53
+
38
54
  print.breakline();
39
55
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "12.26.0",
3
+ "version": "12.27.0",
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",