@putout/printer 2.26.1 → 2.28.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.19, v2.28.0
2
+
3
+ feature:
4
+ - 10230c2 @putout/printer: ObjectExpression inside ArrayExpression: newline
5
+
6
+ 2023.06.19, v2.27.0
7
+
8
+ feature:
9
+ - 9fedd57 @putout/printer: ClassMethod: computed (coderaiser/minify#106)
10
+
1
11
  2023.06.19, v2.26.1
2
12
 
3
13
  fix:
@@ -10,8 +10,13 @@ const {
10
10
  const {
11
11
  isNewlineBetweenElements,
12
12
  isIncreaseIndent,
13
+ isCurrentNewLine,
13
14
  } = require('./new-line');
14
15
 
16
+ const {isObjectExpression} = require('@babel/types');
17
+
18
+ const isNextObject = (a) => a.getNextSibling().isObjectExpression();
19
+
15
20
  const isInsideOneElementArray = ({parentPath}) => parentPath.node.elements.length === 1;
16
21
  const isInsideArray = (path) => path.parentPath.isArrayExpression();
17
22
 
@@ -63,19 +68,22 @@ module.exports.ArrayExpression = {
63
68
  maybe.print.newline(isNewLine && elements.length);
64
69
 
65
70
  for (const [index, element] of elements.entries()) {
66
- maybe.indent(isNewLine);
71
+ const is = isNewLine && isCurrentNewLine(element);
72
+ maybe.indent(is);
67
73
  print(element);
68
- maybe.print(isNewLine, ',');
69
- maybe.print.newline(isNewLine);
74
+ maybe.print(is, ',');
75
+ maybe.print.newline(is && !isNextObject(element));
76
+ maybe.print.space(element.isSpreadElement() && isNextObject(element));
70
77
 
71
- if (!isNewLine && index < n) {
78
+ if (!is && index < n) {
72
79
  print(',');
73
80
  print.space();
74
81
  }
75
82
  }
76
83
 
77
- if (!isTwoLongStrings(elements))
84
+ if (!isTwoLongStrings(elements)) {
78
85
  maybe.indent.dec(shouldIncreaseIndent);
86
+ }
79
87
 
80
88
  const parentElements = path.parentPath.get('elements');
81
89
 
@@ -83,7 +91,7 @@ module.exports.ArrayExpression = {
83
91
  indent.dec();
84
92
  maybe.indent(elements.length && isNewLine);
85
93
  indent.inc();
86
- } else {
94
+ } else if (!isObjectExpression(elements.at(-1))) {
87
95
  maybe.indent(elements.length && isNewLine);
88
96
  }
89
97
 
@@ -226,6 +226,9 @@ function isIncreaseIndent(path) {
226
226
  if (elements[0].isObjectExpression())
227
227
  return true;
228
228
 
229
+ if (isSpreadElement(elements[1]))
230
+ return false;
231
+
229
232
  return isStringAndObject(elements);
230
233
  }
231
234
 
@@ -242,3 +245,10 @@ const isStringAndObject = (elements) => {
242
245
 
243
246
  return isStringLiteral(first) && isObjectExpression(last);
244
247
  };
248
+
249
+ module.exports.isCurrentNewLine = (path) => {
250
+ if (path.isSpreadElement())
251
+ return true;
252
+
253
+ return !path.isObjectExpression();
254
+ };
@@ -5,13 +5,18 @@ const {printParams} = require('./params');
5
5
 
6
6
  const ClassMethod = {
7
7
  print(path, {print, maybe}) {
8
- const {kind} = path.node;
8
+ const {kind, computed} = path.node;
9
9
  const isConstructor = kind === 'constructor';
10
10
  const isMethod = kind === 'method';
11
11
  const isGetter = /get|set/.test(kind);
12
12
 
13
13
  maybe.print(isConstructor, kind);
14
- maybe.print(isMethod, '__key');
14
+
15
+ if (isMethod) {
16
+ maybe.print(computed, '[');
17
+ print('__key');
18
+ maybe.print(computed, ']');
19
+ }
15
20
 
16
21
  if (isGetter) {
17
22
  print(kind);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "2.26.1",
3
+ "version": "2.28.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",