Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const rightOperandPrinter = (node, path, print) => {
// operand like - 1 on its own line
const parent = path.getParentNode();
return node.left.type !== 'BinaryOperation' &&
parent.type !== 'BinaryOperation'
(parent.type !== 'BinaryOperation' || assignment.match(parent.operator))
? group(right)
: right;
};
Expand Down
12 changes: 8 additions & 4 deletions src/slang-printers/create-binary-operation-printer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TerminalKind } from '@nomicfoundation/slang/cst';
import { NonterminalKind, TerminalKind } from '@nomicfoundation/slang/cst';
import { doc } from 'prettier';
import { isBinaryOperation } from '../slang-utils/is-binary-operation.js';

Expand All @@ -24,11 +24,15 @@ function rightOperandPrint(
];
// If it's a single binary operation, avoid having a small right
// operand like - 1 on its own line
const leftOperand = node.leftOperand.variant;
const grandparentNode = path.getNode(2) as StrictAstNode;
const shouldGroup =
!(
node.leftOperand.variant.kind !== TerminalKind.Identifier &&
isBinaryOperation(node.leftOperand.variant)
) && !isBinaryOperation(path.getNode(2) as StrictAstNode);
leftOperand.kind !== TerminalKind.Identifier &&
isBinaryOperation(leftOperand)
) &&
(!isBinaryOperation(grandparentNode) ||
grandparentNode.kind === NonterminalKind.AssignmentExpression);

return shouldGroup ? group(rightOperand) : rightOperand;
}
Expand Down
12 changes: 4 additions & 8 deletions tests/format/BinaryOperators/__snapshots__/format.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,7 @@ contract ArithmeticOperators {
a =
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) +
c;
) + c;
if (
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
Expand Down Expand Up @@ -353,8 +352,7 @@ contract ArithmeticOperators {
a =
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) **
c;
) ** c;
if (
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
Expand Down Expand Up @@ -509,8 +507,7 @@ contract ComparisonOperators {
a =
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) ==
c;
) == c;
if (
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
Expand Down Expand Up @@ -663,8 +660,7 @@ contract LogicalOperators {
a =
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) ||
c;
) || c;
if (
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
Expand Down
3 changes: 1 addition & 2 deletions tests/format/strings/__snapshots__/format.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1475,8 +1475,7 @@ library strings {
ptr,
needle._len,
needle._ptr
) +
needle._len;
) + needle._len;
}
}

Expand Down