@putout/printer 6.12.0 → 6.14.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.12.03, v6.14.0
2
+
3
+ feature:
4
+ - a00d8cb @putout/printer: comment: chain
5
+
6
+ 2023.12.03, v6.13.0
7
+
8
+ feature:
9
+ - bc106c1 @putout/printer: ArrayExpression: object after spread
10
+
1
11
  2023.12.01, v6.12.0
2
12
 
3
13
  feature:
@@ -15,13 +15,26 @@ function isSameLine(path, loc) {
15
15
 
16
16
  const isTrailingIsLeading = (path) => path.node.trailingComments === path.getNextSibling().node?.leadingComments;
17
17
 
18
+ const isNewlineAfter = (path) => {
19
+ const {parentPath} = path;
20
+
21
+ if (parentPath.isMemberExpression())
22
+ return false;
23
+
24
+ return !isLast(path) && !isDecorator(path);
25
+ };
26
+
18
27
  module.exports.isTrailingIsLeading = isTrailingIsLeading;
19
28
 
20
29
  function isCommentOnNextLine(path) {
30
+ const {node, parentPath} = path;
21
31
  const {
22
32
  loc,
23
33
  trailingComments,
24
- } = path.node;
34
+ } = node;
35
+
36
+ if (parentPath.isMemberExpression())
37
+ return false;
25
38
 
26
39
  if (isTrailingIsLeading(path))
27
40
  return false;
@@ -69,7 +82,7 @@ module.exports.parseTrailingComments = (path, {write, maybe}, semantics) => {
69
82
  }
70
83
 
71
84
  write(`//${value}`);
72
- maybe.write.newline(!isLast(path) && !isDecorator(path));
85
+ maybe.write.newline(isNewlineAfter(path));
73
86
 
74
87
  continue;
75
88
  }
@@ -8,22 +8,26 @@ const {
8
8
  } = require('../../is');
9
9
 
10
10
  const {
11
- isMultiLine,
12
11
  isIncreaseIndent,
13
12
  isCurrentNewLine,
13
+ isMultiLine,
14
14
  } = require('./newline');
15
15
 
16
- const {types} = require('@putout/babel');
17
-
18
16
  const {
19
17
  isInsideArray,
20
18
  isArrayInsideArray,
21
19
  isArrayIndented,
22
20
  } = require('./indent');
23
21
 
24
- const {isObjectExpression} = types;
22
+ const {types} = require('@putout/babel');
23
+ const {
24
+ isObjectExpression,
25
+ isSpreadElement,
26
+ } = types;
25
27
 
26
28
  const isNextObject = (a) => a.getNextSibling().isObjectExpression();
29
+ const isPrevObject = (a) => a.getPrevSibling().isObjectExpression();
30
+ const isObjectAfterSpread = (a) => isSpreadElement(a) && isNextObject(a) && !isPrevObject(a);
27
31
 
28
32
  const isInsideOneElementArray = ({parentPath}) => parentPath.node.elements.length === 1;
29
33
 
@@ -53,7 +57,7 @@ module.exports.ArrayExpression = {
53
57
  } = semantics;
54
58
 
55
59
  const elements = path.get('elements');
56
- const shouldIncreaseIndent = isIncreaseIndent(path);
60
+ const shouldIncreaseIndent = !isIncreaseIndent(path);
57
61
 
58
62
  print('[');
59
63
 
