Skip to content

Commit 9d250c1

Browse files
authored
group a leftover right operand in case it's a single binary operation next to an AssignmentExpression (#1161)
1 parent b457922 commit 9d250c1

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const rightOperandPrinter = (node, path, print) => {
1010
// operand like - 1 on its own line
1111
const parent = path.getParentNode();
1212
return node.left.type !== 'BinaryOperation' &&
13-
parent.type !== 'BinaryOperation'
13+
(parent.type !== 'BinaryOperation' || assignment.match(parent.operator))
1414
? group(right)
1515
: right;
1616
};

src/slang-printers/create-binary-operation-printer.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TerminalKind } from '@nomicfoundation/slang/cst';
1+
import { NonterminalKind, TerminalKind } from '@nomicfoundation/slang/cst';
22
import { doc } from 'prettier';
33
import { isBinaryOperation } from '../slang-utils/is-binary-operation.js';
44

@@ -24,11 +24,15 @@ function rightOperandPrint(
2424
];
2525
// If it's a single binary operation, avoid having a small right
2626
// operand like - 1 on its own line
27+
const leftOperand = node.leftOperand.variant;
28+
const grandparentNode = path.getNode(2) as StrictAstNode;
2729
const shouldGroup =
2830
!(
29-
node.leftOperand.variant.kind !== TerminalKind.Identifier &&
30-
isBinaryOperation(node.leftOperand.variant)
31-
) && !isBinaryOperation(path.getNode(2) as StrictAstNode);
31+
leftOperand.kind !== TerminalKind.Identifier &&
32+
isBinaryOperation(leftOperand)
33+
) &&
34+
(!isBinaryOperation(grandparentNode) ||
35+
grandparentNode.kind === NonterminalKind.AssignmentExpression);
3236

3337
return shouldGroup ? group(rightOperand) : rightOperand;
3438
}

tests/format/BinaryOperators/__snapshots__/format.test.js.snap

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,7 @@ contract ArithmeticOperators {
264264
a =
265265
veryVeryVeryVeryVeryLongFunctionCalledA(
266266
veryVeryVeryVeryVeryLongVariableCalledB
267-
) +
268-
c;
267+
) + c;
269268
if (
270269
veryVeryVeryVeryVeryLongFunctionCalledA(
271270
veryVeryVeryVeryVeryLongVariableCalledB
@@ -353,8 +352,7 @@ contract ArithmeticOperators {
353352
a =
354353
veryVeryVeryVeryVeryLongFunctionCalledA(
355354
veryVeryVeryVeryVeryLongVariableCalledB
356-
) **
357-
c;
355+
) ** c;
358356
if (
359357
veryVeryVeryVeryVeryLongFunctionCalledA(
360358
veryVeryVeryVeryVeryLongVariableCalledB
@@ -509,8 +507,7 @@ contract ComparisonOperators {
509507
a =
510508
veryVeryVeryVeryVeryLongFunctionCalledA(
511509
veryVeryVeryVeryVeryLongVariableCalledB
512-
) ==
513-
c;
510+
) == c;
514511
if (
515512
veryVeryVeryVeryVeryLongFunctionCalledA(
516513
veryVeryVeryVeryVeryLongVariableCalledB
@@ -663,8 +660,7 @@ contract LogicalOperators {
663660
a =
664661
veryVeryVeryVeryVeryLongFunctionCalledA(
665662
veryVeryVeryVeryVeryLongVariableCalledB
666-
) ||
667-
c;
663+
) || c;
668664
if (
669665
veryVeryVeryVeryVeryLongFunctionCalledA(
670666
veryVeryVeryVeryVeryLongVariableCalledB

tests/format/strings/__snapshots__/format.test.js.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,8 +1475,7 @@ library strings {
14751475
ptr,
14761476
needle._len,
14771477
needle._ptr
1478-
) +
1479-
needle._len;
1478+
) + needle._len;
14801479
}
14811480
}
14821481

0 commit comments

Comments
 (0)