@putout/printer 1.25.0 → 1.25.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 +7 -0
- package/lib/tokenize/expressions/array-expression.js +87 -42
- package/lib/tokenize/expressions/member-expressions.js +4 -1
- package/lib/tokenize/expressions/object-expression.js +11 -5
- package/lib/tokenize/is.js +6 -0
- package/lib/tokenize/statements/for-of-statement.js +6 -2
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -2,14 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
const {round} = Math;
|
|
4
4
|
|
|
5
|
-
const {
|
|
6
|
-
|
|
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 =
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
|
|
102
|
+
print.breakline();
|
|
55
103
|
indent.inc();
|
|
56
|
-
}
|
|
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
|
|
155
|
-
|
|
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()
|
|
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
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
const
|
|
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
|
|
69
|
+
return path
|
|
70
|
+
.parentPath.parentPath.isSpreadElement();
|
|
65
71
|
}
|
|
66
72
|
module.exports.ObjectProperty = (path, {print, maybe}) => {
|
|
67
73
|
const {
|
package/lib/tokenize/is.js
CHANGED
|
@@ -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
|
-
|
|
37
|
+
traverse(bodyPath);
|
|
38
|
+
|
|
39
|
+
maybe.markAfter(isMarkedAfter(bodyPath), path);
|
|
40
|
+
|
|
37
41
|
indent.dec();
|
|
38
42
|
maybe.print.newline(isNext(path));
|
|
39
43
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.25.
|
|
3
|
+
"version": "1.25.1",
|
|
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",
|