@putout/printer 18.2.5 → 18.2.7

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,16 @@
1
+ 2026.03.08, v18.2.7
2
+
3
+ feature:
4
+ - c873574 @putout/printer: isIncreaseIndent: move out: newlinew -> indent
5
+ - d3672b6 @putout/printer: ArrayExpression: newline: isIncreaseIndent
6
+ - 642c80f @putout/printer: ArrayExpression: indent: isArrayIndented: type-check
7
+
8
+ 2026.03.08, v18.2.6
9
+
10
+ feature:
11
+ - 9363494 @putout/printer: ArrayExpression: isObjectAfterSimple: type-check
12
+ - 463944a @putout/printer: ArrayExpression: isZero -> !Boolean
13
+
1
14
  2026.03.07, v18.2.5
2
15
 
3
16
  fix:
@@ -12,13 +12,13 @@ import {
12
12
  callWithPrev,
13
13
  } from '#is';
14
14
  import {
15
- isIncreaseIndent,
16
15
  isCurrentNewLine,
17
16
  isMultiLine,
18
17
  } from './newline.js';
19
18
  import {
20
19
  isArrayInsideArray,
21
- isArrayIndented,
20
+ isIncreaseIndent,
21
+ isIndentElement,
22
22
  } from './indent.js';
23
23
  import {isObjectAfterSimple} from './is-object-after-simple.js';
