@putout/printer 6.10.0 → 6.11.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,13 @@
1
+ 2023.12.01, v6.11.1
2
+
3
+ feature:
4
+ - 39e3a0c ArrayExpression: simplify
5
+
6
+ 2023.11.30, v6.11.0
7
+
8
+ feature:
9
+ - 6390281 @putout/printer: improve support of comments
10
+
1
11
  2023.11.30, v6.10.0
2
12
 
3
13
  feature:
@@ -29,6 +29,11 @@ function isCommentOnNextLine(path) {
29
29
  const [comment] = trailingComments;
30
30
  const {line} = comment.loc.start;
31
31
 
32
+ const next = path.getNextSibling();
33
+
34
+ if (line < next.node?.loc.start.line)
35
+ return false;
36
+
32
37
  return line === loc.start.line + 1;
33
38
  }
34
39
 
@@ -9,10 +9,10 @@ const {
9
9
  } = require('../../is');
10
10
 
11
11
  const {
12
- isNewlineBetweenElements,
12
+ isMultiLine,
13
13
  isIncreaseIndent,
14
14
  isCurrentNewLine,
15
- } = require('./new-line');
15
+ } = require('./newline');
16
16
 
17
17
  const {types} = require('@putout/babel');
18
18
  const {
@@ -67,7 +67,7 @@ module.exports.ArrayExpression = {
67
67
  } = semantics;
68
68
 
69
69
  const elements = path.get('elements');
70
- const shouldIncreaseIndent = !isIncreaseIndent(path);
70
+ const shouldIncreaseIndent = isIncreaseIndent(path);
71
71
 
72
72
  print('[');
73
73
 
@@ -76,7 +76,7 @@ module.exports.ArrayExpression = {
76
76
  if (indented)
77
77
  maybe.indent.inc(shouldIncreaseIndent);
78
78
 
79
- const isNewLine = isNewlineBetweenElements(path, {
79
+ const isNewLine = isMultiLine(path, {
80
80
  elements,
81
81
  maxElementsInOneLine,
82
82
  });
@@ -47,7 +47,7 @@ const isSimpleAndObject = ([a, b]) => isSimple(a) && isObjectExpression(b);
47
47
  const ONE_LINE = false;
48
48
  const MULTI_LINE = true;
49
49
 
50
- module.exports.isNewlineBetweenElements = (path, {elements, maxElementsInOneLine}) => {
50
+ module.exports.isMultiLine = (path, {elements, maxElementsInOneLine}) => {
51
51
  if (elements.length > 3 && !isObjectExpression(elements[0]))
52
52
  return MULTI_LINE;
53
53
 
@@ -63,7 +63,7 @@ module.exports.isNewlineBetweenElements = (path, {elements, maxElementsInOneLine
63
63
  if (isCallInsideArrow(path))
64
64
  return ONE_LINE;
65
65
 
66
- if (isIncreaseIndent(path))
66
+ if (notIncreaseIndent(path))
67
67
  return ONE_LINE;
68
68
 
69
69
  if (isInsideLoop(path))
@@ -102,7 +102,7 @@ module.exports.isNewlineBetweenElements = (path, {elements, maxElementsInOneLine
102
102
  if (isSimpleAndObject(elements))
103
103
  return ONE_LINE;
104
104
 
105
- if (isStringAndString(elements) && path.parentPath.isArrayExpression() && isArrayExpression(path.parentPath.node.elements[0]))
105
+ if (isStringAndString(elements) && isParentIsArrayWithFirstArrayElement(path))
106
106
  return ONE_LINE;
107
107
 
108
108
  if (tooLong(path) || isCoupleLines(path) || !isNumbers(elements) && !isForOf(path) && isLastArg(path) && !isParentProperty(path))
@@ -111,6 +111,15 @@ module.exports.isNewlineBetweenElements = (path, {elements, maxElementsInOneLine
111
111
  return ONE_LINE;
112
112
  };
113
113
 
114
+ const isParentIsArrayWithFirstArrayElement = ({parentPath}) => {
115
+ if (!isArrayExpression(parentPath))
116
+ return false;
117
+
118
+ const [first] = parentPath.node.elements;
119
+
120
+ return isArrayExpression(first);
121
+ };
122
+
114
123
  const isForOf = ({parentPath}) => parentPath.isForOfStatement();
115
124
 
116
125
  const isStringAndString = ([a, b]) => isStringLiteral(a) && isStringLiteral(b);
@@ -235,23 +244,31 @@ function isParentProperty(path) {
235
244
  return path.find(isObjectProperty);
236
245
  }
237
246
 
238
- module.exports.isIncreaseIndent = isIncreaseIndent;
239
- function isIncreaseIndent(path) {
240
- const elements = path.get('elements');
247
+ const not = (fn) => (...a) => !fn(...a);
248
+
249
+ const isFirstObject = (path) => isObjectExpression(path.node.elements[0]);
250
+ const isSecondSpread = (path) => isSpreadElement(path.node.elements[1]);
251
+
252
+ const isStringAndObject = (path) => {
253
+ const {elements} = path.node;
254
+ const first = elements.at(0);
255
+ const last = elements.at(-1);
241
256
 
242
- if (!elements.length)
257
+ return isStringLiteral(first) && isObjectExpression(last);
258
+ };
259
+
260
+ module.exports.isIncreaseIndent = not(notIncreaseIndent);
261
+ function notIncreaseIndent(path) {
262
+ if (isInsideCallLoop(path))
243
263
  return false;
244
264
 
245
- if (isInsideCallLoop(path))
265
+ if (isSecondSpread(path))
246
266
  return false;
247
267
 
248
- if (elements[0].isObjectExpression())
268
+ if (isStringAndObject(path))
249
269
  return true;
250
270
 
251
- if (isSpreadElement(elements[1]))
252
- return false;
253
-
254
- return isStringAndObject(elements);
271
+ return isFirstObject(path);
255
272
  }
256
273
 
257
274
  function isInsideCallLoop(path) {
@@ -261,13 +278,6 @@ function isInsideCallLoop(path) {
261
278
  return path.parentPath.parentPath.isForOfStatement();
262
279
  }
263
280
 
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
-
271
281
  module.exports.isCurrentNewLine = (path) => {
272
282
  if (path.isSpreadElement())
273
283
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "6.10.0",
3
+ "version": "6.11.1",
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",