@putout/printer 1.13.0 → 1.13.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,8 @@
1
+ 2023.03.29, v1.13.1
2
+
3
+ feature:
4
+ - d320519 @putout/printer: improve support of nested objects
5
+
1
6
  2023.03.29, v1.13.0
2
7
 
3
8
  feature:
@@ -1,32 +1,58 @@
1
1
  'use strict';
2
2
 
3
+ const {round} = Math;
4
+
3
5
  const {isObjectProperty} = require('@babel/types');
4
6
  const {isCoupleLines} = require('../is');
5
- const {entries} = Object;
6
7
  const isForOf = ({parentPath}) => parentPath.isForOfStatement();
7
8
  const SECOND = 1;
8
9
 
9
- module.exports.ArrayExpression = (path, {print, maybe}) => {
10
+ const isStringAndArray = ([a, b]) => a?.isStringLiteral() && b?.isArrayExpression();
11
+ const isTwoLongStrings = ([a, b]) => {
12
+ const LONG_STRING = 20;
13
+
14
+ if (!a?.isStringLiteral() || !b?.isStringLiteral())
15
+ return false;
16
+
17
+ if (a.node.value.length > LONG_STRING)
18
+ return true;
19
+
20
+ return false;
21
+ };
22
+
23
+ module.exports.ArrayExpression = (path, {print, maybe, indent}) => {
10
24
  const elements = path.get('elements');
11
- const elementIsObject = isElementObject(path);
25
+ const shouldIncreaseIndent = isIncreaseIndent(path);
12
26
 
13
27
  print('[');
14
- maybe.indent.inc(!elementIsObject);
15
28
 
16
- const isNewLine = !elementIsObject && isNewLineBefore(path, {elements});
29
+ if (!isTwoLongStrings(elements) && !shouldIncreaseIndent) {
30
+ indent.inc();
31
+ }
32
+
33
+ const isNewLine = isNewlineBetweenElements(path, {elements});
17
34
  const n = elements.length - 1;
18
35
 
19
36
  maybe.print(isNewLine && elements.length, '\n');
20
37
 
21
- for (const [index, element] of entries(elements)) {
38
+ for (const [index, element] of elements.entries()) {
22
39
  maybe.indent(isNewLine);
23
40
  print(element);
24
41
  maybe.print(isNewLine, ',\n');
25
42
  maybe.print(!isNewLine && index < n, ', ');
26
43
  }
27
44
 
28
- maybe.indent.dec(!elementIsObject);
29
- maybe.indent(elements.length && isNewLine);
45
+ if (!isTwoLongStrings(elements))
46
+ maybe.indent.dec(!shouldIncreaseIndent);
47
+
48
+ if (path.parentPath.isArrayExpression() && isStringAndArray(path.parentPath.get('elements'))) {
49
+ indent.dec();
50
+ maybe.indent(elements.length && isNewLine);
51
+ indent.inc();
52
+ } else {
53
+ maybe.indent(elements.length && isNewLine);
54
+ }
55
+
30
56
  print(']');
31
57
  };
32
58
 
@@ -55,7 +81,7 @@ function isParentProperty(path) {
55
81
  return path.find(isObjectProperty);
56
82
  }
57
83
 
58
- function isElementObject(path) {
84
+ function isIncreaseIndent(path) {
59
85
  const elements = path.get('elements');
60
86
 
61
87
  if (!elements.length)
@@ -84,9 +110,29 @@ function tooLong(path) {
84
110
  return false;
85
111
  }
86
112
 
87
- function isNewLineBefore(path, {elements}) {
113
+ function isNewlineBetweenElements(path, {elements}) {
114
+ if (isIncreaseIndent(path))
115
+ return false;
116
+
117
+ if (isTwoStringsDifferentLength(elements))
118
+ return false;
119
+
120
+ if (isStringAndArray(elements))
121
+ return false;
122
+
88
123
  if (tooLong(path) || isCoupleLines(path) || !isNumbers(elements) && !isForOf(path) && isLastArg(path) && !isParentProperty(path))
89
124
  return true;
90
125
 
91
126
  return false;
92
127
  }
128
+
129
+ function isTwoStringsDifferentLength([a, b]) {
130
+ if (!a?.isStringLiteral() || !b?.isStringLiteral())
131
+ return false;
132
+
133
+ const aLength = a.node.value.length;
134
+ const bLength = b.node.value.length;
135
+
136
+ return round(bLength / aLength) > 2;
137
+ }
138
+
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const {isCoupleLines} = require('../is');
4
+
4
5
  const isBodyOfArrow = (path) => path.parentPath.node.body === path.node;
5
6
  const isLogical = (path) => path.get('argument').isLogicalExpression();
6
7
 
@@ -89,3 +90,4 @@ function isOneLine(path) {
89
90
 
90
91
  return isForOf(path);
91
92
  }
93
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.13.0",
3
+ "version": "1.13.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",