Skip to content

Commit bf4df00

Browse files
committed
use the same logic for grouping leftover operators in case of an assignment
1 parent e2b8464 commit bf4df00

File tree

5 files changed

+23
-36
lines changed

5 files changed

+23
-36
lines changed

src/binary-operator-printers/comparison.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,9 @@ export const comparison = {
2424
print: (node, path, print, options) => {
2525
const indentIfNecessary = indentIfNecessaryBuilder(path);
2626

27-
const right = rightOperandPrinter(node, path, print, options);
28-
// If it's a single binary operation, avoid having a small right
29-
// operand like - 1 on its own line
30-
const shouldGroup =
31-
node.left.type !== 'BinaryOperation' &&
32-
path.getParentNode().type !== 'BinaryOperation';
3327
return group([
3428
path.call(print, 'left'),
35-
indentIfNecessary(shouldGroup ? group(right) : right)
29+
indentIfNecessary(rightOperandPrinter(node, path, print, options))
3630
]);
3731
}
3832
};

src/binary-operator-printers/logical.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,9 @@ export const logical = {
3131
const groupIfNecessary = groupIfNecessaryBuilder(path);
3232
const indentIfNecessary = indentIfNecessaryBuilder(path, options);
3333

34-
const right = rightOperandPrinter(node, path, print, options);
35-
// If it's a single binary operation, avoid having a small right
36-
// operand like - 1 on its own line
37-
const shouldGroup =
38-
node.left.type !== 'BinaryOperation' &&
39-
path.getParentNode().type !== 'BinaryOperation';
4034
return groupIfNecessary([
4135
path.call(print, 'left'),
42-
indentIfNecessary(shouldGroup ? group(right) : right)
36+
indentIfNecessary(rightOperandPrinter(node, path, print, options))
4337
]);
4438
}
4539
};

src/binary-operator-printers/printers/create-binary-operation-printer.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { doc } from 'prettier';
2-
import { assignment } from '../assignment.js';
32
import { comparison } from '../comparison.js';
43
import { rightOperandPrinter } from './right-operand-printer.js';
54

@@ -46,16 +45,9 @@ export const createBinaryOperationPrinter =
4645
const groupIfNecessary = groupIfNecessaryBuilder(path);
4746
const indentIfNecessary = indentIfNecessaryBuilder(path);
4847

49-
const right = rightOperandPrinter(node, path, print, options);
50-
// If it's a single binary operation, avoid having a small right
51-
// operand like - 1 on its own line
52-
const parent = path.getParentNode();
53-
const shouldGroup =
54-
node.left.type !== 'BinaryOperation' &&
55-
(parent.type !== 'BinaryOperation' || assignment.match(parent.operator));
5648
return groupIfNecessary([
5749
path.call(print, 'left'),
58-
indentIfNecessary(shouldGroup ? group(right) : right)
50+
indentIfNecessary(rightOperandPrinter(node, path, print, options))
5951
]);
6052
};
6153

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
import { doc } from 'prettier';
2+
import { assignment } from '../assignment.js';
23

3-
const { line } = doc.builders;
4+
const { group, line } = doc.builders;
45

5-
export const rightOperandPrinter = (node, path, print, options) =>
6-
options.experimentalOperatorPosition === 'end'
7-
? [' ', node.operator, line, path.call(print, 'right')]
8-
: [line, node.operator, ' ', path.call(print, 'right')];
6+
export const rightOperandPrinter = (node, path, print, options) => {
7+
const right =
8+
options.experimentalOperatorPosition === 'end'
9+
? [' ', node.operator, line, path.call(print, 'right')]
10+
: [line, node.operator, ' ', path.call(print, 'right')];
11+
12+
// If it's a single binary operation, avoid having a small right
13+
// operand like - 1 on its own line
14+
const parent = path.getParentNode();
15+
return node.left.type !== 'BinaryOperation' &&
16+
(parent.type !== 'BinaryOperation' || assignment.match(parent.operator))
17+
? group(right)
18+
: right;
19+
};

tests/format/BinaryOperators/__snapshots__/jsfmt.spec.js.snap

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,7 @@ contract ComparisonOperators {
509509
a =
510510
veryVeryVeryVeryVeryLongFunctionCalledA(
511511
veryVeryVeryVeryVeryLongVariableCalledB
512-
)
513-
== c;
512+
) == c;
514513
if (
515514
veryVeryVeryVeryVeryLongFunctionCalledA(
516515
veryVeryVeryVeryVeryLongVariableCalledB
@@ -663,8 +662,7 @@ contract LogicalOperators {
663662
a =
664663
veryVeryVeryVeryVeryLongFunctionCalledA(
665664
veryVeryVeryVeryVeryLongVariableCalledB
666-
)
667-
|| c;
665+
) || c;
668666
if (
669667
veryVeryVeryVeryVeryLongFunctionCalledA(
670668
veryVeryVeryVeryVeryLongVariableCalledB
@@ -1193,8 +1191,7 @@ contract ComparisonOperators {
11931191
a =
11941192
veryVeryVeryVeryVeryLongFunctionCalledA(
11951193
veryVeryVeryVeryVeryLongVariableCalledB
1196-
) ==
1197-
c;
1194+
) == c;
11981195
if (
11991196
veryVeryVeryVeryVeryLongFunctionCalledA(
12001197
veryVeryVeryVeryVeryLongVariableCalledB
@@ -1347,8 +1344,7 @@ contract LogicalOperators {
13471344
a =
13481345
veryVeryVeryVeryVeryLongFunctionCalledA(
13491346
veryVeryVeryVeryVeryLongVariableCalledB
1350-
) ||
1351-
c;
1347+
) || c;
13521348
if (
13531349
veryVeryVeryVeryVeryLongFunctionCalledA(
13541350
veryVeryVeryVeryVeryLongVariableCalledB

0 commit comments

Comments
 (0)