@putout/printer 8.11.0 → 8.12.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 +5 -0
- package/lib/tokenize/expressions/array-expression/array-expression.js +4 -1
- package/lib/tokenize/expressions/array-expression/newline.js +5 -0
- package/lib/tokenize/expressions/object-expression/object-expression.js +19 -1
- package/lib/tokenize/is.js +11 -0
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -24,6 +24,7 @@ const {
|
|
|
24
24
|
isObjectExpression,
|
|
25
25
|
isSpreadElement,
|
|
26
26
|
isStringLiteral,
|
|
27
|
+
isIdentifier,
|
|
27
28
|
} = types;
|
|
28
29
|
|
|
29
30
|
const isNextString = (path) => isStringLiteral(path.getNextSibling());
|
|
@@ -32,6 +33,8 @@ const isAroundStrings = (path) => isNextString(path) || isPrevString(path);
|
|
|
32
33
|
const isNextObject = (a) => a.getNextSibling().isObjectExpression();
|
|
33
34
|
const isPrevObject = (a) => a.getPrevSibling().isObjectExpression();
|
|
34
35
|
const isObjectAfterSpread = (a) => isSpreadElement(a) && isNextObject(a) && !isPrevObject(a);
|
|
36
|
+
const isObjectAfterIdentifier = (a) => isIdentifier(a) && isNextObject(a) && !isPrevObject(a);
|
|
37
|
+
const isObjectAfterSimple = (a) => isObjectAfterSpread(a) || isObjectAfterIdentifier(a);
|
|
35
38
|
|
|
36
39
|
const isInsideOneElementArray = ({parentPath}) => parentPath.node.elements.length === 1;
|
|
37
40
|
|
|
@@ -88,7 +91,7 @@ module.exports.ArrayExpression = {
|
|
|
88
91
|
maybe.print(is, ',');
|
|
89
92
|
|
|
90
93
|
maybe.print.newline(is && !isNextObject(element));
|
|
91
|
-
maybe.print.space(
|
|
94
|
+
maybe.print.space(is && isObjectAfterSimple(element));
|
|
92
95
|
|
|
93
96
|
if (!is && index < n) {
|
|
94
97
|
print(',');
|
|
@@ -12,6 +12,7 @@ const {
|
|
|
12
12
|
isNullLiteral,
|
|
13
13
|
isStringLiteral,
|
|
14
14
|
isSpreadElement,
|
|
15
|
+
isIdentifier,
|
|
15
16
|
} = require('@putout/babel').types;
|
|
16
17
|
|
|
17
18
|
const {
|
|
@@ -21,6 +22,7 @@ const {
|
|
|
21
22
|
isCoupleLines,
|
|
22
23
|
isStringAndArray,
|
|
23
24
|
isIdentifierAndIdentifier,
|
|
25
|
+
isIdentifierAndObject,
|
|
24
26
|
} = require('../../is');
|
|
25
27
|
|
|
26
28
|
const {round} = Math;
|
|
@@ -60,6 +62,9 @@ module.exports.isMultiLine = (path, {elements, maxElementsInOneLine}) => {
|
|
|
60
62
|
if (elements.length > 3 && !isObjectExpression(elements[0]))
|
|
61
63
|
return MULTI_LINE;
|
|
62
64
|
|
|
65
|
+
if (isIdentifierAndObject(elements))
|
|
66
|
+
return MULTI_LINE;
|
|
67
|
+
|
|
63
68
|
if (isOneSimple(path))
|
|
64
69
|
return ONE_LINE;
|
|
65
70
|
|
|
@@ -9,6 +9,7 @@ const {
|
|
|
9
9
|
noLeadingComment,
|
|
10
10
|
hasLeadingComment,
|
|
11
11
|
exists,
|
|
12
|
+
isIdentifierAndObject,
|
|
12
13
|
} = require('../../is');
|
|
13
14
|
|
|
14
15
|
const {parseComments} = require('../../comment/comment');
|
|
@@ -88,8 +89,24 @@ module.exports.ObjectExpression = (path, printer, semantics) => {
|
|
|
88
89
|
}
|
|
89
90
|
|
|
90
91
|
indent.dec();
|
|
92
|
+
const isInsideArrayWithIdentifierFirst = ({parentPath}) => {
|
|
93
|
+
if (!parentPath.isArrayExpression())
|
|
94
|
+
return false;
|
|
95
|
+
|
|
96
|
+
return isIdentifierAndObject(parentPath.get('elements'));
|
|
97
|
+
};
|
|
98
|
+
|
|
91
99
|
maybe.indent(manyLines);
|
|
92
|
-
|
|
100
|
+
|
|
101
|
+
if (isInsideArrayWithIdentifierFirst(path)) {
|
|
102
|
+
print('},');
|
|
103
|
+
indent.dec();
|
|
104
|
+
print.breakline();
|
|
105
|
+
indent.inc();
|
|
106
|
+
} else {
|
|
107
|
+
print('}');
|
|
108
|
+
}
|
|
109
|
+
|
|
93
110
|
maybe.print(parens, ')');
|
|
94
111
|
|
|
95
112
|
maybe.indent.dec(isMemberExpressionCallee(path));
|
|
@@ -149,3 +166,4 @@ function isParens(path) {
|
|
|
149
166
|
|
|
150
167
|
return isParentExpression(path);
|
|
151
168
|
}
|
|
169
|
+
|
package/lib/tokenize/is.js
CHANGED
|
@@ -9,6 +9,7 @@ const {
|
|
|
9
9
|
isVariableDeclaration,
|
|
10
10
|
isMemberExpression,
|
|
11
11
|
isArrayExpression,
|
|
12
|
+
isObjectExpression,
|
|
12
13
|
} = require('@putout/babel').types;
|
|
13
14
|
|
|
14
15
|
const isParentProgram = (path) => path.parentPath?.isProgram();
|
|
@@ -60,6 +61,16 @@ function isStringAndIdentifier([a, b]) {
|
|
|
60
61
|
return isStringLiteral(a) && isIdentifier(b);
|
|
61
62
|
}
|
|
62
63
|
|
|
64
|
+
module.exports.isIdentifierAndObject = ([a, b]) => {
|
|
65
|
+
if (!isIdentifier(a))
|
|
66
|
+
return false;
|
|
67
|
+
|
|
68
|
+
if (!isObjectExpression(b))
|
|
69
|
+
return false;
|
|
70
|
+
|
|
71
|
+
return b.node.properties.length;
|
|
72
|
+
};
|
|
73
|
+
|
|
63
74
|
module.exports.isIdentifierAndIdentifier = ([a, b]) => {
|
|
64
75
|
return isIdentifier(a) && isIdentifier(b);
|
|
65
76
|
};
|
package/package.json
CHANGED