@@ -80,7 +84,7 @@ module.exports.ArrayExpression = {
80
84
  maybe.print(is, ',');
81
85
 
82
86
  maybe.print.newline(is && !isNextObject(element));
83
- maybe.print.space(element.isSpreadElement() && isNextObject(element));
87
+ maybe.print.space(isObjectAfterSpread(element));
84
88
 
85
89
  if (!is && index < n) {
86
90
  print(',');
@@ -1,40 +1,6 @@
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
-
13
- const isLotsOfElementsFirstNotObject = (path) => {
14
- const {elements} = path.node;
15
- const [first] = elements;
16
-
17
- return elements.length > 3 && !isObjectExpression(first);
18
- };
19
-
20
- const isTwoSimplesInsideObjectProperty = (path) => {
21
- const {node, parentPath} = path;
22
-
23
- const {elements} = node;
24
- const {length} = elements;
25
- const [a, b] = elements;
26
-
27
- if (length > 2)
28
- return false;
29
-
30
- if (!parentPath.isObjectProperty())
31
- return false;
32
-
33
- if (!isStringLiteral(a) || !isStringLiteral(b))
34
- return false;
35
-
36
- return !isCoupleLines(path);
37
- };
38
4
 
39
5
  const {
40
6
  isObjectExpression,
@@ -46,7 +12,6 @@ const {
46
12
  isNullLiteral,
47
13
  isStringLiteral,
48
14
  isSpreadElement,
49
- isNumericLiteral,
50
15
  } = require('@putout/babel').types;
51
16
 
52
17
  const {
@@ -56,7 +21,6 @@ const {
56
21
  isCoupleLines,
57
22
  isStringAndArray,
58
23
  isIdentifierAndIdentifier,
59
- satisfy,
60
24
  } = require('../../is');
61
25
 
62
26
  const {round} = Math;
@@ -83,70 +47,68 @@ const isSimpleAndObject = ([a, b]) => isSimple(a) && isObjectExpression(b);
83
47
  const ONE_LINE = false;
84
48
  const MULTI_LINE = true;
85
49
 
86
- const isOneLineElements = satisfy([
87
- isOneSimple,
88
- isOneSpread,
89
- isIdentifierAndIdentifier,
90
- isBooleanAndSimple,
91
- isNullAndSimple,
92
- isSimpleAndCall,
93
- isTwoStringsDifferentLength,
94
- isStringAndArray,
95
- isStringAndMember,
96
- isStringAndIdentifier,
97
- isIdentifierAndString,
98
- isSimpleAndObject,
99
- ]);
100
-
101
- const isOneLinePath = satisfy([
102
- isCallInsideArrow,
103
- notIncreaseIndent,
104
- isInsideLoop,
105
- isTwoSimplesInsideObjectProperty,
106
- isTwoStringsTupleAndParentIsArrayWithFirstArrayElement,
107
- ]);
108
-
109
- const isMultiLinePath = satisfy([
110
- tooLong,
111
- isCoupleLines,
112
- notNumbersInsideForOf,
113
- ]);
114
-
115
50
  module.exports.isMultiLine = (path, {elements, maxElementsInOneLine}) => {
116
- if (isLotsOfElementsFirstNotObject(path))
51
+ if (elements.length > 3 && !isObjectExpression(elements[0]))
117
52
  return MULTI_LINE;
118
53
 
119
- if (isOneLineElements(elements))
54
+ if (isOneSimple(path))
55
+ return ONE_LINE;
56
+
57
+ if (isOneSpread(elements))
58
+ return ONE_LINE;
59
+
60
+ if (elements.length === 2 && isIdentifierAndIdentifier(elements))
61
+ return ONE_LINE;
62
+
63
+ if (isCallInsideArrow(path))
120
64
  return ONE_LINE;
121
65
 
122
- if (isOneLinePath(path))
66
+ if (isIncreaseIndent(path))
67
+ return ONE_LINE;
68
+
69
+ if (isInsideLoop(path))
70
+ return ONE_LINE;
71
+
72
+ if (isBooleanAndSimple(elements))
73
+ return ONE_LINE;
74
+
75
+ if (isNullAndSimple(elements))
76
+ return ONE_LINE;
77
+
78
+ if (isSimpleAndCall(elements))
123
79
  return ONE_LINE;
124
80
 
125
81
  if (isShortTwoSimplesInsideCall(path, maxElementsInOneLine))
126
82
  return ONE_LINE;
127
83
 
128
- if (isMultiLinePath(path))
129
- return MULTI_LINE;
84
+ if (isTwoStringsDifferentLength(elements))
85
+ return ONE_LINE;
130
86
 
131
- return ONE_LINE;
132
- };
133
-
134
- function notNumbersInsideForOf(path) {
135
- const {elements} = path.node;
87
+ if (isTwoSimplesInsideObjectProperty(path))
88
+ return ONE_LINE;
136
89
 
137
- if (isNumbers(elements))
138
- return false;
90
+ if (isStringAndArray(elements))
91
+ return ONE_LINE;
139
92
 
140
- return !isForOf(path) && isLastArg(path) && !isParentProperty(path);
141
- }
142
-
143
- const isParentIsArrayWithFirstArrayElement = ({parentPath}) => {
144
- if (!isArrayExpression(parentPath))
145
- return false;
93
+ if (isStringAndMember(elements))
94
+ return ONE_LINE;
95
+
96
+ if (isStringAndIdentifier(elements))
97
+ return ONE_LINE;
98
+
99
+ if (isIdentifierAndString(elements))
100
+ return ONE_LINE;
101
+
102
+ if (isSimpleAndObject(elements))
103
+ return ONE_LINE;
104
+
105
+ if (isStringAndString(elements) && path.parentPath.isArrayExpression() && isArrayExpression(path.parentPath.node.elements[0]))
106
+ return ONE_LINE;
146
107
 
147
- const [first] = parentPath.node.elements;
108
+ if (tooLong(path) || isCoupleLines(path) || !isNumbers(elements) && !isForOf(path) && isLastArg(path) && !isParentProperty(path))
109
+ return MULTI_LINE;
148
110
 
149
- return isArrayExpression(first);
111
+ return ONE_LINE;
150
112
  };
151
113
 
152
114
  const isForOf = ({parentPath}) => parentPath.isForOfStatement();
@@ -169,7 +131,28 @@ const isShortTwoSimplesInsideCall = (path, short) => {
169
131
  return length < short;
170
132
  };
171
133
 
172
- function isOneSimple(elements) {
134
+ const isTwoSimplesInsideObjectProperty = (path) => {
135
+ const {node, parentPath} = path;
136
+
137
+ const {elements} = node;
138
+ const {length} = elements;
139
+ const [a, b] = elements;
140
+
141
+ if (length > 2)
142
+ return false;
143
+
144
+ if (!parentPath.isObjectProperty())
145
+ return false;
146
+
147
+ if (!isStringLiteral(a) || !isStringLiteral(b))
148
+ return false;
149
+
150
+ return !isCoupleLines(path);
151
+ };
152
+
153
+ function isOneSimple(path) {
154
+ const elements = path.get('elements');
155
+
173
156
  if (elements.length !== 1)
174
157
  return false;
175
158
 
@@ -237,7 +220,7 @@ function isCallInsideArrow(path) {
237
220
 
238
221
  function isNumbers(elements) {
239
222
  for (const element of elements) {
240
- if (isNumericLiteral(element))
223
+ if (element.isNumericLiteral())
241
224
  return true;
242
225
  }
243
226
 
@@ -252,31 +235,23 @@ function isParentProperty(path) {
252
235
  return path.find(isObjectProperty);
253
236
  }
254
237
 
255
- const not = (fn) => (...a) => !fn(...a);
256
-
257
- const isFirstObject = (path) => isObjectExpression(path.node.elements[0]);
258
- const isSecondSpread = (path) => isSpreadElement(path.node.elements[1]);
259
-
260
- const isStringAndObject = (path) => {
261
- const {elements} = path.node;
262
- const first = elements.at(0);
263
- const last = elements.at(-1);
238
+ module.exports.isIncreaseIndent = isIncreaseIndent;
239
+ function isIncreaseIndent(path) {
240
+ const elements = path.get('elements');
264
241
 
265
- return isStringLiteral(first) && isObjectExpression(last);
266
- };
267
-
268
- module.exports.isIncreaseIndent = not(notIncreaseIndent);
269
- function notIncreaseIndent(path) {
270
- if (isInsideCallLoop(path))
242
+ if (!elements.length)
271
243
  return false;
272
244
 
273
- if (isSecondSpread(path))
245
+ if (isInsideCallLoop(path))
274
246
  return false;
275
247
 
276
- if (isStringAndObject(path))
248
+ if (elements[0].isObjectExpression())
277
249
  return true;
278
250
 
279
- return isFirstObject(path);
251
+ if (isSpreadElement(elements[1]))
252
+ return false;
253
+
254
+ return isStringAndObject(elements);
280
255
  }
281
256
 
282
257
  function isInsideCallLoop(path) {
@@ -286,6 +261,13 @@ function isInsideCallLoop(path) {
286
261
  return path.parentPath.parentPath.isForOfStatement();
287
262
  }
288
263
 
264
+ const isStringAndObject = (elements) => {
265
+ const first = elements.at(0);
266
+ const last = elements.at(-1);
267
+
268
+ return isStringLiteral(first) && isObjectExpression(last);
269
+ };
270
+
289
271
  module.exports.isCurrentNewLine = (path) => {
290
272
  if (path.isSpreadElement())
291
273
  return true;
@@ -60,12 +60,7 @@ function isStringAndIdentifier([a, b]) {
60
60
  return isStringLiteral(a) && isIdentifier(b);
61
61
  }
62
62
 
63
- module.exports.isIdentifierAndIdentifier = (elements) => {
64
- if (elements.length !== 2)
65
- return false;
66
-
67
- const [a, b] = elements;
68
-
63
+ module.exports.isIdentifierAndIdentifier = ([a, b]) => {
69
64
  return isIdentifier(a) && isIdentifier(b);
70
65
  };
71
66
  module.exports.isStringAndMember = ([a, b]) => isStringLiteral(a) && isMemberExpression(b);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "6.12.0",
3
+ "version": "6.14.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",