@putout/printer 12.28.0 → 12.30.0

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
+ 2025.02.09, v12.30.0
2
+
3
+ feature:
4
+ - a47b560 @putout/printer: introduce Comments Printer so each node can handle printing comments
5
+
6
+ 2025.02.09, v12.29.0
7
+
8
+ feature:
9
+ - 6d8a215 @putout/printer: ParenthesizedExpression: add
10
+
1
11
  2025.02.08, v12.28.0
2
12
 
3
13
  feature:
@@ -0,0 +1,19 @@
1
+ 'use strict';
2
+
3
+ const createPrintComment = (fn, value) => () => fn(`//${value}`);
4
+
5
+ module.exports.hasCommentsPrinter = (currentTraverse) => currentTraverse.printLeadingCommentLine;
6
+
7
+ module.exports.printComments = (path, printer, semantics, {currentTraverse}) => {
8
+ const {print} = printer;
9
+ const {leadingComments} = path.node;
10
+ const {printLeadingCommentLine} = currentTraverse;
11
+
12
+ for (const [index, {type, value}] of leadingComments.entries()) {
13
+ if (type === 'CommentLine')
14
+ printLeadingCommentLine(path, printer, semantics, {
15
+ index,
16
+ printComment: createPrintComment(print, value),
17
+ });
18
+ }
19
+ };
@@ -10,6 +10,11 @@ const {
10
10
  const {markBefore} = require('../mark');
11
11
  const {maybeInsideFn} = require('./maybe-inside-fn');
12
12
 
13
+ const {
14
+ hasCommentsPrinter,
15
+ printComments,
16
+ } = require('./comments-printer/comments-printer');
17
+
13
18
  const {
14
19
  isArrowFunctionExpression,
15
20
  isObjectProperty,
@@ -47,9 +52,6 @@ const hasDecoratorsWithComments = (path) => {
47
52
  if (isDecorator(path))
48
53
  return false;
49
54
 
50
- if (path.node.decorators)
51
- return true;
52
-
53
55
  return path?.parentPath.node?.decorators;
54
56
  };
55
57
 
@@ -90,7 +92,13 @@ const isFirst = (path) => {
90
92
  return false;
91
93
  };
92
94
 
93
- module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics) => {
95
+ module.exports.parseLeadingComments = (path, printer, semantics, {currentTraverse = {}} = {}) => {
96
+ const {
97
+ print,
98
+ maybe,
99
+ indent,
100
+ } = printer;
101
+
94
102
  if (!semantics.comments)
95
103
  return;
96
104
 
@@ -99,6 +107,11 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
99
107
  if (!leadingComments?.length)
100
108
  return;
101
109
 
110
+ if (hasCommentsPrinter(currentTraverse))
111
+ return printComments(path, printer, semantics, {
112
+ currentTraverse,
113
+ });
114
+
102
115
  if (hasDecoratorsWithComments(path))
103
116
  return;
104
117
 
@@ -42,6 +42,7 @@ const {StaticBlock} = require('./class/static-block');
42
42
  const {RecordExpression} = require('./object-expression/record-expression');
43
43
  const {TupleExpression} = require('./array-expression/tuple-expression');
44
44
  const {ImportExpression} = require('./import-expression');
45
+ const {ParenthesizedExpression} = require('./parenthesized-expression/parenthesized-expression');
45
46
 
46
47
  module.exports = {
47
48
  ...functions,
@@ -65,6 +66,7 @@ module.exports = {
65
66
  ObjectExpression,
66
67
  ObjectProperty,
67
68
  ObjectPattern,
69
+ ParenthesizedExpression,
68
70
  PrivateName,
69
71
  RestElement,
70
72
  ImportExpression,
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ const {types} = require('@putout/babel');
4
+ const {isJSXElement} = types;
5
+
6
+ const condition = (path) => {
7
+ const {expression} = path.node;
8
+ return !isJSXElement(expression);
9
+ };
10
+
11
+ module.exports.ParenthesizedExpression = {
12
+ before(path, {print}) {
13
+ print('(');
14
+ },
15
+ condition,
16
+ print(path, {print}) {
17
+ print('__expression');
18
+ },
19
+ after(path, {print}) {
20
+ print(')');
21
+ },
22
+ };
@@ -48,6 +48,9 @@ function condition(path) {
48
48
  if (path.parentPath.isReturnStatement())
49
49
  return true;
50
50
 
51
+ if (path.parentPath.isParenthesizedExpression())
52
+ return true;
53
+
51
54
  if (path.node.extra?.parenthesized)
52
55
  return true;
53
56
 
@@ -219,7 +219,10 @@ module.exports.tokenize = (ast, overrides) => {
219
219
  maybeThrow(!currentTraverse, path, `☝️ Node type '{{ type }}' is not supported yet by @putout/printer: '{{ path }}'`);
220
220
 
221
221
  const currentIndent = i;
222
- parseLeadingComments(path, printer, semantics);
222
+ parseLeadingComments(path, printer, semantics, {
223
+ currentTraverse,
224
+ });
225
+
223
226
  // this is main thing
224
227
  maybeVisitor(currentTraverse, path, printer, semantics);
225
228
  parseTrailingComments(path, printer, semantics);
@@ -40,5 +40,19 @@ module.exports.TSParameterProperty = (path, {print, maybe, indent}) => {
40
40
  }
41
41
 
42
42
  print('__parameter');
43
- maybe.print.breakline(decoratorsLength);
43
+
44
+ if (hasLeadingComment(path) && !path.node.decorators)
45
+ print.breakline();
46
+ };
47
+ module.exports.TSParameterProperty.printLeadingCommentLine = (path, printer, semantics, {printComment}) => {
48
+ const {indent, print} = printer;
49
+
50
+ if (path.parentPath.isClassMethod() && !path.node.decorators) {
51
+ indent.inc();
52
+ print.breakline();
53
+
54
+ printComment();
55
+ print.breakline();
56
+ indent.dec();
57
+ }
44
58
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "12.28.0",
3
+ "version": "12.30.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Simplest possible opinionated Babel AST printer for 🐊Putout",