@putout/printer 2.30.0 → 2.31.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
+ 2023.06.19, v2.31.0
2
+
3
+ feature:
4
+ - bfb0901 @putout/printer: ArrayExpression: newlines
5
+
1
6
  2023.06.19, v2.30.0
2
7
 
3
8
  feature:
@@ -5,6 +5,8 @@ const {
5
5
  isStringAndIdentifier,
6
6
  isIdentifierAndIdentifier,
7
7
  isStringAndArray,
8
+ isIndented,
9
+
8
10
  } = require('../../is');
9
11
 
10
12
  const {
@@ -13,7 +15,10 @@ const {
13
15
  isCurrentNewLine,
14
16
  } = require('./new-line');
15
17
 
16
- const {isObjectExpression} = require('@babel/types');
18
+ const {
19
+ isObjectExpression,
20
+ isStringLiteral,
21
+ } = require('@babel/types');
17
22
 
18
23
  const isNextObject = (a) => a.getNextSibling().isObjectExpression();
19
24
 
@@ -23,7 +28,7 @@ const isInsideArray = (path) => path.parentPath.isArrayExpression();
23
28
  const isTwoLongStrings = ([a, b]) => {
24
29
  const LONG_STRING = 20;
25
30
 
26
- if (!a?.isStringLiteral() || !b?.isStringLiteral())
31
+ if (!isStringLiteral(a) || !isStringLiteral(b))
27
32
  return false;
28
33
 
29
34
  return a.node.value.length > LONG_STRING;
@@ -55,8 +60,11 @@ module.exports.ArrayExpression = {
55
60
 
56
61
  print('[');
57
62
 
58
- if (!isTwoLongStrings(elements))
63
+ const indented = !isTwoLongStrings(elements) || !isInsideArray(path) && isIndented(elements[0]);
64
+
65
+ if (indented) {
59
66
  maybe.indent.inc(shouldIncreaseIndent);
67
+ }
60
68
 
61
69
  const isNewLine = isNewlineBetweenElements(path, {
62
70
  elements,
@@ -81,9 +89,8 @@ module.exports.ArrayExpression = {
81
89
  }
82
90
  }
83
91
 
84
- if (!isTwoLongStrings(elements)) {
92
+ if (indented)
85
93
  maybe.indent.dec(shouldIncreaseIndent);
86
- }
87
94
 
88
95
  const parentElements = path.parentPath.get('elements');
89
96
 
@@ -34,6 +34,11 @@ module.exports.isParentProgram = isParentProgram;
34
34
  module.exports.isParentBlock = isParentBlock;
35
35
  module.exports.isLast = isLast;
36
36
  module.exports.isParentLast = (path) => isLast(path.parentPath);
37
+
38
+ module.exports.isIndented = (path = {}) => {
39
+ const {parentPath, node} = path;
40
+ return node.loc.start.column !== parentPath.node.loc.start.column;
41
+ };
37
42
  module.exports.isCoupleLines = isCoupleLines;
38
43
 
39
44
  function isCoupleLines(path) {
@@ -102,3 +107,4 @@ module.exports.hasLeadingComment = (path) => path.node?.leadingComments?.length;
102
107
 
103
108
  module.exports.noTrailingComment = (path) => !path.node.trailingComments?.length;
104
109
  module.exports.noLeadingComment = (path) => !path.node.leadingComments?.length;
110
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "2.30.0",
3
+ "version": "2.31.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",