@putout/printer 3.8.0 → 4.1.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 +11 -0
- package/README.md +3 -1
- package/lib/json.js +45 -0
- package/lib/printer.js +4 -1
- package/lib/tokenize/expressions/array-expression/array-expression.js +9 -2
- package/lib/tokenize/expressions/object-expression/object-expression.js +5 -2
- package/lib/tokenize/expressions/object-expression/object-property.js +3 -2
- package/lib/tokenize/literals/index.js +17 -1
- package/lib/tokenize/overrides.js +2 -0
- package/lib/tokenize/tokenize.js +9 -0
- package/lib/types.js +3 -2
- package/package.json +2 -1
package/ChangeLog
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
2023.09.04, v4.1.0
|
|
2
|
+
|
|
3
|
+
feature:
|
|
4
|
+
- 317718e @putout/printer: imporove JSON support
|
|
5
|
+
|
|
6
|
+
2023.09.03, v4.0.0
|
|
7
|
+
|
|
8
|
+
feature:
|
|
9
|
+
- d935f8f @putout/printer: semantics: trailingComma
|
|
10
|
+
- 9aa52e4 @putout/printer: format: quote: add
|
|
11
|
+
|
|
1
12
|
2023.09.01, v3.8.0
|
|
2
13
|
|
|
3
14
|
feature:
|
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Prints [**Babel AST**](https://github.com/coderaiser/estree-to-babel) to readabl
|
|
|
8
8
|
- ☝️ Similar to **Recast**, but [twice faster](#speed-comparison), also simpler and easier in maintenance, since it supports only **Babel**.
|
|
9
9
|
- ☝️ As opinionated as **Prettier**, but has more user-friendly output and works directly with **AST**.
|
|
10
10
|
- ☝️ Like **ESLint** but works directly with **Babel AST**.
|
|
11
|
-
- ☝️ Easily
|
|
11
|
+
- ☝️ Easily extendable with help of [Overrides](h#overrides).
|
|
12
12
|
|
|
13
13
|
Supports:
|
|
14
14
|
|
|
@@ -81,6 +81,7 @@ print(ast, {
|
|
|
81
81
|
splitter: '\n',
|
|
82
82
|
roundBraceOpen: '(',
|
|
83
83
|
roundBraceClose: ')',
|
|
84
|
+
quote: `'`,
|
|
84
85
|
},
|
|
85
86
|
semantics: {
|
|
86
87
|
comments: true,
|
|
@@ -88,6 +89,7 @@ print(ast, {
|
|
|
88
89
|
maxElementsInOneLine: 3,
|
|
89
90
|
maxVariablesInOneLine: 4,
|
|
90
91
|
maxPropertiesInOneLine: 2,
|
|
92
|
+
trailingComma: true,
|
|
91
93
|
},
|
|
92
94
|
visitors: {
|
|
93
95
|
AssignmentPattern(path, {print}) {
|
package/lib/json.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
isCallExpression,
|
|
5
|
+
isIdentifier,
|
|
6
|
+
} = require('@babel/types');
|
|
7
|
+
|
|
8
|
+
const {isJSON} = require('@putout/processor-json/is-json');
|
|
9
|
+
const {assign} = Object;
|
|
10
|
+
|
|
11
|
+
module.exports.maybeJSON = (ast, overrides) => {
|
|
12
|
+
if (isASTJSON(ast))
|
|
13
|
+
assign(overrides, {
|
|
14
|
+
format: {
|
|
15
|
+
quote: `"`,
|
|
16
|
+
},
|
|
17
|
+
semantics: {
|
|
18
|
+
trailingComma: false,
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function isASTJSON(ast) {
|
|
24
|
+
const {program} = ast;
|
|
25
|
+
|
|
26
|
+
if (!program)
|
|
27
|
+
return false;
|
|
28
|
+
|
|
29
|
+
const {body} = ast.program;
|
|
30
|
+
|
|
31
|
+
if (!body.length)
|
|
32
|
+
return false;
|
|
33
|
+
|
|
34
|
+
const {expression} = ast.program.body[0];
|
|
35
|
+
|
|
36
|
+
if (!isCallExpression(expression))
|
|
37
|
+
return false;
|
|
38
|
+
|
|
39
|
+
const {callee} = expression;
|
|
40
|
+
|
|
41
|
+
if (!isIdentifier(callee))
|
|
42
|
+
return false;
|
|
43
|
+
|
|
44
|
+
return isJSON(callee.name);
|
|
45
|
+
}
|
package/lib/printer.js
CHANGED
|
@@ -2,8 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
const {tokenize} = require('./tokenize/tokenize');
|
|
4
4
|
const {printTokens} = require('./print-tokens');
|
|
5
|
+
const {maybeJSON} = require('./json');
|
|
5
6
|
|
|
6
|
-
module.exports.print = (ast, overrides) => {
|
|
7
|
+
module.exports.print = (ast, overrides = {}) => {
|
|
8
|
+
maybeJSON(ast, overrides);
|
|
7
9
|
const tokens = tokenize(ast, overrides);
|
|
10
|
+
|
|
8
11
|
return printTokens(tokens);
|
|
9
12
|
};
|
|
@@ -53,7 +53,11 @@ module.exports.ArrayExpression = {
|
|
|
53
53
|
print.breakline();
|
|
54
54
|
},
|
|
55
55
|
print(path, {print, maybe, indent}, semantics) {
|
|
56
|
-
const {
|
|
56
|
+
const {
|
|
57
|
+
maxElementsInOneLine,
|
|
58
|
+
trailingComma,
|
|
59
|
+
} = semantics;
|
|
60
|
+
|
|
57
61
|
const elements = path.get('elements');
|
|
58
62
|
const shouldIncreaseIndent = !isIncreaseIndent(path);
|
|
59
63
|
|
|
@@ -78,7 +82,10 @@ module.exports.ArrayExpression = {
|
|
|
78
82
|
const is = isNewLine && isCurrentNewLine(element);
|
|
79
83
|
maybe.indent(is);
|
|
80
84
|
print(element);
|
|
81
|
-
|
|
85
|
+
|
|
86
|
+
if (index < n || trailingComma)
|
|
87
|
+
maybe.print(is, ',');
|
|
88
|
+
|
|
82
89
|
maybe.print.newline(is && !isNextObject(element));
|
|
83
90
|
maybe.print.space(element.isSpreadElement() && isNextObject(element));
|
|
84
91
|
|
|
@@ -19,6 +19,7 @@ const isValue = (path) => path.get('properties.0.value').node;
|
|
|
19
19
|
const isParentExpression = (path) => path.parentPath.isExpressionStatement();
|
|
20
20
|
|
|
21
21
|
module.exports.ObjectExpression = (path, {print, maybe, indent, write}, semantics) => {
|
|
22
|
+
const {trailingComma} = semantics;
|
|
22
23
|
indent.inc();
|
|
23
24
|
|
|
24
25
|
const properties = path.get('properties');
|
|
@@ -31,7 +32,9 @@ module.exports.ObjectExpression = (path, {print, maybe, indent, write}, semantic
|
|
|
31
32
|
parseComments(path, {write}, semantics);
|
|
32
33
|
maybe.print.newline(manyLines);
|
|
33
34
|
|
|
34
|
-
|
|
35
|
+
const n = properties.length - 1;
|
|
36
|
+
|
|
37
|
+
for (const [index, property] of properties.entries()) {
|
|
35
38
|
if (property.isSpreadElement()) {
|
|
36
39
|
const logical = isLogical(property);
|
|
37
40
|
|
|
@@ -39,7 +42,7 @@ module.exports.ObjectExpression = (path, {print, maybe, indent, write}, semantic
|
|
|
39
42
|
print(property);
|
|
40
43
|
|
|
41
44
|
if (length > 1 || manyLines) {
|
|
42
|
-
print(',');
|
|
45
|
+
maybe.print(index !== n || trailingComma, ',');
|
|
43
46
|
print.newline();
|
|
44
47
|
}
|
|
45
48
|
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
const {isConcatenation} = require('../binary-expression/concatanate');
|
|
4
4
|
const {isOneLine} = require('./object-expression');
|
|
5
5
|
|
|
6
|
-
module.exports.ObjectProperty = (path, {maybe, traverse, write}) => {
|
|
6
|
+
module.exports.ObjectProperty = (path, {maybe, traverse, write}, semantics) => {
|
|
7
|
+
const {trailingComma} = semantics;
|
|
7
8
|
const {shorthand, computed} = path.node;
|
|
8
9
|
|
|
9
10
|
const key = path.get('key');
|
|
@@ -24,7 +25,7 @@ module.exports.ObjectProperty = (path, {maybe, traverse, write}) => {
|
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
if (manyLines)
|
|
27
|
-
write(',');
|
|
28
|
+
maybe.write(!isLast || trailingComma, ',');
|
|
28
29
|
else if (!isLast && properties.length)
|
|
29
30
|
write(', ');
|
|
30
31
|
};
|
|
@@ -39,7 +39,23 @@ module.exports = {
|
|
|
39
39
|
StringLiteral(path, {write}) {
|
|
40
40
|
const {raw, value} = path.node;
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
if (raw && path.parentPath.isJSXAttribute()) {
|
|
43
|
+
write(raw);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (raw) {
|
|
48
|
+
const value = raw.slice(1, -1);
|
|
49
|
+
write.quote();
|
|
50
|
+
write(value);
|
|
51
|
+
write.quote();
|
|
52
|
+
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
write.quote();
|
|
57
|
+
write(value);
|
|
58
|
+
write.quote();
|
|
43
59
|
},
|
|
44
60
|
RegExpLiteral(path, {print}) {
|
|
45
61
|
print(path.node.raw);
|
|
@@ -22,6 +22,7 @@ function initFormat(format) {
|
|
|
22
22
|
splitter: '\n',
|
|
23
23
|
roundBraceOpen: '(',
|
|
24
24
|
roundBraceClose: ')',
|
|
25
|
+
quote: `'`,
|
|
25
26
|
...format,
|
|
26
27
|
};
|
|
27
28
|
}
|
|
@@ -33,6 +34,7 @@ function initSemantics(semantics = {}) {
|
|
|
33
34
|
maxSpecifiersInOneLine: 2,
|
|
34
35
|
maxElementsInOneLine: 5,
|
|
35
36
|
maxVariablesInOneLine: 4,
|
|
37
|
+
trailingComma: true,
|
|
36
38
|
...semantics,
|
|
37
39
|
};
|
|
38
40
|
}
|
package/lib/tokenize/tokenize.js
CHANGED
|
@@ -131,6 +131,13 @@ module.exports.tokenize = (ast, overrides) => {
|
|
|
131
131
|
indent();
|
|
132
132
|
};
|
|
133
133
|
|
|
134
|
+
const quote = () => {
|
|
135
|
+
addToken({
|
|
136
|
+
type: TYPES.QUOTE,
|
|
137
|
+
value: format.quote,
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
|
|
134
141
|
const newline = () => {
|
|
135
142
|
addToken({
|
|
136
143
|
type: TYPES.NEWLINE,
|
|
@@ -145,6 +152,7 @@ module.exports.tokenize = (ast, overrides) => {
|
|
|
145
152
|
breakline,
|
|
146
153
|
space,
|
|
147
154
|
splitter,
|
|
155
|
+
quote,
|
|
148
156
|
});
|
|
149
157
|
|
|
150
158
|
assign(maybeWrite, {
|
|
@@ -173,6 +181,7 @@ module.exports.tokenize = (ast, overrides) => {
|
|
|
173
181
|
debug,
|
|
174
182
|
traverse,
|
|
175
183
|
maybe,
|
|
184
|
+
quote,
|
|
176
185
|
store: fullstore(),
|
|
177
186
|
};
|
|
178
187
|
|
package/lib/types.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.1.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",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"@babel/types": "^7.21.3",
|
|
33
33
|
"@putout/compare": "^12.0.4",
|
|
34
34
|
"@putout/operate": "^10.0.2",
|
|
35
|
+
"@putout/processor-json": "^7.0.0",
|
|
35
36
|
"fullstore": "^3.0.0",
|
|
36
37
|
"just-snake-case": "^3.2.0",
|
|
37
38
|
"parse-import-specifiers": "^1.0.1",
|