@putout/printer 18.19.0 → 18.20.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
+ 2026.06.20, v18.20.0
2
+
3
+ feature:
4
+ - b38b9939 @putout/printer: new line before leading Comment, after CallExpression
5
+
1
6
  2026.06.19, v18.19.0
2
7
 
3
8
  feature:
@@ -1,14 +1,27 @@
1
+ import {types} from '@putout/babel';
1
2
  import {createTypeChecker} from '#type-checker';
2
3
  import {
3
4
  hasTrailingComment,
4
- isCoupleLines,
5
5
  isInsideBlock,
6
+ isInsideReturn,
6
7
  isLast,
7
8
  isNext,
8
9
  isParentLast,
9
10
  satisfy,
11
+ exists,
12
+ isCoupleLines,
10
13
  } from '#is';
11
14
 
15
+ const {
16
+ isCallExpression,
17
+ isAssignmentExpression,
18
+ isArrayExpression,
19
+ isStringLiteral,
20
+ isTemplateLiteral,
21
+ isExpressionStatement,
22
+ isStatement,
23
+ } = types;
24
+
12
25
  function isNotLastOrParentLast(path) {
13
26
  return !isLast(path) && !isParentLast(path);
14
27
  }
@@ -37,16 +50,80 @@ const isBeforeElse = createTypeChecker([
37
50
  ['+: parentPath.node.alternate -> +'],
38
51
  ]);
39
52
 
40
- const hasTrailingCommentNotCoupleLines = createTypeChecker([
41
- ['-: -> !', hasTrailingComment],
42
- ['+: -> !', isCoupleLines],
43
- ]);
53
+ const isNextCallWithLeadingComments = (path) => {
54
+ const next = path.getNextSibling();
55
+
56
+ if (!exists(next))
57
+ return false;
58
+
59
+ if (!path.node.trailingComments)
60
+ return false;
61
+
62
+ if (isStatement(next) && isCoupleLines(path))
63
+ return true;
64
+
65
+ if (!isExpressionStatement(next))
66
+ return false;
67
+
68
+ const {expression} = path.node;
69
+
70
+ if (isCallExpression(expression))
71
+ return true;
72
+
73
+ const nextExpression = next.get('expression');
74
+
75
+ return isCallExpression(nextExpression);
76
+ };
77
+
78
+ function isNextWithLeadingComments(path) {
79
+ const next = path.getNextSibling();
80
+ const {expression} = path.node;
81
+
82
+ if (!path.node.trailingComments)
83
+ return true;
84
+
85
+ if (isAssignmentExpression(expression))
86
+ return true;
87
+
88
+ return !exists(next);
89
+ }
90
+
91
+ export function isNextSimpleWithLeadingComments(path) {
92
+ const next = path.getNextSibling();
93
+
94
+ if (!hasTrailingComment(path))
95
+ return false;
96
+
97
+ if (!isExpressionStatement(next))
98
+ return false;
99
+
100
+ const {expression} = next.node;
101
+
102
+ if (isStringLiteral(expression))
103
+ return true;
104
+
105
+ if (isTemplateLiteral(expression))
106
+ return true;
107
+
108
+ return isArrayExpression(next.node.expression);
109
+ }
110
+
111
+ function isAssignWithTrailingComments(path) {
112
+ const {expression} = path.node;
113
+
114
+ if (!isAssignmentExpression(expression))
115
+ return false;
116
+
117
+ return hasTrailingComment(path);
118
+ }
44
119
 
45
120
  export const afterIf = createTypeChecker([
121
+ ['-', isInsideReturn],
46
122
  ['-', isTopParentLast],
47
- ['-', hasTrailingCommentNotCoupleLines],
123
+ ['+', isNextCallWithLeadingComments],
124
+ ['-: -> !', isNextWithLeadingComments],
125
+ ['+', isAssignWithTrailingComments],
126
+ ['-: node.trailingComments.length -> +'],
48
127
  ['+', satisfyAfter],
49
128
  ['+', isBeforeElse],
50
- ['-: -> !', hasTrailingComment],
51
- ['+', isLast],
52
129
  ]);
@@ -2,11 +2,8 @@ import {types} from '@putout/babel';
2
2
  import {createTypeChecker} from '#type-checker';
3
3
  import {
4
4
  isNext,
5
- isLast,
6
5
  isNewlineBetweenSiblings,
7
6
  noTrailingComment,
8
- hasTrailingComment,
9
- isCoupleLines,
10
7
  isInsideReturn,
11
8
  callWithNext,
12
9
  hasLeadingComment,
@@ -15,7 +12,10 @@ import {
15
12
  printLeadingCommentLine,
16
13
  printLeadingCommentBlock,
17
14
  } from './comments.js';
18
- import {afterIf} from './after-if.js';
15
+ import {
16
+ afterIf,
17
+ isNextSimpleWithLeadingComments,
18
+ } from './after-if.js';
19
19
  import {beforeIf} from './before-if.js';
20
20
 
21
21
  const {isIfStatement} = types;
@@ -30,12 +30,6 @@ const isNextToAssignmentCall = createTypeChecker([
30
30
  ['+', callWithNext(isCallInsideExpression)],
31
31
  ]);
32
32
 
33
- const isBreaklineAfter = createTypeChecker([
34
- ['-: -> !', hasTrailingComment],
35
- ['-: -> !', isLast],
36
- ['+', isCoupleLines],
37
- ]);
38
-
39
33
  const isNextStatementWithBlockComment = createTypeChecker([
40
34
  '-: node.expression -> !CallExpression',
41
35
  '-: node.expression.arguments.0 -> !CallExpression',
@@ -80,13 +74,11 @@ export const ExpressionStatement = {
80
74
  },
81
75
  afterIf,
82
76
  after(path, {print, maybe, store}) {
83
- maybe.print.breakline(isBreaklineAfter(path));
77
+ maybe.indent(isIndentAfter(path));
84
78
 
85
- if (isInsideReturn(path))
86
- return;
79
+ if (!isNextSimpleWithLeadingComments(path))
80
+ print.newline();
87
81
 
88
- maybe.indent(isIndentAfter(path));
89
- print.newline();
90
82
  maybe.markAfter(store(), path);
91
83
  },
92
84
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "18.19.0",
3
+ "version": "18.20.0",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Simplest possible opinionated Babel AST printer for 🐊Putout",