@putout/printer 15.18.1 → 15.19.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.08.05, v15.19.0
2
+
3
+ feature:
4
+ - f518cd6 @putout/printer: SequenceExpression: leadingComments inside AssignmentExpressions: improve
5
+
6
+ 2025.08.05, v15.18.2
7
+
8
+ feature:
9
+ - a7fd7cc @putout/printer: AssignmentExpression: comments: inside SequenceExpression
10
+
1
11
  2025.08.05, v15.18.1
2
12
 
3
13
  feature:
@@ -2,15 +2,36 @@
2
2
 
3
3
  const {types} = require('@putout/babel');
4
4
  const {hasLeadingComment} = require('#is');
5
- const noop = () => {};
6
5
  const {isReturnStatement} = types;
7
6
 
8
- module.exports.printLeadingCommentLine = noop;
7
+ module.exports.printLeadingCommentLine = (path, printer, semantics, {printComment, isLast}) => {
8
+ const {parentPath} = path;
9
+ const {print, maybe} = printer;
10
+
11
+ if (isReturnStatement(parentPath))
12
+ return;
13
+
14
+ maybe.print.breakline(!isLast);
15
+ printComment();
16
+ print.breakline();
17
+ };
18
+
19
+ module.exports.printLeadingCommentBlock = (path, printer, semantics, {printComment}) => {
20
+ const {parentPath} = path;
21
+ const {print} = printer;
22
+
23
+ if (isReturnStatement(parentPath))
24
+ return;
25
+
26
+ printComment();
27
+ print.indent();
28
+ };
9
29
 
