@putout/printer 1.4.1 → 1.5.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 +10 -0
- package/README.md +1 -1
- package/lib/tokenize/expressions/functions.js +39 -3
- package/lib/tokenize/expressions/index.js +2 -0
- package/lib/tokenize/expressions/sequence-expression.js +15 -0
- package/lib/tokenize/expressions/unary-expressions.js +30 -0
- package/lib/tokenize/literals/index.js +3 -2
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
2023.03.18, v1.5.0
|
|
2
|
+
|
|
3
|
+
feature:
|
|
4
|
+
- db7c503 @putout/printer: add support of YieldExpression, SequenceExpressions, FunctionExpression + generator
|
|
5
|
+
|
|
6
|
+
2023.03.18, v1.4.2
|
|
7
|
+
|
|
8
|
+
feature:
|
|
9
|
+
- fe8c410 @putout/printer: add support of AwaitExpression
|
|
10
|
+
|
|
1
11
|
2023.03.17, v1.4.1
|
|
2
12
|
|
|
3
13
|
feature:
|
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/printer.svg?style=flat&longCache=true
|
|
4
4
|
[NPMURL]: https://npmjs.org/package/@putout/printer "npm"
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
Prints [**Babel AST**](https://github.com/coderaiser/estree-to-babel) to readable **JavaScript**.
|
|
7
7
|
|
|
8
8
|
- ☝️ Similar to **Recast**, but 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**.
|
|
@@ -3,7 +3,38 @@
|
|
|
3
3
|
const {isMarkedPrevAfter} = require('../mark');
|
|
4
4
|
const isFirst = (path) => path.node === path.parentPath.node.body[0];
|
|
5
5
|
|
|
6
|
-
module.exports.
|
|
6
|
+
module.exports.FunctionExpression = (path, {write, maybe, traverse}) => {
|
|
7
|
+
const {node} = path;
|
|
8
|
+
|
|
9
|
+
const {
|
|
10
|
+
generator,
|
|
11
|
+
async,
|
|
12
|
+
} = node;
|
|
13
|
+
|
|
14
|
+
maybe.write(async, 'async ');
|
|
15
|
+
write('function');
|
|
16
|
+
maybe.write(generator, '*');
|
|
17
|
+
write(' (');
|
|
18
|
+
|
|
19
|
+
const params = path.get('params');
|
|
20
|
+
const n = params.length;
|
|
21
|
+
|
|
22
|
+
for (let i = 0; i < n; i++) {
|
|
23
|
+
traverse(params[i]);
|
|
24
|
+
|
|
25
|
+
if (i < n - 1)
|
|
26
|
+
write(', ');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
write(') ');
|
|
30
|
+
traverse(path.get('body'));
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
module.exports.ArrowFunctionExpression = ArrowFunctionExpression;
|
|
34
|
+
function ArrowFunctionExpression(path, {write, maybe, traverse}) {
|
|
35
|
+
const {async} = path.node;
|
|
36
|
+
maybe.write(async, 'async ');
|
|
37
|
+
|
|
7
38
|
write('(');
|
|
8
39
|
|
|
9
40
|
const params = path.get('params');
|
|
@@ -18,7 +49,7 @@ module.exports.ArrowFunctionExpression = (path, {write, traverse}) => {
|
|
|
18
49
|
|
|
19
50
|
write(') => ');
|
|
20
51
|
traverse(path.get('body'));
|
|
21
|
-
}
|
|
52
|
+
}
|
|
22
53
|
|
|
23
54
|
module.exports.ObjectMethod = (path, {write, traverse}) => {
|
|
24
55
|
traverse(path.get('key'));
|
|
@@ -38,10 +69,14 @@ module.exports.ObjectMethod = (path, {write, traverse}) => {
|
|
|
38
69
|
traverse(path.get('body'));
|
|
39
70
|
};
|
|
40
71
|
|
|
41
|
-
module.exports.FunctionDeclaration = (path, {write, traverse}) => {
|
|
72
|
+
module.exports.FunctionDeclaration = (path, {write, maybe, traverse}) => {
|
|
73
|
+
const {async} = path.node;
|
|
74
|
+
|
|
42
75
|
if (!isFirst(path) && !isMarkedPrevAfter(path))
|
|
43
76
|
write('\n');
|
|
44
77
|
|
|
78
|
+
maybe.write(async, 'async ');
|
|
79
|
+
|
|
45
80
|
write('function ');
|
|
46
81
|
traverse(path.get('id'));
|
|
47
82
|
write('(');
|
|
@@ -77,3 +112,4 @@ module.exports.ClassMethod = (path, {write, traverse}) => {
|
|
|
77
112
|
write(') ');
|
|
78
113
|
traverse(path.get('body'));
|
|
79
114
|
};
|
|
115
|
+
|
|
@@ -15,6 +15,7 @@ const {ArrayPattern} = require('./array-pattern');
|
|
|
15
15
|
const {AssignmentPattern} = require('./assignment-pattern');
|
|
16
16
|
const {RestElement} = require('./rest-element');
|
|
17
17
|
const {SpreadElement} = require('./spread-element');
|
|
18
|
+
const {SequenceExpression} = require('./sequence-expression');
|
|
18
19
|
|
|
19
20
|
module.exports = {
|
|
20
21
|
...functions,
|
|
@@ -31,6 +32,7 @@ module.exports = {
|
|
|
31
32
|
ObjectPattern,
|
|
32
33
|
RestElement,
|
|
33
34
|
SpreadElement,
|
|
35
|
+
SequenceExpression,
|
|
34
36
|
BinaryExpression(path, {traverse, write}) {
|
|
35
37
|
traverse(path.get('left'));
|
|
36
38
|
write(` ${path.node.operator} `);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {entries} = Object;
|
|
4
|
+
|
|
5
|
+
module.exports.SequenceExpression = (path, {maybe, traverse}) => {
|
|
6
|
+
const expressions = path.get('expressions');
|
|
7
|
+
const n = expressions.length - 1;
|
|
8
|
+
|
|
9
|
+
for (const [index, expression] of entries(expressions)) {
|
|
10
|
+
traverse(expression);
|
|
11
|
+
maybe.write(index < n, ',');
|
|
12
|
+
maybe.write(index < n, ' ');
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
@@ -2,6 +2,35 @@
|
|
|
2
2
|
|
|
3
3
|
module.exports.UnaryExpression = unaryExpressions;
|
|
4
4
|
module.exports.UpdateExpression = unaryExpressions;
|
|
5
|
+
module.exports.AwaitExpression = (path, {write, traverse}) => {
|
|
6
|
+
printUnary(path, 'await', {
|
|
7
|
+
write,
|
|
8
|
+
traverse,
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
module.exports.YieldExpression = (path, {write, traverse}) => {
|
|
12
|
+
printUnary(path, 'yield', {
|
|
13
|
+
write,
|
|
14
|
+
traverse,
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
module.exports.ThrowStatement = (path, {write, indent, traverse}) => {
|
|
19
|
+
indent();
|
|
20
|
+
|
|
21
|
+
printUnary(path, 'throw', {
|
|
22
|
+
write,
|
|
23
|
+
traverse,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
write(';');
|
|
27
|
+
write.newline();
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function printUnary(path, name, {write, traverse}) {
|
|
31
|
+
write(`${name} `);
|
|
32
|
+
traverse(path.get('argument'));
|
|
33
|
+
}
|
|
5
34
|
|
|
6
35
|
const isWord = (a) => /delete|typeof/.test(a);
|
|
7
36
|
|
|
@@ -17,3 +46,4 @@ function unaryExpressions(path, {traverse, write, maybe}) {
|
|
|
17
46
|
if (!prefix)
|
|
18
47
|
write(operator);
|
|
19
48
|
}
|
|
49
|
+
|
|
@@ -5,8 +5,9 @@ const {TemplateLiteral} = require('./template-literal');
|
|
|
5
5
|
module.exports = {
|
|
6
6
|
TemplateLiteral,
|
|
7
7
|
NumericLiteral(path, {write}) {
|
|
8
|
-
const {
|
|
9
|
-
write(raw
|
|
8
|
+
const {raw} = path.node;
|
|
9
|
+
write(raw);
|
|
10
|
+
//write(raw || extra.raw);
|
|
10
11
|
},
|
|
11
12
|
BooleanLiteral(path, {write}) {
|
|
12
13
|
write(path.node.value);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.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",
|