@putout/printer 15.18.0 → 15.18.1
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/assignment-expression/assignment-expression-comments.js +2 -2
- package/lib/tokenize/expressions/assignment-expression/assignment-expression.js +25 -45
- package/lib/tokenize/expressions/assignment-expression/maybe-parens-condition.js +27 -0
- package/lib/tokenize/expressions/assignment-expression/print-separator.js +23 -0
- package/lib/tokenize/maybe/maybe-parens.js +9 -2
- package/package.json +1 -1
- package/lib/tokenize/expressions/assignment-expression/maybe-write-brace.js +0 -51
package/ChangeLog
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
const {types} = require('@putout/babel');
|
|
4
4
|
const {hasLeadingComment} = require('#is');
|
|
5
|
+
const noop = () => {};
|
|
5
6
|
const {isReturnStatement} = types;
|
|
6
7
|
|
|
7
|
-
module.exports.printLeadingCommentLine =
|
|
8
|
+
module.exports.printLeadingCommentLine = noop;
|
|
8
9
|
|
|
9
10
|
module.exports.maybeInsideReturnWithCommentStart = (path, {print, indent}) => {
|
|
10
11
|
const {parentPath} = path;
|
|
@@ -34,4 +35,3 @@ module.exports.maybeInsideReturnWithCommentEnd = (path, {print, indent}) => {
|
|
|
34
35
|
indent.dec();
|
|
35
36
|
print.breakline();
|
|
36
37
|
};
|
|
37
|
-
|
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
|
|
5
|
-
const {
|
|
6
|
-
maybePrintLeftBrace,
|
|
7
|
-
maybePrintRightBrace,
|
|
8
|
-
} = require('./maybe-write-brace');
|
|
3
|
+
const {condition} = require('./maybe-parens-condition');
|
|
9
4
|
|
|
10
5
|
const {
|
|
11
6
|
printLeadingCommentLine,
|
|
@@ -13,48 +8,33 @@ const {
|
|
|
13
8
|
maybeInsideReturnWithCommentStart,
|
|
14
9
|
} = require('./assignment-expression-comments');
|
|
15
10
|
|
|
16
|
-
const {
|
|
17
|
-
|
|
18
|
-
isAssignmentExpression,
|
|
19
|
-
} = types;
|
|
11
|
+
const {maybeParens} = require('../../maybe/maybe-parens');
|
|
12
|
+
const {printSeparator} = require('./print-separator');
|
|
20
13
|
|
|
21
14
|
const isInsideBlock = ({parentPath}) => /BlockStatement|Program/.test(parentPath.type);
|
|
22
15
|
|
|
23
|
-
module.exports.AssignmentExpression = (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (isMultiline(path))
|
|
35
|
-
print.breakline();
|
|
36
|
-
else
|
|
16
|
+
module.exports.AssignmentExpression = maybeParens({
|
|
17
|
+
checkParens: false,
|
|
18
|
+
condition,
|
|
19
|
+
print(path, printer) {
|
|
20
|
+
const {print} = printer;
|
|
21
|
+
const {operator} = path.node;
|
|
22
|
+
|
|
23
|
+
maybeInsideReturnWithCommentStart(path, printer);
|
|
24
|
+
|
|
25
|
+
print('__left');
|
|
37
26
|
print.space();
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
};
|
|
27
|
+
print(operator);
|
|
28
|
+
printSeparator(path, printer);
|
|
29
|
+
print('__right');
|
|
30
|
+
|
|
31
|
+
if (isInsideBlock(path)) {
|
|
32
|
+
print(';');
|
|
33
|
+
print.breakline();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
maybeInsideReturnWithCommentEnd(path, printer);
|
|
37
|
+
},
|
|
38
|
+
});
|
|
50
39
|
|
|
51
40
|
module.exports.AssignmentExpression.printLeadingCommentLine = printLeadingCommentLine;
|
|
52
|
-
|
|
53
|
-
function isMultiline(path) {
|
|
54
|
-
const {right} = path.node;
|
|
55
|
-
|
|
56
|
-
if (!path.parentPath.find(isExpressionStatement))
|
|
57
|
-
return false;
|
|
58
|
-
|
|
59
|
-
return isAssignmentExpression(right);
|
|
60
|
-
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {hasLeadingComment} = require('#is');
|
|
4
|
+
const {isParens} = require('../../maybe/maybe-parens');
|
|
5
|
+
|
|
6
|
+
module.exports.condition = (path, printer, semantics) => {
|
|
7
|
+
const {parentPath} = path;
|
|
8
|
+
const {type} = parentPath;
|
|
9
|
+
const {roundBraces} = semantics;
|
|
10
|
+
|
|
11
|
+
if (path.node.left.type === 'ObjectPattern')
|
|
12
|
+
return true;
|
|
13
|
+
|
|
14
|
+
if (type === 'LogicalExpression')
|
|
15
|
+
return true;
|
|
16
|
+
|
|
17
|
+
if (type === 'BinaryExpression')
|
|
18
|
+
return true;
|
|
19
|
+
|
|
20
|
+
if (type === 'UnaryExpression')
|
|
21
|
+
return true;
|
|
22
|
+
|
|
23
|
+
if (!roundBraces.assign && !hasLeadingComment(path))
|
|
24
|
+
return false;
|
|
25
|
+
|
|
26
|
+
return isParens(path);
|
|
27
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {types} = require('@putout/babel');
|
|
4
|
+
const {
|
|
5
|
+
isAssignmentExpression,
|
|
6
|
+
isExpressionStatement,
|
|
7
|
+
} = types;
|
|
8
|
+
|
|
9
|
+
module.exports.printSeparator = (path, {print}) => {
|
|
10
|
+
if (isMultiline(path))
|
|
11
|
+
print.breakline();
|
|
12
|
+
else
|
|
13
|
+
print.space();
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function isMultiline(path) {
|
|
17
|
+
const {right} = path.node;
|
|
18
|
+
|
|
19
|
+
if (!path.parentPath.find(isExpressionStatement))
|
|
20
|
+
return false;
|
|
21
|
+
|
|
22
|
+
return isAssignmentExpression(right);
|
|
23
|
+
}
|
|
@@ -22,7 +22,14 @@ const maybeParensPrint = (print) => ({
|
|
|
22
22
|
},
|
|
23
23
|
});
|
|
24
24
|
|
|
25
|
-
const maybeParensCondition = ({print, condition}) => ({
|
|
25
|
+
const maybeParensCondition = ({print, condition, checkParens = true}) => ({
|
|
26
26
|
...maybeParensPrint(print),
|
|
27
|
-
condition: (path
|
|
27
|
+
condition: (path, print, semantics) => {
|
|
28
|
+
const is = condition?.(path, print, semantics);
|
|
29
|
+
|
|
30
|
+
if (!checkParens)
|
|
31
|
+
return is;
|
|
32
|
+
|
|
33
|
+
return is || isParens(path);
|
|
34
|
+
},
|
|
28
35
|
});
|
package/package.json
CHANGED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const {hasLeadingComment} = require('#is');
|
|
4
|
-
const {isParens} = require('../../maybe/maybe-parens');
|
|
5
|
-
|
|
6
|
-
module.exports.maybePrintLeftBrace = (path, printer, semantics) => {
|
|
7
|
-
maybeWriteBrace(path, printer, semantics, {
|
|
8
|
-
brace: '(',
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
module.exports.maybePrintRightBrace = (path, printer, semantics) => {
|
|
13
|
-
maybeWriteBrace(path, printer, semantics, {
|
|
14
|
-
brace: ')',
|
|
15
|
-
});
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
function maybeWriteBrace(path, printer, semantics, {brace}) {
|
|
19
|
-
const {parentPath} = path;
|
|
20
|
-
const {type} = parentPath;
|
|
21
|
-
const {roundBraces} = semantics;
|
|
22
|
-
const {write} = printer;
|
|
23
|
-
|
|
24
|
-
if (path.node.left.type === 'ObjectPattern') {
|
|
25
|
-
write(brace);
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (type === 'LogicalExpression') {
|
|
30
|
-
write(brace);
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (type === 'BinaryExpression') {
|
|
35
|
-
write(brace);
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (type === 'UnaryExpression') {
|
|
40
|
-
write(brace);
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (!roundBraces.assign && !hasLeadingComment(path))
|
|
45
|
-
return;
|
|
46
|
-
|
|
47
|
-
if (!isParens(path))
|
|
48
|
-
return;
|
|
49
|
-
|
|
50
|
-
write(brace);
|
|
51
|
-
}
|