diff --git a/src/binary-operator-printers/printers/create-binary-operation-printer.js b/src/binary-operator-printers/printers/create-binary-operation-printer.js index cc6d309c3..e89527756 100644 --- a/src/binary-operator-printers/printers/create-binary-operation-printer.js +++ b/src/binary-operator-printers/printers/create-binary-operation-printer.js @@ -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; }; diff --git a/src/slang-printers/create-binary-operation-printer.ts b/src/slang-printers/create-binary-operation-printer.ts index 4c7b6dc49..711729a4e 100644 --- a/src/slang-printers/create-binary-operation-printer.ts +++ b/src/slang-printers/create-binary-operation-printer.ts @@ -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'; @@ -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; } diff --git a/tests/format/BinaryOperators/__snapshots__/format.test.js.snap b/tests/format/BinaryOperators/__snapshots__/format.test.js.snap index fb57f74ad..2fa95cf23 100644 --- a/tests/format/BinaryOperators/__snapshots__/format.test.js.snap +++ b/tests/format/BinaryOperators/__snapshots__/format.test.js.snap @@ -264,8 +264,7 @@ contract ArithmeticOperators { a = veryVeryVeryVeryVeryLongFunctionCalledA( veryVeryVeryVeryVeryLongVariableCalledB - ) + - c; + ) + c; if ( veryVeryVeryVeryVeryLongFunctionCalledA( veryVeryVeryVeryVeryLongVariableCalledB @@ -353,8 +352,7 @@ contract ArithmeticOperators { a = veryVeryVeryVeryVeryLongFunctionCalledA( veryVeryVeryVeryVeryLongVariableCalledB - ) ** - c; + ) ** c; if ( veryVeryVeryVeryVeryLongFunctionCalledA( veryVeryVeryVeryVeryLongVariableCalledB @@ -509,8 +507,7 @@ contract ComparisonOperators { a = veryVeryVeryVeryVeryLongFunctionCalledA( veryVeryVeryVeryVeryLongVariableCalledB - ) == - c; + ) == c; if ( veryVeryVeryVeryVeryLongFunctionCalledA( veryVeryVeryVeryVeryLongVariableCalledB @@ -663,8 +660,7 @@ contract LogicalOperators { a = veryVeryVeryVeryVeryLongFunctionCalledA( veryVeryVeryVeryVeryLongVariableCalledB - ) || - c; + ) || c; if ( veryVeryVeryVeryVeryLongFunctionCalledA( veryVeryVeryVeryVeryLongVariableCalledB diff --git a/tests/format/strings/__snapshots__/format.test.js.snap b/tests/format/strings/__snapshots__/format.test.js.snap index c60d36973..397392bf3 100644 --- a/tests/format/strings/__snapshots__/format.test.js.snap +++ b/tests/format/strings/__snapshots__/format.test.js.snap @@ -1475,8 +1475,7 @@ library strings { ptr, needle._len, needle._ptr - ) + - needle._len; + ) + needle._len; } }