@putout/printer 8.11.0 → 8.12.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,8 @@
1
+ 2024.04.11, v8.12.0
2
+
3
+ feature:
4
+ - 426fccf @putout/printer: ArrayExpression, ObjectExpression: improve support of printing Identifier, Object inside Arrray
5
+
1
6
  2024.04.09, v8.11.0
2
7
 
3
8
  feature:
@@ -24,6 +24,7 @@ const {
24
24
  isObjectExpression,
25
25
  isSpreadElement,
26
26
  isStringLiteral,
27
+ isIdentifier,
27
28
  } = types;
28
29
 
29
30
  const isNextString = (path) => isStringLiteral(path.getNextSibling());
@@ -32,6 +33,8 @@ const isAroundStrings = (path) => isNextString(path) || isPrevString(path);
32
33
  const isNextObject = (a) => a.getNextSibling().isObjectExpression();
33
34
  const isPrevObject = (a) => a.getPrevSibling().isObjectExpression();
34
35
  const isObjectAfterSpread = (a) => isSpreadElement(a) && isNextObject(a) && !isPrevObject(a);
36
+ const isObjectAfterIdentifier = (a) => isIdentifier(a) && isNextObject(a) && !isPrevObject(a);
37
+ const isObjectAfterSimple = (a) => isObjectAfterSpread(a) || isObjectAfterIdentifier(a);
35
38
 
36
39
  const isInsideOneElementArray = ({parentPath}) => parentPath.node.elements.length === 1;
37
40
 
@@ -88,7 +91,7 @@ module.exports.ArrayExpression = {
88
91
  maybe.print(is, ',');
89
92
 
90
93
  maybe.print.newline(is && !isNextObject(element));
91
- maybe.print.space(isObjectAfterSpread(element));
94
+ maybe.print.space(is && isObjectAfterSimple(element));
92
95
 
93
96
  if (!is && index < n) {
94
97
  print(',');
@@ -12,6 +12,7 @@ const {
12
12
  isNullLiteral,
13
13
  isStringLiteral,
14
14
  isSpreadElement,
15
+ isIdentifier,
15
16
  } = require('@putout/babel').types;
16
17
 
17
18
  const {
@@ -21,6 +22,7 @@ const {
21
22
  isCoupleLines,
22
23
  isStringAndArray,
23
24
  isIdentifierAndIdentifier,
25
+ isIdentifierAndObject,
24
26
  } = require('../../is');
25
27
 
26
28
  const {round} = Math;
@@ -60,6 +62,9 @@ module.exports.isMultiLine = (path, {elements, maxElementsInOneLine}) => {
60
62
  if (elements.length > 3 && !isObjectExpression(elements[0]))
61
63
  return MULTI_LINE;
62
64
 
65
+ if (isIdentifierAndObject(elements))
66
+ return MULTI_LINE;
67
+
63
68
  if (isOneSimple(path))
64
69
  return ONE_LINE;
65
70
 
@@ -9,6 +9,7 @@ const {
9
9
  noLeadingComment,
10
10
  hasLeadingComment,
11
11
  exists,
12
+ isIdentifierAndObject,
12
13
  } = require('../../is');
13
14
 
14
15
  const {parseComments} = require('../../comment/comment');
@@ -88,8 +89,24 @@ module.exports.ObjectExpression = (path, printer, semantics) => {
88
89
  }
89
90
 
90
91
  indent.dec();
92
+ const isInsideArrayWithIdentifierFirst = ({parentPath}) => {
93
+ if (!parentPath.isArrayExpression())
94
+ return false;
95
+
96
+ return isIdentifierAndObject(parentPath.get('elements'));
97
+ };
98
+
91
99
  maybe.indent(manyLines);
92
- print('}');
100
+
101
+ if (isInsideArrayWithIdentifierFirst(path)) {
102
+ print('},');
103
+ indent.dec();
104
+ print.breakline();
105
+ indent.inc();
106
+ } else {
107
+ print('}');
108
+ }
109
+
93
110
  maybe.print(parens, ')');
94
111
 
95
112
  maybe.indent.dec(isMemberExpressionCallee(path));
@@ -149,3 +166,4 @@ function isParens(path) {
149
166
 
150
167
  return isParentExpression(path);
151
168
  }
169
+
@@ -9,6 +9,7 @@ const {
9
9
  isVariableDeclaration,
10
10
  isMemberExpression,
11
11
  isArrayExpression,
12
+ isObjectExpression,
12
13
  } = require('@putout/babel').types;
13
14
 
14
15
  const isParentProgram = (path) => path.parentPath?.isProgram();
@@ -60,6 +61,16 @@ function isStringAndIdentifier([a, b]) {
60
61
  return isStringLiteral(a) && isIdentifier(b);
61
62
  }
62
63
 
64
+ module.exports.isIdentifierAndObject = ([a, b]) => {
65
+ if (!isIdentifier(a))
66
+ return false;
67
+
68
+ if (!isObjectExpression(b))
69
+ return false;
70
+
71
+ return b.node.properties.length;
72
+ };
73
+
63
74
  module.exports.isIdentifierAndIdentifier = ([a, b]) => {
64
75
  return isIdentifier(a) && isIdentifier(b);
65
76
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "8.11.0",
3
+ "version": "8.12.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",