@putout/printer 15.17.0 → 15.18.1

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.18.1
2
+
3
+ feature:
4
+ - 59d5952 @putout/printer: AssignmentExpression: maybeParens
5
+
6
+ 2025.08.04, v15.18.0
7
+
8
+ feature:
9
+ - 3357c63 @putout/printer: AssignmentExpression: leadingComments: CommentBlock
10
+
1
11
  2025.08.04, v15.17.0
2
12
 
3
13
  feature:
@@ -2,9 +2,10 @@
2
2
 
3
3
  const {types} = require('@putout/babel');
4
4
  const {hasLeadingComment} = require('#is');
5
+ const noop = () => {};
5
6
  const {isReturnStatement} = types;
6
7
 
7
- module.exports.printLeadingCommentLine = ({parentPath}) => !isReturnStatement(parentPath);
8
+ module.exports.printLeadingCommentLine = noop;
8
9
 
9
10
  module.exports.maybeInsideReturnWithCommentStart = (path, {print, indent}) => {
10
11
  const {parentPath} = path;
@@ -14,8 +15,12 @@ module.exports.maybeInsideReturnWithCommentStart = (path, {print, indent}) => {
14
15
  const {leadingComments} = path.node;
15
16
 
16
17
  print.breakline();
17
- for (const {value} of leadingComments) {
18
- print(`//${value}`);
18
+ for (const {type, value} of leadingComments) {
19
+ if (type === 'CommentLine')
20
+ print(`//${value}`);
21
+ else
22
+ print(`/*${value}*/`);
23
+
19
24
  print.breakline();
20
25
  }
21
26
  }
@@ -1,11 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {types} = require('@putout/babel');
4
-
5
- const {
6
- maybePrintLeftBrace,
7
- maybePrintRightBrace,
8
- } = require('./maybe-write-brace');
3
+ const {condition} = require('./maybe-parens-condition');
9
4
 
10
5
  const {
11
6
  printLeadingCommentLine,
@@ -13,48 +8,33 @@ const {
13
8
  maybeInsideReturnWithCommentStart,
14
9
  } = require('./assignment-expression-comments');
15
10
 
16
- const {
17
- isExpressionStatement,
18
- isAssignmentExpression,
19
- } = types;
11
+ const {maybeParens} = require('../../maybe/maybe-parens');
12
+ const {printSeparator} = require('./print-separator');
20
13
 
21
14
  const isInsideBlock = ({parentPath}) => /BlockStatement|Program/.test(parentPath.type);
22
15
 
23
- module.exports.AssignmentExpression = (path, printer, semantics) => {
24
- const {print} = printer;
25
- const {operator} = path.node;
26
-
27
- maybePrintLeftBrace(path, printer, semantics);
28
- maybeInsideReturnWithCommentStart(path, printer);
29
-
30
- print('__left');
31
- print.space();
32
- print(operator);
33
-
34
- if (isMultiline(path))
35
- print.breakline();
36
- else
16
+ module.exports.AssignmentExpression = maybeParens({
17
+ checkParens: false,
18
+ condition,
19
+ print(path, printer) {
20
+ const {print} = printer;
21
+ const {operator} = path.node;
22
+
23
+ maybeInsideReturnWithCommentStart(path, printer);
24
+
25
+ print('__left');
37
26
  print.space();
38
-
39
- print('__right');
40
-
41
- if (isInsideBlock(path)) {
42
- print(';');
43
- print.breakline();
44
- }
45
-
46
- maybeInsideReturnWithCommentEnd(path, printer);
47
-
48
- maybePrintRightBrace(path, printer, semantics);
49
- };
27
+ print(operator);
28
+ printSeparator(path, printer);
29
+ print('__right');
30
+
31
+ if (isInsideBlock(path)) {
32
+ print(';');
33
+ print.breakline();
34
+ }
35
+
36
+ maybeInsideReturnWithCommentEnd(path, printer);
37
+ },
38
+ });
50
39
 
51
40
  module.exports.AssignmentExpression.printLeadingCommentLine = printLeadingCommentLine;
52
-
53
- function isMultiline(path) {
54
- const {right} = path.node;
55
-
56
- if (!path.parentPath.find(isExpressionStatement))
57
- return false;
58
-
59
- return isAssignmentExpression(right);
60
- }
@@ -0,0 +1,27 @@
1
+ 'use strict';
2
+
3
+ const {hasLeadingComment} = require('#is');
4
+ const {isParens} = require('../../maybe/maybe-parens');
5
+
6
+ module.exports.condition = (path, printer, semantics) => {
7
+ const {parentPath} = path;
8
+ const {type} = parentPath;
9
+ const {roundBraces} = semantics;
10
+
11
+ if (path.node.left.type === 'ObjectPattern')
12
+ return true;
13
+
14
+ if (type === 'LogicalExpression')
15
+ return true;
16
+
17
+ if (type === 'BinaryExpression')
18
+ return true;
19
+
20
+ if (type === 'UnaryExpression')
21
+ return true;
22
+
23
+ if (!roundBraces.assign && !hasLeadingComment(path))
24
+ return false;
25
+
26
+ return isParens(path);
27
+ };
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ const {types} = require('@putout/babel');
4
+ const {
5
+ isAssignmentExpression,
6
+ isExpressionStatement,
7
+ } = types;
8
+
9
+ module.exports.printSeparator = (path, {print}) => {
10
+ if (isMultiline(path))
11
+ print.breakline();
12
+ else
13
+ print.space();
14
+ };
15
+
16
+ function isMultiline(path) {
17
+ const {right} = path.node;
18
+
19
+ if (!path.parentPath.find(isExpressionStatement))
20
+ return false;
21
+
22
+ return isAssignmentExpression(right);
23
+ }
@@ -22,7 +22,14 @@ const maybeParensPrint = (print) => ({
22
22
  },
23
23
  });
24
24
 
25
- const maybeParensCondition = ({print, condition}) => ({
25
+ const maybeParensCondition = ({print, condition, checkParens = true}) => ({
26
26
  ...maybeParensPrint(print),
27
- condition: (path) => condition?.(path) || isParens(path),
27
+ condition: (path, print, semantics) => {
28
+ const is = condition?.(path, print, semantics);
29
+
30
+ if (!checkParens)
31
+ return is;
32
+
33
+ return is || isParens(path);
34
+ },
28
35
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "15.17.0",
3
+ "version": "15.18.1",
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",
@@ -1,51 +0,0 @@
1
- 'use strict';
2
-
3
- const {hasLeadingComment} = require('#is');
4
- const {isParens} = require('../../maybe/maybe-parens');
5
-
6
- module.exports.maybePrintLeftBrace = (path, printer, semantics) => {
7
- maybeWriteBrace(path, printer, semantics, {
8
- brace: '(',
9
- });
10
- };
11
-
12
- module.exports.maybePrintRightBrace = (path, printer, semantics) => {
13
- maybeWriteBrace(path, printer, semantics, {
14
- brace: ')',
15
- });
16
- };
17
-
18
- function maybeWriteBrace(path, printer, semantics, {brace}) {
19
- const {parentPath} = path;
20
- const {type} = parentPath;
21
- const {roundBraces} = semantics;
22
- const {write} = printer;
23
-
24
- if (path.node.left.type === 'ObjectPattern') {
25
- write(brace);
26
- return;
27
- }
28
-
29
- if (type === 'LogicalExpression') {
30
- write(brace);
31
- return;
32
- }
33
-
34
- if (type === 'BinaryExpression') {
35
- write(brace);
36
- return;
37
- }
38
-
39
- if (type === 'UnaryExpression') {
40
- write(brace);
41
- return;
42
- }
43
-
44
- if (!roundBraces.assign && !hasLeadingComment(path))
45
- return;
46
-
47
- if (!isParens(path))
48
- return;
49
-
50
- write(brace);
51
- }