@putout/printer 18.52.3 → 18.52.4

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.11.22, v18.52.4
2
+
3
+ feature:
4
+ - 63f30d8e @putout/printer: ClassProperty: comments: simplify
5
+
1
6
  2026.11.22, v18.52.3
2
7
 
3
8
  feature:
@@ -1,8 +1,5 @@
1
1
  import {types} from '@putout/babel';
2
- import {
3
- hasTrailingComment,
4
- satisfy,
5
- } from '#is';
2
+ import {hasTrailingComment} from '#is';
6
3
  import {markBefore} from '#mark';
7
4
  import {maybeInsideFn} from './maybe-inside-fn.js';
8
5
  import {
@@ -13,16 +10,11 @@ import {
13
10
  const {
14
11
  isArrowFunctionExpression,
15
12
  isVariableDeclarator,
16
- isClassProperty,
17
- isTSPropertySignature,
18
- isClassBody,
19
13
  isBinaryExpression,
20
14
  isClassMethod,
21
15
  isDecorator,
22
16
  } = types;
23
17
 
24
- const isProperty = satisfy([isClassProperty, isTSPropertySignature]);
25
-
26
18
  const isInsideVar = (path) => {
27
19
  const {parentPath} = path;
28
20
 
@@ -37,30 +29,6 @@ const isInsideVar = (path) => {
37
29
 
38
30
  const hasDecoratorsWithComments = (path) => path?.parentPath.node?.decorators;
39
31
 
40
- function isCommentOnPreviousLine(path) {
41
- const {
42
- loc,
43
- leadingComments,
44
- } = path.node;
45
-
46
- if (!isProperty(path))
47
- return false;
48
-
49
- const [comment] = leadingComments;
50
- const {line} = comment.loc.start;
51
-
52
- return line <= loc.start.line - 1;
53
- }
54
-
55
- const isFirst = (path) => {
56
- const {parentPath} = path;
57
-
58
- if (isClassBody(parentPath) && isClassProperty(path))
59
- return path === parentPath.get('body')[0];
60
-
61
- return false;
62
- };
63
-
64
32
  export const parseLeadingComments = (path, printer, semantics, {currentTraverse = {}} = {}) => {
65
33
  const {
66
34
  print,
@@ -91,18 +59,13 @@ export const parseLeadingComments = (path, printer, semantics, {currentTraverse
91
59
 
92
60
  const insideFn = (path.parentPath.isFunction() || path.parentPath.isTSDeclareMethod()) && !path.isTSTypeParameterDeclaration();
93
61
 
94
- const propIs = isProperty(path);
95
- const isIndent = isFirst(path)
96
- || !looksLikeSwitchCase
97
- && !path.isClassMethod()
98
- && !insideFn
99
- && !propIs;
62
+ const isIndent = !looksLikeSwitchCase && !path.isClassMethod() && !insideFn;
100
63
 
101
64
  const count = leadingComments.length - 1;
102
65
 
103
66
  for (const [index, {type, value}] of leadingComments.entries()) {
104
67
  if (type === 'CommentLine') {
105
- if (index || !path.isClassProperty() && !path.isBinaryExpression())
68
+ if (index || !path.isBinaryExpression())
106
69
  maybe.indent(isIndent);
107
70
 
108
71
  maybeInsideFn(insideFn, {
@@ -110,14 +73,10 @@ export const parseLeadingComments = (path, printer, semantics, {currentTraverse
110
73
  indent,
111
74
  });
112
75
 
113
- if (isCommentOnPreviousLine(path))
114
- maybe.print.breakline(!isFirst(path));
115
-
116
76
  print(`//${value}`);
117
77
 
118
78
  if (index === count) {
119
- maybe.print.breakline(propIs);
120
- maybe.print.newline(!propIs);
79
+ print.newline();
121
80
 
122
81
  if (isClassMethod(path)) {
123
82
  indent();
@@ -1,5 +1,8 @@
1
1
  import {types} from '@putout/babel';
2
- import {exists} from '#is';
2
+ import {
3
+ exists,
4
+ hasTrailingComment,
5
+ } from '#is';
3
6
  import {maybePrintTypeAnnotation} from '../../maybe/maybe-type-annotation.js';
4
7
  import {maybeDecorators} from '../../maybe/maybe-decorators.js';
5
8
  import {printKey} from '../object-expression/print-key.js';
@@ -47,7 +50,43 @@ const processClassProperty = maybeDecorators((path, printer) => {
47
50
  print.newline();
48
51
  });
49
52
 
50
- export const ClassProperty = processClassProperty;
53
+ export const ClassProperty = (path, printer, semantics) => {
54
+ processClassProperty(path, printer, semantics);
55
+ };
56
+
57
+ const isFirst = (path) => path === path.parentPath.get('body')[0];
58
+
59
+ ClassProperty.printLeadingCommentLine = (path, printer, semantics, {index, isLast, isPreviousLine, printComment}) => {
60
+ const {print, maybe} = printer;
61
+
62
+ if (hasTrailingComment(path.getPrevSibling()))
63
+ return;
64
+
65
+ const first = isFirst(path);
66
+
67
+ maybe.indent(index && first);
68
+ maybe.print.breakline(isPreviousLine() && !first);
69
+ printComment();
70
+
71
+ if (isLast) {
72
+ print.breakline();
73
+ return;
74
+ }
75
+
76
+ print.newline();
77
+ };
78
+
79
+ ClassProperty.printLeadingCommentBlock = (path, printer, semantics, {index}) => {
80
+ const {print} = printer;
81
+
82
+ if (hasTrailingComment(path.getPrevSibling()))
83
+ return;
84
+
85
+ const {value} = path.node.leadingComments[index];
86
+
87
+ print(`/*${value}*/`);
88
+ print.breakline();
89
+ };
51
90
  export const ClassPrivateProperty = processClassProperty;
52
91
 
53
92
  export const PrivateName = (path, {print}) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "18.52.3",
3
+ "version": "18.52.4",
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",