@putout/printer 11.2.1 → 11.3.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 +7 -0
- package/lib/tokenize/expressions/assignment-expression/maybe-write-brace.js +1 -0
- package/lib/tokenize/expressions/binary-expression/binary-expression.js +26 -49
- package/lib/tokenize/expressions/binary-expression/concatenate.js +3 -3
- package/lib/tokenize/expressions/index.js +2 -6
- package/lib/tokenize/expressions/logical-expression/chain.js +52 -0
- package/lib/tokenize/expressions/logical-expression/logical-expression.js +46 -0
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
2024.12.12, v11.3.0
|
|
2
|
+
|
|
3
|
+
feature:
|
|
4
|
+
- b5de5fc @putout/printer: split BinaryExpression and LogicalExpression
|
|
5
|
+
- 262f6ab @putout/printer: LogicalExpressions: maxElementsInOneLine
|
|
6
|
+
- 4bccded @putout/printer: LogicalExpressions: newline
|
|
7
|
+
|
|
1
8
|
2024.12.12, v11.2.1
|
|
2
9
|
|
|
3
10
|
feature:
|
|
@@ -6,56 +6,33 @@ const {
|
|
|
6
6
|
} = require('./concatenate');
|
|
7
7
|
|
|
8
8
|
const {maybeSpace} = require('./maybe-space');
|
|
9
|
-
const {
|
|
9
|
+
const {maybeParens} = require('../../maybe/maybe-parens');
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return path.parentPath.isAwaitExpression();
|
|
17
|
-
},
|
|
18
|
-
before(path, {print}) {
|
|
19
|
-
print('(');
|
|
20
|
-
},
|
|
21
|
-
print(path, {print, indent, maybe}) {
|
|
22
|
-
const {operator} = path.node;
|
|
23
|
-
|
|
24
|
-
if (operator === 'instanceof') {
|
|
25
|
-
print('__left');
|
|
26
|
-
print(' instanceof ');
|
|
27
|
-
print('__right');
|
|
28
|
-
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (operator === 'in') {
|
|
33
|
-
print('__left');
|
|
34
|
-
print(' in ');
|
|
35
|
-
print('__right');
|
|
36
|
-
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (isConcatenation(path))
|
|
41
|
-
return concatenate(path, {
|
|
42
|
-
print,
|
|
43
|
-
indent,
|
|
44
|
-
maybe,
|
|
45
|
-
});
|
|
46
|
-
|
|
11
|
+
module.exports.BinaryExpression = maybeParens((path, {print, indent, maybe}) => {
|
|
12
|
+
const {operator} = path.node;
|
|
13
|
+
|
|
14
|
+
if (operator === 'in' || operator === 'instanceof') {
|
|
47
15
|
print('__left');
|
|
48
|
-
print
|
|
49
|
-
print(
|
|
50
|
-
|
|
16
|
+
print(' ');
|
|
17
|
+
print(operator);
|
|
18
|
+
print(' ');
|
|
19
|
+
print('__right');
|
|
20
|
+
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (isConcatenation(path))
|
|
25
|
+
return concatenate(path, {
|
|
51
26
|
print,
|
|
27
|
+
indent,
|
|
28
|
+
maybe,
|
|
52
29
|
});
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
30
|
+
|
|
31
|
+
print('__left');
|
|
32
|
+
print.space();
|
|
33
|
+
print(path.node.operator);
|
|
34
|
+
maybeSpace(path, {
|
|
35
|
+
print,
|
|
36
|
+
});
|
|
37
|
+
print('__right');
|
|
38
|
+
});
|
|
@@ -17,6 +17,9 @@ module.exports.isConcatenation = (path) => {
|
|
|
17
17
|
const {parentPath} = path;
|
|
18
18
|
const {operator} = path.node;
|
|
19
19
|
|
|
20
|
+
if (operator !== '+')
|
|
21
|
+
return false;
|
|
22
|
+
|
|
20
23
|
const startLine = path.node.loc?.start.line;
|
|
21
24
|
const endLine = path.node.loc?.end.line;
|
|
22
25
|
|
|
@@ -26,9 +29,6 @@ module.exports.isConcatenation = (path) => {
|
|
|
26
29
|
const left = path.get('left');
|
|
27
30
|
const right = path.get('right');
|
|
28
31
|
|
|
29
|
-
if (operator !== '+')
|
|
30
|
-
return false;
|
|
31
|
-
|
|
32
32
|
if (isStringLike(left) && isStringLike(right) && isBinaryExpression(parentPath))
|
|
33
33
|
return true;
|
|
34
34
|
|
|
@@ -35,12 +35,8 @@ const {RestElement} = require('./rest-element');
|
|
|
35
35
|
const {SpreadElement} = require('./spread-element');
|
|
36
36
|
const {SequenceExpression} = require('./sequence-expression/sequence-expression');
|
|
37
37
|
const {TaggedTemplateExpression} = require('./tagged-template-expression');
|
|
38
|
-
|
|
39
|
-
const {
|
|
40
|
-
BinaryExpression,
|
|
41
|
-
LogicalExpression,
|
|
42
|
-
} = require('./binary-expression/binary-expression');
|
|
43
|
-
|
|
38
|
+
const {BinaryExpression} = require('./binary-expression/binary-expression');
|
|
39
|
+
const {LogicalExpression} = require('./logical-expression/logical-expression');
|
|
44
40
|
const {ConditionalExpression} = require('./conditional-expression');
|
|
45
41
|
const {StaticBlock} = require('./class/static-block');
|
|
46
42
|
const {RecordExpression} = require('./object-expression/record-expression');
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
isReturnStatement,
|
|
5
|
+
isVariableDeclarator,
|
|
6
|
+
} = require('@putout/babel').types;
|
|
7
|
+
|
|
8
|
+
module.exports.isRootOk = (path) => {
|
|
9
|
+
return isReturnStatement(path) || isVariableDeclarator(path);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
module.exports.chain = (path) => {
|
|
13
|
+
const [downCount] = down(path);
|
|
14
|
+
const [upCount, root] = up(path);
|
|
15
|
+
|
|
16
|
+
return [
|
|
17
|
+
root,
|
|
18
|
+
downCount + upCount,
|
|
19
|
+
downCount,
|
|
20
|
+
upCount,
|
|
21
|
+
];
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
function down(current) {
|
|
25
|
+
let count = 0;
|
|
26
|
+
|
|
27
|
+
do {
|
|
28
|
+
++count;
|
|
29
|
+
current = current.get('left');
|
|
30
|
+
const {operator} = current.node;
|
|
31
|
+
|
|
32
|
+
if (operator !== '||' && operator !== '&&')
|
|
33
|
+
break;
|
|
34
|
+
} while (current.isLogicalExpression());
|
|
35
|
+
|
|
36
|
+
return [count];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function up(current) {
|
|
40
|
+
let count = 0;
|
|
41
|
+
|
|
42
|
+
do {
|
|
43
|
+
++count;
|
|
44
|
+
current = current.parentPath;
|
|
45
|
+
} while (current.isLogicalExpression());
|
|
46
|
+
|
|
47
|
+
return [
|
|
48
|
+
count, {
|
|
49
|
+
type: current.type,
|
|
50
|
+
},
|
|
51
|
+
];
|
|
52
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {isParens} = require('../../maybe/maybe-parens');
|
|
4
|
+
const {chain, isRootOk} = require('./chain');
|
|
5
|
+
|
|
6
|
+
module.exports.LogicalExpression = {
|
|
7
|
+
condition(path) {
|
|
8
|
+
if (isParens(path))
|
|
9
|
+
return true;
|
|
10
|
+
|
|
11
|
+
return path.parentPath.isAwaitExpression();
|
|
12
|
+
},
|
|
13
|
+
before(path, {print}) {
|
|
14
|
+
print('(');
|
|
15
|
+
},
|
|
16
|
+
print(path, {print, maybe}, semantics) {
|
|
17
|
+
print('__left');
|
|
18
|
+
|
|
19
|
+
const needNewLine = isNewLine(path, semantics);
|
|
20
|
+
|
|
21
|
+
maybe.indent.inc(needNewLine);
|
|
22
|
+
needNewLine ? print.breakline() : print.space();
|
|
23
|
+
maybe.indent.dec(needNewLine);
|
|
24
|
+
|
|
25
|
+
print(path.node.operator);
|
|
26
|
+
print.space();
|
|
27
|
+
print('__right');
|
|
28
|
+
},
|
|
29
|
+
after(path, {print}) {
|
|
30
|
+
print(')');
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
function isNewLine(path, semantics) {
|
|
35
|
+
const [root, count] = chain(path);
|
|
36
|
+
|
|
37
|
+
if (!isRootOk(root))
|
|
38
|
+
return false;
|
|
39
|
+
|
|
40
|
+
if (count <= semantics.maxElementsInOneLine)
|
|
41
|
+
return false;
|
|
42
|
+
|
|
43
|
+
const {operator} = path.node;
|
|
44
|
+
|
|
45
|
+
return operator === '||' || operator === '&&';
|
|
46
|
+
}
|
package/package.json
CHANGED