@putout/printer 6.11.3 → 6.11.5

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.12.01, v6.11.5
2
+
3
+ feature:
4
+ - 96f6f4b @putout/printer: ArrayExpression: newline: isMultiLine
5
+
6
+ 2023.12.01, v6.11.4
7
+
8
+ feature:
9
+ - d181e1c @putout/printer: ArrayExpression: notNumbersInsideForOf
10
+
1
11
  2023.12.01, v6.11.3
2
12
 
3
13
  feature:
@@ -1,6 +1,39 @@
1
1
  'use strict';
2
2
 
3
3
  const {isSimple} = require('@putout/operate');
4
+ const isTwoStringsTupleAndParentIsArrayWithFirstArrayElement = (path) => {
5
+ const {elements} = path.node;
6
+
7
+ if (!isStringAndString(elements))
8
+ return false;
9
+
10
+ return isParentIsArrayWithFirstArrayElement(path);
11
+ };
12
+ const isLotsOfElementsFirstNotObject = (path) => {
13
+ const {elements} = path.node;
14
+ const [first] = elements;
15
+
16
+ return elements.length > 3 && !isObjectExpression(first);
17
+ };
18
+
19
+ const isTwoSimplesInsideObjectProperty = (path) => {
20
+ const {node, parentPath} = path;
21
+
22
+ const {elements} = node;
23
+ const {length} = elements;
24
+ const [a, b] = elements;
25
+
26
+ if (length > 2)
27
+ return false;
28
+
29
+ if (!parentPath.isObjectProperty())
30
+ return false;
31
+
32
+ if (!isStringLiteral(a) || !isStringLiteral(b))
33
+ return false;
34
+
35
+ return !isCoupleLines(path);
36
+ };
4
37
 
5
38
  const {
6
39
  isObjectExpression,
@@ -12,6 +45,7 @@ const {
12
45
  isNullLiteral,
13
46
  isStringLiteral,
14
47
  isSpreadElement,
48
+ isNumericLiteral,
15
49
  } = require('@putout/babel').types;
16
50
 
17
51
  const {
@@ -22,7 +56,6 @@ const {
22
56
  isStringAndArray,
23
57
  isIdentifierAndIdentifier,
24
58
  satisfy,
25
-
26
59
  } = require('../../is');
27
60
 
28
61
  const {round} = Math;
@@ -49,7 +82,7 @@ const isSimpleAndObject = ([a, b]) => isSimple(a) && isObjectExpression(b);
49
82
  const ONE_LINE = false;
50
83
  const MULTI_LINE = true;
51
84
 
52
- const isOneLine = satisfy([
85
+ const isOneLineElements = satisfy([
53
86
  isOneSimple,
54
87
  isOneSpread,
55
88
  isIdentifierAndIdentifier,
@@ -64,37 +97,48 @@ const isOneLine = satisfy([
64
97
  isSimpleAndObject,
65
98
  ]);
66
99
 
100
+ const isOneLinePath = satisfy([
101
+ isCallInsideArrow,
102
+ notIncreaseIndent,
103
+ isInsideLoop,
104
+ isTwoSimplesInsideObjectProperty,
105
+ isTwoStringsTupleAndParentIsArrayWithFirstArrayElement,
106
+ ]);
107
+
108
+ const isMultiLinePath = satisfy([
109
+ tooLong,
110
+ isCoupleLines,
111
+ notNumbersInsideForOf,
112
+ ]);
113
+
67
114
  module.exports.isMultiLine = (path, {elements, maxElementsInOneLine}) => {
68
- if (elements.length > 3 && !isObjectExpression(elements[0]))
115
+ if (isLotsOfElementsFirstNotObject(path))
69
116
  return MULTI_LINE;
70
117
 
71
- if (isOneLine(elements))
118
+ if (isOneLineElements(elements))
72
119
  return ONE_LINE;
73
120
 
74
- if (isCallInsideArrow(path))
75
- return ONE_LINE;
76
-
77
- if (notIncreaseIndent(path))
78
- return ONE_LINE;
79
-
80
- if (isInsideLoop(path))
121
+ if (isOneLinePath(path))
81
122
  return ONE_LINE;
82
123
 
83
124
  if (isShortTwoSimplesInsideCall(path, maxElementsInOneLine))
84
125
  return ONE_LINE;
85
126
 
86
- if (isTwoSimplesInsideObjectProperty(path))
87
- return ONE_LINE;
88
-
89
- if (isStringAndString(elements) && isParentIsArrayWithFirstArrayElement(path))
90
- return ONE_LINE;
91
-
92
- if (tooLong(path) || isCoupleLines(path) || !isNumbers(elements) && !isForOf(path) && isLastArg(path) && !isParentProperty(path))
127
+ if (isMultiLinePath(path))
93
128
  return MULTI_LINE;
94
129
 
95
130
  return ONE_LINE;
96
131
  };
97
132
 
133
+ function notNumbersInsideForOf(path) {
134
+ const {elements} = path.node;
135
+
136
+ if (isNumbers(elements))
137
+ return false;
138
+
139
+ return !isForOf(path) && isLastArg(path) && !isParentProperty(path);
140
+ }
141
+
98
142
  const isParentIsArrayWithFirstArrayElement = ({parentPath}) => {
99
143
  if (!isArrayExpression(parentPath))
100
144
  return false;
@@ -124,25 +168,6 @@ const isShortTwoSimplesInsideCall = (path, short) => {
124
168
  return length < short;
125
169
  };
126
170
 
127
- const isTwoSimplesInsideObjectProperty = (path) => {
128
- const {node, parentPath} = path;
129
-
130
- const {elements} = node;
131
- const {length} = elements;
132
- const [a, b] = elements;
133
-
134
- if (length > 2)
135
- return false;
136
-
137
- if (!parentPath.isObjectProperty())
138
- return false;
139
-
140
- if (!isStringLiteral(a) || !isStringLiteral(b))
141
- return false;
142
-
143
- return !isCoupleLines(path);
144
- };
145
-
146
171
  function isOneSimple(elements) {
147
172
  if (elements.length !== 1)
148
173
  return false;
@@ -211,7 +236,7 @@ function isCallInsideArrow(path) {
211
236
 
212
237
  function isNumbers(elements) {
213
238
  for (const element of elements) {
214
- if (element.isNumericLiteral())
239
+ if (isNumericLiteral(element))
215
240
  return true;
216
241
  }
217
242
 
@@ -266,3 +291,4 @@ module.exports.isCurrentNewLine = (path) => {
266
291
 
267
292
  return !path.isObjectExpression();
268
293
  };
294
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "6.11.3",
3
+ "version": "6.11.5",
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",