@putout/printer 18.52.3 → 18.52.5

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,16 @@
1
+ 2026.11.22, v18.52.5
2
+
3
+ feature:
4
+ - d16fbe76 @putout/printer: TSEnumMember: comments: simplify
5
+ - 9ef70bdd @putout/printer: Directive: comments: simplify
6
+ - 4b778dcd @putout/printer: ClassMethod: comments: simplify
7
+ - a7745403 @putout/printer: SwitchStatement: comments
8
+
9
+ 2026.11.22, v18.52.4
10
+
11
+ feature:
12
+ - 63f30d8e @putout/printer: ClassProperty: comments: simplify
13
+
1
14
  2026.11.22, v18.52.3
2
15
 
3
16
  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,10 @@ import {
13
10
  const {
14
11
  isArrowFunctionExpression,
15
12
  isVariableDeclarator,
16
- isClassProperty,
17
- isTSPropertySignature,
18
- isClassBody,
19
13
  isBinaryExpression,
20
- isClassMethod,
21
14
  isDecorator,
22
15
  } = types;
23
16
 
24
- const isProperty = satisfy([isClassProperty, isTSPropertySignature]);
25
-
26
17
  const isInsideVar = (path) => {
27
18
  const {parentPath} = path;
28
19
 
@@ -37,30 +28,6 @@ const isInsideVar = (path) => {
37
28
 
38
29
  const hasDecoratorsWithComments = (path) => path?.parentPath.node?.decorators;
39
30
 
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
31
  export const parseLeadingComments = (path, printer, semantics, {currentTraverse = {}} = {}) => {
65
32
  const {
66
33
  print,
@@ -84,25 +51,18 @@ export const parseLeadingComments = (path, printer, semantics, {currentTraverse
84
51
  if (hasDecoratorsWithComments(path))
85
52
  return;
86
53
 
87
- const looksLikeSwitchCase = path.isSwitchCase();
88
-
89
- if (!looksLikeSwitchCase && hasTrailingComment(path.getPrevSibling()))
54
+ if (hasTrailingComment(path.getPrevSibling()))
90
55
  return;
91
56
 
92
57
  const insideFn = (path.parentPath.isFunction() || path.parentPath.isTSDeclareMethod()) && !path.isTSTypeParameterDeclaration();
93
58
 
94
- const propIs = isProperty(path);
95
- const isIndent = isFirst(path)
96
- || !looksLikeSwitchCase
97
- && !path.isClassMethod()
98
- && !insideFn
99
- && !propIs;
59
+ const isIndent = !insideFn;
100
60
 
101
61
  const count = leadingComments.length - 1;
102
62
 
103
63
  for (const [index, {type, value}] of leadingComments.entries()) {
104
64
  if (type === 'CommentLine') {
105
- if (index || !path.isClassProperty() && !path.isBinaryExpression())
65
+ if (index || !path.isBinaryExpression())
106
66
  maybe.indent(isIndent);
107
67
 
108
68
  maybeInsideFn(insideFn, {
@@ -110,18 +70,12 @@ export const parseLeadingComments = (path, printer, semantics, {currentTraverse
110
70
  indent,
111
71
  });
112
72
 
113
- if (isCommentOnPreviousLine(path))
114
- maybe.print.breakline(!isFirst(path));
115
-
116
73
  print(`//${value}`);
117
74
 
118
75
  if (index === count) {
119
- maybe.print.breakline(propIs);
120
- maybe.print.newline(!propIs);
76
+ print.newline();
121
77
 
122
- if (isClassMethod(path)) {
123
- indent();
124
- } else if (isBinaryExpression(path) || isDecorator(path)) {
78
+ if (isBinaryExpression(path) || isDecorator(path)) {
125
79
  indent.inc();
126
80
  indent();
127
81
  indent.dec();
@@ -142,15 +96,11 @@ export const parseLeadingComments = (path, printer, semantics, {currentTraverse
142
96
  if (type === 'CommentBlock') {
143
97
  maybe.indent(isIndent);
144
98
 
145
- const looksLikeMethod = path.isClassMethod();
146
- const looksLikeDirective = path.isDirective();
147
-
148
99
  print(`/*${value}*/`);
149
100
 
150
- if (path.isStatement() || path.isTSEnumMember() || looksLikeDirective || looksLikeMethod || looksLikeSwitchCase) {
101
+ if (path.isStatement()) {
151
102
  print.newline();
152
103
  markBefore(path);
153
- maybe.indent(looksLikeMethod || looksLikeSwitchCase);
154
104
  }
155
105
 
156
106
  continue;
@@ -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}) => {
@@ -2,6 +2,7 @@ import {
2
2
  isNext,
3
3
  hasTrailingComment,
4
4
  } from '#is';
5
+ import {markBefore} from '#mark';
5
6
  import {printReturnType} from '#return-type';
6
7
  import {maybeDecorators} from '../../../maybe/maybe-decorators.js';
7
8
  import {printKey} from '../../object-expression/print-key.js';
@@ -58,3 +59,32 @@ export const ClassMethod = {
58
59
  };
59
60
 
60
61
  export const ClassPrivateMethod = ClassMethod;
62
+
63
+ ClassMethod.printLeadingCommentLine = (path, printer, semantics, {index, isLast, printComment}) => {
64
+ const {
65
+ print,
66
+ maybe,
67
+ indent,
68
+ } = printer;
69
+
70
+ if (hasTrailingComment(path.getPrevSibling()))
71
+ return;
72
+
73
+ maybe.indent(Boolean(index));
74
+ printComment();
75
+ print.newline();
76
+
77
+ if (isLast)
78
+ indent();
79
+ };
80
+
81
+ ClassMethod.printLeadingCommentBlock = (path, printer, semantics, {printComment}) => {
82
+ const {indent} = printer;
83
+
84
+ if (hasTrailingComment(path.getPrevSibling()))
85
+ return;
86
+
87
+ printComment();
88
+ markBefore(path);
89
+ indent();
90
+ };
@@ -1,3 +1,5 @@
1
+ import {markBefore} from '#mark';
2
+
1
3
  export {TemplateLiteral} from './template-literal.js';
2
4
  export {Identifier} from './identifier.js';
3
5
  export {Decorator} from './decorator/decorator.js';
@@ -20,11 +22,33 @@ export const NumericLiteral = (path, {write}) => {
20
22
  write(raw || extra?.raw || value);
21
23
  };
22
24
 
23
- export const Directive = (path, {print, maybe}) => {
24
- maybe.print.breakline(path.node.leadingComments?.length);
25
+ export const Directive = (path, {print}) => {
25
26
  print('__value');
26
27
  };
27
28
 
29
+ const maybeHeaderBreak = (path, print) => {
30
+ if (path.parentPath.isProgram() && !path.getPrevSibling().node)
31
+ print.newline();
32
+ };
33
+
34
+ Directive.printLeadingCommentLine = (path, printer, semantics, {printComment}) => {
35
+ const {print, indent} = printer;
36
+
37
+ indent();
38
+ printComment();
39
+ print.newline();
40
+ maybeHeaderBreak(path, print);
41
+ };
42
+
43
+ Directive.printLeadingCommentBlock = (path, printer, semantics, {printComment}) => {
44
+ const {print, indent} = printer;
45
+
46
+ indent();
47
+ printComment();
48
+ markBefore(path);
49
+ maybeHeaderBreak(path, print);
50
+ };
51
+
28
52
  export const BooleanLiteral = (path, {write}) => {
29
53
  write(path.node.value);
30
54
  };
@@ -0,0 +1,18 @@
1
+ import {markBefore} from '#mark';
2
+
3
+ export const SwitchCase = {};
4
+
5
+ SwitchCase.printLeadingCommentLine = (path, printer, semantics, {printComment}) => {
6
+ const {print} = printer;
7
+
8
+ printComment();
9
+ print.newline();
10
+ };
11
+
12
+ SwitchCase.printLeadingCommentBlock = (path, printer, semantics, {printComment}) => {
13
+ const {indent} = printer;
14
+
15
+ printComment();
16
+ markBefore(path);
17
+ indent();
18
+ };
@@ -6,6 +6,7 @@ import {
6
6
  } from '#is';
7
7
  import {parseLeadingComments} from '#comments';
8
8
  import {createTypeChecker} from '#type-checker';
9
+ import {SwitchCase} from './switch-case.js';
9
10
  import {maybeSpaceAfterKeyword} from '../return-statement/maybe-space-after-keyword.js';
10
11
 
11
12
  const {isBlockStatement} = types;
@@ -37,7 +38,9 @@ export const SwitchStatement = {
37
38
 
38
39
  indent();
39
40
 
40
- parseLeadingComments(switchCase, printer, semantics);
41
+ parseLeadingComments(switchCase, printer, semantics, {
42
+ currentTraverse: SwitchCase,
43
+ });
41
44
 
42
45
  if (exists(test)) {
43
46
  print('case');
@@ -1,3 +1,5 @@
1
+ import {markBefore} from '#mark';
2
+
1
3
  export const TSEnumMember = (path, {print, indent}) => {
2
4
  const {initializer} = path.node;
3
5
  indent();
@@ -10,3 +12,19 @@ export const TSEnumMember = (path, {print, indent}) => {
10
12
  print('__initializer');
11
13
  }
12
14
  };
15
+
16
+ TSEnumMember.printLeadingCommentLine = (path, printer, semantics, {printComment}) => {
17
+ const {print, indent} = printer;
18
+
19
+ indent();
20
+ printComment();
21
+ print.newline();
22
+ };
23
+
24
+ TSEnumMember.printLeadingCommentBlock = (path, printer, semantics, {printComment}) => {
25
+ const {indent} = printer;
26
+
27
+ indent();
28
+ printComment();
29
+ markBefore(path);
30
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "18.52.3",
3
+ "version": "18.52.5",
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",