@putout/printer 2.0.2 → 2.2.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.2.0
2
+
3
+ feature:
4
+ - 5be9df9 printer: comments
5
+
6
+ 2023.06.12, v2.1.0
7
+
8
+ feature:
9
+ - 2a0f610 @putout/printer: add maxVariablesInOneLine
10
+
1
11
  2023.06.12, v2.0.2
2
12
 
3
13
  feature:
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}) {
@@ -103,7 +104,8 @@ write(ast, {
103
104
 
104
105
  ### `format`
105
106
 
106
- With help of `format` you can override next options:
107
+ Options related to visuals and not related to logic of output can be changed with help of `format`,
108
+ you can override next options:
107
109
 
108
110
  ```js
109
111
  const overrides = {
@@ -111,8 +113,9 @@ const overrides = {
111
113
  indent: ' ',
112
114
  newline: '\n',
113
115
  space: ' ',
114
- comments: true,
115
116
  splitter: '\n',
117
+ roundBraceOpen: '(',
118
+ roundBraceClose: ')',
116
119
  },
117
120
  };
118
121
  ```
@@ -120,8 +123,8 @@ const overrides = {
120
123
  - `indent` - use two spaces, tabs, or anything you want;
121
124
  - `newline` - symbol for used for line separation;
122
125
  - `space` - default symbol used for space character;
123
- - `comments` - produce comments in output or skip them;
124
126
  - `splitter` - mandatory symbol that used inside of statements like this:
127
+ - `roundBraceOpen` and `roundBraceClose` symbols to to output braces in a single argument arrow function expressions: `(a) => {}`.
125
128
 
126
129
  Default options produce:
127
130
 
@@ -147,11 +150,15 @@ const overrides = {
147
150
 
148
151
  And have minified code:
149
152
 
153
+ ```
154
+ if(a>3)console.log('ok');else console.log('not ok');
155
+ ```
156
+
157
+ ### Semantics
158
+
159
+ Options used to configure logic of output, similar to ESLint rules:
160
+
150
161
  ```js
151
- if (a > 3)
152
- console.log('ok');
153
- else
154
- console.log('not ok');
155
162
  ```
156
163
 
157
164
  ### `write`
@@ -104,7 +104,7 @@ module.exports.parseComments = (path, {write}, semantics) => {
104
104
  };
105
105
 
106
106
  function isSameLine(path, loc) {
107
- return path.node.loc.start.line === loc.start.line;
107
+ return path.node.loc.start.line === loc.start.line || path.node.loc.end.line === loc.end.line;
108
108
  }
109
109
 
110
110
  function maybeInsideFn(insideFn, {print, indent}) {
@@ -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
  }
@@ -35,7 +35,9 @@ module.exports.BlockStatement = {
35
35
  print(element);
36
36
  }
37
37
 
38
- parseComments(path, {write}, semantics);
38
+ parseComments(path, {
39
+ write,
40
+ }, semantics);
39
41
 
40
42
  indent.dec();
41
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "2.0.2",
3
+ "version": "2.2.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",