@putout/printer 18.52.4 → 18.52.6

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.23, v18.52.6
2
+
3
+ fix:
4
+ - 99e2d3ed @putout/printer: loops: inside Function
5
+
6
+ 2026.11.22, v18.52.5
7
+
8
+ feature:
9
+ - d16fbe76 @putout/printer: TSEnumMember: comments: simplify
10
+ - 9ef70bdd @putout/printer: Directive: comments: simplify
11
+ - 4b778dcd @putout/printer: ClassMethod: comments: simplify
12
+ - a7745403 @putout/printer: SwitchStatement: comments
13
+
1
14
  2026.11.22, v18.52.4
2
15
 
3
16
  feature:
@@ -11,7 +11,6 @@ const {
11
11
  isArrowFunctionExpression,
12
12
  isVariableDeclarator,
13
13
  isBinaryExpression,
14
- isClassMethod,
15
14
  isDecorator,
16
15
  } = types;
17
16
 
@@ -52,14 +51,12 @@ export const parseLeadingComments = (path, printer, semantics, {currentTraverse
52
51
  if (hasDecoratorsWithComments(path))
53
52
  return;
54
53
 
55
- const looksLikeSwitchCase = path.isSwitchCase();
56
-
57
- if (!looksLikeSwitchCase && hasTrailingComment(path.getPrevSibling()))
54
+ if (hasTrailingComment(path.getPrevSibling()))
58
55
  return;
59
56
 
60
57
  const insideFn = (path.parentPath.isFunction() || path.parentPath.isTSDeclareMethod()) && !path.isTSTypeParameterDeclaration();
61
58
 
62
- const isIndent = !looksLikeSwitchCase && !path.isClassMethod() && !insideFn;
59
+ const isIndent = !insideFn;
63
60
 
64
61
  const count = leadingComments.length - 1;
65
62
 
@@ -78,9 +75,7 @@ export const parseLeadingComments = (path, printer, semantics, {currentTraverse
78
75
  if (index === count) {
79
76
  print.newline();
80
77
 
81
- if (isClassMethod(path)) {
82
- indent();
83
- } else if (isBinaryExpression(path) || isDecorator(path)) {
78
+ if (isBinaryExpression(path) || isDecorator(path)) {
84
79
  indent.inc();
85
80
  indent();
86
81
  indent.dec();
@@ -101,15 +96,11 @@ export const parseLeadingComments = (path, printer, semantics, {currentTraverse
101
96
  if (type === 'CommentBlock') {
102
97
  maybe.indent(isIndent);
103
98
 
104
- const looksLikeMethod = path.isClassMethod();
105
- const looksLikeDirective = path.isDirective();
106
-
107
99
  print(`/*${value}*/`);
108
100
 
109
- if (path.isStatement() || path.isTSEnumMember() || looksLikeDirective || looksLikeMethod || looksLikeSwitchCase) {
101
+ if (path.isStatement()) {
110
102
  print.newline();
111
103
  markBefore(path);
112
- maybe.indent(looksLikeMethod || looksLikeSwitchCase);
113
104
  }
114
105
 
115
106
  continue;
@@ -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
+ };
@@ -231,3 +231,13 @@ export const isLastArgInCall = createTypeChecker([
231
231
  ['-: parentPath -> !CallExpression'],
232
232
  ['+', isPathLastArg],
233
233
  ]);
234
+
235
+ const hasBodyStatements = (path) => path.node.body.body.length;
236
+
237
+ export const isLastEmptyInBlock = createTypeChecker([
238
+ ['-', isNext],
239
+ ['-', hasBodyStatements],
240
+ ['+: parentPath -> BlockStatement'],
241
+ ['-: parentPath -> !LabeledStatement'],
242
+ ['+: parentPath.parentPath -> BlockStatement'],
243
+ ]);
@@ -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
  };
@@ -1,5 +1,10 @@
1
- export const ForInStatement = (path, {print, indent}) => {
2
- indent();
1
+ import {
2
+ isInsideLabel,
3
+ isLastEmptyInBlock,
4
+ } from '#is';
5
+
6
+ export const ForInStatement = (path, {print, maybe}) => {
7
+ maybe.indent(!isInsideLabel(path));
3
8
  print('for');
4
9
  print.space();
5
10
  print('(');
@@ -12,4 +17,5 @@ export const ForInStatement = (path, {print, indent}) => {
12
17
  print.space();
13
18
 
14
19
  print('__body');
20
+ maybe.print.newline(isLastEmptyInBlock(path));
15
21
  };
@@ -1,9 +1,10 @@
1
+ import {markAfter} from '#mark';
1
2
  import {
2
3
  isInsideLabel,
3
4
  exists,
4
5
  callWithNext,
6
+ isLastEmptyInBlock,
5
7
  } from '#is';
6
- import {markAfter} from '#mark';
7
8
 
8
9
  export const ForStatement = {
9
10
  print(path, {print, maybe}) {
@@ -30,6 +31,7 @@ export const ForStatement = {
30
31
  if (body.body) {
31
32
  print.space();
32
33
  print('__body');
34
+ maybe.print.newline(isLastEmptyInBlock(path));
33
35
 
34
36
  return;
35
37
  }
@@ -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,4 +1,8 @@
1
- import {isInsideLabel, isNext} from '#is';
1
+ import {
2
+ isInsideLabel,
3
+ isNext,
4
+ isLastEmptyInBlock,
5
+ } from '#is';
2
6
  import {markAfter} from '#mark';
3
7
  import {
4
8
  printTrailingCommentBlock,
@@ -14,7 +18,7 @@ export const WhileStatement = {
14
18
  before: (path, {indent}) => {
15
19
  indent();
16
20
  },
17
- print(path, {print, indent}) {
21
+ print(path, {print, indent, maybe}) {
18
22
  print('while');
19
23
  print.space();
20
24
  print('(');
@@ -24,6 +28,7 @@ export const WhileStatement = {
24
28
  if (path.node.body.body) {
25
29
  print.space();
26
30
  print('__body');
31
+ maybe.print.newline(isLastEmptyInBlock(path));
27
32
 
28
33
  return;
29
34
  }
@@ -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.4",
3
+ "version": "18.52.6",
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",