@putout/printer 18.52.0 → 18.52.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,13 @@
1
+ 2026.11.22, v18.52.2
2
+
3
+ feature:
4
+ - e27532f0 @putout/printer: ObjectProperty: comments: improve
5
+
6
+ 2026.11.22, v18.52.1
7
+
8
+ feature:
9
+ - 8b19aaa1 VariableDeclarator: comments: improve
10
+
1
11
  2026.11.22, v18.52.0
2
12
 
3
13
  feature:
@@ -1,3 +1,4 @@
1
+ import {isCommentOnPreviousLine} from './is-comment-on-previous-line.js';
1
2
  import {printCommentBlock} from './print-comment-block.js';
2
3
 
3
4
  const createPrintCommentLine = (fn, value) => () => fn(`//${value}`);
@@ -60,6 +61,7 @@ export const printLeadingComments = (path, printer, semantics, {currentTraverse}
60
61
  index,
61
62
  printComment: createPrintCommentLine(print, value),
62
63
  isLast: index === n,
64
+ isPreviousLine: () => isCommentOnPreviousLine(path),
63
65
  });
64
66
  continue;
65
67
  }
@@ -0,0 +1,23 @@
1
+ const isCommentOfPrevious = (path) => {
2
+ const [comment] = path.node.leadingComments;
3
+ const prev = path.getPrevSibling();
4
+
5
+ if (!prev.node)
6
+ return false;
7
+
8
+ return prev.node.loc.start.line === comment.loc.start.line;
9
+ };
10
+
11
+ export const isCommentOnPreviousLine = (path) => {
12
+ const {
13
+ loc,
14
+ leadingComments,
15
+ } = path.node;
16
+
17
+ const [comment] = leadingComments;
18
+
19
+ if (isCommentOfPrevious(path))
20
+ return false;
21
+
22
+ return comment.loc.start.line <= loc.start.line - 1;
23
+ };
@@ -12,7 +12,6 @@ import {
12
12
 
13
13
  const {
14
14
  isArrowFunctionExpression,
15
- isObjectProperty,
16
15
  isVariableDeclarator,
17
16
  isClassProperty,
18
17
  isTSPropertySignature,
@@ -24,8 +23,6 @@ const {
24
23
  } = types;
25
24
 
26
25
  const isProperty = satisfy([
27
- isObjectProperty,
28
- isVariableDeclarator,
29
26
  isClassProperty,
30
27
  isTSPropertySignature,
31
28
  isSpreadElement,
@@ -167,17 +164,13 @@ export const parseLeadingComments = (path, printer, semantics, {currentTraverse
167
164
 
168
165
  const looksLikeMethod = path.isClassMethod();
169
166
  const looksLikeDirective = path.isDirective();
170
- const looksLikeProp = path.isObjectProperty();
171
-
172
- if (looksLikeProp)
173
- print.breakline();
174
167
 
175
168
  print(`/*${value}*/`);
176
169
 
177
- if (path.isStatement() || path.isTSEnumMember() || looksLikeDirective || looksLikeMethod || looksLikeProp || looksLikeSwitchCase) {
170
+ if (path.isStatement() || path.isTSEnumMember() || looksLikeDirective || looksLikeMethod || looksLikeSwitchCase) {
178
171
  print.newline();
179
172
  markBefore(path);
180
- maybe.indent(looksLikeMethod || looksLikeProp || looksLikeSwitchCase);
173
+ maybe.indent(looksLikeMethod || looksLikeSwitchCase);
181
174
  }
182
175
 
183
176
  continue;
@@ -1,4 +1,5 @@
1
1
  import {isConcatenation} from '#is-concatenation';
2
+ import {markBefore} from '#mark';
2
3
  import {isMultiline} from './object-expression.js';
3
4
  import {printKey} from './print-key.js';
4
5
  import {isCommaAfterProperty} from './comma.js';
@@ -31,3 +32,37 @@ export const ObjectProperty = (path, printer, semantics) => {
31
32
  if (isSpaceAfterComma(path, {multiline}))
32
33
  write.space();
33
34
  };
35
+
36
+ const isFirst = (path) => path === path.parentPath.get('properties')[0];
37
+
38
+ ObjectProperty.printLeadingCommentLine = (path, printer, semantics, {printComment, isLast, isPreviousLine}) => {
39
+ const {print, maybe} = printer;
40
+ const first = isFirst(path);
41
+
42
+ maybe.indent(first);
43
+
44
+ if (isPreviousLine())
45
+ maybe.print.breakline(!first);
46
+ else
47
+ print.space();
48
+
49
+ printComment();
50
+
51
+ if (isLast) {
52
+ print.breakline();
53
+ return;
54
+ }
55
+
56
+ print.newline();
57
+ };
58
+
59
+ ObjectProperty.printLeadingCommentBlock = (path, printer, semantics, {printComment}) => {
60
+ const {maybe, indent} = printer;
61
+ const first = isFirst(path);
62
+
63
+ maybe.indent(first);
64
+ maybe.print.breakline(!first);
65
+ printComment();
66
+ markBefore(path);
67
+ indent();
68
+ };
@@ -19,6 +19,7 @@ import {
19
19
  } from './after-if.js';
20
20
  import {maybeSpaceAfterKeyword} from './maybe-space-after-keyword.js';
21
21
  import {beforeIf} from './before-if.js';
22
+ import {VariableDeclarator} from './variable-declarator.js';
22
23
  import {
23
24
  isInsideParentLike,
24
25
  isNeedBreaklineAfterComma,
@@ -95,7 +96,9 @@ export const VariableDeclaration = {
95
96
  write(',');
96
97
 
97
98
  if (hasLeadingComment(next)) {
98
- parseLeadingComments(next, printer, semantics);
99
+ parseLeadingComments(next, printer, semantics, {
100
+ currentTraverse: VariableDeclarator,
101
+ });
99
102
  continue;
100
103
  }
101
104
 
@@ -0,0 +1,26 @@
1
+ export const VariableDeclarator = {};
2
+
3
+ VariableDeclarator.printLeadingCommentLine = (path, printer, semantics, {printComment, isLast, isPreviousLine}) => {
4
+ const {print} = printer;
5
+
6
+ if (isPreviousLine())
7
+ print.breakline();
8
+ else
9
+ print.space();
10
+
11
+ printComment();
12
+
13
+ if (isLast) {
14
+ print.breakline();
15
+ return;
16
+ }
17
+
18
+ print.newline();
19
+ };
20
+
21
+ VariableDeclarator.printLeadingCommentBlock = (path, printer, semantics, {index}) => {
22
+ const {print} = printer;
23
+ const {value} = path.node.leadingComments[index];
24
+
25
+ print(`/*${value}*/`);
26
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "18.52.0",
3
+ "version": "18.52.2",
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",