@putout/printer 1.14.3 → 1.15.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,14 @@
1
+ 2023.03.31, v1.15.1
2
+
3
+ feature:
4
+ - 0fd6fd2 @putout/printer: ArrayExpression: add ability to avoid newline when inside of CallExpression inside of ForOfStatement
5
+
6
+ 2023.03.31, v1.15.0
7
+
8
+ feature:
9
+ - 67b6b53 @putout/printer: add support of nested Binary and Logical Expressions
10
+ - 6bcba84 @putout/printer: improve support of last ExpressionStatement inside of BlockStatement
11
+
1
12
  2023.03.30, v1.14.3
2
13
 
3
14
  feature:
@@ -33,12 +33,13 @@ module.exports.ArrayExpression = (path, {print, maybe, indent}) => {
33
33
  const isNewLine = isNewlineBetweenElements(path, {elements});
34
34
  const n = elements.length - 1;
35
35
 
36
- maybe.print(isNewLine && elements.length, '\n');
36
+ maybe.print.newline(isNewLine && elements.length);
37
37
 
38
38
  for (const [index, element] of elements.entries()) {
39
39
  maybe.indent(isNewLine);
40
40
  print(element);
41
- maybe.print(isNewLine, ',\n');
41
+ maybe.print(isNewLine, ',');
42
+ maybe.print.newline(isNewLine);
42
43
  maybe.print(!isNewLine && index < n, ', ');
43
44
  }
44
45
 
@@ -87,6 +88,9 @@ function isIncreaseIndent(path) {
87
88
  if (!elements.length)
88
89
  return false;
89
90
 
91
+ if (isInsideCallLoop(path))
92
+ return false;
93
+
90
94
  if (elements[0].isObjectExpression())
91
95
  return true;
92
96
 
@@ -111,6 +115,9 @@ function tooLong(path) {
111
115
  }
112
116
 
113
117
  function isNewlineBetweenElements(path, {elements}) {
118
+ if (isInsideCallLoop(path))
119
+ return false;
120
+
114
121
  if (isIncreaseIndent(path))
115
122
  return false;
116
123
 
@@ -136,3 +143,12 @@ function isTwoStringsDifferentLength([a, b]) {
136
143
  return round(bLength / aLength) > 2;
137
144
  }
138
145
 
146
+ function isInsideCallLoop(path) {
147
+ if (!path.parentPath.isCallExpression())
148
+ return false;
149
+
150
+ if (!path.parentPath.parentPath.isForOfStatement())
151
+ return false;
152
+
153
+ return true;
154
+ }
@@ -3,10 +3,24 @@
3
3
  module.exports.BinaryExpression = BinaryExpression;
4
4
  module.exports.LogicalExpression = BinaryExpression;
5
5
 
6
- function BinaryExpression(path, {print}) {
7
- print('__left');
8
- print.space();
9
- print(path.node.operator);
10
- print.space();
11
- print('__right');
6
+ const isLogical = (path) => path.isLogicalExpression();
7
+
8
+ function BinaryExpression(path, {write, traverse, maybe}) {
9
+ const left = path.get('left');
10
+ const right = path.get('right');
11
+ const isLeft = isLogical(left);
12
+ const isRight = isLogical(right);
13
+
14
+ maybe.write(isLeft, '(');
15
+ traverse(left);
16
+ maybe.write(isLeft, ')');
17
+
18
+ write.space();
19
+ write(path.node.operator);
20
+ write.space();
21
+
22
+ maybe.write(isRight, '(');
23
+ traverse(right);
24
+ maybe.write(isRight, ')');
12
25
  }
26
+
@@ -63,7 +63,7 @@ function shouldBreakline(path) {
63
63
  }
64
64
 
65
65
  function shouldAddNewLineAfter(path) {
66
- if (isLast(path) || isParentLast(path))
66
+ if (!isParentBlock(path) && (isLast(path) || isParentLast(path)))
67
67
  return false;
68
68
 
69
69
  if (isParentBlock(path) && !isParentProgram(path))
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const {round} = Math;
4
+
3
5
  const fullstore = require('fullstore');
4
6
  const isObject = (a) => a && typeof a === 'object';
5
7
  const babelTraverse = require('@babel/traverse').default;
@@ -76,6 +78,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
76
78
  const maybeNewline = (a) => a && newline();
77
79
  const maybeBreakline = (a) => a && breakline();
78
80
  const maybeLinebreak = (a) => a && linebreak();
81
+ const maybeWrite = (a, b) => a && write(b);
79
82
  let i = 0;
80
83
  const incIndent = () => ++i;
81
84
  const decIndent = () => --i;
@@ -125,12 +128,14 @@ module.exports.tokenize = (ast, overrides = {}) => {
125
128
  newline,
126
129
  linebreak,
127
130
  breakline,
131
+ space,
128
132
  });
129
133
 
130
134
  const maybe = {
131
135
  indent: maybeIndent,
132
136
  markBefore: maybeMarkBefore,
133
137
  markAfter: maybeMarkAfter,
138
+ write: maybeWrite,
134
139
  };
135
140
 
136
141
  assign(maybe.indent, {
@@ -173,6 +178,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
173
178
 
174
179
  assign(print, write, {
175
180
  space,
181
+ round,
176
182
  });
177
183
 
178
184
  assign(printer, {
@@ -217,14 +223,23 @@ const createPrint = (path, {traverse, write}) => (maybeLine) => {
217
223
  if (maybeLine === path)
218
224
  return null;
219
225
 
226
+ const computed = computePath(path, maybeLine);
227
+
228
+ if (isObject(computed))
229
+ return traverse(computed);
230
+
231
+ return write(computed);
232
+ };
233
+ const computePath = (path, maybeLine) => {
220
234
  if (isString(maybeLine) && maybeLine.startsWith(GET))
221
- return traverse(get(
235
+ return get(
222
236
  path,
223
237
  maybeLine,
224
- ));
238
+ );
225
239
 
226
240
  if (isObject(maybeLine))
227
- return traverse(maybeLine);
241
+ return maybeLine;
228
242
 
229
- return write(maybeLine);
243
+ return maybeLine;
230
244
  };
245
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.14.3",
3
+ "version": "1.15.1",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Easiest possible opinionated Babel AST printer made with ❤️ to use in 🐊Putout",