@putout/printer 3.3.0 → 3.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/lib/tokenize/expressions/call-expression.js +0 -4
- package/lib/tokenize/expressions/function/arrow-function-expression.js +36 -47
- package/lib/tokenize/expressions/function/function-expression.js +3 -2
- package/lib/tokenize/expressions/function/parens.js +14 -0
- package/lib/tokenize/expressions/unary-expression/unary-expressions.js +15 -21
- package/lib/tokenize/literals/identifier.js +3 -11
- package/lib/tokenize/statements/block-statement/block-statement.js +8 -1
- package/lib/tokenize/typescript/function/ts-function-type.js +4 -8
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -7,16 +7,12 @@ function CallExpression(path, {indent, print, maybe, traverse}) {
|
|
|
7
7
|
|
|
8
8
|
const callee = path.get('callee');
|
|
9
9
|
const typeParameters = path.get('typeParameters');
|
|
10
|
-
const isFn = callee.isFunction();
|
|
11
10
|
|
|
12
|
-
maybe.write(isFn, '(');
|
|
13
11
|
traverse(callee);
|
|
14
12
|
|
|
15
13
|
if (exists(typeParameters))
|
|
16
14
|
traverse(typeParameters);
|
|
17
15
|
|
|
18
|
-
maybe.write(isFn, ')');
|
|
19
|
-
|
|
20
16
|
if (path.node.optional)
|
|
21
17
|
print('?.');
|
|
22
18
|
|
|
@@ -2,51 +2,40 @@
|
|
|
2
2
|
|
|
3
3
|
const {exists} = require('../../is');
|
|
4
4
|
const {printParams} = require('./params');
|
|
5
|
+
const {maybeParens} = require('./parens');
|
|
5
6
|
|
|
6
|
-
module.exports.ArrowFunctionExpression = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const insideCall = path.parentPath.isCallExpression();
|
|
43
|
-
const isJSX = body.isJSXElement();
|
|
44
|
-
|
|
45
|
-
maybe.print.space(!insideCall && isJSX);
|
|
46
|
-
maybe.print.space(!isJSX);
|
|
47
|
-
print('__body');
|
|
48
|
-
},
|
|
49
|
-
after(path, {write}) {
|
|
50
|
-
write(')');
|
|
51
|
-
},
|
|
52
|
-
};
|
|
7
|
+
module.exports.ArrowFunctionExpression = maybeParens((path, printer, semantics) => {
|
|
8
|
+
const {
|
|
9
|
+
print,
|
|
10
|
+
maybe,
|
|
11
|
+
write,
|
|
12
|
+
traverse,
|
|
13
|
+
} = printer;
|
|
14
|
+
|
|
15
|
+
const {async} = path.node;
|
|
16
|
+
|
|
17
|
+
print('__typeParameters');
|
|
18
|
+
maybe.print(async, 'async ');
|
|
19
|
+
|
|
20
|
+
printParams(path, printer, semantics);
|
|
21
|
+
|
|
22
|
+
const returnType = path.get('returnType');
|
|
23
|
+
|
|
24
|
+
if (exists(returnType)) {
|
|
25
|
+
write(':');
|
|
26
|
+
write.space();
|
|
27
|
+
traverse(returnType);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
print.space();
|
|
31
|
+
print('=>');
|
|
32
|
+
|
|
33
|
+
const body = path.get('body');
|
|
34
|
+
|
|
35
|
+
const insideCall = path.parentPath.isCallExpression();
|
|
36
|
+
const isJSX = body.isJSXElement();
|
|
37
|
+
|
|
38
|
+
maybe.print.space(!insideCall && isJSX);
|
|
39
|
+
maybe.print.space(!isJSX);
|
|
40
|
+
print('__body');
|
|
41
|
+
});
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
const {exists} = require('../../is');
|
|
4
4
|
const {printParams} = require('./params');
|
|
5
|
+
const {maybeParens} = require('./parens');
|
|
5
6
|
|
|
6
|
-
module.exports.FunctionExpression = (path, printer, semantics) => {
|
|
7
|
+
module.exports.FunctionExpression = maybeParens((path, printer, semantics) => {
|
|
7
8
|
const {
|
|
8
9
|
print,
|
|
9
10
|
maybe,
|
|
@@ -29,4 +30,4 @@ module.exports.FunctionExpression = (path, printer, semantics) => {
|
|
|
29
30
|
|
|
30
31
|
print.space();
|
|
31
32
|
print('__body');
|
|
32
|
-
};
|
|
33
|
+
});
|
|
@@ -1,10 +1,21 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {isLast, isNext} = require('../../is');
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
|
|
5
|
+
const {maybeParens} = require('../function/parens');
|
|
6
|
+
|
|
7
|
+
const isWord = (a) => /^(delete|typeof|void|throw)$/.test(a);
|
|
8
|
+
|
|
9
|
+
const unaryExpression = maybeParens((path, printer) => {
|
|
10
|
+
const {maybe, traverse} = printer;
|
|
11
|
+
const {prefix, operator} = path.node;
|
|
12
|
+
const argPath = path.get('argument');
|
|
13
|
+
|
|
14
|
+
maybe.print(prefix, operator);
|
|
15
|
+
maybe.print(isWord(operator), ' ');
|
|
16
|
+
traverse(argPath);
|
|
17
|
+
maybe.print(!prefix, operator);
|
|
18
|
+
});
|
|
8
19
|
|
|
9
20
|
module.exports.UnaryExpression = unaryExpression;
|
|
10
21
|
module.exports.UpdateExpression = unaryExpression;
|
|
@@ -40,20 +51,3 @@ function printUnary(path, name, {print}) {
|
|
|
40
51
|
print(`${name} `);
|
|
41
52
|
print('__argument');
|
|
42
53
|
}
|
|
43
|
-
|
|
44
|
-
const isWord = (a) => /^(delete|typeof|void|throw)$/.test(a);
|
|
45
|
-
|
|
46
|
-
function unaryExpression(path, printer) {
|
|
47
|
-
const {maybe, traverse} = printer;
|
|
48
|
-
const {prefix, operator} = path.node;
|
|
49
|
-
const argPath = path.get('argument');
|
|
50
|
-
|
|
51
|
-
maybeParenOpen(path, printer);
|
|
52
|
-
|
|
53
|
-
maybe.print(prefix, operator);
|
|
54
|
-
maybe.print(isWord(operator), ' ');
|
|
55
|
-
traverse(argPath);
|
|
56
|
-
maybe.print(!prefix, operator);
|
|
57
|
-
|
|
58
|
-
maybeParenClose(path, printer);
|
|
59
|
-
}
|
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
maybeParenOpen,
|
|
5
|
-
maybeParenClose,
|
|
6
|
-
} = require('../expressions/unary-expression/parens');
|
|
7
|
-
|
|
8
3
|
const {maybeDecorators} = require('../maybe-get');
|
|
4
|
+
const {maybeParens} = require('../expressions/function/parens');
|
|
9
5
|
|
|
10
|
-
module.exports.Identifier = (path, printer) => {
|
|
6
|
+
module.exports.Identifier = maybeParens((path, printer) => {
|
|
11
7
|
const {
|
|
12
8
|
write,
|
|
13
9
|
maybe,
|
|
@@ -20,8 +16,6 @@ module.exports.Identifier = (path, printer) => {
|
|
|
20
16
|
|
|
21
17
|
const typeAnnotation = path.get('typeAnnotation');
|
|
22
18
|
|
|
23
|
-
maybeParenOpen(path, printer);
|
|
24
|
-
|
|
25
19
|
for (const decorator of maybeDecorators(path)) {
|
|
26
20
|
traverse(decorator);
|
|
27
21
|
print(' ');
|
|
@@ -30,6 +24,4 @@ module.exports.Identifier = (path, printer) => {
|
|
|
30
24
|
write(name);
|
|
31
25
|
maybe.write(optional, '?');
|
|
32
26
|
traverse(typeAnnotation);
|
|
33
|
-
|
|
34
|
-
maybeParenClose(path, printer);
|
|
35
|
-
};
|
|
27
|
+
});
|
|
@@ -21,6 +21,13 @@ const isFirstStatement = (path) => path.node.body[0];
|
|
|
21
21
|
const isFirstDirective = (path) => path.node.directives?.[0];
|
|
22
22
|
const isMethodOrArrow = (path) => isArrowFunctionExpression(path) || isObjectMethod(path);
|
|
23
23
|
|
|
24
|
+
const parentIfWithoutElse = ({parentPath}) => {
|
|
25
|
+
if (!parentPath.isIfStatement())
|
|
26
|
+
return false;
|
|
27
|
+
|
|
28
|
+
return !parentPath.node.alternate;
|
|
29
|
+
};
|
|
30
|
+
|
|
24
31
|
module.exports.BlockStatement = {
|
|
25
32
|
print(path, {indent, maybe, write, traverse}, semantics) {
|
|
26
33
|
const body = path.get('body');
|
|
@@ -77,7 +84,7 @@ function shouldAddNewlineAfter(path) {
|
|
|
77
84
|
if (insideIfWithNoBody(path))
|
|
78
85
|
return false;
|
|
79
86
|
|
|
80
|
-
if (path
|
|
87
|
+
if (parentIfWithoutElse(path) && path.find(isMethodOrArrow))
|
|
81
88
|
return true;
|
|
82
89
|
|
|
83
90
|
if (parentPath.isStatement() && !path.node.body.length && !isNext(parentPath))
|
|
@@ -2,19 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
const {printParams} = require('../../expressions/function/params');
|
|
4
4
|
const {printReturnType} = require('./print-return-type');
|
|
5
|
-
const {
|
|
6
|
-
maybeParenOpen,
|
|
7
|
-
maybeParenClose,
|
|
8
|
-
} = require('../../expressions/unary-expression/parens');
|
|
9
5
|
|
|
10
|
-
|
|
6
|
+
const {maybeParens} = require('../../expressions/function/parens');
|
|
7
|
+
|
|
8
|
+
module.exports.TSFunctionType = maybeParens((path, printer, semantics) => {
|
|
11
9
|
const {print} = printer;
|
|
12
10
|
|
|
13
|
-
maybeParenOpen(path, printer);
|
|
14
11
|
printParams(path, printer, semantics);
|
|
15
12
|
print.space();
|
|
16
13
|
print('=>');
|
|
17
14
|
print.space();
|
|
18
15
|
printReturnType(path, printer);
|
|
19
|
-
|
|
20
|
-
};
|
|
16
|
+
});
|
package/package.json
CHANGED