@putout/printer 11.2.1 → 11.4.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 +12 -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 +40 -0
- package/lib/tokenize/maybe/maybe-parens.js +14 -1
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
2024.12.13, v11.4.0
|
|
2
|
+
|
|
3
|
+
feature:
|
|
4
|
+
- 332efd1 @putout/printer: LogicalExpression inside UnaryExpression: parens
|
|
5
|
+
|
|
6
|
+
2024.12.12, v11.3.0
|
|
7
|
+
|
|
8
|
+
feature:
|
|
9
|
+
- b5de5fc @putout/printer: split BinaryExpression and LogicalExpression
|
|
10
|
+
- 262f6ab @putout/printer: LogicalExpressions: maxElementsInOneLine
|
|
11
|
+
- 4bccded @putout/printer: LogicalExpressions: newline
|
|
12
|
+
|
|
1
13
|
2024.12.12, v11.2.1
|
|
2
14
|
|
|
3
15
|
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,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {maybeParens} = require('../../maybe/maybe-parens');
|
|
4
|
+
const {chain, isRootOk} = require('./chain');
|
|
5
|
+
|
|
6
|
+
module.exports.LogicalExpression = maybeParens({
|
|
7
|
+
condition(path) {
|
|
8
|
+
if (path.parentPath.isUnaryExpression())
|
|
9
|
+
return true;
|
|
10
|
+
|
|
11
|
+
return path.parentPath.isAwaitExpression();
|
|
12
|
+
},
|
|
13
|
+
print(path, {print, maybe}, semantics) {
|
|
14
|
+
print('__left');
|
|
15
|
+
|
|
16
|
+
const needNewLine = isNewLine(path, semantics);
|
|
17
|
+
|
|
18
|
+
maybe.indent.inc(needNewLine);
|
|
19
|
+
needNewLine ? print.breakline() : print.space();
|
|
20
|
+
maybe.indent.dec(needNewLine);
|
|
21
|
+
|
|
22
|
+
print(path.node.operator);
|
|
23
|
+
print.space();
|
|
24
|
+
print('__right');
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
function isNewLine(path, semantics) {
|
|
29
|
+
const [root, count] = chain(path);
|
|
30
|
+
|
|
31
|
+
if (!isRootOk(root))
|
|
32
|
+
return false;
|
|
33
|
+
|
|
34
|
+
if (count <= semantics.maxElementsInOneLine)
|
|
35
|
+
return false;
|
|
36
|
+
|
|
37
|
+
const {operator} = path.node;
|
|
38
|
+
|
|
39
|
+
return operator === '||' || operator === '&&';
|
|
40
|
+
}
|
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const isFn = (a) => typeof a === 'function';
|
|
3
4
|
const isParens = (path) => path.node.extra?.parenthesized;
|
|
4
5
|
|
|
5
6
|
module.exports.isParens = isParens;
|
|
6
|
-
module.exports.maybeParens = (print) =>
|
|
7
|
+
module.exports.maybeParens = (print) => {
|
|
8
|
+
if (isFn(print))
|
|
9
|
+
return maybeParensPrint(print);
|
|
10
|
+
|
|
11
|
+
return maybeParensCondition(print);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const maybeParensPrint = (print) => ({
|
|
7
15
|
condition: isParens,
|
|
8
16
|
before(path, {write}) {
|
|
9
17
|
write('(');
|
|
@@ -13,3 +21,8 @@ module.exports.maybeParens = (print) => ({
|
|
|
13
21
|
write(')');
|
|
14
22
|
},
|
|
15
23
|
});
|
|
24
|
+
|
|
25
|
+
const maybeParensCondition = ({print, condition}) => ({
|
|
26
|
+
...maybeParensPrint(print),
|
|
27
|
+
condition: (path) => condition(path) || isParens(path),
|
|
28
|
+
});
|
package/package.json
CHANGED