@putout/printer 2.0.1 → 2.1.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
+ 2023.06.12, v2.1.0
2
+
3
+ feature:
4
+ - 2a0f610 @putout/printer: add maxVariablesInOneLine
5
+
6
+ 2023.06.12, v2.0.2
7
+
8
+ feature:
9
+ - 56c4cdd @putout/printer: improve handling of comments
10
+
1
11
  2023.06.12, v2.0.1
2
12
 
3
13
  fix:
package/README.md CHANGED
@@ -88,6 +88,7 @@ write(ast, {
88
88
  comments: true,
89
89
  maxSpecifiersInOneLine: 2,
90
90
  maxElementsInOneLine: 3,
91
+ maxVariablesInOneLine: 4,
91
92
  },
92
93
  visitors: {
93
94
  AssignmentPattern(path, {write}) {
@@ -8,8 +8,8 @@ const {
8
8
  const {markBefore} = require('./mark');
9
9
  const {isVariableDeclarator} = require('@babel/types');
10
10
 
11
- module.exports.parseLeadingComments = (path, {print, maybe, indent}, format = {}) => {
12
- if (!format.comments)
11
+ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics) => {
12
+ if (!semantics.comments)
13
13
  return;
14
14
 
15
15
  const {leadingComments} = path.node;
@@ -55,8 +55,8 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, format = {}
55
55
  }
56
56
  };
57
57
 
58
- module.exports.parseTrailingComments = (path, {write, maybe}, format) => {
59
- if (!format.comments)
58
+ module.exports.parseTrailingComments = (path, {write, maybe}, semantics) => {
59
+ if (!semantics.comments)
60
60
  return;
61
61
 
62
62
  const {trailingComments} = path.node;
@@ -84,7 +84,10 @@ module.exports.parseTrailingComments = (path, {write, maybe}, format) => {
84
84
  }
85
85
  };
86
86
 
87
- module.exports.parseComments = (path, {write}) => {
87
+ module.exports.parseComments = (path, {write}, semantics) => {
88
+ if (!semantics.comments)
89
+ return;
90
+
88
91
  const comments = path.node.comments || path.node.innerComments;
89
92
 
90
93
  if (!comments)
@@ -18,7 +18,7 @@ const isLogical = (path) => path.get('argument').isLogicalExpression();
18
18
  const isValue = (path) => path.get('properties.0.value').node;
19
19
  const isParentExpression = (path) => path.parentPath.isExpressionStatement();
20
20
 
21
- module.exports.ObjectExpression = (path, {print, maybe, indent, write}) => {
21
+ module.exports.ObjectExpression = (path, {print, maybe, indent, write}, semantics) => {
22
22
  indent.inc();
23
23
 
24
24
  const properties = path.get('properties');
@@ -30,7 +30,7 @@ module.exports.ObjectExpression = (path, {print, maybe, indent, write}) => {
30
30
  print('{');
31
31
  parseComments(path, {
32
32
  write,
33
- });
33
+ }, semantics);
34
34
  maybe.print.newline(manyLines);
35
35
 
36
36
  for (const property of properties) {
@@ -31,6 +31,7 @@ function initSemantics(semantics = {}) {
31
31
  comments: true,
32
32
  maxSpecifiersInOneLine: 2,
33
33
  maxElementsInOneLine: 5,
34
+ maxVariablesInOneLine: 4,
34
35
  ...semantics,
35
36
  };
36
37
  }
@@ -19,7 +19,7 @@ const isFirstStatement = (path) => path.get('body.0')?.isStatement();
19
19
  const isMethodOrArrow = (path) => isArrowFunctionExpression(path) || isObjectMethod(path);
20
20
 
21
21
  module.exports.BlockStatement = {
22
- print(path, {indent, maybe, print, write}) {
22
+ print(path, {indent, maybe, print, write}, semantics) {
23
23
  const body = path.get('body');
24
24
 
25
25
  if (path.parentPath.isBlockStatement())
@@ -37,7 +37,7 @@ module.exports.BlockStatement = {
37
37
 
38
38
  parseComments(path, {
39
39
  write,
40
- });
40
+ }, semantics);
41
41
 
42
42
  indent.dec();
43
43
  maybe.indent(body.length);
@@ -24,6 +24,7 @@ module.exports.VariableDeclaration = {
24
24
  print.breakline();
25
25
  },
26
26
  print(path, {maybe, store, write, traverse, print, indent}, semantics) {
27
+ const {maxVariablesInOneLine} = semantics;
27
28
  maybe.indent(isInsideBlock(path));
28
29
  write(path.node.kind);
29
30
  maybeSpaceAfterKeyword(path, {
@@ -53,13 +54,18 @@ module.exports.VariableDeclaration = {
53
54
  const next = declarations[index + 1];
54
55
 
55
56
  write(',');
57
+
58
+ if (!next.node.leadingComments) {
59
+ maybe.write.breakline(n > maxVariablesInOneLine);
60
+ maybe.write.space(n <= maxVariablesInOneLine);
61
+ continue;
62
+ }
63
+
56
64
  parseLeadingComments(next, {
57
65
  print,
58
66
  maybe,
59
67
  indent,
60
68
  }, semantics);
61
-
62
- maybe.write.breakline(!next.node.leadingComments);
63
69
  }
64
70
  }
65
71
 
@@ -172,4 +178,3 @@ const isNextAssign = (path) => {
172
178
  .get('expression')
173
179
  .isAssignmentExpression();
174
180
  };
175
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "2.0.1",
3
+ "version": "2.1.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",