@putout/printer 8.10.1 → 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 +12 -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/lib/tokenize/statements/import-declaration/import-declaration.js +12 -1
- package/package.json +3 -3
package/ChangeLog
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
2024.04.11, v8.12.0
|
|
2
|
+
|
|
3
|
+
feature:
|
|
4
|
+
- 426fccf @putout/printer: ArrayExpression, ObjectExpression: improve support of printing Identifier, Object inside Arrray
|
|
5
|
+
|
|
6
|
+
2024.04.09, v8.11.0
|
|
7
|
+
|
|
8
|
+
feature:
|
|
9
|
+
- 914b94a @putout/printer: ImportDeclaration: when there is couple specifiers and one of them renamed - always write each on separate line
|
|
10
|
+
- c1961a5 @putout/printer: eslint-plugin-n v17.0.0
|
|
11
|
+
- abd17d5 @putout/printer: eslint v9.0.0
|
|
12
|
+
|
|
1
13
|
2024.04.06, v8.10.1
|
|
2
14
|
|
|
3
15
|
feature:
|
|
@@ -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
|
};
|
|
@@ -16,7 +16,6 @@ module.exports.ImportDeclaration = {
|
|
|
16
16
|
const {phase} = path.node;
|
|
17
17
|
const isType = path.node.importKind === 'type';
|
|
18
18
|
const specifiers = path.get('specifiers');
|
|
19
|
-
const {maxSpecifiersInOneLine} = options;
|
|
20
19
|
|
|
21
20
|
indent();
|
|
22
21
|
write('import');
|
|
@@ -45,6 +44,7 @@ module.exports.ImportDeclaration = {
|
|
|
45
44
|
traverse(spec.get('local'));
|
|
46
45
|
}
|
|
47
46
|
|
|
47
|
+
const maxSpecifiersInOneLine = parseMaxSpecifiers(imports, options);
|
|
48
48
|
const importsCount = imports.length - 1;
|
|
49
49
|
|
|
50
50
|
for (const [index, spec] of imports.entries()) {
|
|
@@ -109,3 +109,14 @@ module.exports.ImportDeclaration = {
|
|
|
109
109
|
markAfter(path);
|
|
110
110
|
},
|
|
111
111
|
};
|
|
112
|
+
|
|
113
|
+
function parseMaxSpecifiers(imports, options) {
|
|
114
|
+
const {maxSpecifiersInOneLine} = options;
|
|
115
|
+
|
|
116
|
+
for (const {node} of imports) {
|
|
117
|
+
if (node.imported.name !== node.local.name)
|
|
118
|
+
return 0;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return maxSpecifiersInOneLine;
|
|
122
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.12.0",
|
|
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",
|
|
@@ -55,8 +55,8 @@
|
|
|
55
55
|
"c8": "^9.1.0",
|
|
56
56
|
"check-dts": "^0.7.2",
|
|
57
57
|
"escover": "^4.0.1",
|
|
58
|
-
"eslint": "^
|
|
59
|
-
"eslint-plugin-n": "^
|
|
58
|
+
"eslint": "^9.0.0",
|
|
59
|
+
"eslint-plugin-n": "^17.0.0",
|
|
60
60
|
"eslint-plugin-putout": "^22.0.0",
|
|
61
61
|
"estree-to-babel": "^9.0.0",
|
|
62
62
|
"just-kebab-case": "^4.2.0",
|