24
24
  import {
@@ -93,7 +93,7 @@ export const ArrayExpression = {
93
93
 
94
94
  print('[');
95
95
 
96
- const indented = isArrayIndented(path);
96
+ const indented = isIndentElement(path);
97
97
 
98
98
  maybe.indent.inc(indented && shouldIncreaseIndent);
99
99
 
@@ -1,14 +1,46 @@
1
1
  import {types} from '@putout/babel';
2
- import {isIndented, isInsideArray} from '#is';
2
+ import {isInsideArray, isInsideCall} from '#is';
3
+ import {createTypeChecker} from '#type-checker';
4
+
5
+ const isTwoLongStrings = (path) => {
6
+ const [a, b] = path.node.elements;
7
+ const LONG_STRING = 20;
8
+
9
+ if (!isStringLiteral(a) || !isStringLiteral(b))
10
+ return false;
11
+
12
+ return a.value.length > LONG_STRING;
13
+ };
3
14
 
4
15
  const {
5
16
  isStringLiteral,
6
17
  isArrayExpression,
7
18
  isObjectExpression,
8
19
  isTemplateLiteral,
20
+ isBooleanLiteral,
9
21
  } = types;
10
22
 
11
- const isObjectAfterString = ([first, second]) => {
23
+ const isInsideCallLoop = createTypeChecker([
24
+ ['-: -> !', isInsideCall],
25
+ ['+: parentPath.parentPath -> ForOfStatement'],
26
+ ]);
27
+
28
+ const isStringAndObject = (path) => {
29
+ const {elements} = path.node;
30
+ const first = elements.at(0);
31
+ const last = elements.at(-1);
32
+
33
+ return isStringLiteral(first) && isObjectExpression(last);
34
+ };
35
+
36
+ const isBooleanAndObject = (path) => {
37
+ const [a, b] = path.node.elements;
38
+ return isBooleanLiteral(a) && isObjectExpression(b);
39
+ };
40
+
41
+ const isObjectAfterString = (path) => {
42
+ const [first, second] = path.node.elements;
43
+
12
44
  if (!first || !second)
13
45
  return false;
14
46
 
@@ -21,19 +53,21 @@ const isObjectAfterString = ([first, second]) => {
21
53
  return isTemplateLiteral(first);
22
54
  };
23
55
 
24
- export const isArrayIndented = (path) => {
25
- const elements = path.get('elements');
26
-
27
- if (isArrayInsideArray(path))
28
- return false;
29
-
30
- const [first] = elements;
31
-
32
- if (isObjectAfterString(elements))
33
- return false;
34
-
35
- return !isTwoLongStrings(elements) || !isInsideArray(path) && isIndented(first);
36
- };
56
+ export const isIndentElement = createTypeChecker([
57
+ ['-', isArrayInsideArray],
58
+ ['-', isObjectAfterString],
59
+ ['+: -> !', isTwoLongStrings],
60
+ ['+: -> !', isInsideArray],
61
+ ]);
62
+
63
+ export const isIncreaseIndent = createTypeChecker([
64
+ ['-: node.elements.length -> !', Boolean],
65
+ ['+', isBooleanAndObject],
66
+ ['-', isInsideCallLoop],
67
+ ['+: node.elements.0 -> ObjectExpression'],
68
+ ['-: node.elements.1 -> SpreadElement'],
69
+ ['+', isStringAndObject],
70
+ ]);
37
71
 
38
72
  export function isArrayInsideArray(path) {
39
73
  if (!path.isArrayExpression() || !path.parentPath.isArrayExpression())
@@ -51,11 +85,3 @@ export function isArrayInsideArray(path) {
51
85
  return length <= 3 && length !== 1;
52
86
  }
53
87
 
54
- const isTwoLongStrings = ([a, b]) => {
55
- const LONG_STRING = 20;
56
-
57
- if (!isStringLiteral(a) || !isStringLiteral(b))
58
- return false;
59
-
60
- return a.node.value.length > LONG_STRING;
61
- };
@@ -1,4 +1,5 @@
1
- import {isNextObject, isPrevObject} from '#is';
1
+ import {isNextObject} from '#is';
2
+ import {createTypeChecker} from '#type-checker';
2
3
 
3
4
  const SIMPLE_TYPES = [
4
5
  'ArrayExpression',
@@ -9,11 +10,9 @@ const SIMPLE_TYPES = [
9
10
  'NewExpression',
10
11
  ];
11
12
 
12
- export const isObjectAfterSimple = (a) => {
13
- const {type} = a;
14
-
15
- if (!isNextObject(a) || isPrevObject(a))
16
- return false;
17
-
18
- return SIMPLE_TYPES.includes(type);
19
- };
13
+ const isSimpleType = ({type}) => SIMPLE_TYPES.includes(type);
14
+
15
+ export const isObjectAfterSimple = createTypeChecker([
16
+ ['-: -> !', isNextObject],
17
+ ['+', isSimpleType],
18
+ ]);
@@ -12,8 +12,7 @@ import {
12
12
  isInsideCall,
13
13
  isInsideArray,
14
14
  } from '#is';
15
-
16
- const isZero = (length) => !length;
15
+ import {isIncreaseIndent} from './indent.js';
17
16
 
18
17
  const isParentProperty = (path) => path.find(isObjectProperty);
19
18
 
@@ -113,8 +112,6 @@ const isBooleanAndSimple = (path) => {
113
112
  return isBooleanLiteral(a) && isSimple(b);
114
113
  };
115
114
 
116
- const isBooleanAndObject = ([a, b]) => isBooleanLiteral(a) && isObjectExpression(b);
117
-
118
115
  const isNullAndSimple = (path) => {
119
116
  const {elements} = path.node;
120
117
  const [a, b] = elements;
@@ -203,7 +200,7 @@ const isBodyWithOneElement = createTypeChecker([
203
200
  ]);
204
201
 
205
202
  export const isMultiLine = createTypeChecker([
206
- ['-: node.elements.length', isZero],
203
+ ['-: node.elements.length -> !', Boolean],
207
204
  ['+', isBodyWithOneElement],
208
205
  ['+', isMoreThenMaxElementLengthInOneLine],
209
206
  ['+', isElementsMoreThenMaxWithFirstString],
@@ -308,39 +305,6 @@ function isNumbers(elements) {
308
305
  return false;
309
306
  }
310
307
 
311
- export function isIncreaseIndent(path) {
312
- const elements = path.get('elements');
313
-
314
- if (!elements.length)
315
- return false;
316
-
317
- if (isBooleanAndObject(elements))
318
- return true;
319
-
320
- if (isInsideCallLoop(path))
321
- return false;
322
-
323
- if (elements[0].isObjectExpression())
324
- return true;
325
-
326
- if (isSpreadElement(elements[1]))
327
- return false;
328
-
329
- return isStringAndObject(elements);
330
- }
331
-
332
- const isInsideCallLoop = createTypeChecker([
333
- ['-: -> !', isInsideCall],
334
- ['+: parentPath.parentPath -> ForOfStatement'],
335
- ]);
336
-
337
- const isStringAndObject = (elements) => {
338
- const first = elements.at(0);
339
- const last = elements.at(-1);
340
-
341
- return isStringLiteral(first) && isObjectExpression(last);
342
- };
343
-
344
308
  export const isCurrentNewLine = createTypeChecker([
345
309
  '+: -> SpreadElement',
346
310
  '+: -> !ObjectExpression',
@@ -65,10 +65,6 @@ export const isNextObject = (a) => a
65
65
  .getNextSibling()
66
66
  .isObjectExpression();
67
67
 
68
- export const isPrevObject = (a) => a
69
- .getPrevSibling()
70
- .isObjectExpression();
71
-
72
68
  export const isFirst = (path) => path.node === path.parentPath.node.body?.[0];
73
69
  export const isPrevBody = (path) => path
74
70
  .getPrevSibling()
@@ -76,15 +72,6 @@ export const isPrevBody = (path) => path
76
72
 
77
73
  export const isParentLast = (path) => isLast(path.parentPath);
78
74
 
79
- export const isIndented = (path = {}) => {
80
- const {parentPath, node} = path;
81
-
82
- if (!parentPath.node.loc)
83
- return true;
84
-
85
- return node.loc?.start.column !== parentPath.node.loc.start.column;
86
- };
87
-
88
75
  export function isCoupleLines(path) {
89
76
  const start = path.node?.loc?.start?.line;
90
77
  const end = path.node?.loc?.end?.line;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "18.2.5",
3
+ "version": "18.2.7",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Simplest possible opinionated Babel AST printer for 🐊Putout",