10
30
  module.exports.maybeInsideReturnWithCommentStart = (path, {print, indent}) => {
11
31
  const {parentPath} = path;
32
+ const is = isReturnStatement(parentPath);
12
33
 
13
- if (isReturnStatement(parentPath) && hasLeadingComment(path)) {
34
+ if (is && hasLeadingComment(path)) {
14
35
  indent.inc();
15
36
  const {leadingComments} = path.node;
16
37
 
@@ -28,8 +49,9 @@ module.exports.maybeInsideReturnWithCommentStart = (path, {print, indent}) => {
28
49
 
29
50
  module.exports.maybeInsideReturnWithCommentEnd = (path, {print, indent}) => {
30
51
  const {parentPath} = path;
52
+ const is = isReturnStatement(parentPath);
31
53
 
32
- if (!isReturnStatement(parentPath) || !hasLeadingComment(path))
54
+ if (!is || !hasLeadingComment(path))
33
55
  return;
34
56
 
35
57
  indent.dec();
@@ -6,6 +6,7 @@ const {
6
6
  printLeadingCommentLine,
7
7
  maybeInsideReturnWithCommentEnd,
8
8
  maybeInsideReturnWithCommentStart,
9
+ printLeadingCommentBlock,
9
10
  } = require('./assignment-expression-comments');
10
11
 
11
12
  const {maybeParens} = require('../../maybe/maybe-parens');
@@ -38,3 +39,4 @@ module.exports.AssignmentExpression = maybeParens({
38
39
  });
39
40
 
40
41
  module.exports.AssignmentExpression.printLeadingCommentLine = printLeadingCommentLine;
42
+ module.exports.AssignmentExpression.printLeadingCommentBlock = printLeadingCommentBlock;
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const {hasLeadingComment} = require('#is');
4
+
4
5
  const {isParens} = require('../../maybe/maybe-parens');
5
6
 
6
7
  module.exports.condition = (path, printer, semantics) => {
@@ -1,40 +1,18 @@
1
1
  'use strict';
2
2
 
3
- module.exports.maybeWriteLeftBrace = (path, printer, semantics) => {
4
- maybeWriteBrace(path, printer, semantics, {
5
- brace: '(',
6
- });
7
- };
8
-
9
- module.exports.maybeWriteRightBrace = (path, printer, semantics) => {
10
- maybeWriteBrace(path, printer, semantics, {
11
- brace: ')',
12
- });
13
- };
14
-
15
- function maybeWriteBrace(path, printer, semantics, {brace}) {
3
+ module.exports.condition = (path, printer, semantics) => {
16
4
  const {parentPath} = path;
17
5
  const {type} = parentPath;
18
6
  const {roundBraces} = semantics;
19
- const {write} = printer;
20
-
21
- if (type === 'ArrowFunctionExpression') {
22
- write(brace);
23
- return;
24
- }
25
7
 
26
- if (type === 'ConditionalExpression' && path !== parentPath.get('test')) {
27
- write(brace);
28
- return;
29
- }
8
+ if (type === 'ArrowFunctionExpression')
9
+ return true;
30
10
 
31
- if (type === 'LogicalExpression') {
32
- write(brace);
33
- return;
34
- }
11
+ if (type === 'ConditionalExpression' && path !== parentPath.get('test'))
12
+ return true;
35
13
 
36
- if (!roundBraces.sequence)
37
- return;
14
+ if (type === 'LogicalExpression')
15
+ return true;
38
16
 
39
- write(brace);
40
- }
17
+ return roundBraces.sequence;
18
+ };
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ const {hasLeadingComment} = require('#is');
4
+ const noop = () => {};
5
+
6
+ module.exports.printLeadingCommentLine = noop;
7
+ module.exports.printLeadingCommentBlock = noop;
8
+
9
+ module.exports.maybePrintComments = (path, {print}) => {
10
+ if (hasLeadingComment(path)) {
11
+ const {leadingComments} = path.node;
12
+
13
+ for (const {type, value} of leadingComments) {
14
+ if (type === 'CommentLine')
15
+ print(`//${value}`);
16
+ else
17
+ print(`/*${value}*/`);
18
+
19
+ print.breakline();
20
+ }
21
+ }
22
+ };
@@ -1,23 +1,53 @@
1
1
  'use strict';
2
2
 
3
+ const {hasLeadingComment} = require('#is');
4
+ const {maybeParens} = require('../../maybe/maybe-parens');
5
+ const {condition} = require('./maybe-write-brace');
6
+
3
7
  const {
4
- maybeWriteLeftBrace,
5
- maybeWriteRightBrace,
6
- } = require('./maybe-write-brace');
8
+ maybePrintComments,
9
+ printLeadingCommentLine,
10
+ printLeadingCommentBlock,
11
+ } = require('./sequence-expression-comments');
12
+
13
+ module.exports.SequenceExpression = maybeParens({
14
+ checkParens: false,
15
+ condition,
16
+ print(path, printer) {
17
+ const {
18
+ write,
19
+ traverse,
20
+ indent,
21
+ maybe,
22
+ } = printer;
23
+
24
+ const expressions = path.get('expressions');
25
+ const n = expressions.length - 1;
26
+
27
+ const withComment = hasLeadingComment(path);
28
+
29
+ if (withComment) {
30
+ indent.inc();
31
+ write.breakline();
32
+ maybePrintComments(path, printer);
33
+ }
34
+
35
+ for (const [index, expression] of expressions.entries()) {
36
+ traverse(expression);
37
+
38
+ if (index < n) {
39
+ write(',');
40
+ maybe.write.space(!withComment);
41
+ maybe.print.breakline(withComment);
42
+ }
43
+ }
44
+
45
+ if (withComment) {
46
+ indent.dec();
47
+ write.breakline();
48
+ }
49
+ },
50
+ });
7
51
 
8
- module.exports.SequenceExpression = (path, printer, semantics) => {
9
- const {maybe, traverse} = printer;
10
-
11
- const expressions = path.get('expressions');
12
- const n = expressions.length - 1;
13
-
14
- maybeWriteLeftBrace(path, printer, semantics);
15
-
16
- for (const [index, expression] of expressions.entries()) {
17
- traverse(expression);
18
- maybe.write(index < n, ',');
19
- maybe.write.space(index < n);
20
- }
21
-
22
- maybeWriteRightBrace(path, printer, semantics);
23
- };
52
+ module.exports.SequenceExpression.printLeadingCommentLine = printLeadingCommentLine;
53
+ module.exports.SequenceExpression.printLeadingCommentBlock = printLeadingCommentBlock;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "15.18.1",
3
+ "version": "15.19.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",