@putout/printer 1.25.0 → 1.26.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,15 @@
1
+ 2023.04.06, v1.26.0
2
+
3
+ feature:
4
+ - 3ad5aab @putout/printer: prevent indent infinite loop
5
+
6
+ 2023.04.06, v1.25.1
7
+
8
+ feature:
9
+ - 3250fd6 @putout/printer: improve chaining
10
+ - 8d302ba @putout/printer: improve format of ArrayExpression tuple
11
+ - 4970dd9 @putout/printer: ForOfStatement: newline
12
+
1
13
  2023.04.06, v1.25.0
2
14
 
3
15
  feature:
@@ -2,14 +2,22 @@
2
2
 
3
3
  const {round} = Math;
4
4
 
5
- const {isObjectProperty} = require('@babel/types');
6
- const {isCoupleLines} = require('../is');
5
+ const {
6
+ isObjectProperty,
7
+ isStringLiteral,
8
+ isArrayExpression,
9
+ } = require('@babel/types');
10
+
11
+ const {
12
+ isCoupleLines,
13
+ isStringAndIdentifier,
14
+ } = require('../is');
15
+
7
16
  const isForOf = ({parentPath}) => parentPath.isForOfStatement();
8
17
  const SECOND = 1;
9
-
10
- const isStringAndArray = ([a, b]) => a?.isStringLiteral() && b?.isArrayExpression();
11
- const isStringAndIdentifier = ([a, b]) => a?.isStringLiteral() && b?.isIdentifier();
18
+ const isStringAndArray = ([a, b]) => isStringLiteral(a) && isArrayExpression(b);
12
19
  const isArrayParent = (path) => path.parentPath.isArrayExpression();
