@putout/printer 18.52.2 → 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,13 @@
1
+ 2026.11.22, v18.52.4
2
+
3
+ feature:
4
+ - 63f30d8e @putout/printer: ClassProperty: comments: simplify
5
+
6
+ 2026.11.22, v18.52.3
7
+
8
+ feature:
9
+ - 591947ec @putout/printer: SpreadElement: comments: simplify
10
+
1
11
  2026.11.22, v18.52.2
2
12
 
3
13
  feature:
@@ -0,0 +1,22 @@
1
+ export const isFirstProperty = (path) => path === path.parentPath.get('properties')[0];
2
+
3
+ export const printPropertyLeadingCommentLine = (path, printer, semantics, {printComment, isLast, isPreviousLine}) => {
4
+ const {print, maybe} = printer;
5
+ const first = isFirstProperty(path);
6
+
7
+ maybe.indent(first);
8
+
9
+ if (isPreviousLine())
10
+ maybe.print.breakline(!first);
11
+ else
12
+ print.space();
13
+
14
+ printComment();
15
+
16
+ if (isLast) {
17
+ print.breakline();
18
+ return;
19
+ }
20
+
21
+ print.newline();
22
+ };
@@ -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,21 +10,11 @@ import {
13
10
  const {
14
11
  isArrowFunctionExpression,
15
12
  isVariableDeclarator,
16
- isClassProperty,
17
- isTSPropertySignature,
18
- isSpreadElement,
19
- isClassBody,
20
13
  isBinaryExpression,
21
14
  isClassMethod,
22
15
  isDecorator,
23
16
  } = types;
24
17
 
25
- const isProperty = satisfy([
26
- isClassProperty,
27
- isTSPropertySignature,
28
- isSpreadElement,
29
- ]);
30
-
31
18
  const isInsideVar = (path) => {
32
19
  const {parentPath} = path;
33
20
 
@@ -42,43 +29,6 @@ const isInsideVar = (path) => {
42
29
 
43
30
  const hasDecoratorsWithComments = (path) => path?.parentPath.node?.decorators;
44
31
 
45
- function isCommentOfPrevious(path) {
46
- const [comment] = path.node.leadingComments;
47
- const {line} = comment.loc.start;
48
-
49
- return path.getPrevSibling().node?.loc.start.line === line;
50
- }
51
-
52
- function isCommentOnPreviousLine(path) {
53
- const {
54
- loc,
55
- leadingComments,
56
- } = path.node;
57
-
58
- if (!isProperty(path))
59
- return false;
60
-
61
- const [comment] = leadingComments;
62
- const {line} = comment.loc.start;
63
-
64
- if (isCommentOfPrevious(path))
65
- return false;
66
-
67
- return line <= loc.start.line - 1;
68
- }
69
-
70
- const isFirst = (path) => {
71
- const {parentPath} = path;
72
-
73
- if (path === parentPath.get('properties')[0])
74
- return true;
75
-
76
- if (isClassBody(parentPath) && isClassProperty(path))
77
- return path === parentPath.get('body')[0];
78
-
79
- return false;
80
- };
81
-
82
32
  export const parseLeadingComments = (path, printer, semantics, {currentTraverse = {}} = {}) => {
83
33
  const {
84
34
  print,
@@ -109,18 +59,13 @@ export const parseLeadingComments = (path, printer, semantics, {currentTraverse
109
59
 
110
60
  const insideFn = (path.parentPath.isFunction() || path.parentPath.isTSDeclareMethod()) && !path.isTSTypeParameterDeclaration();
111
61
 
112
- const propIs = isProperty(path);
113
- const isIndent = isFirst(path)
114
- || !looksLikeSwitchCase
115
- && !path.isClassMethod()
116
- && !insideFn
117
- && !propIs;
62
+ const isIndent = !looksLikeSwitchCase && !path.isClassMethod() && !insideFn;
118
63
 
119
64
  const count = leadingComments.length - 1;
120
65
 
121
66
  for (const [index, {type, value}] of leadingComments.entries()) {
122
67
  if (type === 'CommentLine') {
123
- if (index || !path.isClassProperty() && !path.isBinaryExpression())
68
+ if (index || !path.isBinaryExpression())
124
69
  maybe.indent(isIndent);
125
70
 
126
71
  maybeInsideFn(insideFn, {
@@ -128,16 +73,10 @@ export const parseLeadingComments = (path, printer, semantics, {currentTraverse
128
73
  indent,
129
74
  });
130
75
 
131
- if (isCommentOnPreviousLine(path))
132
- maybe.print.breakline(!isFirst(path));
133
- else
134
- maybe.print.space(propIs && !path.isClassProperty());
135
-
136
76
  print(`//${value}`);
137
77
 
138
78
  if (index === count) {
139
- maybe.print.breakline(propIs);
140
- maybe.print.newline(!propIs);
79
+ print.newline();
141
80
 
142
81
  if (isClassMethod(path)) {
143
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}) => {
@@ -1,5 +1,9 @@
1
1
  import {isConcatenation} from '#is-concatenation';
2
2
  import {markBefore} from '#mark';
3
+ import {
4
+ isFirstProperty,
5
+ printPropertyLeadingCommentLine,
6
+ } from '../../comments/comments-printer/property-comments.js';
3
7
  import {isMultiline} from './object-expression.js';
4
8
  import {printKey} from './print-key.js';
5
9
  import {isCommaAfterProperty} from './comma.js';
@@ -33,32 +37,11 @@ export const ObjectProperty = (path, printer, semantics) => {
33
37
  write.space();
34
38
  };
35
39
 
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
- };
40
+ ObjectProperty.printLeadingCommentLine = printPropertyLeadingCommentLine;
58
41
 
59
42
  ObjectProperty.printLeadingCommentBlock = (path, printer, semantics, {printComment}) => {
60
43
  const {maybe, indent} = printer;
61
- const first = isFirst(path);
44
+ const first = isFirstProperty(path);
62
45
 
63
46
  maybe.indent(first);
64
47
  maybe.print.breakline(!first);
@@ -1,4 +1,8 @@
1
1
  import {hasTrailingComment} from '#is';
2
+ import {
3
+ isFirstProperty,
4
+ printPropertyLeadingCommentLine,
5
+ } from '../../comments/comments-printer/property-comments.js';
2
6
 
3
7
  export const SpreadElement = (path, printer) => {
4
8
  const {print} = printer;
@@ -8,3 +12,14 @@ export const SpreadElement = (path, printer) => {
8
12
  if (hasTrailingComment(path))
9
13
  print(',');
10
14
  };
15
+
16
+ SpreadElement.printLeadingCommentLine = printPropertyLeadingCommentLine;
17
+
18
+ SpreadElement.printLeadingCommentBlock = (path, printer, semantics, {index}) => {
19
+ const {print, maybe} = printer;
20
+ const {value} = path.node.leadingComments[index];
21
+
22
+ maybe.indent(isFirstProperty(path));
23
+ print(`/*${value}*/`);
24
+ print.space();
25
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "18.52.2",
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",