20
+
13
21
  const isTwoLongStrings = ([a, b]) => {
14
22
  const LONG_STRING = 20;
15
23
 
@@ -22,42 +30,78 @@ const isTwoLongStrings = ([a, b]) => {
22
30
  return false;
23
31
  };
24
32
 
25
- module.exports.ArrayExpression = (path, {print, maybe, indent}) => {
26
- const elements = path.get('elements');
27
- const shouldIncreaseIndent = isIncreaseIndent(path);
28
-
29
- print('[');
30
-
31
- if (!isTwoLongStrings(elements))
32
- maybe.indent.inc(!shouldIncreaseIndent);
33
-
34
- const isNewLine = isNewlineBetweenElements(path, {elements});
35
- const n = elements.length - 1;
36
-
37
- maybe.print.newline(isNewLine && elements.length);
38
-
39
- for (const [index, element] of elements.entries()) {
40
- maybe.indent(isNewLine);
41
- print(element);
42
- maybe.print(isNewLine, ',');
43
- maybe.print.newline(isNewLine);
44
- maybe.print(!isNewLine && index < n, ', ');
45
- }
46
-
47
- if (!isTwoLongStrings(elements))
48
- maybe.indent.dec(!shouldIncreaseIndent);
49
-
50
- const parentElements = path.parentPath.get('elements');
51
-
52
- if (isArrayParent(path) && isStringAndArray(parentElements)) {
33
+ module.exports.ArrayExpression = {
34
+ beforeIf(path) {
35
+ const {parentPath} = path;
36
+ const {elements} = path.node;
37
+
38
+ if (!parentPath.isArrayExpression())
39
+ return false;
40
+
41
+ if (isCoupleLines(parentPath))
42
+ return false;
43
+
44
+ return isStringAndIdentifier(elements);
45
+ },
46
+ before(path, {print}) {
47
+ print.breakline();
48
+ },
49
+ print(path, {print, maybe, indent}) {
50
+ const elements = path.get('elements');
51
+ const shouldIncreaseIndent = isIncreaseIndent(path);
52
+
53
+ print('[');
54
+
55
+ if (!isTwoLongStrings(elements))
56
+ maybe.indent.inc(!shouldIncreaseIndent);
57
+
58
+ const isNewLine = isNewlineBetweenElements(path, {
59
+ elements,
60
+ });
61
+ const n = elements.length - 1;
62
+
63
+ maybe.print.newline(isNewLine && elements.length);
64
+
65
+ for (const [index, element] of elements.entries()) {
66
+ maybe.indent(isNewLine);
67
+ print(element);
68
+ maybe.print(isNewLine, ',');
69
+ maybe.print.newline(isNewLine);
70
+ maybe.print(!isNewLine && index < n, ', ');
71
+ }
72
+
73
+ if (!isTwoLongStrings(elements))
74
+ maybe.indent.dec(!shouldIncreaseIndent);
75
+
76
+ const parentElements = path.parentPath.get('elements');
77
+
78
+ if (isArrayParent(path) && isStringAndArray(parentElements)) {
79
+ indent.dec();
80
+ maybe.indent(elements.length && isNewLine);
81
+ indent.inc();
82
+ } else {
83
+ maybe.indent(elements.length && isNewLine);
84
+ }
85
+
86
+ print(']');
87
+ },
88
+ afterIf(path) {
89
+ const {parentPath} = path;
90
+ const {elements} = path.node;
91
+
92
+ if (!parentPath.isArrayExpression())
93
+ return false;
94
+
95
+ if (isCoupleLines(parentPath))
96
+ return false;
97
+
98
+ return isStringAndIdentifier(elements);
99
+ },
100
+ after(path, {print, indent}) {
53
101
  indent.dec();
54
- maybe.indent(elements.length && isNewLine);
102
+ print.breakline();
55
103
  indent.inc();
56
- } else {
57
- maybe.indent(elements.length && isNewLine);
58
- }
59
-
60
- print(']');
104
+ },
61
105
  };
62
106
 
63
107
  function isNumbers(elements) {
@@ -151,8 +195,10 @@ function isTwoStringsDifferentLength(strings) {
151
195
  if (!a?.isStringLiteral() || !b?.isStringLiteral())
152
196
  return false;
153
197
 
154
- const aLength = a.node.value.length;
155
- const bLength = b.node.value.length;
198
+ const aLength = a
199
+ .node.value.length;
200
+ const bLength = b
201
+ .node.value.length;
156
202
 
157
203
  return round(bLength / aLength) > 2;
158
204
  }
@@ -173,4 +219,3 @@ function isInsideLoop(path) {
173
219
 
174
220
  return true;
175
221
  }
176
-
@@ -75,7 +75,10 @@ function looksLikeChain(path) {
75
75
  if (!itMember && !path.parentPath.isExpressionStatement() && !parentPath.isCallExpression())
76
76
  return false;
77
77
 
78
- if (parentPath.isCallExpression() && parentPath.get('callee') !== path)
78
+ if (!parentPath.isCallExpression())
79
+ return false;
80
+
81
+ if (parentPath.get('callee') !== path)
79
82
  return false;
80
83
 
81
84
  if (compare(parentPath, '__a.__b(__args);') && !itMember && !itExpression)
@@ -1,10 +1,15 @@
1
1
  'use strict';
2
2
 
3
3
  const {isCoupleLines} = require('../is');
4
- const isBodyOfArrow = (path) => path.parentPath.node.body === path.node;
5
- const isLogical = (path) => path.get('argument').isLogicalExpression();
6
- const isValue = (path) => path.get('properties.0.value').node;
7
- const isIf = (path) => path.parentPath.parentPath.isIfStatement();
4
+
5
+ const isBodyOfArrow = (path) => path
6
+ .parentPath.node.body === path.node;
7
+ const isLogical = (path) => path
8
+ .get('argument').isLogicalExpression();
9
+ const isValue = (path) => path
10
+ .get('properties.0.value').node;
11
+ const isIf = (path) => path
12
+ .parentPath.parentPath.isIfStatement();
8
13
  const isParentExpression = (path) => path.parentPath.isExpressionStatement();
9
14
 
10
15
  const isForOf = (path) => {
@@ -61,7 +66,8 @@ function shouldAddNewline(path) {
61
66
  if (!path.parentPath.isLogicalExpression())
62
67
  return false;
63
68
 
64
- return path.parentPath.parentPath.isSpreadElement();
69
+ return path
70
+ .parentPath.parentPath.isSpreadElement();
65
71
  }
66
72
  module.exports.ObjectProperty = (path, {print, maybe}) => {
67
73
  const {
@@ -1,6 +1,10 @@
1
1
  'use strict';
2
2
 
3
3
  const {isMarkedAfter} = require('./mark');
4
+ const {
5
+ isStringLiteral,
6
+ isIdentifier,
7
+ } = require('@babel/types');
4
8
  const isParentProgram = (path) => path.parentPath?.isProgram();
5
9
  const isParentBlock = (path) => path.parentPath.isBlockStatement();
6
10
 
@@ -40,3 +44,5 @@ function isCoupleLines(path) {
40
44
  function isNextCoupleLines(path) {
41
45
  return isCoupleLines(path.getNextSibling());
42
46
  }
47
+
48
+ module.exports.isStringAndIdentifier = ([a, b]) => isStringLiteral(a) && isIdentifier(b);
@@ -4,6 +4,7 @@ const {
4
4
  hasPrevNewline,
5
5
  markAfter,
6
6
  markBefore,
7
+ isMarkedAfter,
7
8
  } = require('../mark');
8
9
 
9
10
  const {
@@ -20,7 +21,7 @@ module.exports.ForOfStatement = {
20
21
  print.newline();
21
22
  markBefore(path);
22
23
  },
23
- print(path, {indent, print, maybe}) {
24
+ print(path, {indent, print, maybe, traverse}) {
24
25
  indent();
25
26
  print('for (');
26
27
  print('__left');
@@ -33,7 +34,10 @@ module.exports.ForOfStatement = {
33
34
  if (bodyPath.isExpressionStatement()) {
34
35
  indent.inc();
35
36
  print.newline();
36
- print('__body');
37
+ traverse(bodyPath);
38
+
39
+ maybe.markAfter(isMarkedAfter(bodyPath), path);
40
+
37
41
  indent.dec();
38
42
  maybe.print.newline(isNext(path));
39
43
 
@@ -215,7 +215,7 @@ function printIndent(i, indent) {
215
215
  let result = '';
216
216
  ++i;
217
217
 
218
- while (--i) {
218
+ while (--i > 0) {
219
219
  result += indent;
220
220
  }
221
221
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.25.0",
3
+ "version": "1.26.0",
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",